更新
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
export * from './manifest';
|
||||
export declare function checkPagesJson(jsonStr: string, inputDir: string): boolean;
|
||||
export declare function normalizeUniAppXAppPagesJson(jsonStr: string): UniApp.PagesJson;
|
||||
/**
|
||||
* TODO 应该闭包,通过globalThis赋值?
|
||||
* @param pagesJson
|
||||
* @param manifestJson
|
||||
* @returns
|
||||
*/
|
||||
export declare function normalizeUniAppXAppConfig(pagesJson: UniApp.PagesJson, manifestJson: Record<string, any>): string;
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
"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);
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.normalizeUniAppXAppConfig = exports.normalizeUniAppXAppPagesJson = exports.checkPagesJson = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const shared_1 = require("@vue/shared");
|
||||
const jsonc_parser_1 = require("jsonc-parser");
|
||||
const json_1 = require("../json");
|
||||
const pages_1 = require("../pages");
|
||||
const utils_1 = require("../../utils");
|
||||
const uniRoutes_1 = require("../app/pages/uniRoutes");
|
||||
const uniConfig_1 = require("./uniConfig");
|
||||
const utils_2 = require("../../vite/plugins/vitejs/utils");
|
||||
const preprocess_1 = require("../../preprocess");
|
||||
__exportStar(require("./manifest"), exports);
|
||||
function checkPagesJson(jsonStr, inputDir) {
|
||||
if (!inputDir) {
|
||||
return false;
|
||||
}
|
||||
const errors = [];
|
||||
const root = (0, jsonc_parser_1.parseTree)(jsonStr, errors);
|
||||
if (!root) {
|
||||
if (errors.length) {
|
||||
for (const error of errors) {
|
||||
const { line, column } = (0, utils_2.offsetToLineColumn)(jsonStr, error.offset);
|
||||
throw {
|
||||
name: 'SyntaxError',
|
||||
code: error.error,
|
||||
message: (0, jsonc_parser_1.printParseErrorCode)(error.error),
|
||||
loc: {
|
||||
start: { line, column },
|
||||
},
|
||||
offsetStart: error.offset,
|
||||
offsetEnd: error.offset + error.length,
|
||||
};
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const pagePathNodes = walkNodes(findRootNode(root, ['pages']));
|
||||
findRootNode(root, ['subPackages', 'subpackages']).forEach((node) => {
|
||||
const subPackageRoot = findSubPackageRoot(node);
|
||||
if (subPackageRoot) {
|
||||
findRootNode(node, ['pages']).forEach((subNode) => {
|
||||
walkNodes(subNode.children ?? []).forEach((node) => {
|
||||
pagePathNodes.push({
|
||||
...node,
|
||||
value: (0, utils_1.normalizePath)(path_1.default.join(subPackageRoot, node.value)),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
if (pagePathNodes.length) {
|
||||
for (const node of pagePathNodes) {
|
||||
const pagePath = node.value ?? '';
|
||||
if (pageExistsWithCaseSync(path_1.default.join(inputDir, pagePath))) {
|
||||
continue;
|
||||
}
|
||||
const { line, column } = (0, utils_2.offsetToLineColumn)(jsonStr, node.offset);
|
||||
throw {
|
||||
name: 'CompilerError',
|
||||
code: 'CompilerError',
|
||||
message: `The page path "${pagePath}" does not exist`,
|
||||
loc: {
|
||||
start: { line, column },
|
||||
},
|
||||
offsetStart: node.offset,
|
||||
offsetEnd: node.offset + node.length,
|
||||
};
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
exports.checkPagesJson = checkPagesJson;
|
||||
function pageExistsWithCaseSync(pagePath) {
|
||||
try {
|
||||
const files = fs_1.default.readdirSync(path_1.default.dirname(pagePath));
|
||||
const basename = path_1.default.basename(pagePath);
|
||||
const uvuePage = basename + '.uvue';
|
||||
const vuePage = basename + '.vue';
|
||||
return files.some((file) => file === uvuePage || file === vuePage);
|
||||
}
|
||||
catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function findSubPackageRoot(node) {
|
||||
const child = node.children?.find((child) => child.type === 'property' &&
|
||||
child.children &&
|
||||
child.children.find((child) => child.type === 'string' && child.value === 'root'));
|
||||
if (child && child.children?.length === 2) {
|
||||
return child.children[1].value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
function findRootNode(node, property) {
|
||||
const { type, children } = node;
|
||||
if (type === 'object' && children) {
|
||||
const child = children.find((child) => child.type === 'property' &&
|
||||
child.children &&
|
||||
child.children.find((child) => child.type === 'string' && property.includes(child.value)));
|
||||
if (child) {
|
||||
const node = child.children.find((child) => child.type === 'array');
|
||||
return node?.children ?? [];
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
function walkNodes(node) {
|
||||
const pagePathNodes = [];
|
||||
node.forEach((node) => walkNode(node, pagePathNodes));
|
||||
return pagePathNodes;
|
||||
}
|
||||
function walkNode(node, pagePathNodes) {
|
||||
const { type, children } = node;
|
||||
if (type === 'property' && children && children.length === 2) {
|
||||
const maybePagePathNode = children[0];
|
||||
const maybePagePathValueNode = children[1];
|
||||
if (maybePagePathNode.type === 'string' &&
|
||||
maybePagePathNode.value === 'path' &&
|
||||
maybePagePathValueNode.type === 'string' &&
|
||||
(0, shared_1.isString)(maybePagePathValueNode.value)) {
|
||||
pagePathNodes.push(maybePagePathValueNode);
|
||||
}
|
||||
}
|
||||
if (children) {
|
||||
children.forEach((node) => walkNode(node, pagePathNodes));
|
||||
}
|
||||
}
|
||||
function normalizeUniAppXAppPagesJson(jsonStr) {
|
||||
// 先条件编译
|
||||
jsonStr = (0, preprocess_1.preUVueJson)(jsonStr, 'pages.json');
|
||||
checkPagesJson(jsonStr, process.env.UNI_INPUT_DIR);
|
||||
const pagesJson = {
|
||||
pages: [],
|
||||
globalStyle: {},
|
||||
};
|
||||
let userPagesJson = {
|
||||
pages: [],
|
||||
globalStyle: {},
|
||||
};
|
||||
try {
|
||||
// 此处不需要条件编译了
|
||||
userPagesJson = (0, json_1.parseJson)(jsonStr, false, 'pages.json');
|
||||
}
|
||||
catch (e) {
|
||||
console.error(`[vite] Error: pages.json parse failed.\n`, jsonStr, e);
|
||||
}
|
||||
// pages
|
||||
(0, pages_1.validatePages)(userPagesJson, jsonStr);
|
||||
userPagesJson.subPackages =
|
||||
userPagesJson.subPackages || userPagesJson.subpackages;
|
||||
// subPackages
|
||||
if (userPagesJson.subPackages) {
|
||||
userPagesJson.pages.push(...normalizeSubPackages(userPagesJson.subPackages));
|
||||
}
|
||||
pagesJson.pages = userPagesJson.pages;
|
||||
// pageStyle
|
||||
normalizePages(pagesJson.pages);
|
||||
// globalStyle
|
||||
pagesJson.globalStyle = normalizePageStyle(userPagesJson.globalStyle);
|
||||
// tabBar
|
||||
if (userPagesJson.tabBar) {
|
||||
pagesJson.tabBar = userPagesJson.tabBar;
|
||||
}
|
||||
// condition
|
||||
if (userPagesJson.condition) {
|
||||
pagesJson.condition = userPagesJson.condition;
|
||||
}
|
||||
// uniIdRouter
|
||||
if (userPagesJson.uniIdRouter) {
|
||||
pagesJson.uniIdRouter = userPagesJson.uniIdRouter;
|
||||
}
|
||||
// 是否应该用 process.env.UNI_UTS_PLATFORM
|
||||
(0, pages_1.filterPlatformPages)(process.env.UNI_PLATFORM, pagesJson);
|
||||
return pagesJson;
|
||||
}
|
||||
exports.normalizeUniAppXAppPagesJson = normalizeUniAppXAppPagesJson;
|
||||
function normalizeSubPackages(subPackages) {
|
||||
const pages = [];
|
||||
if ((0, shared_1.isArray)(subPackages)) {
|
||||
subPackages.forEach(({ root, pages: subPages }) => {
|
||||
if (root && subPages.length) {
|
||||
subPages.forEach((subPage) => {
|
||||
subPage.path = (0, utils_1.normalizePath)(path_1.default.join(root, subPage.path));
|
||||
subPage.style = subPage.style;
|
||||
pages.push(subPage);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
function normalizePages(pages) {
|
||||
pages.forEach((page) => {
|
||||
page.style = normalizePageStyle(page.style);
|
||||
});
|
||||
}
|
||||
function normalizePageStyle(pageStyle) {
|
||||
if (pageStyle) {
|
||||
(0, shared_1.extend)(pageStyle, pageStyle['app']);
|
||||
(0, pages_1.removePlatformStyle)(pageStyle);
|
||||
return pageStyle;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
/**
|
||||
* TODO 应该闭包,通过globalThis赋值?
|
||||
* @param pagesJson
|
||||
* @param manifestJson
|
||||
* @returns
|
||||
*/
|
||||
function normalizeUniAppXAppConfig(pagesJson, manifestJson) {
|
||||
const uniConfig = (0, uniConfig_1.normalizeAppXUniConfig)(pagesJson, manifestJson);
|
||||
const tabBar = uniConfig.tabBar;
|
||||
delete uniConfig.tabBar;
|
||||
let appConfigJs = `const __uniConfig = ${JSON.stringify(uniConfig)};
|
||||
__uniConfig.getTabBarConfig = () => {return ${tabBar ? JSON.stringify(tabBar) : 'undefined'}};
|
||||
__uniConfig.tabBar = __uniConfig.getTabBarConfig();
|
||||
const __uniRoutes = ${(0, uniRoutes_1.normalizeAppUniRoutes)(pagesJson)}.map(uniRoute=>(uniRoute.meta.route=uniRoute.path,__uniConfig.pages.push(uniRoute.path),uniRoute.path='/'+uniRoute.path,uniRoute)).concat(typeof __uniSystemRoutes !== 'undefined' ? __uniSystemRoutes : []);
|
||||
|
||||
`;
|
||||
if (process.env.UNI_UTS_PLATFORM === 'app-harmony') {
|
||||
appConfigJs += `globalThis.__uniConfig = __uniConfig;
|
||||
globalThis.__uniRoutes = __uniRoutes;`;
|
||||
}
|
||||
return appConfigJs;
|
||||
}
|
||||
exports.normalizeUniAppXAppConfig = normalizeUniAppXAppConfig;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export declare function parseUniXFlexDirection(manifestJson: Record<string, any>): any;
|
||||
export declare function parseUniXSplashScreen(manifestJson: Record<string, any>): false | object;
|
||||
export declare function parseUniXUniStatistics(manifestJson: Record<string, any>): false | object;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.parseUniXUniStatistics = exports.parseUniXSplashScreen = exports.parseUniXFlexDirection = void 0;
|
||||
const shared_1 = require("@vue/shared");
|
||||
const flexDirs = ['row', 'row-reverse', 'column', 'column-reverse'];
|
||||
function parseUniXFlexDirection(manifestJson) {
|
||||
const flexDir = manifestJson?.['uni-app-x']?.['flex-direction'];
|
||||
if (flexDir && flexDirs.includes(flexDir)) {
|
||||
return flexDir;
|
||||
}
|
||||
return 'column';
|
||||
}
|
||||
exports.parseUniXFlexDirection = parseUniXFlexDirection;
|
||||
function parseUniXSplashScreen(manifestJson) {
|
||||
const splashScreen = manifestJson?.['app']?.['splashScreen'];
|
||||
if ((0, shared_1.isPlainObject)(splashScreen)) {
|
||||
return splashScreen;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
exports.parseUniXSplashScreen = parseUniXSplashScreen;
|
||||
function parseUniXUniStatistics(manifestJson) {
|
||||
const uniStatistics = manifestJson?.['app']?.['uniStatistics'];
|
||||
if ((0, shared_1.isPlainObject)(uniStatistics)) {
|
||||
return uniStatistics;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
exports.parseUniXUniStatistics = parseUniXUniStatistics;
|
||||
Generated
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
interface AppXUniConfig {
|
||||
pages: unknown[];
|
||||
globalStyle: unknown;
|
||||
appname: string;
|
||||
compilerVersion: string;
|
||||
fallbackLocale: unknown;
|
||||
tabBar: unknown;
|
||||
entryPagePath: string;
|
||||
entryPageQuery?: string;
|
||||
conditionUrl?: string;
|
||||
realEntryPagePath?: string;
|
||||
themeConfig?: unknown;
|
||||
}
|
||||
export declare function normalizeAppXUniConfig(pagesJson: UniApp.PagesJson, manifestJson: Record<string, any>): AppXUniConfig;
|
||||
export {};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.normalizeAppXUniConfig = void 0;
|
||||
const uniConfig_1 = require("../app/pages/uniConfig");
|
||||
// app-config.js 内容
|
||||
function normalizeAppXUniConfig(pagesJson, manifestJson) {
|
||||
const config = {
|
||||
pages: [],
|
||||
globalStyle: pagesJson.globalStyle,
|
||||
appname: manifestJson.name || '',
|
||||
compilerVersion: process.env.UNI_COMPILER_VERSION,
|
||||
...(0, uniConfig_1.parseEntryPagePath)(pagesJson),
|
||||
tabBar: pagesJson.tabBar,
|
||||
fallbackLocale: manifestJson.fallbackLocale,
|
||||
};
|
||||
if (config.realEntryPagePath) {
|
||||
config.conditionUrl = config.entryPagePath;
|
||||
config.entryPagePath = config.realEntryPagePath;
|
||||
}
|
||||
// darkmode
|
||||
if (pagesJson.themeConfig) {
|
||||
config.themeConfig = pagesJson.themeConfig;
|
||||
}
|
||||
// TODO 待支持分包
|
||||
return config;
|
||||
}
|
||||
exports.normalizeAppXUniConfig = normalizeAppXUniConfig;
|
||||
Reference in New Issue
Block a user