447 lines
19 KiB
JavaScript
447 lines
19 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.findUsingComponentsJson = exports.findMiniProgramUsingComponents = exports.isMiniProgramUsingComponent = exports.resetMiniProgramJsonFiles = exports.addMiniProgramUsingComponents = exports.addMiniProgramComponentJson = exports.addMiniProgramPageJson = exports.addMiniProgramAppJson = exports.findChangedJsonFiles = exports.normalizeJsonFilename = exports.resolveMiniProgramComponentPackageRoot = exports.findMiniProgramComponentPackageRoots = exports.findMiniProgramComponentPackageRoot = exports.addMiniProgramComponentPackageRoot = exports.findMiniProgramSubPackageRoot = exports.findUsingComponents = exports.findJsonFile = exports.getComponentJsonFilenames = exports.hasJsonFile = exports.isMiniProgramPageSfcFile = exports.isMiniProgramPageFile = void 0;
|
||
const path_1 = __importDefault(require("path"));
|
||
const fs_1 = __importDefault(require("fs"));
|
||
const shared_1 = require("@vue/shared");
|
||
const utils_1 = require("../../utils");
|
||
const resolve_1 = require("../../resolve");
|
||
const utils_2 = require("../../vue/utils");
|
||
const uni_shared_1 = require("@dcloudio/uni-shared");
|
||
const subpackage_1 = require("./subpackage");
|
||
const x_1 = require("../../x");
|
||
let appJsonCache = {};
|
||
let subPackageRootsCache = [];
|
||
let independentRootsCache = [];
|
||
const jsonFilesCache = new Map();
|
||
const jsonPagesCache = new Map();
|
||
const jsonComponentsCache = new Map();
|
||
const jsonUsingComponentsCache = new Map();
|
||
const componentPackageRootsCache = new Map();
|
||
const mainPackageRoot = '';
|
||
function isMiniProgramPageFile(file, inputDir) {
|
||
if (inputDir && path_1.default.isAbsolute(file)) {
|
||
file = (0, utils_1.normalizePath)(path_1.default.relative(inputDir, file));
|
||
}
|
||
return jsonPagesCache.has((0, utils_1.removeExt)(file));
|
||
}
|
||
exports.isMiniProgramPageFile = isMiniProgramPageFile;
|
||
function isMiniProgramPageSfcFile(file, inputDir) {
|
||
return (0, utils_2.isVueSfcFile)(file) && isMiniProgramPageFile(file, inputDir);
|
||
}
|
||
exports.isMiniProgramPageSfcFile = isMiniProgramPageSfcFile;
|
||
function hasJsonFile(filename) {
|
||
return (filename === 'app' ||
|
||
jsonPagesCache.has(filename) ||
|
||
jsonComponentsCache.has(filename));
|
||
}
|
||
exports.hasJsonFile = hasJsonFile;
|
||
function getComponentJsonFilenames() {
|
||
return [...jsonComponentsCache.keys()];
|
||
}
|
||
exports.getComponentJsonFilenames = getComponentJsonFilenames;
|
||
function findJsonFile(filename) {
|
||
if (filename === 'app') {
|
||
return appJsonCache;
|
||
}
|
||
return jsonPagesCache.get(filename) || jsonComponentsCache.get(filename);
|
||
}
|
||
exports.findJsonFile = findJsonFile;
|
||
function findUsingComponents(filename) {
|
||
return jsonUsingComponentsCache.get(filename);
|
||
}
|
||
exports.findUsingComponents = findUsingComponents;
|
||
function findMiniProgramSubPackageRoot(filename) {
|
||
if (!(0, x_1.isUniAppX)()) {
|
||
return;
|
||
}
|
||
return findSubPackageRoot(filename, subPackageRootsCache);
|
||
}
|
||
exports.findMiniProgramSubPackageRoot = findMiniProgramSubPackageRoot;
|
||
function addMiniProgramComponentPackageRoot(filename, packageRoot) {
|
||
if (!(0, x_1.isUniAppX)()) {
|
||
return;
|
||
}
|
||
const normalizedFilename = normalizeComponentPackageFilename(filename);
|
||
if (!normalizedFilename.startsWith('uni_modules/')) {
|
||
return;
|
||
}
|
||
const roots = componentPackageRootsCache.get(normalizedFilename) || new Set();
|
||
roots.add(packageRoot || mainPackageRoot);
|
||
componentPackageRootsCache.set(normalizedFilename, roots);
|
||
}
|
||
exports.addMiniProgramComponentPackageRoot = addMiniProgramComponentPackageRoot;
|
||
function findMiniProgramComponentPackageRoot(filename) {
|
||
if (!(0, x_1.isUniAppX)()) {
|
||
return;
|
||
}
|
||
const roots = getMiniProgramComponentPackageRoots(filename);
|
||
if (roots?.size === 1) {
|
||
const [root] = [...roots];
|
||
return root || undefined;
|
||
}
|
||
}
|
||
exports.findMiniProgramComponentPackageRoot = findMiniProgramComponentPackageRoot;
|
||
function findMiniProgramComponentPackageRoots(filename) {
|
||
if (!(0, x_1.isUniAppX)()) {
|
||
return;
|
||
}
|
||
const roots = getMiniProgramComponentPackageRoots(filename);
|
||
if (roots) {
|
||
if (roots.has(mainPackageRoot) || roots.size > 1) {
|
||
return [undefined];
|
||
}
|
||
return [...roots].map((root) => root || undefined);
|
||
}
|
||
}
|
||
exports.findMiniProgramComponentPackageRoots = findMiniProgramComponentPackageRoots;
|
||
function resolveMiniProgramComponentPackageRoot(filename, packageRoot) {
|
||
if (!(0, x_1.isUniAppX)()) {
|
||
return;
|
||
}
|
||
const roots = getMiniProgramComponentPackageRoots(filename);
|
||
if (!roots) {
|
||
return packageRoot;
|
||
}
|
||
if (roots.has(mainPackageRoot)) {
|
||
return;
|
||
}
|
||
if (roots.size > 1) {
|
||
return;
|
||
}
|
||
const normalizedPackageRoot = packageRoot || mainPackageRoot;
|
||
if (roots.has(normalizedPackageRoot)) {
|
||
return packageRoot;
|
||
}
|
||
if (roots.size === 1) {
|
||
const [root] = [...roots];
|
||
return root || undefined;
|
||
}
|
||
}
|
||
exports.resolveMiniProgramComponentPackageRoot = resolveMiniProgramComponentPackageRoot;
|
||
function normalizeJsonFilename(filename) {
|
||
return (0, utils_1.normalizeNodeModules)(filename);
|
||
}
|
||
exports.normalizeJsonFilename = normalizeJsonFilename;
|
||
function findChangedJsonFiles(supportGlobalUsingComponents = true) {
|
||
const changedJsonFiles = new Map();
|
||
function findChangedFile(filename, json) {
|
||
const cacheFilename = filename;
|
||
const outputFilename = (0, x_1.isUniAppX)()
|
||
? normalizeJsonPackageFilename(filename)
|
||
: filename;
|
||
const newJson = JSON.parse(JSON.stringify(json));
|
||
if (!newJson.usingComponents) {
|
||
newJson.usingComponents = {};
|
||
}
|
||
(0, shared_1.extend)(newJson.usingComponents, jsonUsingComponentsCache.get(cacheFilename));
|
||
// 格式化为相对路径,这样作为分包也可以直接运行
|
||
// app.json mp-baidu 在 win 不支持相对路径。所有平台改用绝对路径
|
||
if (outputFilename !== 'app') {
|
||
let usingComponents = newJson.usingComponents;
|
||
const independentRoot = findSubPackageRoot(outputFilename, independentRootsCache);
|
||
const supportGlobalUsingComponentsForFile = typeof supportGlobalUsingComponents === 'function'
|
||
? supportGlobalUsingComponents(filename)
|
||
: supportGlobalUsingComponents;
|
||
// 如果小程序不支持 global 的 usingComponents,或独立分包冷启动不能依赖 app.json
|
||
if (!supportGlobalUsingComponentsForFile || independentRoot) {
|
||
// 从取全局的 usingComponents 并补充到子组件 usingComponents 中
|
||
const globalUsingComponents = appJsonCache?.usingComponents || {};
|
||
const globalComponents = findUsingComponents('app') || {};
|
||
usingComponents = {
|
||
...globalUsingComponents,
|
||
...globalComponents,
|
||
...newJson.usingComponents,
|
||
};
|
||
}
|
||
if (independentRoot) {
|
||
validateIndependentUsingComponents(outputFilename, independentRoot, usingComponents);
|
||
}
|
||
Object.keys(usingComponents).forEach((name) => {
|
||
const componentFilename = (0, x_1.isUniAppX)()
|
||
? normalizeUsingComponentPackageFilename(usingComponents[name], outputFilename)
|
||
: usingComponents[name];
|
||
if (componentFilename.startsWith('/')) {
|
||
usingComponents[name] = (0, resolve_1.relativeFile)(outputFilename, componentFilename.slice(1));
|
||
}
|
||
});
|
||
newJson.usingComponents = usingComponents;
|
||
}
|
||
const jsonStr = JSON.stringify(newJson, null, 2);
|
||
if (jsonFilesCache.get(outputFilename) !== jsonStr) {
|
||
changedJsonFiles.set(outputFilename, jsonStr);
|
||
jsonFilesCache.set(outputFilename, jsonStr);
|
||
}
|
||
}
|
||
function findChangedFiles(jsonsCache) {
|
||
for (const name of jsonsCache.keys()) {
|
||
findChangedFile(name, jsonsCache.get(name));
|
||
}
|
||
}
|
||
if (process.env.UNI_COMPILE_TARGET !== 'uni_modules') {
|
||
findChangedFile('app', appJsonCache);
|
||
findChangedFiles(jsonPagesCache);
|
||
}
|
||
findChangedFiles(jsonComponentsCache);
|
||
return changedJsonFiles;
|
||
}
|
||
exports.findChangedJsonFiles = findChangedJsonFiles;
|
||
function findSubPackageRoot(filename, roots) {
|
||
return roots.find((root) => {
|
||
return filename === root || filename.startsWith(root + '/');
|
||
});
|
||
}
|
||
function validateIndependentUsingComponents(filename, root, usingComponents) {
|
||
Object.keys(usingComponents).forEach((name) => {
|
||
const componentFilename = usingComponents[name];
|
||
if (isLocalUsingComponent(componentFilename) &&
|
||
!isUsingComponentInRoot(componentFilename, root, filename)) {
|
||
throw new Error(`独立分包 "${root}" 不能在 "${filename}" 中使用 root 外组件 "${name}"(${componentFilename}),请移动到 "${root}" 内或改为页面局部组件。`);
|
||
}
|
||
});
|
||
}
|
||
function isUsingComponentInRoot(componentFilename, root, ownerFilename) {
|
||
const filename = normalizeUsingComponentFilename(componentFilename, ownerFilename);
|
||
return filename === root || filename.startsWith(root + '/');
|
||
}
|
||
function normalizeUsingComponentFilename(componentFilename, ownerFilename) {
|
||
if (componentFilename.startsWith('/')) {
|
||
return (0, utils_1.normalizePath)(componentFilename).replace(/^\/+/, '');
|
||
}
|
||
if (componentFilename.startsWith('.')) {
|
||
return (0, utils_1.normalizePath)(path_1.default.join(path_1.default.dirname(ownerFilename), componentFilename));
|
||
}
|
||
return (0, utils_1.normalizePath)(componentFilename);
|
||
}
|
||
function isLocalUsingComponent(componentFilename) {
|
||
return (!/^(?:plugin|dynamicLib|ext):\/\//.test(componentFilename) &&
|
||
!componentFilename.startsWith('weui-miniprogram'));
|
||
}
|
||
function addMiniProgramAppJson(appJson, options = {}) {
|
||
appJsonCache = appJson;
|
||
componentPackageRootsCache.clear();
|
||
subPackageRootsCache =
|
||
!(0, x_1.isUniAppX)() || options.subPackages === false
|
||
? []
|
||
: parseSubPackageRoots(appJson);
|
||
independentRootsCache = (0, subpackage_1.parseIndependentSubPackages)(appJson).map(({ root }) => root);
|
||
}
|
||
exports.addMiniProgramAppJson = addMiniProgramAppJson;
|
||
function addMiniProgramPageJson(filename, json) {
|
||
jsonPagesCache.set(filename, json);
|
||
}
|
||
exports.addMiniProgramPageJson = addMiniProgramPageJson;
|
||
function addMiniProgramComponentJson(filename, json) {
|
||
jsonComponentsCache.set(filename, json);
|
||
}
|
||
exports.addMiniProgramComponentJson = addMiniProgramComponentJson;
|
||
function addMiniProgramUsingComponents(filename, json) {
|
||
jsonUsingComponentsCache.set(filename, json);
|
||
}
|
||
exports.addMiniProgramUsingComponents = addMiniProgramUsingComponents;
|
||
function resetMiniProgramJsonFiles() {
|
||
appJsonCache = {};
|
||
subPackageRootsCache = [];
|
||
independentRootsCache = [];
|
||
(0, subpackage_1.setIndependentSubPackages)([]);
|
||
componentPackageRootsCache.clear();
|
||
jsonFilesCache.clear();
|
||
jsonPagesCache.clear();
|
||
jsonComponentsCache.clear();
|
||
jsonUsingComponentsCache.clear();
|
||
}
|
||
exports.resetMiniProgramJsonFiles = resetMiniProgramJsonFiles;
|
||
function parseSubPackageRoots(pagesJson) {
|
||
const subPackages = pagesJson?.subPackages || pagesJson?.subpackages || [];
|
||
if (!Array.isArray(subPackages)) {
|
||
return [];
|
||
}
|
||
return subPackages
|
||
.map((subPackage) => {
|
||
if (!subPackage ||
|
||
subPackage.independent === true ||
|
||
typeof subPackage.root !== 'string') {
|
||
return '';
|
||
}
|
||
return (0, utils_1.normalizePath)(subPackage.root).replace(/^\/+|\/+$/g, '');
|
||
})
|
||
.filter(Boolean)
|
||
.sort((a, b) => b.length - a.length);
|
||
}
|
||
function normalizeJsonPackageFilename(filename) {
|
||
const unrootedFilename = withoutSubPackageRootForUniModules(filename);
|
||
const ownerRoot = findMiniProgramSubPackageRoot(filename);
|
||
const roots = getMiniProgramComponentPackageRoots(unrootedFilename);
|
||
if (unrootedFilename !== filename &&
|
||
roots &&
|
||
(roots.has(mainPackageRoot) ||
|
||
roots.size > 1 ||
|
||
!ownerRoot ||
|
||
!roots.has(ownerRoot))) {
|
||
return unrootedFilename;
|
||
}
|
||
return filename;
|
||
}
|
||
function normalizeUsingComponentPackageFilename(componentFilename, ownerFilename) {
|
||
if (!componentFilename.startsWith('/')) {
|
||
return componentFilename;
|
||
}
|
||
const normalizedFilename = (0, utils_1.normalizePath)(componentFilename).replace(/^\/+/, '');
|
||
const unrootedFilename = withoutSubPackageRootForUniModules(normalizedFilename);
|
||
if (!unrootedFilename.startsWith('uni_modules/')) {
|
||
return componentFilename;
|
||
}
|
||
const roots = getMiniProgramComponentPackageRoots(unrootedFilename);
|
||
if (!roots) {
|
||
return componentFilename;
|
||
}
|
||
if (roots.has(mainPackageRoot) || roots.size > 1) {
|
||
return '/' + unrootedFilename;
|
||
}
|
||
const ownerRoot = findMiniProgramSubPackageRoot(ownerFilename);
|
||
if (ownerRoot && roots.has(ownerRoot)) {
|
||
return '/' + `${ownerRoot}/${unrootedFilename}`;
|
||
}
|
||
const packageRoot = findMiniProgramComponentPackageRoot(unrootedFilename);
|
||
return '/' + (packageRoot ? `${packageRoot}/` : '') + unrootedFilename;
|
||
}
|
||
function withoutSubPackageRootForUniModules(filename) {
|
||
for (const root of subPackageRootsCache) {
|
||
if (filename.startsWith(`${root}/uni_modules/`)) {
|
||
return filename.slice(root.length + 1);
|
||
}
|
||
}
|
||
return filename;
|
||
}
|
||
function normalizeComponentPackageFilename(filename) {
|
||
let normalizedFilename = withoutSubPackageRootForUniModules((0, utils_1.normalizePath)(filename).split('?')[0].replace(/^\/+/, ''));
|
||
const uniModulesIndex = normalizedFilename.indexOf('uni_modules/');
|
||
if (uniModulesIndex > -1) {
|
||
normalizedFilename = normalizedFilename.slice(uniModulesIndex);
|
||
}
|
||
return (0, utils_1.removeExt)(normalizedFilename);
|
||
}
|
||
function getMiniProgramComponentPackageRoots(filename) {
|
||
return componentPackageRootsCache.get(normalizeComponentPackageFilename(filename));
|
||
}
|
||
function isMiniProgramUsingComponent(name, options) {
|
||
return !!findMiniProgramUsingComponents(options)[name];
|
||
}
|
||
exports.isMiniProgramUsingComponent = isMiniProgramUsingComponent;
|
||
function findMiniProgramUsingComponents({ filename, inputDir, componentsDir, }) {
|
||
const globalUsingComponents = appJsonCache && appJsonCache.usingComponents;
|
||
// 避免 uniad 相关插件 被当作 vue 组件处理
|
||
const enableUniAdPlugin = process.env.UNI_PLATFORM === 'mp-weixin';
|
||
const miniProgramComponents = enableUniAdPlugin
|
||
? uni_shared_1.UNI_AD_PLUGINS.reduce((acc, name) => {
|
||
acc[name] = 'plugin';
|
||
return acc;
|
||
}, {})
|
||
: {};
|
||
if (globalUsingComponents) {
|
||
(0, shared_1.extend)(miniProgramComponents, findMiniProgramUsingComponent(globalUsingComponents, componentsDir));
|
||
}
|
||
const ownerFilename = (0, utils_1.removeExt)((0, utils_1.normalizeMiniProgramFilename)(filename, inputDir));
|
||
const jsonFile = findJsonFile(ownerFilename);
|
||
if (jsonFile) {
|
||
if (jsonFile.usingComponents) {
|
||
(0, shared_1.extend)(miniProgramComponents, findMiniProgramUsingComponent(jsonFile.usingComponents, componentsDir, ownerFilename));
|
||
}
|
||
// mp-baidu 特有
|
||
if (jsonFile.usingSwanComponents) {
|
||
(0, shared_1.extend)(miniProgramComponents, findMiniProgramUsingComponent(jsonFile.usingSwanComponents, componentsDir, ownerFilename));
|
||
}
|
||
}
|
||
return miniProgramComponents;
|
||
}
|
||
exports.findMiniProgramUsingComponents = findMiniProgramUsingComponents;
|
||
function findMiniProgramUsingComponent(usingComponents, componentsDir, ownerFilename) {
|
||
return Object.keys(usingComponents).reduce((res, name) => {
|
||
const path = usingComponents[name];
|
||
if (path.includes('plugin://')) {
|
||
// mp-weixin & mp-alipay
|
||
res[name] = 'plugin';
|
||
}
|
||
else if (path.includes('dynamicLib://')) {
|
||
// mp-baidu
|
||
res[name] = 'dynamicLib';
|
||
}
|
||
else if (path.includes('ext://')) {
|
||
// mp-toutiao
|
||
res[name] = 'ext';
|
||
}
|
||
else if (componentsDir &&
|
||
path.includes(componentsDir + '/') &&
|
||
findUsingComponentsJson(path, componentsDir, ownerFilename).renderer ===
|
||
'xr-frame') {
|
||
// mp-weixin & x-frame
|
||
res[name] = 'xr-frame';
|
||
}
|
||
else if (componentsDir && path.includes(componentsDir + '/')) {
|
||
res[name] = 'component';
|
||
}
|
||
else if (path.startsWith('weui-miniprogram')) {
|
||
res[name] = 'weui';
|
||
}
|
||
return res;
|
||
}, {});
|
||
}
|
||
/**
|
||
* 开发者在配置usingComponents时,可以指向具体路径(不含文件后缀),也可以指向目录(指向目录时查找目录下的index.wxml/.json等)
|
||
* 当usingComponents配置为`"demo": "/components/demo"`时,查找优先级为:
|
||
* 1. /components/demo.wxml
|
||
* 2. /components/demo/index.wxml
|
||
*
|
||
* 注意如下配置是非法的:
|
||
* - "demo": "/components/demo.wxml"
|
||
* - "demo": "/components/demo/"
|
||
*
|
||
* 注意用户的pages.json内可以配置如下三种路径:
|
||
* - "demo": "/wxcomponents/demo"
|
||
* - "demo": "wxcomponents/demo"
|
||
* - [TODO 待确认] "demo": "../wxcomponents/demo"
|
||
*/
|
||
function findUsingComponentsJson(pathInpages, componentsDir, ownerFilename) {
|
||
// 兼容test case
|
||
if (!process.env.UNI_INPUT_DIR)
|
||
return {};
|
||
const fulldir = resolveUsingComponentsDir(pathInpages, componentsDir, ownerFilename);
|
||
if (!fulldir) {
|
||
console.warn(`${pathInpages} 路径里没有找到对应的 ${componentsDir} 目录`);
|
||
return {};
|
||
}
|
||
let jsonPath = fulldir + '.json';
|
||
if (fs_1.default.existsSync(jsonPath)) {
|
||
return require(jsonPath);
|
||
}
|
||
jsonPath = path_1.default.resolve(fulldir, 'index.json');
|
||
if (fs_1.default.existsSync(jsonPath)) {
|
||
return require(jsonPath);
|
||
}
|
||
console.warn(`${pathInpages} 路径下没有找到对应的json文件`);
|
||
return {};
|
||
}
|
||
exports.findUsingComponentsJson = findUsingComponentsJson;
|
||
function resolveUsingComponentsDir(pathInpages, componentsDir, ownerFilename) {
|
||
const normalizedPath = (0, utils_1.normalizePath)(pathInpages);
|
||
if (normalizedPath.startsWith('/')) {
|
||
return path_1.default.resolve(process.env.UNI_INPUT_DIR, '.' + normalizedPath);
|
||
}
|
||
if (ownerFilename && normalizedPath.startsWith('.')) {
|
||
return path_1.default.resolve(process.env.UNI_INPUT_DIR, path_1.default.dirname(ownerFilename), normalizedPath);
|
||
}
|
||
const marker = componentsDir + '/';
|
||
const index = normalizedPath.indexOf(marker);
|
||
if (index === -1) {
|
||
return;
|
||
}
|
||
const dir = normalizedPath.slice(index + componentsDir.length);
|
||
if (!dir) {
|
||
return;
|
||
}
|
||
return path_1.default.resolve(process.env.UNI_INPUT_DIR, componentsDir, '.' + dir);
|
||
}
|