222 lines
9.5 KiB
JavaScript
222 lines
9.5 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.uniEasycomPlugin = void 0;
|
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const pluginutils_1 = require("@rollup/pluginutils");
|
|
const estree_walker_1 = require("estree-walker");
|
|
const shared_1 = require("@vue/shared");
|
|
const uni_shared_1 = require("@dcloudio/uni-shared");
|
|
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
const H5_COMPONENTS_PATH = '@dcloudio/uni-h5';
|
|
const VAPOR_EASYCOM_IMPORT = "import { createComponent as __createEasycomComponent } from 'vue';";
|
|
const xBaseComponents = ['slider', 'switch', 'loading', 'page-container'];
|
|
// Vapor 未支持组件仅用带 uni-view 的节点显示提示,无需独立 CSS,也不能回退加载旧宿主 CSS。
|
|
// 已接入组件的样式优先于此名单;非 Vapor 仍按原路径加载。
|
|
const vaporPlaceholderComponents = new Set([
|
|
'ad-content-page',
|
|
'ad-draw',
|
|
'camera',
|
|
'live-player',
|
|
'live-pusher',
|
|
]);
|
|
const baseComponents = [
|
|
// 'audio',
|
|
'button',
|
|
'canvas',
|
|
'checkbox',
|
|
'checkbox-group',
|
|
'editor',
|
|
'form',
|
|
'icon',
|
|
'image',
|
|
'input',
|
|
'label',
|
|
'movable-area',
|
|
'movable-view',
|
|
'navigator',
|
|
'picker-view',
|
|
'picker-view-column',
|
|
'progress',
|
|
'radio',
|
|
'radio-group',
|
|
'resize-sensor',
|
|
'refresher',
|
|
'rich-text',
|
|
'scroll-view',
|
|
'slider',
|
|
'swiper',
|
|
'swiper-item',
|
|
'switch',
|
|
'text',
|
|
'textarea',
|
|
'view',
|
|
'list-view',
|
|
'list-item',
|
|
'sticky-section',
|
|
'sticky-header',
|
|
];
|
|
let componentDepsCss;
|
|
function uniEasycomPlugin(options) {
|
|
const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
|
|
let needCombineBuiltInCss = false;
|
|
componentDepsCss = (0, uni_cli_shared_1.COMPONENT_DEPS_CSS)(process.env.UNI_APP_X === 'true');
|
|
const isDevX = process.env.UNI_HX_VERSION_DEV === 'true' &&
|
|
process.env.UNI_APP_X === 'true';
|
|
const isWebVapor = options.vapor ?? (0, uni_cli_shared_1.isUniAppXWebVapor)();
|
|
const excludedComponents = new Set(options.excludeBuiltInComponents);
|
|
const isBuiltInComponent = (name) => {
|
|
name = name.replace(uni_shared_1.COMPONENT_PREFIX, '');
|
|
return (!excludedComponents.has(name) &&
|
|
(isWebVapor
|
|
? (0, uni_shared_1.isDom2WebVueComponentTag)(name)
|
|
: (0, uni_shared_1.isWebBuiltInComponent)(name)));
|
|
};
|
|
return {
|
|
name: 'uni:h5-easycom',
|
|
configResolved(config) {
|
|
needCombineBuiltInCss = (0, uni_cli_shared_1.isCombineBuiltInCss)(config);
|
|
},
|
|
transform(code, id) {
|
|
if (!filter(id)) {
|
|
return;
|
|
}
|
|
const { filename } = (0, uni_cli_shared_1.parseVueRequest)(id);
|
|
if (!uni_cli_shared_1.EXTNAME_VUE_TEMPLATE.includes(path_1.default.extname(filename))) {
|
|
return;
|
|
}
|
|
if (!code.includes('_resolveComponent') &&
|
|
!(isWebVapor && code.includes('_createAssetComponent'))) {
|
|
return;
|
|
}
|
|
let i = 0;
|
|
const importDeclarations = [];
|
|
if (isWebVapor && code.includes('_createAssetComponent')) {
|
|
const transformed = replaceVaporAssetComponents(code, this.parse(code), isDevX, isBuiltInComponent);
|
|
if (transformed !== code) {
|
|
code = transformed;
|
|
importDeclarations.push(VAPOR_EASYCOM_IMPORT);
|
|
}
|
|
}
|
|
if (!code.includes('_resolveComponent')) {
|
|
return;
|
|
}
|
|
code = code.replace(/_resolveComponent\("(.+?)"(, true)?\)/g, (str, name) => {
|
|
if (name && !name.startsWith('_')) {
|
|
// 为了兼容性,仅处理dev模式
|
|
if (!isDevX) {
|
|
const result = buildInComponent();
|
|
if (result) {
|
|
return result;
|
|
}
|
|
}
|
|
if (isDevX && name.startsWith('v-uni-')) {
|
|
name = name.replace('v-uni-', '');
|
|
}
|
|
const source = (0, uni_cli_shared_1.matchEasycom)(name);
|
|
if (source) {
|
|
const isHelpers = source.includes('?uni_helpers');
|
|
if (isHelpers) {
|
|
const cssFilename = path_1.default.join(process.env.UNI_MODULES_ENCRYPT_CACHE_DIR, path_1.default.relative(process.env.UNI_INPUT_DIR, source.replace('?uni_helpers', '/components/' + name + '/' + name + '.css')));
|
|
if (fs_extra_1.default.existsSync(cssFilename)) {
|
|
importDeclarations.push(`import "${(0, uni_cli_shared_1.normalizePath)(cssFilename)}";`);
|
|
}
|
|
}
|
|
// 处理easycom组件优先级
|
|
return (0, uni_cli_shared_1.genResolveEasycomCode)(importDeclarations, str, (0, uni_cli_shared_1.addImportDeclaration)(importDeclarations, `__easycom_${i++}`, source, isHelpers ? (0, shared_1.capitalize)((0, shared_1.camelize)(name)) : ''));
|
|
}
|
|
if (isDevX) {
|
|
const result = buildInComponent();
|
|
if (result) {
|
|
return result;
|
|
}
|
|
}
|
|
function buildInComponent() {
|
|
if (isBuiltInComponent(name)) {
|
|
name = name.replace(uni_shared_1.COMPONENT_PREFIX, '');
|
|
const local = `__syscom_${i++}`;
|
|
if (needCombineBuiltInCss) {
|
|
// 发行模式下,应该将内置组件css输出到入口css中
|
|
resolveBuiltInCssImport(name, isWebVapor).forEach((cssImport) => uni_cli_shared_1.buildInCssSet.add(cssImport));
|
|
return (0, uni_cli_shared_1.addImportDeclaration)(importDeclarations, local, H5_COMPONENTS_PATH, (0, shared_1.capitalize)((0, shared_1.camelize)(name)));
|
|
}
|
|
return addBuiltInImportDeclaration(importDeclarations, local, name, isWebVapor);
|
|
}
|
|
}
|
|
}
|
|
return str;
|
|
});
|
|
if (importDeclarations.length) {
|
|
code = importDeclarations.join('') + code;
|
|
}
|
|
return {
|
|
code,
|
|
map: null,
|
|
};
|
|
},
|
|
};
|
|
}
|
|
exports.uniEasycomPlugin = uniEasycomPlugin;
|
|
// 只处理真实调用,并移除 createAssetComponent 独有的尾部参数。
|
|
function replaceVaporAssetComponents(code, ast, isDevX, isBuiltInComponent) {
|
|
const edits = [];
|
|
(0, estree_walker_1.walk)(ast, {
|
|
enter(node) {
|
|
const call = node;
|
|
if (call.type === 'CallExpression' &&
|
|
call.callee.type === 'Identifier' &&
|
|
call.callee.name === '_createAssetComponent' &&
|
|
call.arguments[0]?.type === 'Literal' &&
|
|
typeof call.arguments[0].value === 'string') {
|
|
const first = call.arguments[0];
|
|
const name = first.value;
|
|
const easycomName = isDevX && name.startsWith('v-uni-') ? name.slice(6) : name;
|
|
if (!name.startsWith('_') &&
|
|
(isBuiltInComponent(name) || (0, uni_cli_shared_1.matchEasycom)(easycomName))) {
|
|
edits.push([
|
|
call.start,
|
|
first.end,
|
|
`__createEasycomComponent(_resolveComponent(${code.slice(first.start, first.end)})`,
|
|
]);
|
|
if (call.arguments.length > 5) {
|
|
edits.push([call.arguments[4].end, call.end - 1, '']);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
});
|
|
for (const [start, end, replacement] of edits.sort((a, b) => b[0] - a[0])) {
|
|
code = code.slice(0, start) + replacement + code.slice(end);
|
|
}
|
|
return code;
|
|
}
|
|
function resolveBuiltInCssImport(name, isWebVapor) {
|
|
const cssImports = [];
|
|
const vaporStyle = isWebVapor && (0, uni_cli_shared_1.resolveWebVaporComponentStyle)(name);
|
|
if (vaporStyle) {
|
|
cssImports.push(vaporStyle);
|
|
}
|
|
else if (!isWebVapor || !vaporPlaceholderComponents.has(name)) {
|
|
const isX = process.env.UNI_APP_X === 'true';
|
|
if (isX && xBaseComponents.includes(name)) {
|
|
cssImports.push(uni_cli_shared_1.X_BASE_COMPONENTS_STYLE_PATH + name + '.css');
|
|
}
|
|
else if (baseComponents.includes(name)) {
|
|
cssImports.push(uni_cli_shared_1.BASE_COMPONENTS_STYLE_PATH + name + '.css');
|
|
}
|
|
else {
|
|
cssImports.push(uni_cli_shared_1.H5_COMPONENTS_STYLE_PATH + name + '.css');
|
|
}
|
|
}
|
|
const deps = componentDepsCss[name];
|
|
deps && deps.forEach((dep) => cssImports.push(dep));
|
|
return cssImports;
|
|
}
|
|
function addBuiltInImportDeclaration(importDeclarations, local, name, isWebVapor) {
|
|
resolveBuiltInCssImport(name, isWebVapor).forEach((cssImport) => importDeclarations.push(`import '${cssImport}';`));
|
|
return (0, uni_cli_shared_1.addImportDeclaration)(importDeclarations, local, H5_COMPONENTS_PATH, (0, shared_1.capitalize)((0, shared_1.camelize)(name)));
|
|
}
|