前端 uni-app 重写: 9页面 H5

This commit is contained in:
zhiqui
2026-11-15 15:09:37 +08:00
parent 513b53672e
commit ea7c2529e9
24104 changed files with 3667749 additions and 0 deletions
@@ -0,0 +1 @@
export declare function getMiniProgramAIPaths(inputDir?: string, platform?: UniApp.PLATFORM): string[];
@@ -0,0 +1,31 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMiniProgramAIPaths = void 0;
const shared_1 = require("@vue/shared");
const manifest_1 = require("../manifest");
function getMiniProgramAIPaths(inputDir, platform) {
if (!inputDir || !platform) {
return [];
}
const manifestJson = (0, manifest_1.parseManifestJsonOnce)(inputDir);
const config = manifestJson[platform];
if (!config?.agent) {
return [];
}
const agentConfig = config.agent;
const paths = [];
const instruction = agentConfig.instruction;
if (instruction) {
paths.push(instruction);
}
const pageMetadata = agentConfig.pageMetadata;
if (pageMetadata) {
paths.push(pageMetadata);
}
const skills = agentConfig.skills;
if ((0, shared_1.isArray)(skills) && skills.length > 0) {
paths.push(...skills.map((skill) => skill.path).filter(Boolean));
}
return paths;
}
exports.getMiniProgramAIPaths = getMiniProgramAIPaths;
@@ -0,0 +1,7 @@
export * from './jsonFile';
export { getMiniProgramAIPaths } from './agent';
export { AppJson, ComponentJson, MiniProgramComponentsType } from './types';
export { mergeMiniProgramAppJson, parseMiniProgramPagesJson } from './pages';
export { parseMiniProgramProjectJson } from './project';
export { MP_INDEPENDENT_ROOT_QUERY, MP_INDEPENDENT_MAIN_PREFIX, MP_INDEPENDENT_VIRTUAL_ROOT_QUERY, formatIndependentVirtualId, getIndependentRootByFilename, getIndependentRoots, getIndependentSubPackages, hasIndependentRoot, isAppPagesJson, isInIndependentRoot, normalizeIndependentRoot, parseIndependentMainRoot, parseIndependentRoot, parseIndependentSubPackages, parseIndependentVirtualRoot, resolveIndependentRoot, setIndependentSubPackages, stringifyIndependentRoots, withIndependentRoot, withIndependentRootIfNeeded, withoutIndependentRoot, } from './subpackage';
export type { IndependentSubPackage } from './subpackage';
@@ -0,0 +1,47 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withoutIndependentRoot = exports.withIndependentRootIfNeeded = exports.withIndependentRoot = exports.stringifyIndependentRoots = exports.setIndependentSubPackages = exports.resolveIndependentRoot = exports.parseIndependentVirtualRoot = exports.parseIndependentSubPackages = exports.parseIndependentRoot = exports.parseIndependentMainRoot = exports.normalizeIndependentRoot = exports.isInIndependentRoot = exports.isAppPagesJson = exports.hasIndependentRoot = exports.getIndependentSubPackages = exports.getIndependentRoots = exports.getIndependentRootByFilename = exports.formatIndependentVirtualId = exports.MP_INDEPENDENT_VIRTUAL_ROOT_QUERY = exports.MP_INDEPENDENT_MAIN_PREFIX = exports.MP_INDEPENDENT_ROOT_QUERY = exports.parseMiniProgramProjectJson = exports.parseMiniProgramPagesJson = exports.mergeMiniProgramAppJson = exports.getMiniProgramAIPaths = void 0;
__exportStar(require("./jsonFile"), exports);
var agent_1 = require("./agent");
Object.defineProperty(exports, "getMiniProgramAIPaths", { enumerable: true, get: function () { return agent_1.getMiniProgramAIPaths; } });
var pages_1 = require("./pages");
Object.defineProperty(exports, "mergeMiniProgramAppJson", { enumerable: true, get: function () { return pages_1.mergeMiniProgramAppJson; } });
Object.defineProperty(exports, "parseMiniProgramPagesJson", { enumerable: true, get: function () { return pages_1.parseMiniProgramPagesJson; } });
var project_1 = require("./project");
Object.defineProperty(exports, "parseMiniProgramProjectJson", { enumerable: true, get: function () { return project_1.parseMiniProgramProjectJson; } });
var subpackage_1 = require("./subpackage");
Object.defineProperty(exports, "MP_INDEPENDENT_ROOT_QUERY", { enumerable: true, get: function () { return subpackage_1.MP_INDEPENDENT_ROOT_QUERY; } });
Object.defineProperty(exports, "MP_INDEPENDENT_MAIN_PREFIX", { enumerable: true, get: function () { return subpackage_1.MP_INDEPENDENT_MAIN_PREFIX; } });
Object.defineProperty(exports, "MP_INDEPENDENT_VIRTUAL_ROOT_QUERY", { enumerable: true, get: function () { return subpackage_1.MP_INDEPENDENT_VIRTUAL_ROOT_QUERY; } });
Object.defineProperty(exports, "formatIndependentVirtualId", { enumerable: true, get: function () { return subpackage_1.formatIndependentVirtualId; } });
Object.defineProperty(exports, "getIndependentRootByFilename", { enumerable: true, get: function () { return subpackage_1.getIndependentRootByFilename; } });
Object.defineProperty(exports, "getIndependentRoots", { enumerable: true, get: function () { return subpackage_1.getIndependentRoots; } });
Object.defineProperty(exports, "getIndependentSubPackages", { enumerable: true, get: function () { return subpackage_1.getIndependentSubPackages; } });
Object.defineProperty(exports, "hasIndependentRoot", { enumerable: true, get: function () { return subpackage_1.hasIndependentRoot; } });
Object.defineProperty(exports, "isAppPagesJson", { enumerable: true, get: function () { return subpackage_1.isAppPagesJson; } });
Object.defineProperty(exports, "isInIndependentRoot", { enumerable: true, get: function () { return subpackage_1.isInIndependentRoot; } });
Object.defineProperty(exports, "normalizeIndependentRoot", { enumerable: true, get: function () { return subpackage_1.normalizeIndependentRoot; } });
Object.defineProperty(exports, "parseIndependentMainRoot", { enumerable: true, get: function () { return subpackage_1.parseIndependentMainRoot; } });
Object.defineProperty(exports, "parseIndependentRoot", { enumerable: true, get: function () { return subpackage_1.parseIndependentRoot; } });
Object.defineProperty(exports, "parseIndependentSubPackages", { enumerable: true, get: function () { return subpackage_1.parseIndependentSubPackages; } });
Object.defineProperty(exports, "parseIndependentVirtualRoot", { enumerable: true, get: function () { return subpackage_1.parseIndependentVirtualRoot; } });
Object.defineProperty(exports, "resolveIndependentRoot", { enumerable: true, get: function () { return subpackage_1.resolveIndependentRoot; } });
Object.defineProperty(exports, "setIndependentSubPackages", { enumerable: true, get: function () { return subpackage_1.setIndependentSubPackages; } });
Object.defineProperty(exports, "stringifyIndependentRoots", { enumerable: true, get: function () { return subpackage_1.stringifyIndependentRoots; } });
Object.defineProperty(exports, "withIndependentRoot", { enumerable: true, get: function () { return subpackage_1.withIndependentRoot; } });
Object.defineProperty(exports, "withIndependentRootIfNeeded", { enumerable: true, get: function () { return subpackage_1.withIndependentRootIfNeeded; } });
Object.defineProperty(exports, "withoutIndependentRoot", { enumerable: true, get: function () { return subpackage_1.withoutIndependentRoot; } });
@@ -0,0 +1,51 @@
import type { ComponentJson, MiniProgramComponentsType, PageWindowOptions, UsingComponents } from './types';
export declare function isMiniProgramPageFile(file: string, inputDir?: string): boolean;
export declare function isMiniProgramPageSfcFile(file: string, inputDir?: string): boolean;
export declare function hasJsonFile(filename: string): boolean;
export declare function getComponentJsonFilenames(): string[];
export declare function findJsonFile(filename: string): Record<string, any> | ComponentJson | PageWindowOptions | undefined;
export declare function findUsingComponents(filename: string): UsingComponents | undefined;
export declare function findMiniProgramSubPackageRoot(filename: string): string | undefined;
export declare function addMiniProgramComponentPackageRoot(filename: string, packageRoot?: string): void;
export declare function findMiniProgramComponentPackageRoot(filename: string): string | undefined;
export declare function findMiniProgramComponentPackageRoots(filename: string): (string | undefined)[] | undefined;
export declare function resolveMiniProgramComponentPackageRoot(filename: string, packageRoot?: string): string | undefined;
export declare function normalizeJsonFilename(filename: string): string;
export declare function findChangedJsonFiles(supportGlobalUsingComponents?: boolean | ((filename: string) => boolean)): Map<string, string>;
export declare function addMiniProgramAppJson(appJson: Record<string, any>, options?: {
subPackages?: boolean;
}): void;
export declare function addMiniProgramPageJson(filename: string, json: PageWindowOptions): void;
export declare function addMiniProgramComponentJson(filename: string, json: ComponentJson): void;
export declare function addMiniProgramUsingComponents(filename: string, json: UsingComponents): void;
export declare function resetMiniProgramJsonFiles(): void;
export declare function isMiniProgramUsingComponent(name: string, options: {
filename: string;
inputDir: string;
componentsDir?: string;
}): boolean;
interface MiniProgramComponents {
[name: string]: MiniProgramComponentsType;
}
export declare function findMiniProgramUsingComponents({ filename, inputDir, componentsDir, }: {
filename: string;
inputDir: string;
componentsDir?: string;
}): MiniProgramComponents;
/**
* 开发者在配置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"
*/
export declare function findUsingComponentsJson(pathInpages: string, componentsDir: string, ownerFilename?: string): Record<any, any>;
export {};
@@ -0,0 +1,446 @@
"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);
}
@@ -0,0 +1,18 @@
import type { AppJson, NetworkTimeout, PageWindowOptions } from './types';
interface ParsePagesJsonOptions {
debug?: boolean;
darkmode?: boolean;
subpackages: boolean;
independentSubpackages?: boolean;
windowOptionsMap?: Record<string, string | string[]>;
tabBarOptionsMap?: Record<string, string>;
tabBarItemOptionsMap?: Record<string, string>;
networkTimeout?: NetworkTimeout;
}
export declare function parseMiniProgramPagesJson(jsonStr: string, platform: UniApp.PLATFORM, options?: ParsePagesJsonOptions): {
appJson: AppJson;
pageJsons: Record<string, PageWindowOptions>;
nvuePages: string[];
};
export declare function mergeMiniProgramAppJson(appJson: Record<string, any>, platformJson?: Record<string, any>, source?: Record<string, any>): void;
export {};
@@ -0,0 +1,158 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeMiniProgramAppJson = exports.parseMiniProgramPagesJson = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const shared_1 = require("@vue/shared");
const json_1 = require("../json");
const pages_1 = require("../pages");
const utils_1 = require("./utils");
const utils_2 = require("../../utils");
const project_1 = require("./project");
const manifest_1 = require("../manifest");
const theme_1 = require("../theme");
function parseMiniProgramPagesJson(jsonStr, platform, options = { subpackages: false }) {
return parsePagesJson(jsonStr, platform, options);
}
exports.parseMiniProgramPagesJson = parseMiniProgramPagesJson;
const NON_APP_JSON_KEYS = [
'unipush',
'secureNetwork',
'usingComponents',
'optimization',
'scopedSlotsCompiler',
'uniStatistics',
'mergeVirtualHostAttributes',
'styleIsolation',
'enableVirtualHost',
'nativeTags',
];
function mergeMiniProgramAppJson(appJson, platformJson = {}, source = {}) {
Object.keys(source).forEach((key) => {
if (!project_1.projectKeys.includes(key)) {
project_1.projectKeys.push(key);
}
});
Object.keys(platformJson).forEach((name) => {
if (!(0, project_1.isMiniProgramProjectJsonKey)(name) &&
!NON_APP_JSON_KEYS.includes(name)) {
appJson[name] = platformJson[name];
}
});
}
exports.mergeMiniProgramAppJson = mergeMiniProgramAppJson;
function parsePagesJson(jsonStr, platform, { debug, darkmode, independentSubpackages, networkTimeout, subpackages, windowOptionsMap, tabBarOptionsMap, tabBarItemOptionsMap, } = {
subpackages: false,
}) {
let appJson = {
pages: [],
};
let pageJsons = {};
let nvuePages = [];
// preprocess
const pagesJson = (0, json_1.parseJson)(jsonStr, true, 'pages.json');
if (!pagesJson) {
throw new Error(`[vite] Error: pages.json parse failed.\n`);
}
function addPageJson(pagePath, style) {
const filename = path_1.default.join(process.env.UNI_INPUT_DIR, pagePath);
if (fs_1.default.existsSync(filename + '.nvue') &&
!fs_1.default.existsSync(filename + '.vue')) {
nvuePages.push(pagePath);
}
const windowOptions = {};
if (platform === 'mp-baidu') {
// 仅百度小程序需要页面配置 component:true
// 快手小程序反而不能配置 component:true,故不能统一添加,目前硬编码处理
windowOptions.component = true;
}
pageJsons[pagePath] = (0, shared_1.extend)(windowOptions, (0, utils_1.parseWindowOptions)(style, platform, windowOptionsMap));
}
// pages
(0, pages_1.validatePages)(pagesJson, jsonStr);
pagesJson.pages.forEach((page) => {
appJson.pages.push(page.path);
addPageJson(page.path, page.style);
});
// subpackages
pagesJson.subPackages = pagesJson.subPackages || pagesJson.subpackages;
if (pagesJson.subPackages) {
if (subpackages) {
appJson.subPackages = pagesJson.subPackages.map((subPackage) => {
const { root, pages, ...rest } = subPackage;
if (!independentSubpackages) {
delete rest.independent;
}
return (0, shared_1.extend)({
root,
pages: pages.map((page) => {
addPageJson((0, utils_2.normalizePath)(path_1.default.join(root, page.path)), page.style);
return page.path;
}),
}, rest);
});
}
else {
pagesJson.subPackages.forEach(({ root, pages }) => {
pages.forEach((page) => {
const pagePath = (0, utils_2.normalizePath)(path_1.default.join(root, page.path));
appJson.pages.push(pagePath);
addPageJson(pagePath, page.style);
});
});
}
}
// window
if (pagesJson.globalStyle) {
const windowOptions = (0, utils_1.parseWindowOptions)(pagesJson.globalStyle, platform, windowOptionsMap);
const { usingComponents } = windowOptions;
if (usingComponents) {
delete windowOptions.usingComponents;
appJson.usingComponents = usingComponents;
}
else {
delete appJson.usingComponents;
}
appJson.window = windowOptions;
}
// tabBar
if (pagesJson.tabBar) {
const tabBar = (0, utils_1.parseTabBar)(pagesJson.tabBar, platform, tabBarOptionsMap, tabBarItemOptionsMap);
if (tabBar) {
appJson.tabBar = tabBar;
}
}
(0, pages_1.filterPlatformPages)(platform, pagesJson);
['preloadRule', 'workers', 'plugins', 'entryPagePath'].forEach((name) => {
if ((0, shared_1.hasOwn)(pagesJson, name)) {
appJson[name] = pagesJson[name];
}
});
if (debug) {
appJson.debug = debug;
}
if (networkTimeout) {
appJson.networkTimeout = networkTimeout;
}
const manifestJson = (0, manifest_1.getPlatformManifestJsonOnce)();
if (!darkmode) {
const { pages, window, tabBar } = (0, theme_1.initTheme)(manifestJson, appJson);
(0, shared_1.extend)(appJson, JSON.parse(JSON.stringify({ pages, window, tabBar })));
delete appJson.darkmode;
delete appJson.themeLocation;
pageJsons = (0, theme_1.initTheme)(manifestJson, pageJsons);
}
else {
const themeLocation = manifestJson.themeLocation || 'theme.json';
if ((0, theme_1.hasThemeJson)(path_1.default.join(process.env.UNI_INPUT_DIR, themeLocation)))
appJson.themeLocation = themeLocation;
}
return {
appJson,
pageJsons,
nvuePages,
};
}
@@ -0,0 +1,15 @@
interface ParseMiniProgramProjectJsonOptions {
template: Record<string, any>;
pagesJson: UniApp.PagesJson;
}
interface ProjectConfig {
appid: string;
projectname: string;
condition?: {
miniprogram?: UniApp.PagesJson['condition'];
};
}
export declare const projectKeys: string[];
export declare function isMiniProgramProjectJsonKey(name: string): boolean;
export declare function parseMiniProgramProjectJson(jsonStr: string, platform: UniApp.PLATFORM, { template, pagesJson }: ParseMiniProgramProjectJsonOptions): ProjectConfig;
export {};
@@ -0,0 +1,118 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseMiniProgramProjectJson = exports.isMiniProgramProjectJsonKey = exports.projectKeys = void 0;
const shared_1 = require("@vue/shared");
const merge_1 = require("merge");
const json_1 = require("../json");
exports.projectKeys = [
'appid',
'setting',
'miniprogramRoot',
'cloudfunctionRoot',
'qcloudRoot',
'pluginRoot',
'compileType',
'libVersion',
'projectname',
'packOptions',
'debugOptions',
'scripts',
'cloudbaseRoot',
'watchOptions',
];
function isMiniProgramProjectJsonKey(name) {
return exports.projectKeys.includes(name);
}
exports.isMiniProgramProjectJsonKey = isMiniProgramProjectJsonKey;
function parseMiniProgramProjectJson(jsonStr, platform, { template, pagesJson }) {
const projectJson = JSON.parse(JSON.stringify(template));
const manifestJson = (0, json_1.parseJson)(jsonStr, false, '');
if (manifestJson) {
projectJson.projectname = manifestJson.name;
// 用户的平台配置
const platformConfig = manifestJson[platform];
if (platformConfig) {
const setProjectJson = (name) => {
if ((0, shared_1.hasOwn)(platformConfig, name)) {
if ((0, shared_1.isPlainObject)(platformConfig[name]) &&
(0, shared_1.isPlainObject)(projectJson[name])) {
;
projectJson[name] = (0, merge_1.recursive)(true, projectJson[name], platformConfig[name]);
}
else {
;
projectJson[name] = platformConfig[name];
}
}
};
// 读取 template 中的配置
Object.keys(template).forEach((name) => {
if (!exports.projectKeys.includes(name)) {
exports.projectKeys.push(name);
}
});
// common mp config
exports.projectKeys.forEach((name) => {
setProjectJson(name);
});
// 使用了微信小程序手势系统,自动开启 ES6=>ES5
platform === 'mp-weixin' &&
weixinSkyline(platformConfig) &&
openES62ES5(projectJson);
}
}
// 其实仅开发期间 condition 生效即可,暂不做判断
const miniprogram = parseMiniProgramCondition(pagesJson);
if (miniprogram) {
if (!projectJson.condition) {
projectJson.condition = {};
}
projectJson.condition.miniprogram = miniprogram;
}
// appid
if (!projectJson.appid) {
projectJson.appid = 'touristappid';
}
return projectJson;
}
exports.parseMiniProgramProjectJson = parseMiniProgramProjectJson;
function weixinSkyline(config) {
return (config.renderer === 'skyline' &&
config.lazyCodeLoading === 'requiredComponents');
}
function openES62ES5(config) {
if (!config.setting) {
config.setting = {};
}
if (!config.setting.es6) {
config.setting.es6 = true;
}
}
function parseMiniProgramCondition(pagesJson) {
const launchPagePath = process.env.UNI_CLI_LAUNCH_PAGE_PATH || '';
if (launchPagePath) {
return {
current: 0,
list: [
{
id: 0,
name: launchPagePath, // 模式名称
pathName: launchPagePath, // 启动页面,必选
query: process.env.UNI_CLI_LAUNCH_PAGE_QUERY || '', // 启动参数,在页面的onLoad函数里面得到。
},
],
};
}
const condition = pagesJson.condition;
if (!condition || !(0, shared_1.isArray)(condition.list) || !condition.list.length) {
return;
}
condition.list.forEach(function (item, index) {
item.id = item.id || index;
if (item.path) {
item.pathName = item.path;
delete item.path;
}
});
return condition;
}
@@ -0,0 +1,26 @@
export declare const MP_INDEPENDENT_ROOT_QUERY = "uni_mp_independent_root";
export declare const MP_INDEPENDENT_MAIN_PREFIX = "\0uni:mp-independent-main";
export declare const MP_INDEPENDENT_VIRTUAL_ROOT_QUERY = "root";
export interface IndependentSubPackage {
root: string;
pages: string[];
independent: true;
}
export declare function parseIndependentSubPackages(pagesJson: UniApp.PagesJson | undefined): IndependentSubPackage[];
export declare function setIndependentSubPackages(packages: IndependentSubPackage[]): void;
export declare function getIndependentSubPackages(): IndependentSubPackage[];
export declare function getIndependentRoots(): Set<string>;
export declare function getIndependentRootByFilename(filename: string, inputDir: string | undefined): string | undefined;
export declare function resolveIndependentRoot(id: string, importer: string | undefined, inputDir: string | undefined, platform: string | undefined): string | undefined;
export declare function withIndependentRootIfNeeded(id: string, root: string | undefined, inputDir: string | undefined): string;
export declare function isInIndependentRoot(filename: string, inputDir: string, root: string): boolean;
export declare function isAppPagesJson(filename: string, inputDir: string): boolean;
export declare function stringifyIndependentRoots(packages: IndependentSubPackage[]): string;
export declare function normalizeIndependentRoot(root: string): string;
export declare function parseIndependentRoot(id: string): string | undefined;
export declare function withIndependentRoot(id: string, root: string): string;
export declare function withoutIndependentRoot(id: string): string;
export declare function hasIndependentRoot(id: string): boolean;
export declare function formatIndependentVirtualId(prefix: string, root: string): string;
export declare function parseIndependentMainRoot(id: string): string | undefined;
export declare function parseIndependentVirtualRoot(id: string, prefix: string): string | undefined;
@@ -0,0 +1,236 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseIndependentVirtualRoot = exports.parseIndependentMainRoot = exports.formatIndependentVirtualId = exports.hasIndependentRoot = exports.withoutIndependentRoot = exports.withIndependentRoot = exports.parseIndependentRoot = exports.normalizeIndependentRoot = exports.stringifyIndependentRoots = exports.isAppPagesJson = exports.isInIndependentRoot = exports.withIndependentRootIfNeeded = exports.resolveIndependentRoot = exports.getIndependentRootByFilename = exports.getIndependentRoots = exports.getIndependentSubPackages = exports.setIndependentSubPackages = exports.parseIndependentSubPackages = exports.MP_INDEPENDENT_VIRTUAL_ROOT_QUERY = exports.MP_INDEPENDENT_MAIN_PREFIX = exports.MP_INDEPENDENT_ROOT_QUERY = void 0;
const path_1 = __importDefault(require("path"));
const utils_1 = require("../../utils");
exports.MP_INDEPENDENT_ROOT_QUERY = 'uni_mp_independent_root';
exports.MP_INDEPENDENT_MAIN_PREFIX = '\0uni:mp-independent-main';
exports.MP_INDEPENDENT_VIRTUAL_ROOT_QUERY = 'root';
function parseIndependentSubPackages(pagesJson) {
if (!pagesJson) {
return [];
}
const subPackages = pagesJson.subPackages || pagesJson.subpackages || [];
if (!Array.isArray(subPackages)) {
return [];
}
return subPackages.reduce((packages, subPackage) => {
if (!subPackage || subPackage.independent !== true) {
return packages;
}
const root = normalizeSubPackageRoot(subPackage.root);
const pages = normalizeSubPackagePages(subPackage.pages);
if (!root || !pages.length) {
return packages;
}
packages.push({
root,
pages,
independent: true,
});
return packages;
}, []);
}
exports.parseIndependentSubPackages = parseIndependentSubPackages;
let independentSubPackages = [];
let independentRootMatchers = [];
function setIndependentSubPackages(packages) {
independentSubPackages = packages.reduce((result, pkg) => {
const root = normalizeIndependentRoot(pkg.root);
if (root) {
result.push({
...pkg,
root,
});
}
return result;
}, []);
independentRootMatchers = independentSubPackages
.map(({ root }) => ({
root,
normalizedRoot: root,
}))
.sort((a, b) => b.normalizedRoot.length - a.normalizedRoot.length);
}
exports.setIndependentSubPackages = setIndependentSubPackages;
function getIndependentSubPackages() {
return independentSubPackages;
}
exports.getIndependentSubPackages = getIndependentSubPackages;
function getIndependentRoots() {
return new Set(independentSubPackages.map(({ root }) => root));
}
exports.getIndependentRoots = getIndependentRoots;
function getIndependentRootByFilename(filename, inputDir) {
const cleanFilename = splitIdQuery(withoutIndependentRoot(filename)).filename;
if (!inputDir || !path_1.default.isAbsolute(cleanFilename)) {
return;
}
const relativeFilename = (0, utils_1.normalizePath)(path_1.default.relative(inputDir, cleanFilename));
const matcher = independentRootMatchers.find(({ normalizedRoot }) => {
return (relativeFilename === normalizedRoot ||
relativeFilename.startsWith(`${normalizedRoot}/`));
});
return matcher?.root;
}
exports.getIndependentRootByFilename = getIndependentRootByFilename;
function resolveIndependentRoot(id, importer, inputDir, platform) {
if (!platform?.startsWith('mp-')) {
return;
}
const root = parseIndependentRoot(id) ||
(importer
? parseIndependentRoot(importer) ||
getIndependentRootByFilename(importer, inputDir)
: undefined);
const normalizedRoot = root && normalizeIndependentRoot(root);
if (normalizedRoot && getIndependentRoots().has(normalizedRoot)) {
return normalizedRoot;
}
}
exports.resolveIndependentRoot = resolveIndependentRoot;
function withIndependentRootIfNeeded(id, root, inputDir) {
if (!root ||
!inputDir ||
parseIndependentRoot(id) ||
isIndependentStyleRequest(id)) {
return id;
}
if (isInIndependentRoot(id, inputDir, root) || isAppPagesJson(id, inputDir)) {
return withIndependentRoot(id, root);
}
return id;
}
exports.withIndependentRootIfNeeded = withIndependentRootIfNeeded;
function isInIndependentRoot(filename, inputDir, root) {
const cleanFilename = (0, utils_1.normalizePath)(withoutIndependentRoot(filename)).split('?')[0];
const normalizedInputDir = (0, utils_1.normalizePath)(inputDir);
const normalizedRoot = normalizeIndependentRoot(root);
const rootDir = `${normalizedInputDir}/${normalizedRoot}`;
return cleanFilename === rootDir || cleanFilename.startsWith(`${rootDir}/`);
}
exports.isInIndependentRoot = isInIndependentRoot;
function isAppPagesJson(filename, inputDir) {
const cleanFilename = (0, utils_1.normalizePath)(withoutIndependentRoot(filename)).split('?')[0];
const pagesJson = (0, utils_1.normalizePath)(path_1.default.join(inputDir, 'pages.json'));
// uni-app x 的 UTS resolver 会把 JSON 文件映射为 .json.ts 参与类型编译。
return cleanFilename === pagesJson || cleanFilename === `${pagesJson}.ts`;
}
exports.isAppPagesJson = isAppPagesJson;
function stringifyIndependentRoots(packages) {
return packages
.map(({ root }) => normalizeIndependentRoot(root))
.filter(Boolean)
.sort()
.join('\n');
}
exports.stringifyIndependentRoots = stringifyIndependentRoots;
function normalizeIndependentRoot(root) {
return (0, utils_1.normalizePath)(root).replace(/^\/+|\/+$/g, '');
}
exports.normalizeIndependentRoot = normalizeIndependentRoot;
function isIndependentStyleRequest(id) {
const cleanId = withoutIndependentRoot(id);
// 这里保持独立判断,避免 json/mp 基础能力反向依赖 Vite CSS 插件实现。
return /\.(?:css|less|sass|scss|styl|stylus|pcss|postcss)(?:$|\?)/.test(cleanId);
}
function normalizeSubPackageRoot(root) {
if (typeof root !== 'string') {
return '';
}
return normalizeIndependentRoot(root.trim());
}
function normalizeSubPackagePages(pages) {
if (!Array.isArray(pages)) {
return [];
}
return pages.reduce((paths, page) => {
if (typeof page === 'string' && page) {
paths.push((0, utils_1.normalizePath)(page));
}
else if (page && typeof page.path === 'string' && page.path) {
paths.push((0, utils_1.normalizePath)(page.path));
}
return paths;
}, []);
}
function parseIndependentRoot(id) {
const query = splitIdQuery(id).query;
if (!query) {
return;
}
for (const item of query.split('&')) {
const [name, value = ''] = splitQueryItem(item);
if (name === exports.MP_INDEPENDENT_ROOT_QUERY) {
return decodeURIComponent(value);
}
}
}
exports.parseIndependentRoot = parseIndependentRoot;
function withIndependentRoot(id, root) {
const cleanId = withoutIndependentRoot(id);
const { filename, query } = splitIdQuery(cleanId);
const rootQuery = `${exports.MP_INDEPENDENT_ROOT_QUERY}=${encodeURIComponent(root)}`;
return `${(0, utils_1.normalizePath)(filename)}?${query ? query + '&' : ''}${rootQuery}`;
}
exports.withIndependentRoot = withIndependentRoot;
function withoutIndependentRoot(id) {
const { filename, query } = splitIdQuery(id);
if (!query) {
return id;
}
const nextQuery = query
.split('&')
.filter((item) => splitQueryItem(item)[0] !== exports.MP_INDEPENDENT_ROOT_QUERY)
.join('&');
return nextQuery ? `${filename}?${nextQuery}` : filename;
}
exports.withoutIndependentRoot = withoutIndependentRoot;
function hasIndependentRoot(id) {
return parseIndependentRoot(id) !== undefined;
}
exports.hasIndependentRoot = hasIndependentRoot;
function formatIndependentVirtualId(prefix, root) {
return `${prefix}?${exports.MP_INDEPENDENT_VIRTUAL_ROOT_QUERY}=${encodeURIComponent(root)}`;
}
exports.formatIndependentVirtualId = formatIndependentVirtualId;
function parseIndependentMainRoot(id) {
return parseIndependentVirtualRoot(id, exports.MP_INDEPENDENT_MAIN_PREFIX);
}
exports.parseIndependentMainRoot = parseIndependentMainRoot;
function parseIndependentVirtualRoot(id, prefix) {
if (!id.startsWith(`${prefix}?`)) {
return;
}
const { query } = splitIdQuery(id);
if (!query) {
return;
}
for (const item of query.split('&')) {
const [name, value = ''] = splitQueryItem(item);
if (name === exports.MP_INDEPENDENT_VIRTUAL_ROOT_QUERY) {
return decodeURIComponent(value);
}
}
}
exports.parseIndependentVirtualRoot = parseIndependentVirtualRoot;
function splitIdQuery(id) {
const queryIndex = id.indexOf('?');
if (queryIndex === -1) {
return { filename: id, query: '' };
}
return {
filename: id.slice(0, queryIndex),
query: id.slice(queryIndex + 1),
};
}
function splitQueryItem(item) {
const equalIndex = item.indexOf('=');
if (equalIndex === -1) {
return [item, ''];
}
return [item.slice(0, equalIndex), item.slice(equalIndex + 1)];
}
@@ -0,0 +1,130 @@
export interface ComponentJson {
component: true;
usingComponents?: UsingComponents;
usingSwanComponents?: UsingComponents;
styleIsolation?: 'apply-shared' | 'shared' | 'isolated';
}
interface ShareWindowOptions {
navigationBarBackgroundColor?: string;
navigationBarTextStyle?: 'white' | 'black';
navigationBarTitleText?: string;
navigationStyle?: 'default' | 'custom';
backgroundColor?: string;
backgroundTextStyle?: 'dark' | 'light';
backgroundColorTop?: string;
backgroundColorBottom?: string;
enablePullDownRefresh?: boolean;
onReachBottomDistance?: number;
pageOrientation?: 'portrait' | 'landscape' | 'auto';
}
type Style = 'v2' | string;
type RestartStrategy = 'homePage' | 'homePageAndLatestPage' | string;
export interface PageWindowOptions extends ShareWindowOptions {
component?: true;
disableScroll?: boolean;
usingComponents?: UsingComponents;
usingSwanComponents?: UsingComponents;
initialRenderingCache?: 'static' | string;
style?: Style;
singlePage?: SinglePage;
restartStrategy?: RestartStrategy;
styleIsolation?: 'shared' | 'isolated';
}
export interface AppWindowOptions extends ShareWindowOptions {
visualEffectInBackground?: 'none' | 'hidden';
}
interface SubPackage {
name?: string;
root: string;
pages: string[];
independent?: boolean;
plugins?: Plugins;
}
interface TabBarItem {
pagePath: string;
text: string;
iconPath?: string;
selectedIconPath?: string;
}
export interface TabBar {
color: string;
selectedColor: string;
backgroundColor: string;
borderStyle?: 'black' | 'white';
list: TabBarItem[];
position?: 'bottom' | 'top';
custom?: boolean;
}
export interface NetworkTimeout {
request?: number;
requeconnectSocketst?: number;
uploadFile?: number;
downloadFile?: number;
}
interface Plugins {
[name: string]: {
version: string;
provider: string;
export?: string;
lazy?: boolean;
};
}
interface PreloadRule {
[name: string]: {
network: 'wifi' | 'all';
packages: string[];
};
}
export interface UsingComponents {
[name: string]: string;
}
interface Permission {
[name: string]: {
desc: string;
};
}
interface UseExtendedLib {
kbone: boolean;
weui: boolean;
}
interface EntranceDeclare {
locationMessage: {
path: string;
query: string;
};
}
interface SinglePage {
navigationBarFit?: 'squeezed' | 'float';
}
export interface AppJson {
entryPagePath?: string;
pages: string[];
window?: AppWindowOptions;
tabBar?: TabBar;
networkTimeout?: NetworkTimeout;
debug?: boolean;
functionalPages?: boolean;
subPackages?: SubPackage[];
workers?: string | {
path: string;
isSubpackage: boolean;
};
requiredBackgroundModes?: string[];
plugins?: Plugins;
preloadRule?: PreloadRule;
resizable?: boolean;
usingComponents?: UsingComponents;
permission?: Permission;
sitemapLocation?: string;
style?: Style;
useExtendedLib?: UseExtendedLib;
entranceDeclare?: EntranceDeclare;
darkmode?: boolean;
themeLocation?: string;
lazyCodeLoading?: 'requiredComponents' | string;
singlePage?: SinglePage;
restartStrategy?: RestartStrategy;
[name: string]: unknown;
}
export type MiniProgramComponentsType = 'plugin' | 'component' | 'dynamicLib' | 'ext' | 'xr-frame' | 'weui';
export {};
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
import type { AppWindowOptions, PageWindowOptions, TabBar } from './types';
export declare function parseWindowOptions(style: UniApp.PagesJsonPageStyle, platform: UniApp.PLATFORM, windowOptionsMap?: Record<string, string | string[]>): PageWindowOptions | AppWindowOptions;
export declare function parseTabBar(tabBar: UniApp.TabBarOptions, platform: UniApp.PLATFORM, tabBarOptionsMap?: Record<string, string>, tabBarItemOptionsMap?: Record<string, string>): TabBar;
@@ -0,0 +1,78 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTabBar = exports.parseWindowOptions = void 0;
const shared_1 = require("@vue/shared");
const pages_1 = require("../pages");
function trimJson(json) {
delete json.maxWidth;
delete json.topWindow;
delete json.leftWindow;
delete json.rightWindow;
if (json.tabBar) {
delete json.tabBar.matchMedia;
}
return json;
}
function convert(to, from, map) {
Object.keys(map).forEach((key) => {
if ((0, shared_1.hasOwn)(from, map[key])) {
to[key] = from[map[key]];
}
});
return to;
}
function convertWindowOptions(to, from, map) {
Object.entries(map).forEach(([key, value]) => {
const names = (0, shared_1.isArray)(value) ? value : [value];
for (const name of names) {
if ((0, shared_1.hasOwn)(from, name)) {
to[key] = from[name];
break;
}
}
});
return to;
}
function parseWindowOptions(style, platform, windowOptionsMap) {
if (!style) {
return {};
}
const platformStyle = style[platform] || {};
(0, pages_1.removePlatformStyle)(trimJson(style));
const res = {};
if (windowOptionsMap) {
return (0, shared_1.extend)(convertWindowOptions(res, style, windowOptionsMap), platformStyle);
}
return (0, shared_1.extend)(res, style, platformStyle);
}
exports.parseWindowOptions = parseWindowOptions;
function trimTabBarJson(tabBar) {
;
[
'fontSize',
'height',
'iconWidth',
'midButton',
'selectedIndex',
'spacing',
].forEach((name) => {
delete tabBar[name];
});
return tabBar;
}
function parseTabBar(tabBar, platform, tabBarOptionsMap, tabBarItemOptionsMap) {
const platformStyle = tabBar[platform] || {};
(0, pages_1.removePlatformStyle)(trimTabBarJson(tabBar));
const res = {};
if (tabBarOptionsMap) {
if (tabBarItemOptionsMap && tabBar.list) {
tabBar.list = tabBar.list.map((item) => {
return convert({}, item, tabBarItemOptionsMap);
});
}
convert(res, tabBar, tabBarOptionsMap);
return (0, shared_1.extend)(res, platformStyle);
}
return (0, shared_1.extend)(res, tabBar, platformStyle);
}
exports.parseTabBar = parseTabBar;