You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
8.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uniPagesJsonPlugin = exports.getNVueCssPaths = void 0;
const path_1 = __importDefault(require("path"));
const debug_1 = __importDefault(require("debug"));
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
const entry_1 = require("./entry");
const uni_i18n_1 = require("@dcloudio/uni-i18n");
const shared_1 = require("@vue/shared");
const debugPagesJson = (0, debug_1.default)('uni:pages-json');
const nvueCssPathsCache = new Map();
function getNVueCssPaths(config) {
return nvueCssPathsCache.get(config);
}
exports.getNVueCssPaths = getNVueCssPaths;
function uniPagesJsonPlugin(options) {
let resolvedConfig;
const platform = process.env.UNI_PLATFORM;
const inputDir = process.env.UNI_INPUT_DIR;
return (0, uni_cli_shared_1.defineUniPagesJsonPlugin)((opts) => {
let allPagePaths = [];
let isFirst = true;
return {
name: 'uni:mp-pages-json',
enforce: 'pre',
configResolved(config) {
resolvedConfig = config;
},
transform(code, id) {
if (process.env.UNI_APP_X === 'true') {
if (isFirst && allPagePaths.length) {
const { filename } = (0, uni_cli_shared_1.parseVueRequest)(id);
if (filename.endsWith('.vue') || filename.endsWith('.uvue')) {
const vueFilename = (0, uni_cli_shared_1.removeExt)((0, uni_cli_shared_1.normalizePath)(path_1.default.relative(process.env.UNI_INPUT_DIR, filename)));
// 项目内的
if (!vueFilename.startsWith('.')) {
// const index = allPagePaths.indexOf(pagePath)
// if (index > -1) {
if ((0, uni_cli_shared_1.runByHBuilderX)()) {
console.log(`当前工程${allPagePaths.length}个页面,正在编译${vueFilename}...${'\u200D'}`);
}
// }
}
}
}
}
if (!opts.filter(id)) {
return null;
}
if (process.env.UNI_APP_X === 'true') {
// 调整换行符,确保 parseTree 的loc正确
const jsonCode = code.replace(/\r\n/g, '\n');
try {
(0, uni_cli_shared_1.checkPagesJson)((0, uni_cli_shared_1.preUVueJson)(jsonCode, 'pages.json'), process.env.UNI_INPUT_DIR);
}
catch (err) {
if (err.loc) {
const error = (0, uni_cli_shared_1.createRollupError)('uni:mp-pages-json', path_1.default.resolve(process.env.UNI_INPUT_DIR, 'pages.json'), err, jsonCode);
this.error(error);
}
else {
throw err;
}
}
}
this.addWatchFile(path_1.default.resolve(inputDir, 'pages.json'));
(0, uni_cli_shared_1.getLocaleFiles)(path_1.default.resolve(inputDir, 'locale')).forEach((filepath) => {
this.addWatchFile(filepath);
});
const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir);
const { appJson, pageJsons, nvuePages } = (0, uni_cli_shared_1.parseMiniProgramPagesJson)(code, platform, {
debug: !!manifestJson.debug,
darkmode: options.app.darkmode,
networkTimeout: manifestJson.networkTimeout,
subpackages: !!options.app.subpackages,
...options.json,
});
nvueCssPathsCache.set(resolvedConfig, nvuePages.map((pagePath) => pagePath + options.style.extname));
// add source
(0, uni_cli_shared_1.mergeMiniProgramAppJson)(appJson, manifestJson[platform], options.project?.source ?? {});
if (process.env.UNI_APP_X === 'true') {
// 当前平台支持workers且manifest.json中配置了workers则合并workers配置
if (options.app.workers && Object.keys((0, uni_cli_shared_1.getWorkers)()).length) {
// 如果没有配置则默认为workers目录
appJson.workers = manifestJson.workers || 'workers';
}
if ((0, shared_1.isPlainObject)(appJson.workers) && appJson.workers.path) {
// 微信小程序测试对象结构的话如果isSubpackage是false会报找不到
// 故只有isSubpackage为true保持对象结构否则用字符串
if (!appJson.workers.isSubpackage) {
appJson.workers = appJson.workers.path;
}
}
}
if (options.json?.formatAppJson) {
options.json.formatAppJson(appJson, manifestJson, pageJsons);
}
// 使用 once 获取的话,可以节省编译时间,但 i18n 内容发生变化时pages.json 不会自动更新
const i18nOptions = (0, uni_cli_shared_1.initI18nOptionsOnce)(platform, inputDir, false, true);
if (i18nOptions) {
const { locale, locales, delimiters } = i18nOptions;
(0, uni_i18n_1.parseI18nJson)(appJson, locales[locale], delimiters);
(0, uni_i18n_1.parseI18nJson)(pageJsons, locales[locale], delimiters);
}
const { normalize } = options.app;
(0, uni_cli_shared_1.addMiniProgramAppJson)(normalize ? normalize(appJson) : appJson);
Object.keys(pageJsons).forEach((name) => {
if (isNormalPage(name)) {
(0, uni_cli_shared_1.addMiniProgramPageJson)(name, pageJsons[name]);
allPagePaths.push(name);
}
});
return {
code: `import './${uni_cli_shared_1.MANIFEST_JSON_JS}'\n` + importPagesCode(appJson),
map: { mappings: '' },
};
},
generateBundle() {
(0, uni_cli_shared_1.findChangedJsonFiles)(options.app.usingComponents).forEach((value, key) => {
debugPagesJson('json.changed', key);
this.emitFile({
type: 'asset',
fileName: key + '.json',
source: value,
});
});
},
buildEnd() {
isFirst = false;
},
};
});
}
exports.uniPagesJsonPlugin = uniPagesJsonPlugin;
/**
* 字节跳动小程序可以配置 ext:// 开头的插件页面模板,如 ext://microapp-trade-plugin/order-confirm
* @param pagePath
* @returns
*/
function isNormalPage(pagePath) {
return !pagePath.startsWith('ext://');
}
function importPagesCode(pagesJson) {
const importPagesCode = [];
function importPageCode(pagePath) {
if (!isNormalPage(pagePath)) {
return;
}
const pagePathWithExtname = (0, uni_cli_shared_1.normalizePagePath)(pagePath, process.env.UNI_PLATFORM);
if (pagePathWithExtname) {
importPagesCode.push(`import('${(0, entry_1.virtualPagePath)(pagePathWithExtname)}')`);
}
}
pagesJson.pages.forEach((pagePath) => importPageCode(pagePath));
if (pagesJson.subPackages) {
pagesJson.subPackages.forEach(({ root, pages }) => {
pages &&
pages.forEach((pagePath) => importPageCode(path_1.default.join(root, pagePath)));
});
}
let workerCode = [];
if (process.env.UNI_APP_X === 'true') {
const workers = (0, uni_cli_shared_1.getWorkers)();
workerCode = Object.keys(workers).map((key) => {
return `import('@/${key}')`;
});
}
return `if(!Math){
${importPagesCode.join('\n')}
${workerCode.join('\n')}
}`;
}