H5-contact/node_modules/@dcloudio/uni-cli-shared/dist/easycom.js

375 lines
16 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genUTSComponentPublicInstanceImported = exports.genUTSComponentPublicInstanceIdent = exports.addUTSEasyComAutoImports = exports.getUTSEasyComAutoImports = exports.UNI_EASYCOM_EXCLUDE = exports.genResolveEasycomCode = exports.addImportDeclaration = exports.matchEasycom = exports.initEasycomsOnce = exports.initEasycoms = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const debug_1 = __importDefault(require("debug"));
const shared_1 = require("@vue/shared");
const pluginutils_1 = require("@rollup/pluginutils");
const uni_shared_1 = require("@dcloudio/uni-shared");
const x_1 = require("./x");
const utils_1 = require("./utils");
const pages_1 = require("./json/pages");
const messages_1 = require("./messages");
const uts_1 = require("./uts");
const utsUtils_1 = require("./utsUtils");
const debugEasycom = (0, debug_1.default)('uni:easycom');
const easycoms = [];
const easycomsCache = new Map();
const easycomsInvalidCache = new Set();
let hasEasycom = false;
function clearEasycom() {
easycoms.length = 0;
easycomsCache.clear();
easycomsInvalidCache.clear();
}
function initEasycoms(inputDir, { dirs, platform, isX, }) {
const componentsDir = path_1.default.resolve(inputDir, 'components');
const uniModulesDir = path_1.default.resolve(inputDir, 'uni_modules');
const initEasycomOptions = (pagesJson) => {
// 初始化时从once中读取缓存refresh时实时读取
const { easycom } = pagesJson || (0, pages_1.parsePagesJson)(inputDir, platform, false);
const easycomOptions = {
platform,
isX,
dirs: easycom && easycom.autoscan === false
? [...dirs] // 禁止自动扫描
: [
...dirs,
componentsDir,
...initUniModulesEasycomDirs(uniModulesDir),
],
rootDir: inputDir,
autoscan: !!(easycom && easycom.autoscan),
custom: (easycom && easycom.custom) || {},
extensions: [...(isX ? ['.uvue'] : []), ...['.vue', '.jsx', '.tsx']],
};
debugEasycom(easycomOptions);
return easycomOptions;
};
const easyComOptions = initEasycomOptions((0, pages_1.parsePagesJsonOnce)(inputDir, platform));
const initUTSEasycom = () => {
(0, uts_1.initUTSComponents)(inputDir, platform).forEach((item) => {
const index = easycoms.findIndex((easycom) => item.name === easycom.name);
if (index > -1) {
easycoms.splice(index, 1, item);
}
else {
easycoms.push(item);
}
});
if (isX && globalThis.uts2jsSourceCodeMap) {
;
globalThis.uts2jsSourceCodeMap.initUts2jsEasycom(easycoms);
}
// UTS / customElements 组件是在 initEasycom 之后补充进去的,
// 这里补一次状态,避免项目里只有这类组件时被误判为没有 easycom。
hasEasycom = !!easycoms.length;
};
const initUTSEasycomCustomElements = () => {
(0, uts_1.initUTSCustomElements)(inputDir, platform).forEach((item) => {
const index = easycoms.findIndex((easycom) => item.name === easycom.name);
if (index > -1) {
easycoms.splice(index, 1, item);
}
else {
easycoms.push(item);
}
});
};
const supportCustomElements = isX && process.env.UNI_APP_X_DOM2 !== 'true';
// ext-api 模式下,不存在 easycom 特性
if (process.env.UNI_COMPILE_TARGET !== 'ext-api') {
clearEasycom();
(0, uts_1.clearUTSComponents)();
(0, uts_1.clearUTSCustomElements)();
initEasycom(easyComOptions);
// dom2 模式下,不需要注册 customElements 组件
if (supportCustomElements) {
initUTSEasycomCustomElements();
}
initUTSEasycom();
}
const componentExtNames = isX ? 'uvue|vue' : 'vue';
const res = {
easyComOptions,
filter: (0, pluginutils_1.createFilter)([
'components/*/*.(' + componentExtNames + '|jsx|tsx)',
'uni_modules/*/components/*/*.(' + componentExtNames + '|jsx|tsx)',
'utssdk/*/**/*.(' + componentExtNames + ')',
'uni_modules/*/utssdk/*/*.(' + componentExtNames + ')',
...(supportCustomElements
? ['uni_modules/*/customElements/*/*.uts']
: []),
], [], {
resolve: inputDir,
}),
refresh() {
res.easyComOptions = initEasycomOptions();
if (process.env.UNI_COMPILE_TARGET !== 'ext-api') {
clearEasycom();
(0, uts_1.clearUTSComponents)();
(0, uts_1.clearUTSCustomElements)();
initEasycom(res.easyComOptions);
if (supportCustomElements) {
initUTSEasycomCustomElements();
}
initUTSEasycom();
}
},
easycoms,
};
return res;
}
exports.initEasycoms = initEasycoms;
exports.initEasycomsOnce = (0, uni_shared_1.once)(initEasycoms);
function initUniModulesEasycomDirs(uniModulesDir, componentsDir = 'components') {
if (!fs_1.default.existsSync(uniModulesDir)) {
return [];
}
return fs_1.default
.readdirSync(uniModulesDir)
.map((uniModuleDir) => {
const uniModuleComponentsDir = path_1.default.resolve(uniModulesDir, uniModuleDir, componentsDir);
if (fs_1.default.existsSync(uniModuleComponentsDir)) {
return uniModuleComponentsDir;
}
})
.filter(Boolean);
}
function initEasycom({ platform, isX, dirs, rootDir, custom, extensions, }) {
rootDir = (0, utils_1.normalizePath)(rootDir);
const easycomsObj = Object.create(null);
if (dirs && dirs.length && rootDir) {
const autoEasyComObj = initAutoScanEasycoms(platform, dirs, rootDir, extensions);
if (isX) {
Object.keys(autoEasyComObj).forEach((tagName) => {
let source = autoEasyComObj[tagName];
tagName = tagName.slice(1, -1);
if (path_1.default.isAbsolute(source) && source.startsWith(rootDir)) {
source = '@/' + (0, utils_1.normalizePath)(path_1.default.relative(rootDir, source));
}
// 加密插件easycom类型导入
if (source.includes('?uts-proxy')) {
const moduleId = path_1.default.basename(source.split('?uts-proxy')[0]);
source = `uts.sdk.modules.${(0, shared_1.camelize)(moduleId)}`;
}
const ident = genUTSComponentPublicInstanceIdent(tagName);
addUTSEasyComAutoImports(source, [ident, ident]);
});
}
(0, shared_1.extend)(easycomsObj, autoEasyComObj);
}
if (custom) {
Object.keys(custom).forEach((name) => {
const componentPath = custom[name];
easycomsObj[name] = componentPath.startsWith('@/')
? (0, utils_1.normalizePath)(path_1.default.join(rootDir, componentPath.slice(2)))
: componentPath;
});
}
Object.keys(easycomsObj).forEach((name) => {
easycoms.push({
name: name.startsWith('^') && name.endsWith('$') ? name.slice(1, -1) : name,
pattern: new RegExp(name),
replacement: easycomsObj[name],
});
});
debugEasycom(easycoms);
hasEasycom = !!easycoms.length;
return easycoms;
}
function matchEasycom(tag) {
if (!hasEasycom) {
return;
}
let source = easycomsCache.get(tag);
if (source) {
return source;
}
if (easycomsInvalidCache.has(tag)) {
return false;
}
const matcher = easycoms.find((matcher) => matcher.pattern.test(tag));
if (!matcher) {
easycomsInvalidCache.add(tag);
return false;
}
// dom2 不支持旧版 uni-app 兼容模式组件,这里在 easycom 真正命中时再报错,
// 避免扫描阶段就对未使用的插件组件产生误报。
if (process.env.UNI_APP_X_DOM2 === 'true' && matcher.dom2IncompatibleFile) {
throw new Error(messages_1.M['dom2.compatible.component']
.replace('{name}', `<${tag}>`)
.replace('{file}', matcher.dom2IncompatibleFile));
}
source = tag.replace(matcher.pattern, matcher.replacement);
easycomsCache.set(tag, source);
debugEasycom('matchEasycom', tag, source);
// 检查 H5 专用组件 ask163203
const H5_ONLY_COMPONENTS = ['custom-tab-bar'];
if (H5_ONLY_COMPONENTS.includes(tag) && process.env.UNI_PLATFORM !== 'h5') {
console.warn(`[uni-app] 组件 <${tag}> 是 H5 平台专用组件,在 ${process.env.UNI_PLATFORM} 平台使用可能导致错误。如果你要引入自定义 tabBar 可重命名为其他名称。`);
}
return source;
}
exports.matchEasycom = matchEasycom;
const isDir = (path) => {
const stat = fs_1.default.lstatSync(path);
if (stat.isDirectory()) {
return true;
}
else if (stat.isSymbolicLink()) {
return fs_1.default.lstatSync(fs_1.default.realpathSync(path)).isDirectory();
}
return false;
};
function initAutoScanEasycom(platform, dir, rootDir, extensions) {
if (!path_1.default.isAbsolute(dir)) {
dir = path_1.default.resolve(rootDir, dir);
}
const easycoms = Object.create(null);
if (!fs_1.default.existsSync(dir)) {
return easycoms;
}
const isDevX = process.env.UNI_HX_VERSION_DEV === 'true' &&
process.env.UNI_APP_X === 'true';
const isMp = platform.startsWith('mp-');
const is_uni_modules = path_1.default.basename(path_1.default.resolve(dir, '../..')) === 'uni_modules';
const has_uts_sdk_dir = fs_1.default.existsSync(path_1.default.resolve(dir, '../utssdk'));
const is_easycom_encrypt_uni_modules = // uni_modules模式不需要此逻辑
process.env.UNI_COMPILE_TARGET !== 'uni_modules' &&
is_uni_modules &&
// 前端加密插件不能包含utssdk目录
fs_1.default.existsSync(path_1.default.resolve(dir, '../encrypt')) &&
!has_uts_sdk_dir;
const uni_modules_plugin_id = is_easycom_encrypt_uni_modules && path_1.default.basename(path_1.default.resolve(dir, '..'));
fs_1.default.readdirSync(dir).forEach((name) => {
const folder = path_1.default.resolve(dir, name);
if (!isDir(folder)) {
return;
}
if (isDevX) {
if (has_uts_sdk_dir &&
process.env.UNI_UTS_PLATFORM &&
(0, uni_shared_1.isBuiltInComponent)(name)) {
// dev下如果是内置组件且存在utssdk目录则判断utssdk下是否存在当前平台如果不存在则跳过
const utsSdkPlatformDir = path_1.default.resolve(dir, '../utssdk', process.env.UNI_UTS_PLATFORM);
if (!fs_1.default.existsSync(utsSdkPlatformDir)) {
return;
}
}
}
const importDir = (0, utils_1.normalizePath)(folder);
const files = fs_1.default.readdirSync(folder);
// 读取文件夹文件列表比对文件名fs.existsSync在大小写不敏感的系统会匹配不准确
for (let i = 0; i < extensions.length; i++) {
const ext = extensions[i];
if (files.includes(name + ext)) {
easycoms[`^${name}$`] =
// mp平台这里不处理由uniEntryPlugin处理
is_easycom_encrypt_uni_modules && !isMp
? (0, utils_1.normalizePath)(path_1.default.join(rootDir, `uni_modules/${uni_modules_plugin_id}?${(0, x_1.isUniAppXAndroidNative)() ? 'uts-proxy' : 'uni_helpers'}`))
: `${importDir}/${name}${ext}`;
break;
}
}
});
return easycoms;
}
function initAutoScanEasycoms(platform, dirs, rootDir, extensions) {
const conflict = {};
const res = dirs.reduce((easycoms, dir) => {
const curEasycoms = initAutoScanEasycom(platform, dir, rootDir, extensions);
Object.keys(curEasycoms).forEach((name) => {
// Use the first component when name conflict
const componentPath = easycoms[name];
if (!componentPath) {
easycoms[name] = curEasycoms[name];
}
else {
;
(conflict[componentPath] || (conflict[componentPath] = [])).push(normalizeComponentPath(curEasycoms[name], rootDir));
}
});
return easycoms;
}, Object.create(null));
const conflictComponents = Object.keys(conflict);
if (conflictComponents.length) {
console.warn(messages_1.M['easycom.conflict']);
conflictComponents.forEach((com) => {
console.warn([normalizeComponentPath(com, rootDir), conflict[com]].join(','));
});
}
return res;
}
function normalizeComponentPath(componentPath, rootDir) {
return (0, utils_1.normalizePath)(path_1.default.relative(rootDir, componentPath));
}
function addImportDeclaration(importDeclarations, local, source, imported) {
importDeclarations.push(createImportDeclaration(local, source, imported));
return local;
}
exports.addImportDeclaration = addImportDeclaration;
function createImportDeclaration(local, source, imported) {
if (imported && local) {
return `import { ${imported} as ${local} } from '${source}';`;
}
if (local) {
return `import ${local} from '${source}';`;
}
return `import '${source}';`;
}
const RESOLVE_EASYCOM_IMPORT_CODE = `import { resolveDynamicComponent as __resolveDynamicComponent } from 'vue';import { resolveEasycom } from '@dcloudio/uni-app';`;
function genResolveEasycomCode(importDeclarations, code, name) {
if (process.env.UNI_APP_X_DOM2 === 'true' ||
// 内部 dev 版本需要本地开发内置组件比如web-view此时需要直接返回本地easycom不然会找到基座内置的
(process.env.UNI_HX_VERSION_DEV === 'true' &&
process.env.UNI_APP_X === 'true')) {
// dom2 模式下,为了性能,不再考虑优先级问题,如果开发者需要区别,可以手动导入为其他名称
return name;
}
if (!importDeclarations.includes(RESOLVE_EASYCOM_IMPORT_CODE)) {
importDeclarations.push(RESOLVE_EASYCOM_IMPORT_CODE);
}
return `resolveEasycom(${code
.replace('_resolveComponent', '__resolveDynamicComponent')
// 移除 maybeSelfReference 逻辑easycom 优先级高于自引用组件
.replace(', true)', ')')}, ${name})`;
}
exports.genResolveEasycomCode = genResolveEasycomCode;
exports.UNI_EASYCOM_EXCLUDE = [/@dcloudio\/uni-h5/];
const utsEasyComAutoImports = {};
function getUTSEasyComAutoImports() {
return utsEasyComAutoImports;
}
exports.getUTSEasyComAutoImports = getUTSEasyComAutoImports;
function addUTSEasyComAutoImports(source, imports) {
if (!utsEasyComAutoImports[source]) {
utsEasyComAutoImports[source] = [imports];
}
else {
if (!utsEasyComAutoImports[source].find((item) => item[0] === imports[0])) {
utsEasyComAutoImports[source].push(imports);
}
}
}
exports.addUTSEasyComAutoImports = addUTSEasyComAutoImports;
function genUTSComponentPublicInstanceIdent(tagName) {
return (0, shared_1.capitalize)((0, shared_1.camelize)(tagName)) + 'ComponentPublicInstance';
}
exports.genUTSComponentPublicInstanceIdent = genUTSComponentPublicInstanceIdent;
function genUTSComponentPublicInstanceImported(root, fileName) {
root = (0, utils_1.normalizePath)(root);
if (path_1.default.isAbsolute(fileName) && fileName.startsWith(root)) {
fileName = (0, utils_1.normalizePath)(path_1.default.relative(root, fileName));
}
if (fileName.startsWith('@/')) {
return ((0, utsUtils_1.genUTSClassName)(fileName.replace('@/', '')) + 'ComponentPublicInstance');
}
return (0, utsUtils_1.genUTSClassName)(fileName) + 'ComponentPublicInstance';
}
exports.genUTSComponentPublicInstanceImported = genUTSComponentPublicInstanceImported;