更新
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function uniConfusionPlugin(): Plugin;
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniConfusionPlugin = void 0;
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function uniConfusionPlugin() {
|
||||
const inputDir = process.env.UNI_INPUT_DIR;
|
||||
const hasConfusion = process.env.NODE_ENV === 'production' && (0, uni_cli_shared_1.hasConfusionFile)(inputDir);
|
||||
return {
|
||||
name: 'uni:app-vue-confusion',
|
||||
enforce: 'post',
|
||||
apply: 'build',
|
||||
config() {
|
||||
if (!hasConfusion) {
|
||||
return;
|
||||
}
|
||||
return {
|
||||
build: {
|
||||
rollupOptions: {
|
||||
output: {
|
||||
format: process.env.UNI_APP_CODE_SPLITING ? 'amd' : 'cjs',
|
||||
manualChunks(id) {
|
||||
if ((0, uni_cli_shared_1.isConfusionFile)(path_1.default.relative(inputDir, id))) {
|
||||
return (0, uni_cli_shared_1.removeExt)(uni_cli_shared_1.APP_CONFUSION_FILENAME);
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
generateBundle(_, bundle) {
|
||||
if (!hasConfusion) {
|
||||
return;
|
||||
}
|
||||
const appConfusionChunk = bundle[uni_cli_shared_1.APP_CONFUSION_FILENAME];
|
||||
if (!appConfusionChunk) {
|
||||
return;
|
||||
}
|
||||
appConfusionChunk.code = wrapperAppConfusionCode(appConfusionChunk.code);
|
||||
const appServiceChunk = bundle[uni_cli_shared_1.APP_SERVICE_FILENAME];
|
||||
if (!appServiceChunk) {
|
||||
return;
|
||||
}
|
||||
appServiceChunk.code = wrapperAppServiceCode(appServiceChunk.code);
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.uniConfusionPlugin = uniConfusionPlugin;
|
||||
function replaceRequireVueCode(code) {
|
||||
// 目前会生成require("@vue/shared"); 理论上摇树之后,是不应该有的,后续排查为什么
|
||||
return code
|
||||
.replace(/require\(['"]vue['"]\)/gi, `$cjs_require$('vue')`)
|
||||
.replace(/require\(['"]@vue\/shared['"]\)/gi, `$cjs_require$('@vue/shared')`);
|
||||
}
|
||||
function replaceRequireAppConfusionCode(code) {
|
||||
return code.replace(new RegExp(`require\\(['"].\\/${uni_cli_shared_1.APP_CONFUSION_FILENAME}['"]\\)`, 'gi'), `$cjs_require$('./${uni_cli_shared_1.APP_CONFUSION_FILENAME}')`);
|
||||
}
|
||||
function wrapperAppServiceCode(code) {
|
||||
return replaceRequireAppConfusionCode(replaceRequireVueCode(code));
|
||||
}
|
||||
function wrapperAppConfusionCode(code) {
|
||||
return `function $cjs_require$(name){if(name==='vue'){return Vue;}if(name==='./${uni_cli_shared_1.APP_CONFUSION_FILENAME}'){return $appConfusion$;}};const $appConfusion$ = {};(function(exports){${replaceRequireVueCode(code)}})($appConfusion$);
|
||||
`;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function uniMainJsPlugin(): import("vite").Plugin<any>;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniMainJsPlugin = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function uniMainJsPlugin() {
|
||||
return (0, uni_cli_shared_1.defineUniMainJsPlugin)((opts) => {
|
||||
return {
|
||||
name: 'uni:app-vue-main-js',
|
||||
enforce: 'pre',
|
||||
transform(code, id) {
|
||||
if (opts.filter(id)) {
|
||||
code = code.includes('createSSRApp')
|
||||
? createApp(code)
|
||||
: createLegacyApp(code);
|
||||
return {
|
||||
code: `import './${uni_cli_shared_1.PAGES_JSON_JS}';` + code,
|
||||
map: { mappings: '' },
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
exports.uniMainJsPlugin = uniMainJsPlugin;
|
||||
function createApp(code) {
|
||||
return `${code.replace('createSSRApp', 'createVueApp as createSSRApp')};const {app:__app__,Vuex:__Vuex__,Pinia:__Pinia__}=createApp();uni.Vuex=__Vuex__;uni.Pinia=__Pinia__;__app__.provide('__globalStyles', __uniConfig.styles);__app__._component.mpType='app';__app__._component.render=()=>{};__app__.mount("#app");`;
|
||||
}
|
||||
function createLegacyApp(code) {
|
||||
return `function createApp(rootComponent,rootProps){rootComponent.mpTye='app';rootComponent.render=()=>{};const app=createVueApp(rootComponent,rootProps);app.provide('__globalStyles', __uniConfig.styles);const oldMount=app.mount;app.mount=(container)=>{const appVm=oldMount.call(app,container);return appVm;};return app;};${code.replace('createApp', 'createVueApp')}`;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function uniPagesJsonPlugin(): Plugin;
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniPagesJsonPlugin = void 0;
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const esbuild_1 = require("../../nvue/plugins/esbuild");
|
||||
function uniPagesJsonPlugin() {
|
||||
return (0, uni_cli_shared_1.defineUniPagesJsonPlugin)((opts) => {
|
||||
return {
|
||||
name: 'uni:app-vue-pages-json',
|
||||
enforce: 'pre',
|
||||
transform(code, id) {
|
||||
if (!opts.filter(id)) {
|
||||
return;
|
||||
}
|
||||
this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'pages.json'));
|
||||
(0, uni_cli_shared_1.getLocaleFiles)(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'locale')).forEach((filepath) => {
|
||||
this.addWatchFile(filepath);
|
||||
});
|
||||
const pagesJson = (0, uni_cli_shared_1.normalizePagesJson)(code, process.env.UNI_PLATFORM);
|
||||
pagesJson.pages.forEach((page) => {
|
||||
if (!page.style.isNVue) {
|
||||
this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, page.path + '.vue'));
|
||||
}
|
||||
});
|
||||
this.emitFile({
|
||||
fileName: uni_cli_shared_1.APP_CONFIG_SERVICE,
|
||||
type: 'asset',
|
||||
source: (0, uni_cli_shared_1.normalizeAppConfigService)(pagesJson, (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR)),
|
||||
});
|
||||
return {
|
||||
code: `import './${uni_cli_shared_1.MANIFEST_JSON_JS}'\n` +
|
||||
(0, uni_cli_shared_1.normalizeAppPagesJson)(pagesJson, process.env.UNI_PLATFORM),
|
||||
map: { mappings: '' },
|
||||
};
|
||||
},
|
||||
generateBundle(_, bundle) {
|
||||
const outputFile = bundle[uni_cli_shared_1.APP_CONFIG_SERVICE];
|
||||
if (outputFile && outputFile.type === 'asset') {
|
||||
// 补充 nvue styles
|
||||
;
|
||||
outputFile.source = (0, esbuild_1.wrapperNVueAppStyles)(outputFile.source);
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
exports.uniPagesJsonPlugin = uniPagesJsonPlugin;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare const APP_WXS_JS = "app-wxs.js";
|
||||
export declare const APP_RENDERJS_JS = "app-renderjs.js";
|
||||
export declare function uniRenderjsPlugin(): Plugin;
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniRenderjsPlugin = exports.APP_RENDERJS_JS = exports.APP_WXS_JS = void 0;
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const debug_1 = __importDefault(require("debug"));
|
||||
const uni_shared_1 = require("@dcloudio/uni-shared");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const debugRenderjs = (0, debug_1.default)('uni:app-renderjs');
|
||||
exports.APP_WXS_JS = 'app-wxs.js';
|
||||
exports.APP_RENDERJS_JS = 'app-renderjs.js';
|
||||
const wxsModulesCache = new WeakMap();
|
||||
const renderjsModulesCache = new WeakMap();
|
||||
function uniRenderjsPlugin() {
|
||||
let resolvedConfig;
|
||||
let userConfig;
|
||||
let changed = false;
|
||||
return {
|
||||
name: 'uni:app-vue-renderjs',
|
||||
config(config) {
|
||||
userConfig = config;
|
||||
},
|
||||
configResolved(config) {
|
||||
resolvedConfig = config;
|
||||
wxsModulesCache.set(resolvedConfig, new Map());
|
||||
renderjsModulesCache.set(resolvedConfig, new Map());
|
||||
},
|
||||
async transform(code, id) {
|
||||
const { type, name, filename } = (0, uni_cli_shared_1.parseRenderjs)(id);
|
||||
if (!type) {
|
||||
return;
|
||||
}
|
||||
if (type !== 'wxs' && type !== 'renderjs') {
|
||||
return;
|
||||
}
|
||||
debugRenderjs(id);
|
||||
this.addWatchFile((0, uni_cli_shared_1.cleanUrl)(id));
|
||||
if (!name) {
|
||||
this.error((0, uni_cli_shared_1.missingModuleName)(type, code));
|
||||
}
|
||||
const modulePath = (0, uni_cli_shared_1.normalizePath)(path_1.default.normalize(path_1.default.relative(process.env.UNI_INPUT_DIR, id)));
|
||||
const moduleHashId = (0, uni_cli_shared_1.hash)(modulePath);
|
||||
const globalName = type === 'wxs' ? uni_shared_1.WXS_MODULES : uni_shared_1.RENDERJS_MODULES;
|
||||
const { isProduction } = resolvedConfig;
|
||||
const resultCode = normalizeCode(type === 'wxs'
|
||||
? await transformWxs(code, filename, `__${globalName}['${moduleHashId}']`, isProduction, userConfig)
|
||||
: await transformRenderjs(code, filename, `__${globalName}['${moduleHashId}']`, isProduction, userConfig), globalName, isProduction);
|
||||
if (type === 'wxs') {
|
||||
wxsModulesCache.get(resolvedConfig).set(moduleHashId, resultCode);
|
||||
}
|
||||
else {
|
||||
renderjsModulesCache.get(resolvedConfig).set(moduleHashId, resultCode);
|
||||
}
|
||||
changed = true;
|
||||
debugRenderjs(type, modulePath, moduleHashId);
|
||||
return {
|
||||
code: `export default Comp => {
|
||||
;(Comp.$${type} || (Comp.$${type} = [])).push('${name}')
|
||||
;(Comp.$${globalName} || (Comp.$${globalName} = {}))['${name}'] = '${moduleHashId}'
|
||||
}`,
|
||||
map: { mappings: '' },
|
||||
};
|
||||
},
|
||||
generateBundle() {
|
||||
if (!changed) {
|
||||
return;
|
||||
}
|
||||
const wxsCode = [...wxsModulesCache.get(resolvedConfig).values()].join('\n');
|
||||
if (wxsCode) {
|
||||
this.emitFile({
|
||||
fileName: exports.APP_WXS_JS,
|
||||
source: `var __${uni_shared_1.WXS_MODULES}={};\n` + wxsCode,
|
||||
type: 'asset',
|
||||
});
|
||||
}
|
||||
const renderjsCode = [
|
||||
...renderjsModulesCache.get(resolvedConfig).values(),
|
||||
].join('\n');
|
||||
if (renderjsCode) {
|
||||
this.emitFile({
|
||||
fileName: exports.APP_RENDERJS_JS,
|
||||
source: `var __${uni_shared_1.RENDERJS_MODULES}={};\n` + renderjsCode,
|
||||
type: 'asset',
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.uniRenderjsPlugin = uniRenderjsPlugin;
|
||||
function normalizeCode(code, globalName, isProduction) {
|
||||
return code.replace(isProduction
|
||||
? `var __${globalName}=__${globalName}||{};`
|
||||
: `var __${globalName} = __${globalName} || {};`, '');
|
||||
}
|
||||
function transformWxs(code, filename, globalName, isProduction, config) {
|
||||
return (0, uni_cli_shared_1.transformWithEsbuild)(code, filename, {
|
||||
format: 'iife',
|
||||
globalName,
|
||||
target: config.build?.target || 'es6',
|
||||
minify: isProduction ? true : false,
|
||||
bundle: true,
|
||||
write: false,
|
||||
}).then((res) => {
|
||||
if (res.outputFiles) {
|
||||
return res.outputFiles[0].text;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
}
|
||||
function transformRenderjs(code, filename, globalName, isProduction, config) {
|
||||
return (0, uni_cli_shared_1.transformWithEsbuild)(code, filename, {
|
||||
format: 'iife',
|
||||
globalName,
|
||||
target: config.build?.target || 'es6',
|
||||
minify: isProduction ? true : false,
|
||||
bundle: true,
|
||||
write: false,
|
||||
}).then((res) => {
|
||||
if (res.outputFiles) {
|
||||
return res.outputFiles[0].text;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user