| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.filterPlatformPages = exports.parseSubpackagesRootOnce = exports.normalizePagesRoute = exports.removePlatformStyle = exports.validatePages = exports.normalizePagesJson = exports.parsePagesJsonOnce = exports.parsePagesJson = exports.isUniPageSfcFile = exports.isUniPageSetupAndTs = exports.isUniPageFile = void 0;
- const fs_1 = __importDefault(require("fs"));
- const path_1 = __importDefault(require("path"));
- const shared_1 = require("@vue/shared");
- const uni_shared_1 = require("@dcloudio/uni-shared");
- const utils_1 = require("../utils");
- const json_1 = require("./json");
- const utils_2 = require("../vue/utils");
- const vite_1 = require("../vite");
- const constants_1 = require("../constants");
- const theme_1 = require("./theme");
- const manifest_1 = require("./manifest");
- const pagesCacheSet = new Set();
- function isUniPageFile(file, inputDir = process.env.UNI_INPUT_DIR) {
- if (inputDir && path_1.default.isAbsolute(file)) {
- file = (0, utils_1.normalizePath)(path_1.default.relative(inputDir, file));
- }
- return pagesCacheSet.has((0, utils_1.removeExt)(file));
- }
- exports.isUniPageFile = isUniPageFile;
- function isUniPageSetupAndTs(file) {
- const { filename, query } = (0, vite_1.parseVueRequest)(file);
- return !!(query.vue &&
- query.setup &&
- (0, shared_1.hasOwn)(query, 'lang.ts') &&
- constants_1.EXTNAME_VUE_RE.test(filename));
- }
- exports.isUniPageSetupAndTs = isUniPageSetupAndTs;
- function isUniPageSfcFile(file, inputDir = process.env.UNI_INPUT_DIR) {
- return (0, utils_2.isVueSfcFile)(file) && isUniPageFile(file, inputDir);
- }
- exports.isUniPageSfcFile = isUniPageSfcFile;
- /**
- * 小程序平台慎用,因为该解析不支持 subpackages
- * @param inputDir
- * @param platform
- * @param normalize
- * @returns
- */
- const parsePagesJson = (inputDir, platform, normalize = true) => {
- const jsonStr = fs_1.default.readFileSync(path_1.default.join(inputDir, 'pages.json'), 'utf8');
- if (normalize) {
- return normalizePagesJson(jsonStr, platform);
- }
- return (0, json_1.parseJson)(jsonStr, true);
- };
- exports.parsePagesJson = parsePagesJson;
- /**
- * 该方法解析出来的是不支持 subpackages,会被合并入 pages
- */
- exports.parsePagesJsonOnce = (0, uni_shared_1.once)(exports.parsePagesJson);
- /**
- * 目前 App 和 H5 使用了该方法
- * @param jsonStr
- * @param platform
- * @param param2
- * @returns
- */
- function normalizePagesJson(jsonStr, platform, { subpackages, } = { subpackages: false }) {
- let pagesJson = {
- pages: [],
- globalStyle: {
- navigationBar: {},
- },
- };
- // preprocess
- try {
- pagesJson = (0, json_1.parseJson)(jsonStr, true);
- }
- catch (e) {
- console.error(`[vite] Error: pages.json parse failed.\n`, jsonStr, e);
- }
- // pages
- validatePages(pagesJson, jsonStr);
- pagesJson.subPackages = pagesJson.subPackages || pagesJson.subpackages;
- delete pagesJson.subpackages;
- // subpackages
- if (pagesJson.subPackages) {
- if (subpackages) {
- pagesJson.subPackages.forEach(({ pages }) => {
- pages && normalizePages(pages, platform);
- });
- }
- else {
- pagesJson.pages.push(...normalizeSubpackages(pagesJson.subPackages));
- delete pagesJson.subPackages;
- }
- }
- else {
- delete pagesJson.subPackages;
- }
- // pageStyle
- normalizePages(pagesJson.pages, platform);
- // globalStyle
- pagesJson.globalStyle = normalizePageStyle(null, pagesJson.globalStyle, platform);
- // tabBar
- if (pagesJson.tabBar) {
- const tabBar = normalizeTabBar(pagesJson.tabBar, platform);
- if (tabBar) {
- pagesJson.tabBar = tabBar;
- }
- else {
- delete pagesJson.tabBar;
- }
- }
- // 过滤平台page
- filterPlatformPages(platform, pagesJson);
- // 缓存页面列表
- pagesCacheSet.clear();
- pagesJson.pages.forEach((page) => pagesCacheSet.add(page.path));
- const manifestJsonPlatform = (0, manifest_1.getPlatformManifestJsonOnce)();
- if (!manifestJsonPlatform.darkmode) {
- const { pages, globalStyle, tabBar } = (0, theme_1.initTheme)(manifestJsonPlatform, pagesJson);
- (0, shared_1.extend)(pagesJson, { pages, globalStyle, tabBar });
- }
- return pagesJson;
- }
- exports.normalizePagesJson = normalizePagesJson;
- function validatePages(pagesJson, jsonStr) {
- if (!(0, shared_1.isArray)(pagesJson.pages)) {
- pagesJson.pages = [];
- console.error(`[uni-app] Error: pages.json->pages parse failed.`);
- process.exit(0);
- }
- else if (!pagesJson.pages.length) {
- console.error(`[uni-app] Error: pages.json->pages must contain at least 1 page.`);
- process.exit(0);
- }
- else {
- const pages = [];
- pagesJson.pages.forEach((page) => {
- if (pages.indexOf(page.path) !== -1) {
- console.error(`[uni-app] Error: pages.json->${page.path} duplication.`);
- process.exit(0);
- }
- pages.push(page.path);
- });
- }
- }
- exports.validatePages = validatePages;
- function normalizePages(pages, platform) {
- pages.forEach((page) => {
- page.style = normalizePageStyle(page.path, page.style, platform);
- });
- if (platform !== 'app') {
- return;
- }
- const subNVuePages = [];
- // subNVues
- pages.forEach(({ style: { subNVues } }) => {
- if (!(0, shared_1.isArray)(subNVues)) {
- return;
- }
- subNVues.forEach((subNVue) => {
- if (subNVue && subNVue.path) {
- subNVuePages.push({
- path: subNVue.path,
- style: { isSubNVue: true, isNVue: true, navigationBar: {} },
- });
- }
- });
- });
- if (subNVuePages.length) {
- pages.push(...subNVuePages);
- }
- }
- 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 = normalizeSubpackageSubNVues(root, subPage.style);
- pages.push(subPage);
- });
- }
- });
- }
- return pages;
- }
- function normalizeSubpackageSubNVues(root, style = { navigationBar: {} }) {
- const platformStyle = style['app'] || style['app-plus'];
- if (!platformStyle) {
- return style;
- }
- if ((0, shared_1.isArray)(platformStyle.subNVues)) {
- platformStyle.subNVues.forEach((subNVue) => {
- if (subNVue.path) {
- subNVue.path = (0, utils_1.normalizePath)(path_1.default.join(root, subNVue.path));
- }
- });
- }
- return style;
- }
- function normalizePageStyle(pagePath, pageStyle, platform) {
- let isNVue = false;
- if (process.env.UNI_APP_X === 'true') {
- isNVue = undefined;
- }
- else {
- const hasNVue = pagePath &&
- process.env.UNI_INPUT_DIR &&
- fs_1.default.existsSync(path_1.default.join(process.env.UNI_INPUT_DIR, pagePath + '.nvue'))
- ? true
- : undefined;
- if (hasNVue) {
- const hasVue = fs_1.default.existsSync(path_1.default.join(process.env.UNI_INPUT_DIR, pagePath + '.vue'));
- if (hasVue) {
- if (platform === 'app') {
- if (process.env.UNI_NVUE_COMPILER !== 'vue') {
- isNVue = true;
- }
- }
- }
- else {
- isNVue = true;
- }
- }
- }
- if (pageStyle) {
- if (platform === 'h5') {
- (0, shared_1.extend)(pageStyle, pageStyle['app'] || pageStyle['app-plus'], pageStyle['web'] || pageStyle['h5']);
- }
- else if (platform === 'app') {
- (0, shared_1.extend)(pageStyle, pageStyle['app'] || pageStyle['app-plus']);
- }
- else {
- (0, shared_1.extend)(pageStyle, pageStyle[platform]);
- }
- if (['h5', 'app'].includes(platform)) {
- pageStyle.navigationBar = normalizeNavigationBar(pageStyle);
- if (isEnablePullDownRefresh(pageStyle)) {
- pageStyle.enablePullDownRefresh = true;
- pageStyle.pullToRefresh = normalizePullToRefresh(pageStyle);
- }
- if (platform === 'app') {
- pageStyle.disableSwipeBack === true
- ? (pageStyle.popGesture = 'none')
- : delete pageStyle.popGesture;
- delete pageStyle.disableSwipeBack;
- }
- }
- pageStyle.isNVue = isNVue;
- removePlatformStyle(pageStyle);
- return pageStyle;
- }
- return { navigationBar: {}, isNVue };
- }
- const navigationBarMaps = {
- navigationBarBackgroundColor: 'backgroundColor',
- navigationBarTextStyle: 'textStyle',
- navigationBarTitleText: 'titleText',
- navigationStyle: 'style',
- titleImage: 'titleImage',
- titlePenetrate: 'titlePenetrate',
- transparentTitle: 'transparentTitle',
- };
- function normalizeNavigationBar(pageStyle) {
- const navigationBar = Object.create(null);
- Object.keys(navigationBarMaps).forEach((name) => {
- if ((0, shared_1.hasOwn)(pageStyle, name)) {
- navigationBar[navigationBarMaps[name]] =
- pageStyle[name];
- delete pageStyle[name];
- }
- });
- navigationBar.type = navigationBar.type || 'default';
- const { titleNView } = pageStyle;
- if ((0, shared_1.isPlainObject)(titleNView)) {
- (0, shared_1.extend)(navigationBar, titleNView);
- delete pageStyle.titleNView;
- }
- else if (titleNView === false) {
- navigationBar.style = 'custom';
- }
- if ((0, shared_1.hasOwn)(navigationBar, 'transparentTitle')) {
- const transparentTitle = navigationBar.transparentTitle;
- if (transparentTitle === 'always') {
- navigationBar.style = 'custom';
- navigationBar.type = 'float';
- }
- else if (transparentTitle === 'auto') {
- navigationBar.type = 'transparent';
- }
- else {
- navigationBar.type = 'default';
- }
- delete navigationBar.transparentTitle;
- }
- if (navigationBar.titleImage && navigationBar.titleText) {
- delete navigationBar.titleText;
- }
- if (!navigationBar.titleColor && (0, shared_1.hasOwn)(navigationBar, 'textStyle')) {
- const textStyle = navigationBar.textStyle;
- if (constants_1.TEXT_STYLE.includes(textStyle)) {
- navigationBar.titleColor = (0, uni_shared_1.normalizeTitleColor)(textStyle);
- }
- else {
- navigationBar.titleColor = navigationBar.textStyle;
- }
- delete navigationBar.textStyle;
- }
- if (pageStyle.navigationBarShadow &&
- pageStyle.navigationBarShadow.colorType) {
- navigationBar.shadowColorType = pageStyle.navigationBarShadow.colorType;
- delete pageStyle.navigationBarShadow;
- }
- const parsedNavigationBar = (0, theme_1.initTheme)((0, manifest_1.getPlatformManifestJsonOnce)(), navigationBar);
- if ((0, shared_1.isArray)(navigationBar.buttons)) {
- navigationBar.buttons = navigationBar.buttons.map((btn) => normalizeNavigationBarButton(btn, navigationBar.type, parsedNavigationBar.titleColor));
- }
- if ((0, shared_1.isPlainObject)(navigationBar.searchInput)) {
- navigationBar.searchInput = normalizeNavigationBarSearchInput(navigationBar.searchInput);
- }
- if (navigationBar.type === 'transparent') {
- navigationBar.coverage = navigationBar.coverage || '132px';
- }
- return navigationBar;
- }
- function normalizeNavigationBarButton(btn, type, titleColor) {
- btn.color = btn.color || titleColor;
- if (!btn.fontSize) {
- btn.fontSize =
- type === 'transparent' || (btn.text && /\\u/.test(btn.text))
- ? '22px'
- : '27px';
- }
- else if (/\d$/.test(btn.fontSize)) {
- btn.fontSize += 'px';
- }
- btn.text = btn.text || '';
- return btn;
- }
- function normalizeNavigationBarSearchInput(searchInput) {
- return (0, shared_1.extend)({
- autoFocus: false,
- align: 'center',
- color: '#000',
- backgroundColor: 'rgba(255,255,255,0.5)',
- borderRadius: '0px',
- placeholder: '',
- placeholderColor: '#CCCCCC',
- disabled: false,
- }, searchInput);
- }
- const DEFAULT_TAB_BAR = {
- position: 'bottom',
- color: '#999',
- selectedColor: '#007aff',
- borderStyle: 'black',
- blurEffect: 'none',
- fontSize: '10px',
- iconWidth: '24px',
- spacing: '3px',
- height: uni_shared_1.TABBAR_HEIGHT + 'px',
- };
- function normalizeTabBar(tabBar, platform) {
- const { list, midButton } = tabBar;
- if (!list || !list.length) {
- return;
- }
- tabBar = (0, shared_1.extend)({}, DEFAULT_TAB_BAR, tabBar);
- list.forEach((item) => {
- if (item.iconPath) {
- item.iconPath = normalizeFilepath(item.iconPath);
- }
- if (item.selectedIconPath) {
- item.selectedIconPath = normalizeFilepath(item.selectedIconPath);
- }
- });
- if (midButton && midButton.backgroundImage) {
- midButton.backgroundImage = normalizeFilepath(midButton.backgroundImage);
- }
- tabBar.selectedIndex = 0;
- tabBar.shown = true;
- return tabBar;
- }
- const SCHEME_RE = /^([a-z-]+:)?\/\//i;
- const DATA_RE = /^data:.*,.*/;
- function normalizeFilepath(filepath) {
- const themeConfig = (0, theme_1.normalizeThemeConfigOnce)()['light'] || {};
- if (themeConfig[filepath.replace('@', '')])
- return filepath;
- if (!(SCHEME_RE.test(filepath) || DATA_RE.test(filepath)) &&
- filepath.indexOf('/') !== 0) {
- return (0, uni_shared_1.addLeadingSlash)(filepath);
- }
- return filepath;
- }
- const platforms = ['h5', 'app', 'mp-', 'quickapp', 'web'];
- function removePlatformStyle(pageStyle) {
- Object.keys(pageStyle).forEach((name) => {
- if (platforms.find((prefix) => name.startsWith(prefix))) {
- delete pageStyle[name];
- }
- });
- return pageStyle;
- }
- exports.removePlatformStyle = removePlatformStyle;
- function normalizePagesRoute(pagesJson) {
- const firstPagePath = pagesJson.pages[0].path;
- const tabBarList = (pagesJson.tabBar && pagesJson.tabBar.list) || [];
- return pagesJson.pages.map((pageOptions) => {
- const pagePath = pageOptions.path;
- const isEntry = firstPagePath === pagePath ? true : undefined;
- const tabBarIndex = tabBarList.findIndex((tabBarPage) => tabBarPage.pagePath === pagePath);
- const isTabBar = tabBarIndex !== -1 ? true : undefined;
- let windowTop = 0;
- const meta = (0, shared_1.extend)({
- isQuit: isEntry || isTabBar ? true : undefined,
- isEntry: isEntry || undefined,
- isTabBar: isTabBar || undefined,
- tabBarIndex: isTabBar ? tabBarIndex : undefined,
- windowTop: windowTop || undefined,
- }, pageOptions.style);
- return {
- path: pageOptions.path,
- meta,
- };
- });
- }
- exports.normalizePagesRoute = normalizePagesRoute;
- function isEnablePullDownRefresh(pageStyle) {
- return pageStyle.enablePullDownRefresh || pageStyle.pullToRefresh?.support;
- }
- function normalizePullToRefresh(pageStyle) {
- return pageStyle.pullToRefresh;
- }
- function parseSubpackagesRoot(inputDir, platform) {
- const pagesJson = (0, exports.parsePagesJson)(inputDir, platform, false);
- const subpackages = pagesJson.subPackages || pagesJson.subpackages;
- const roots = [];
- if ((0, shared_1.isArray)(subpackages)) {
- subpackages.forEach(({ root }) => {
- if (root) {
- roots.push(root);
- }
- });
- }
- return roots;
- }
- exports.parseSubpackagesRootOnce = (0, uni_shared_1.once)(parseSubpackagesRoot);
- let isInvalidPagesWarned = false;
- function filterPlatformPages(platform, pagesJson) {
- const invalidPages = [];
- pagesJson.pages = pagesJson.pages.filter(({ path }) => {
- if (isPlatformPage(platform, path)) {
- return true;
- }
- invalidPages.push(path);
- return false;
- });
- if (pagesJson.subPackages) {
- pagesJson.subPackages.forEach((subPackage) => {
- if (subPackage.pages) {
- subPackage.pages = subPackage.pages.filter(({ path }) => {
- const pagePath = subPackage.root + '/' + path;
- if (isPlatformPage(platform, pagePath)) {
- return true;
- }
- invalidPages.push(pagePath);
- return false;
- });
- }
- });
- }
- if (pagesJson.subpackages) {
- pagesJson.subpackages.forEach((subPackage) => {
- if (subPackage.pages) {
- subPackage.pages = subPackage.pages.filter(({ path }) => {
- const pagePath = subPackage.root + '/' + path;
- if (isPlatformPage(platform, pagePath)) {
- return true;
- }
- invalidPages.push(pagePath);
- return false;
- });
- }
- });
- }
- // 目前仅启动的时候警告一次,该方法可能会被调用很多次
- if (invalidPages.length) {
- if (!isInvalidPagesWarned) {
- isInvalidPagesWarned = true;
- console.log(`已忽略页面:${invalidPages.join('、')}。详见:https://uniapp.dcloud.net.cn/tutorial/platform.html#platforms`);
- }
- }
- }
- exports.filterPlatformPages = filterPlatformPages;
- function isPlatformPage(platform, pagePath) {
- if (pagePath.startsWith('platforms/')) {
- if (platform === 'app' || platform === 'app-plus') {
- return (pagePath.startsWith('platforms/app/') ||
- pagePath.startsWith('platforms/app-plus/'));
- }
- else if (platform === 'h5' || platform === 'web') {
- return (pagePath.startsWith('platforms/h5/') ||
- pagePath.startsWith('platforms/web/'));
- }
- return pagePath.startsWith('platforms/' + platform + '/');
- }
- return true;
- }
|