|
|
"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 utils_1 = require("../utils");
|
|
|
function uniPagesJsonPlugin() {
|
|
|
return (0, uni_cli_shared_1.defineUniPagesJsonPlugin)((opts) => {
|
|
|
return {
|
|
|
name: 'uni:h5-pages-json',
|
|
|
enforce: 'pre',
|
|
|
transform(code, id, opt) {
|
|
|
if (opts.filter(id)) {
|
|
|
const { resolvedConfig } = opts;
|
|
|
const ssr = (0, utils_1.isSSR)(opt);
|
|
|
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:h5-pages-json', path_1.default.resolve(process.env.UNI_INPUT_DIR, 'pages.json'), err, jsonCode);
|
|
|
this.error(error);
|
|
|
}
|
|
|
else {
|
|
|
throw err;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return {
|
|
|
code: registerGlobalCode(resolvedConfig, ssr) +
|
|
|
generatePagesJsonCode(ssr, code, resolvedConfig),
|
|
|
map: { mappings: '' },
|
|
|
};
|
|
|
}
|
|
|
},
|
|
|
};
|
|
|
});
|
|
|
}
|
|
|
exports.uniPagesJsonPlugin = uniPagesJsonPlugin;
|
|
|
function generatePagesJsonCode(ssr, jsonStr, config) {
|
|
|
const globalName = getGlobal(ssr);
|
|
|
const pagesJson = (0, uni_cli_shared_1.normalizePagesJson)(jsonStr, process.env.UNI_PLATFORM);
|
|
|
const { importLayoutComponentsCode, defineLayoutComponentsCode } = generateLayoutComponentsCode(globalName, pagesJson);
|
|
|
const definePagesCode = generatePagesDefineCode(pagesJson, config);
|
|
|
const uniRoutesCode = generateRoutes(globalName, pagesJson, config);
|
|
|
const uniConfigCode = generateConfig(globalName, pagesJson, config);
|
|
|
const cssCode = generateCssCode(config);
|
|
|
const vueType = process.env.UNI_APP_X === 'true' ? 'uvue' : 'nvue';
|
|
|
let workersCode = '';
|
|
|
if (config.command === 'build' && process.env.UNI_APP_X === 'true') {
|
|
|
const workers = (0, uni_cli_shared_1.getWorkers)();
|
|
|
const workerCode = Object.keys(workers).map((key) => {
|
|
|
return `import('@/${key}?worker')`;
|
|
|
});
|
|
|
if (workerCode.length) {
|
|
|
// 仅用于激活动态chunk,运行时不能执行
|
|
|
workersCode = `if(!Math){\n${workerCode.join('\n')}\n}`;
|
|
|
}
|
|
|
}
|
|
|
return `
|
|
|
import { defineAsyncComponent, resolveComponent, createVNode, withCtx, openBlock, createBlock } from 'vue'
|
|
|
import { PageComponent, useI18n, setupWindow, setupPage } from '@dcloudio/uni-h5'
|
|
|
import { appId, appName, appVersion, appVersionCode, debug, networkTimeout, router, async, sdkConfigs, qqMapKey, googleMapKey, aMapKey, bMapKey, aMapSecurityJsCode, aMapServiceHost, ${vueType}, locale, fallbackLocale, darkmode, themeConfig } from './${uni_cli_shared_1.MANIFEST_JSON_JS}'
|
|
|
const locales = import.meta.glob('./locale/*.json', { eager: true })
|
|
|
${importLayoutComponentsCode}
|
|
|
const extend = Object.assign
|
|
|
${cssCode}
|
|
|
${uniConfigCode}
|
|
|
${defineLayoutComponentsCode}
|
|
|
${definePagesCode}
|
|
|
${uniRoutesCode}
|
|
|
${workersCode}
|
|
|
${config.command === 'serve' ? hmrCode : ''}
|
|
|
export {}
|
|
|
`;
|
|
|
}
|
|
|
const hmrCode = `if(import.meta.hot){
|
|
|
import.meta.hot.on('invalidate', (data) => {
|
|
|
import.meta.hot.invalidate()
|
|
|
})
|
|
|
}`;
|
|
|
function getGlobal(ssr) {
|
|
|
return ssr ? 'global' : 'window';
|
|
|
}
|
|
|
// 兼容 wx 对象
|
|
|
function registerGlobalCode(config, ssr) {
|
|
|
const name = getGlobal(ssr);
|
|
|
const enableTreeShaking = (0, uni_cli_shared_1.isEnableTreeShaking)((0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR));
|
|
|
if (enableTreeShaking && config.command === 'build' && !ssr) {
|
|
|
// 非 SSR 的发行模式,补充全局 uni 对象
|
|
|
return `import { upx2px, getApp } from '@dcloudio/uni-h5';${name}.uni = {};${name}.wx = {};${name}.rpx2px = upx2px`;
|
|
|
}
|
|
|
return `
|
|
|
import {uni,upx2px,getCurrentPages,getApp,UniServiceJSBridge,UniViewJSBridge} from '@dcloudio/uni-h5'
|
|
|
${name}.getApp = getApp
|
|
|
${name}.getCurrentPages = getCurrentPages
|
|
|
${name}.wx = uni
|
|
|
${name}.uni = uni
|
|
|
${name}.UniViewJSBridge = UniViewJSBridge
|
|
|
${name}.UniServiceJSBridge = UniServiceJSBridge
|
|
|
${name}.rpx2px = upx2px
|
|
|
${name}.__setupPage = (com)=>setupPage(com)
|
|
|
`;
|
|
|
}
|
|
|
function generateCssCode(config) {
|
|
|
const define = config.define;
|
|
|
const cssFiles = [uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'base.css'];
|
|
|
if (config.isProduction) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'shadow.css');
|
|
|
}
|
|
|
// if (define.__UNI_FEATURE_PAGES__) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'async.css');
|
|
|
// }
|
|
|
if (define.__UNI_FEATURE_RESPONSIVE__) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'layout.css');
|
|
|
}
|
|
|
if (define.__UNI_FEATURE_NAVIGATIONBAR__) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'pageHead.css');
|
|
|
}
|
|
|
if (define.__UNI_FEATURE_TABBAR__) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'tabBar.css');
|
|
|
}
|
|
|
// x 项目直接集成 uvue.css
|
|
|
if (process.env.UNI_APP_X === 'true') {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'uvue.css');
|
|
|
}
|
|
|
else {
|
|
|
if (define.__UNI_FEATURE_NVUE__) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'nvue.css');
|
|
|
}
|
|
|
}
|
|
|
if (define.__UNI_FEATURE_PULL_DOWN_REFRESH__) {
|
|
|
cssFiles.push(uni_cli_shared_1.H5_FRAMEWORK_STYLE_PATH + 'pageRefresh.css');
|
|
|
}
|
|
|
if (define.__UNI_FEATURE_NAVIGATIONBAR_SEARCHINPUT__) {
|
|
|
cssFiles.push(uni_cli_shared_1.BASE_COMPONENTS_STYLE_PATH + 'input.css');
|
|
|
}
|
|
|
const enableTreeShaking = (0, uni_cli_shared_1.isEnableTreeShaking)((0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR));
|
|
|
if (config.command === 'serve' || !enableTreeShaking) {
|
|
|
const apiDepsCss = (0, uni_cli_shared_1.API_DEPS_CSS)(process.env.UNI_APP_X === 'true');
|
|
|
// 开发模式或禁用摇树优化,自动添加所有API相关css
|
|
|
Object.keys(apiDepsCss).forEach((name) => {
|
|
|
const styles = apiDepsCss[name];
|
|
|
styles.forEach((style) => {
|
|
|
if (!cssFiles.includes(style)) {
|
|
|
cssFiles.push(style);
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
return cssFiles.map((file) => `import '${file}'`).join('\n');
|
|
|
}
|
|
|
function generateLayoutComponentsCode(globalName, pagesJson) {
|
|
|
const windowNames = {
|
|
|
topWindow: -1,
|
|
|
leftWindow: -2,
|
|
|
rightWindow: -3,
|
|
|
};
|
|
|
let importLayoutComponentsCode = '';
|
|
|
let defineLayoutComponentsCode = `${globalName}.__uniLayout = ${globalName}.__uniLayout || {}\n`;
|
|
|
Object.keys(windowNames).forEach((name) => {
|
|
|
const windowConfig = pagesJson[name];
|
|
|
if (windowConfig && windowConfig.path) {
|
|
|
importLayoutComponentsCode += `const ${name} = defineAsyncComponent(()=>import('./${windowConfig.path}').then(com=>setupWindow(com.default || com,${windowNames[name]})))\n`;
|
|
|
defineLayoutComponentsCode += `${globalName}.__uniConfig.${name}.component = ${name}\n`;
|
|
|
}
|
|
|
});
|
|
|
return {
|
|
|
importLayoutComponentsCode,
|
|
|
defineLayoutComponentsCode,
|
|
|
};
|
|
|
}
|
|
|
function generatePageDefineCode(pageOptions) {
|
|
|
let pagePathWithExtname = (0, uni_cli_shared_1.normalizePagePath)(pageOptions.path, 'h5');
|
|
|
if (!pagePathWithExtname) {
|
|
|
// 不存在时,仍引用,此时编译会报错文件不存在
|
|
|
pagePathWithExtname = pageOptions.path + '.vue';
|
|
|
}
|
|
|
const pageIdent = (0, uni_cli_shared_1.normalizeIdentifier)(pageOptions.path);
|
|
|
return `const ${pageIdent}Loader = ()=>import('./${pagePathWithExtname}').then(com => setupPage(com.default || com))
|
|
|
const ${pageIdent} = defineAsyncComponent(extend({loader:${pageIdent}Loader},AsyncComponentOptions))`;
|
|
|
}
|
|
|
function generatePagesDefineCode(pagesJson, _config) {
|
|
|
const { pages } = pagesJson;
|
|
|
return (`const AsyncComponentOptions = {
|
|
|
delay: async.delay,
|
|
|
timeout: async.timeout,
|
|
|
suspensible: async.suspensible
|
|
|
}
|
|
|
if(async.loading){
|
|
|
AsyncComponentOptions.loadingComponent = {
|
|
|
name:'SystemAsyncLoading',
|
|
|
render(){
|
|
|
return createVNode(resolveComponent(async.loading))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if(async.error){
|
|
|
AsyncComponentOptions.errorComponent = {
|
|
|
name:'SystemAsyncError',
|
|
|
props:['error'],
|
|
|
render(){
|
|
|
return createVNode(resolveComponent(async.error), { error: this.error })
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
` + pages.map((pageOptions) => generatePageDefineCode(pageOptions)).join('\n'));
|
|
|
}
|
|
|
function generatePageRoute({ path, meta }, _config) {
|
|
|
const { isEntry } = meta;
|
|
|
const alias = isEntry ? `\n alias:'/${path}',` : '';
|
|
|
// 目前单页面未处理 query=>props
|
|
|
const queryCode = process.env.UNI_APP_X === 'true'
|
|
|
? 'app && app.vm && app.vm.$route && app.vm.$route.query || {};'
|
|
|
: 'app && app.$route && app.$route.query || {};';
|
|
|
return `{
|
|
|
path:'/${isEntry ? '' : path}',${alias}
|
|
|
component:{setup(){ const app = getApp(); const query = ${queryCode} return ()=>renderPage(${(0, uni_cli_shared_1.normalizeIdentifier)(path)},query)}},
|
|
|
loader: ${(0, uni_cli_shared_1.normalizeIdentifier)(path)}Loader,
|
|
|
meta: ${JSON.stringify(meta)}
|
|
|
}`;
|
|
|
}
|
|
|
function generatePagesRoute(pagesRouteOptions, config) {
|
|
|
return pagesRouteOptions.map((pageOptions) => generatePageRoute(pageOptions, config));
|
|
|
}
|
|
|
function generateRoutes(globalName, pagesJson, config) {
|
|
|
return `
|
|
|
function renderPage(component,props){
|
|
|
return (openBlock(), createBlock(PageComponent, null, {page: withCtx(() => [createVNode(component, extend({},props,{ref: "page"}), null, 512 /* NEED_PATCH */)]), _: 1 /* STABLE */}))
|
|
|
}
|
|
|
${globalName}.__uniRoutes=[${[
|
|
|
...generatePagesRoute((0, uni_cli_shared_1.normalizePagesRoute)(pagesJson), config),
|
|
|
].join(',')}].map(uniRoute=>(uniRoute.meta.route = (uniRoute.alias || uniRoute.path).slice(1),uniRoute))`;
|
|
|
}
|
|
|
function generateConfig(globalName, pagesJson, config) {
|
|
|
delete pagesJson.pages;
|
|
|
delete pagesJson.subPackages;
|
|
|
delete pagesJson.subpackages;
|
|
|
pagesJson.compilerVersion = process.env.UNI_COMPILER_VERSION;
|
|
|
const isX = process.env.UNI_APP_X === 'true';
|
|
|
const vueType = isX ? 'uvue' : 'nvue';
|
|
|
let tabBarCode = '';
|
|
|
if (isX) {
|
|
|
const tabBar = pagesJson.tabBar;
|
|
|
delete pagesJson.tabBar;
|
|
|
tabBarCode = `${globalName}.__uniConfig.getTabBarConfig = () => {return ${tabBar ? JSON.stringify(tabBar) : 'undefined'}};
|
|
|
${globalName}.__uniConfig.tabBar = __uniConfig.getTabBarConfig();`;
|
|
|
}
|
|
|
return `${isX ? `${globalName}.__uniX = true` : ''}
|
|
|
${globalName}.__uniConfig=extend(${JSON.stringify(pagesJson)},{
|
|
|
appId,
|
|
|
appName,
|
|
|
appVersion,
|
|
|
appVersionCode,
|
|
|
async,
|
|
|
debug,
|
|
|
networkTimeout,
|
|
|
sdkConfigs,
|
|
|
qqMapKey,
|
|
|
bMapKey,
|
|
|
googleMapKey,
|
|
|
aMapKey,
|
|
|
aMapSecurityJsCode,
|
|
|
aMapServiceHost,
|
|
|
${vueType},
|
|
|
locale,
|
|
|
fallbackLocale,
|
|
|
locales:Object.keys(locales).reduce((res,name)=>{const locale=name.replace(/\\.\\/locale\\/(uni-app.)?(.*).json/,'$2');extend(res[locale]||(res[locale]={}),locales[name].default);return res},{}),
|
|
|
router,
|
|
|
darkmode,
|
|
|
themeConfig,
|
|
|
})
|
|
|
${tabBarCode}
|
|
|
`;
|
|
|
}
|