| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.uniPagesJsonPlugin = void 0;
- 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正确
- code = code.replace(/\r\n/g, '\n');
- try {
- (0, uni_cli_shared_1.checkPagesJson)((0, uni_cli_shared_1.preUVueJson)(code), process.env.UNI_INPUT_DIR);
- }
- catch (err) {
- if (err.loc) {
- const error = (0, uni_cli_shared_1.createRollupError)('uni:app-pages', 'pages.json', err, code);
- 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';
- 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}
- ${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) {
- // 开发模式或禁用摇树优化,自动添加所有API相关css
- Object.keys(uni_cli_shared_1.API_DEPS_CSS).forEach((name) => {
- const styles = uni_cli_shared_1.API_DEPS_CSS[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 += `import ${name} from './${windowConfig.path}'\n`;
- defineLayoutComponentsCode += `${globalName}.__uniConfig.${name}.component = setupWindow(${name},${windowNames[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',
- render(){
- return createVNode(resolveComponent(async.error))
- }
- }
- }
- ` + pages.map((pageOptions) => generatePageDefineCode(pageOptions)).join('\n'));
- }
- function generatePageRoute({ path, meta }, _config) {
- const { isEntry } = meta;
- const alias = isEntry ? `\n alias:'/${path}',` : '';
- // 目前单页面未处理 query=>props
- return `{
- path:'/${isEntry ? '' : path}',${alias}
- component:{setup(){ const app = getApp(); const query = app && app.$route && app.$route.query || {}; 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';
- 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,
- })
- `;
- }
|