353 lines
16 KiB
JavaScript
353 lines
16 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.initPluginViteLegacyOptions = exports.initPluginVueJsxOptions = exports.initPluginVueOptions = exports.createPluginVueInstance = void 0;
|
|
const path_1 = __importDefault(require("path"));
|
|
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 utils_1 = require("../utils");
|
|
const pluginVuePath = require.resolve('@vitejs/plugin-vue');
|
|
const normalizedPluginVuePath = (0, uni_cli_shared_1.normalizePath)(pluginVuePath);
|
|
/**
|
|
* 每次创建新的 plugin-vue 实例。因为该插件内部会 cache descriptor,而相同的vue文件在编译到vue页面和nvue页面时,不能共享缓存(条件编译,css scoped等均不同)
|
|
* @returns
|
|
*/
|
|
function createPluginVueInstance(options) {
|
|
delete require.cache[pluginVuePath];
|
|
delete require.cache[normalizedPluginVuePath];
|
|
const vuePluginModule = require(pluginVuePath);
|
|
const vuePlugin = vuePluginModule.default || vuePluginModule;
|
|
const vuePluginInstance = vuePlugin(options);
|
|
if (process.env.NODE_ENV === 'development') {
|
|
// 删除 buildEnd 逻辑,因为里边清理了缓存,导致 watch 模式失效 https://github.com/vitejs/vite-plugin-vue/commit/96dbb220ff210d2f7391f43a807bcd8cfb0da776
|
|
delete vuePluginInstance.buildEnd;
|
|
}
|
|
return vuePluginInstance;
|
|
}
|
|
exports.createPluginVueInstance = createPluginVueInstance;
|
|
function initPluginVueOptions(options, uniPluginOptions) {
|
|
const vueOptions = options.vueOptions || (options.vueOptions = {});
|
|
// if (!hasOwn(vueOptions, 'reactivityTransform')) {
|
|
// vueOptions.reactivityTransform = true
|
|
// }
|
|
if (!vueOptions.include) {
|
|
vueOptions.include = [];
|
|
}
|
|
if (!(0, shared_1.isArray)(vueOptions.include)) {
|
|
vueOptions.include = [vueOptions.include];
|
|
}
|
|
vueOptions.include.push(uni_cli_shared_1.EXTNAME_VUE_RE);
|
|
const styleOptions = vueOptions.style || (vueOptions.style = {});
|
|
if (!styleOptions.postcssPlugins) {
|
|
styleOptions.postcssPlugins = [];
|
|
}
|
|
// 解析 scoped 中 deep 等特殊语法
|
|
styleOptions.postcssPlugins.push((0, uni_cli_shared_1.uniPostcssScopedPlugin)());
|
|
// 小程序平台下处理 externalClasses 的样式隔离与优先级
|
|
if (process.env.UNI_APP_STYLE_ISOLATION_VERSION === '2' &&
|
|
process.env.UNI_APP_X === 'true') {
|
|
styleOptions.postcssPlugins.push((0, uni_cli_shared_1.uniPostcssExternalPlugin)());
|
|
}
|
|
const templateOptions = vueOptions.template || (vueOptions.template = {});
|
|
const compilerOptions = templateOptions.compilerOptions || (templateOptions.compilerOptions = {});
|
|
const isDom2 = process.env.UNI_APP_X_DOM2 === 'true';
|
|
const isWebVapor = (0, uni_cli_shared_1.isUniAppXWebVapor)();
|
|
compilerOptions.isX = process.env.UNI_APP_X === 'true';
|
|
compilerOptions.dom2 = isDom2;
|
|
// 默认就移除comments节点
|
|
compilerOptions.comments = false;
|
|
const { compiler, styleOptions: { postcssPlugins }, compilerOptions: { miniProgram, isNativeTag, isVoidTag, isCustomElement, nodeTransforms, directiveTransforms, ssrPreTagTransforms, whitespace, }, } = uniPluginOptions;
|
|
if (postcssPlugins) {
|
|
styleOptions.postcssPlugins.push(...postcssPlugins);
|
|
}
|
|
if (compiler) {
|
|
templateOptions.compiler = compiler;
|
|
}
|
|
if (isWebVapor) {
|
|
const features = vueOptions.features || (vueOptions.features = {});
|
|
features.vapor = true;
|
|
features.componentIdGenerator = createAppComponentIdGenerator(options.inputDir, features.componentIdGenerator);
|
|
Object.assign(compilerOptions, {
|
|
isUniPage(filename) {
|
|
// 自定义窗口同样接收 App 样式,名单随配置更新,不能读取首次解析的旧配置。
|
|
return ((0, uni_cli_shared_1.isUniPageFile)(filename, options.inputDir) ||
|
|
uni_cli_shared_1.windowsCacheSet.has((0, uni_cli_shared_1.normalizePath)(path_1.default.relative(options.inputDir, filename))));
|
|
},
|
|
});
|
|
// plugin-vue 的 compiler 是完整 SFC compiler;显式传入 Web Vapor 定制版,
|
|
// 避免从项目根目录解析到标准 Vue compiler-sfc。
|
|
vueOptions.compiler = require('vue/compiler-sfc');
|
|
if (ssrPreTagTransforms) {
|
|
Object.assign(compilerOptions, { ssrPreTagTransforms });
|
|
}
|
|
}
|
|
if (miniProgram) {
|
|
;
|
|
compilerOptions.miniProgram = miniProgram;
|
|
}
|
|
const isDevX = process.env.UNI_HX_VERSION_DEV === 'true' &&
|
|
process.env.UNI_APP_X === 'true';
|
|
if (isNativeTag) {
|
|
const userIsNativeTag = compilerOptions.isNativeTag;
|
|
compilerOptions.isNativeTag = (tag) => {
|
|
if (isDevX) {
|
|
const source = (0, uni_cli_shared_1.matchEasycom)(tag);
|
|
// 不能是uts插件的easycom组件
|
|
if (source && !source.includes('?uts-proxy')) {
|
|
return false;
|
|
}
|
|
}
|
|
if (isNativeTag(tag)) {
|
|
return true;
|
|
}
|
|
if (userIsNativeTag && userIsNativeTag(tag)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
}
|
|
if (isVoidTag) {
|
|
const userIsVoidTag = compilerOptions.isVoidTag;
|
|
compilerOptions.isVoidTag = (tag) => {
|
|
if (isVoidTag(tag)) {
|
|
return true;
|
|
}
|
|
if (userIsVoidTag && userIsVoidTag(tag)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
}
|
|
if (whitespace) {
|
|
compilerOptions.whitespace = whitespace;
|
|
}
|
|
if (isCustomElement) {
|
|
const userIsCustomElement = compilerOptions.isCustomElement;
|
|
compilerOptions.isCustomElement = (tag) => {
|
|
if (isCustomElement(tag)) {
|
|
return true;
|
|
}
|
|
if (userIsCustomElement && userIsCustomElement(tag)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
}
|
|
compilerOptions.directiveTransforms = {
|
|
...compilerOptions.directiveTransforms,
|
|
...directiveTransforms,
|
|
};
|
|
if (!compilerOptions.nodeTransforms) {
|
|
compilerOptions.nodeTransforms = [];
|
|
}
|
|
// 合并 transformAssetUrls
|
|
// 内置配置
|
|
const builtInTransformAssetUrls = (0, uni_cli_shared_1.createUniVueTransformAssetUrls)((0, uni_cli_shared_1.isExternalUrl)(options.base) ? options.base : '');
|
|
// 用户传递配置 eg: transformAssetUrls.tags = {'my-image': ['src']}
|
|
// docs: https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue
|
|
const userOptionsTransformAssetUrls = templateOptions.transformAssetUrls;
|
|
templateOptions.transformAssetUrls = builtInTransformAssetUrls;
|
|
if (typeof userOptionsTransformAssetUrls !== 'boolean' &&
|
|
!!userOptionsTransformAssetUrls?.tags &&
|
|
!(0, shared_1.isArray)(userOptionsTransformAssetUrls.tags)) {
|
|
templateOptions.transformAssetUrls = {
|
|
...builtInTransformAssetUrls,
|
|
...userOptionsTransformAssetUrls,
|
|
tags: {
|
|
...builtInTransformAssetUrls.tags,
|
|
...userOptionsTransformAssetUrls.tags,
|
|
},
|
|
};
|
|
}
|
|
if (options.platform !== 'h5' && options.platform !== 'web') {
|
|
compilerOptions.nodeTransforms.push(...(0, uni_cli_shared_1.getBaseNodeTransforms)(options.base, isDom2 ? (0, uni_cli_shared_1.createResolveStaticAsset)(options.inputDir) : undefined));
|
|
}
|
|
if (nodeTransforms) {
|
|
compilerOptions.nodeTransforms.push(...nodeTransforms);
|
|
}
|
|
// const compatConfig = parseCompatConfigOnce(options.inputDir)
|
|
// compilerOptions.compatConfig = extend(
|
|
// compilerOptions.compatConfig || {},
|
|
// compatConfig
|
|
// )
|
|
// App,MP 平台不支持使用静态节点
|
|
compilerOptions.hoistStatic = false;
|
|
compilerOptions.root = process.env.UNI_INPUT_DIR;
|
|
const isX = process.env.UNI_APP_X === 'true';
|
|
// app-nvue | app-uvue 需要启用 customElement 机制来内联 styles
|
|
if (process.env.UNI_COMPILER === 'nvue' ||
|
|
(isX && (options.platform === 'app' || options.platform === 'app-harmony'))) {
|
|
vueOptions.customElement = true;
|
|
if (process.env.UNI_RENDERER_NATIVE !== 'appService' || isX) {
|
|
// nvue 需要使用自己的 compiler,来移除 scoped
|
|
vueOptions.compiler = (0, utils_1.createNVueCompiler)();
|
|
}
|
|
}
|
|
if (!vueOptions.script) {
|
|
vueOptions.script = {};
|
|
}
|
|
// TODO 目前暂不支持通过@/开头引入文件,因为需要tsconfig.json配置,建议使用相对路径
|
|
// https://github.com/vuejs/core/blob/main/packages/compiler-sfc/src/script/resolveType.ts#L911
|
|
require('@vue/compiler-sfc').registerTS(() => {
|
|
if (isX) {
|
|
return (0, uni_cli_shared_1.resolveUniTypeScript)();
|
|
}
|
|
return require('typescript');
|
|
});
|
|
if (!vueOptions.script.fs) {
|
|
vueOptions.script.fs = (0, uni_cli_shared_1.createVueCompilerFs)();
|
|
}
|
|
if (isX) {
|
|
// DOM2 的定制 compiler-sfc 会根据 script lang 选择 parser,避免 JS 被按 TS 解析。
|
|
if (!isDom2) {
|
|
if (!vueOptions.script) {
|
|
vueOptions.script = {
|
|
babelParserPlugins: [],
|
|
};
|
|
}
|
|
if (!vueOptions.script.babelParserPlugins) {
|
|
vueOptions.script.babelParserPlugins = [];
|
|
}
|
|
// Android VDOM 保持原有 UTS 兼容;标准脚本平台由 compiler-sfc 按 lang 启用 TypeScript。
|
|
if (!(0, uni_cli_shared_1.isUniAppXStandardScriptSupported)() &&
|
|
!vueOptions.script.babelParserPlugins.includes('typescript')) {
|
|
vueOptions.script.babelParserPlugins.push('typescript');
|
|
}
|
|
// 旧 compiler-sfc 仅用 includes('decorators') 识别现代装饰器,tuple 配置仍需字符串哨兵。
|
|
const hasDecoratorParser = vueOptions.script.babelParserPlugins.some((plugin) => plugin === 'decorators' || plugin === 'decorators-legacy');
|
|
if (!hasDecoratorParser) {
|
|
vueOptions.script.babelParserPlugins.push('decorators');
|
|
}
|
|
}
|
|
if (isDom2) {
|
|
// 该扩展点依赖定制 plugin-vue,仅用于 compileScript 后的 DOM2 SharedData 等转换。
|
|
;
|
|
vueOptions.uniAppXVaporScriptTransform =
|
|
uniPluginOptions.uniAppXVaporScriptTransform;
|
|
const utsPlatform = process.env.UNI_UTS_PLATFORM || process.env.UNI_PLATFORM;
|
|
if ((0, uni_cli_shared_1.isUniAppXAppPlatform)(utsPlatform)) {
|
|
const defaultLang = (0, uni_cli_shared_1.getUniAppXVaporScriptLang)(process.env.UNI_INPUT_DIR);
|
|
vueOptions.uniAppXVaporSfcTransform = (descriptor) => {
|
|
const scripts = [descriptor.script, descriptor.scriptSetup].filter((script) => !!script);
|
|
// compiler-sfc 可能复用缓存中的 descriptor,保留首次处理时记录的隐式 lang 状态。
|
|
let hasImplicitLang = descriptor.__uniAppXVaporSfcMeta?.hasImplicitLang === true;
|
|
scripts.forEach((script) => {
|
|
if (script.lang == null) {
|
|
script.lang = defaultLang;
|
|
script.attrs.lang = defaultLang;
|
|
hasImplicitLang = true;
|
|
}
|
|
});
|
|
if (descriptor.scriptSetup) {
|
|
;
|
|
descriptor.vapor = true;
|
|
}
|
|
const scriptLang = descriptor.scriptSetup?.lang ||
|
|
descriptor.script?.lang ||
|
|
defaultLang;
|
|
descriptor.__uniAppXVaporSfcMeta = {
|
|
scriptLang,
|
|
...(process.env.NODE_ENV === 'development'
|
|
? { hasImplicitLang, defaultLang }
|
|
: {}),
|
|
};
|
|
};
|
|
}
|
|
const appVue = (0, uni_cli_shared_1.resolveAppVue)(process.env.UNI_INPUT_DIR);
|
|
function isAppVue(id) {
|
|
return (0, uni_cli_shared_1.normalizePath)(id) === appVue;
|
|
}
|
|
;
|
|
compilerOptions.isEasyComponent = (tag) => {
|
|
return (0, uni_shared_1.isDom2VueComponentTag)(tag) || !!(0, uni_cli_shared_1.matchEasycom)(tag);
|
|
};
|
|
compilerOptions.isUserComponent = (element) => {
|
|
return (0, uni_shared_1.isDom2AppUserVueComponentTag)(element.tag);
|
|
};
|
|
vueOptions.script.extraOptions = (descriptor) => {
|
|
return {
|
|
// Web 只需要组件类型,不使用 App 的 SharedData,也不依赖 HBuilderX 插件。
|
|
...(isWebVapor
|
|
? {}
|
|
: {
|
|
isWatch: process.env.NODE_ENV === 'development',
|
|
// TODO 目前使用全局开关,后续可能是页面级别的开关?
|
|
dynamicSharedData: process.env.UNI_APP_X_DOM2_DYNAMIC === 'true',
|
|
helper: (0, uni_cli_shared_1.requireUniHelpers)(),
|
|
}),
|
|
componentType: (0, uni_cli_shared_1.isUniPageFile)(descriptor.filename)
|
|
? 'page'
|
|
: isAppVue(descriptor.filename)
|
|
? 'app'
|
|
: 'component',
|
|
};
|
|
};
|
|
if (!isWebVapor) {
|
|
;
|
|
vueOptions.template.compilerOptions.extraOptions = (descriptor) => {
|
|
return (0, uni_cli_shared_1.initVueTemplateCompilerExtraOptions)(descriptor);
|
|
};
|
|
}
|
|
}
|
|
else {
|
|
// 如果是 easyComponent 组件,则不需要从setup中导入组件且不支持 self 引用
|
|
;
|
|
compilerOptions.isEasyComponent = (tag) => {
|
|
return (0, uni_shared_1.isBuiltInComponent)(tag) || !!(0, uni_cli_shared_1.matchEasycom)(tag);
|
|
};
|
|
}
|
|
}
|
|
if (isWebVapor) {
|
|
// Web Vapor 内置组件与项目 Easycom 均应优先于同名 setup 绑定。
|
|
;
|
|
compilerOptions.isEasyComponent = (tag) => {
|
|
const name = tag.startsWith('v-uni-') ? tag.slice(6) : tag;
|
|
return (0, uni_shared_1.isDom2WebVueComponentTag)(name) || !!(0, uni_cli_shared_1.matchEasycom)(name);
|
|
};
|
|
}
|
|
return vueOptions;
|
|
}
|
|
exports.initPluginVueOptions = initPluginVueOptions;
|
|
function initPluginVueJsxOptions(options, { isCustomElement, }, jsxOptions) {
|
|
const vueJsxOptions = (0, shared_1.isPlainObject)(options.vueJsxOptions)
|
|
? options.vueJsxOptions
|
|
: (options.vueJsxOptions = {});
|
|
if (!(0, shared_1.hasOwn)(vueJsxOptions, 'optimize')) {
|
|
vueJsxOptions.optimize = true;
|
|
}
|
|
vueJsxOptions.isCustomElement = isCustomElement;
|
|
if (!vueJsxOptions.babelPlugins) {
|
|
vueJsxOptions.babelPlugins = [];
|
|
}
|
|
if ((0, shared_1.isArray)(jsxOptions.babelPlugins)) {
|
|
vueJsxOptions.babelPlugins.push(...jsxOptions.babelPlugins);
|
|
}
|
|
return vueJsxOptions;
|
|
}
|
|
exports.initPluginVueJsxOptions = initPluginVueJsxOptions;
|
|
function initPluginViteLegacyOptions(options) {
|
|
const viteLegacyOptions = options.viteLegacyOptions || (options.viteLegacyOptions = {});
|
|
return viteLegacyOptions;
|
|
}
|
|
exports.initPluginViteLegacyOptions = initPluginViteLegacyOptions;
|
|
// 仅固定根 App 的 ID;其他组件保留 plugin-vue 的默认策略及开发者配置。
|
|
function createAppComponentIdGenerator(inputDir, generator) {
|
|
const appFilename = (0, uni_cli_shared_1.normalizePath)(path_1.default.relative(process.env.VITE_ROOT_DIR || process.cwd(), (0, uni_cli_shared_1.resolveAppVue)(inputDir)));
|
|
const { UNI_APP_SCOPE_ID } = require('@dcloudio/compiler-vapor-web');
|
|
const appId = UNI_APP_SCOPE_ID.slice(7);
|
|
return (filename, source, isProduction, getHash) => {
|
|
if (filename === appFilename)
|
|
return appId;
|
|
if (typeof generator === 'function') {
|
|
return generator(filename, source, isProduction, getHash);
|
|
}
|
|
return getHash(filename +
|
|
(generator === 'filepath-source' || (!generator && isProduction)
|
|
? source
|
|
: ''));
|
|
};
|
|
}
|