jialh
2025-01-07 bb59b053247ef82969b64979260e2478bd732e1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"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");
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',
    'usingComponents',
    'uniStatistics',
];
function mergeMiniProgramAppJson(appJson, platformJson = {}) {
    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, networkTimeout, subpackages, windowOptionsMap, tabBarOptionsMap, tabBarItemOptionsMap, } = {
    subpackages: false,
}) {
    const appJson = {
        pages: [],
    };
    const pageJsons = {};
    const nvuePages = [];
    // preprocess
    const pagesJson = (0, json_1.parseJson)(jsonStr, true);
    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(({ root, pages, ...rest }) => {
                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;
        }
    }
    ;
    ['preloadRule', 'workers', 'plugins'].forEach((name) => {
        if ((0, shared_1.hasOwn)(pagesJson, name)) {
            appJson[name] = pagesJson[name];
        }
    });
    if (debug) {
        appJson.debug = debug;
    }
    if (networkTimeout) {
        appJson.networkTimeout = networkTimeout;
    }
    if (darkmode) {
        appJson.darkmode = true;
        appJson.themeLocation = 'theme.json';
    }
    return {
        appJson,
        pageJsons,
        nvuePages,
    };
}