提交 2326c09e 编写于 作者: fxy060608's avatar fxy060608

wip(mp): project.config.json

上级 ef697f1d
...@@ -10,79 +10,79 @@ const __uniRoutes = instanceContext.__uniRoutes; ...@@ -10,79 +10,79 @@ const __uniRoutes = instanceContext.__uniRoutes;
var serviceContext = (function (vue) { var serviceContext = (function (vue) {
'use strict'; 'use strict';
/* /*
* base64-arraybuffer * base64-arraybuffer
* https://github.com/niklasvh/base64-arraybuffer * https://github.com/niklasvh/base64-arraybuffer
* *
* Copyright (c) 2012 Niklas von Hertzen * Copyright (c) 2012 Niklas von Hertzen
* Licensed under the MIT license. * Licensed under the MIT license.
*/ */
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
// Use a lookup table to find the index. // Use a lookup table to find the index.
var lookup = /*#__PURE__*/ (function () { var lookup = /*#__PURE__*/ (function () {
const lookup = new Uint8Array(256); const lookup = new Uint8Array(256);
for (var i = 0; i < chars.length; i++) { for (var i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i; lookup[chars.charCodeAt(i)] = i;
} }
return lookup return lookup
})(); })();
function encode$3(arraybuffer) { function encode$3(arraybuffer) {
var bytes = new Uint8Array(arraybuffer), var bytes = new Uint8Array(arraybuffer),
i, i,
len = bytes.length, len = bytes.length,
base64 = ''; base64 = '';
for (i = 0; i < len; i += 3) { for (i = 0; i < len; i += 3) {
base64 += chars[bytes[i] >> 2]; base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)]; base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)]; base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
base64 += chars[bytes[i + 2] & 63]; base64 += chars[bytes[i + 2] & 63];
} }
if (len % 3 === 2) { if (len % 3 === 2) {
base64 = base64.substring(0, base64.length - 1) + '='; base64 = base64.substring(0, base64.length - 1) + '=';
} else if (len % 3 === 1) { } else if (len % 3 === 1) {
base64 = base64.substring(0, base64.length - 2) + '=='; base64 = base64.substring(0, base64.length - 2) + '==';
} }
return base64 return base64
} }
function decode$1(base64) { function decode$1(base64) {
var bufferLength = base64.length * 0.75, var bufferLength = base64.length * 0.75,
len = base64.length, len = base64.length,
i, i,
p = 0, p = 0,
encoded1, encoded1,
encoded2, encoded2,
encoded3, encoded3,
encoded4; encoded4;
if (base64[base64.length - 1] === '=') { if (base64[base64.length - 1] === '=') {
bufferLength--; bufferLength--;
if (base64[base64.length - 2] === '=') { if (base64[base64.length - 2] === '=') {
bufferLength--; bufferLength--;
} }
} }
var arraybuffer = new ArrayBuffer(bufferLength), var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer); bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i += 4) { for (i = 0; i < len; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)]; encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i + 1)]; encoded2 = lookup[base64.charCodeAt(i + 1)];
encoded3 = lookup[base64.charCodeAt(i + 2)]; encoded3 = lookup[base64.charCodeAt(i + 2)];
encoded4 = lookup[base64.charCodeAt(i + 3)]; encoded4 = lookup[base64.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
} }
return arraybuffer return arraybuffer
} }
/** /**
......
export * from './mp'
export * from './app' export * from './app'
export * from './json' export * from './json'
export * from './pages' export * from './pages'
......
export { AppJson } from './types'
export { parseMiniProgramPagesJson } from './pages'
export { parseMiniProgramProjectJson } from './project'
import { ComponentJson, PageWindowOptions, UsingComponents } from './types'
export const jsonPagesCache = new Map<string, PageWindowOptions>()
export const jsonComponentsCache = new Map<string, ComponentJson>()
export const jsonUsingComponentsCache = new Map<string, UsingComponents>()
export function addPageJson(filename: string, json: PageWindowOptions) {
jsonPagesCache.set(filename, json)
}
export function addComponentJson(filename: string, json: ComponentJson) {
jsonComponentsCache.set(filename, json)
}
export function addUsingComponents(filename: string, json: UsingComponents) {
jsonUsingComponentsCache.set(filename, json)
}
import path from 'path'
import { hasOwn } from '@vue/shared'
import { parseJson } from '../json'
import { validatePages } from '../pages'
import { AppJson, NetworkTimeout, PageWindowOptions } from './types'
import { parseTabBar, parseWindowOptions } from './utils'
import { normalizePath } from '../../utils'
interface ParsePagesJsonOptions {
debug?: boolean
darkmode?: boolean
subpackages: boolean
windowOptionsMap?: Record<string, string>
tabBarOptionsMap?: Record<string, string>
tabBarItemOptionsMap?: Record<string, string>
networkTimeout?: NetworkTimeout
}
export function parseMiniProgramPagesJson(
jsonStr: string,
platform: UniApp.PLATFORM,
options: ParsePagesJsonOptions = { subpackages: false }
) {
return parsePagesJson(jsonStr, platform, options)
}
function parsePagesJson(
jsonStr: string,
platform: UniApp.PLATFORM,
{
debug,
darkmode,
networkTimeout,
subpackages,
windowOptionsMap,
}: ParsePagesJsonOptions = {
subpackages: false,
}
) {
const appJson: AppJson = {
pages: [],
}
const pageJsons: Record<string, PageWindowOptions> = {}
// preprocess
const pagesJson = parseJson(jsonStr, true) as UniApp.PagesJson
if (!pagesJson) {
throw new Error(`[vite] Error: pages.json parse failed.\n`)
}
function addPageJson(pagePath: string, style: UniApp.PagesJsonPageStyle) {
pageJsons[pagePath] = parseWindowOptions(style, platform, windowOptionsMap)
}
// pages
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 }) => {
return {
root,
pages: pages.map((page) => {
addPageJson(normalizePath(path.join(root, page.path)), page.style)
return page.path
}),
}
})
} else {
pagesJson.subPackages.forEach(({ root, pages }) => {
pages.forEach((page) => {
const pagePath = normalizePath(path.join(root, page.path))
appJson.pages.push(pagePath)
addPageJson(pagePath, page.style)
})
})
}
}
// window
if (pagesJson.globalStyle) {
appJson.window = parseWindowOptions(
pagesJson.globalStyle,
platform,
windowOptionsMap
)
}
// tabBar
if (pagesJson.tabBar) {
const tabBar = parseTabBar(pagesJson.tabBar!, platform)
if (tabBar) {
appJson.tabBar = tabBar
}
}
;['preloadRule', 'workers', 'usingComponents'].forEach((name) => {
if (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,
}
}
import { hasOwn } from '@vue/shared'
import { parseJson } from '../json'
interface ParseMiniProgramProjectJsonOptions {
template: Record<string, any>
}
interface ProjectConfig {
projectname: string
}
const projectKeys = [
'appid',
'setting',
'miniprogramRoot',
'cloudfunctionRoot',
'qcloudRoot',
'pluginRoot',
'compileType',
'libVersion',
'projectname',
'packOptions',
'debugOptions',
'scripts',
'cloudbaseRoot',
]
export function parseMiniProgramProjectJson(
jsonStr: string,
platform: UniApp.PLATFORM,
{ template }: ParseMiniProgramProjectJsonOptions
) {
const project = JSON.parse(JSON.stringify(template)) as ProjectConfig
const manifestJson = parseJson(jsonStr)
if (manifestJson) {
project.projectname = manifestJson.name
const platformConfig = manifestJson[platform]
if (platformConfig) {
projectKeys.forEach((name) => {
if (hasOwn(platformConfig, name)) {
;(project as Record<string, any>)[name] = platformConfig[name]
}
})
}
}
return project
}
export interface ComponentJson {
component: true
usingComponents?: UsingComponents
}
interface ShareWindowOptions {
navigationBarBackgroundColor?: string // #000000
navigationBarTextStyle?: 'white' | 'black' // white
navigationBarTitleText?: string
navigationStyle?: 'default' | 'custom' // default
backgroundColor?: string // #ffffff
backgroundTextStyle?: 'dark' | 'light' // dark
backgroundColorTop?: string // #ffffff
backgroundColorBottom?: string // #ffffff
enablePullDownRefresh?: boolean // false
onReachBottomDistance?: number // 50
pageOrientation?: 'portrait' | 'landscape' | 'auto' // portrait
}
type Style = 'v2' | string
type RestartStrategy = 'homePage' | 'homePageAndLatestPage' | string
export interface PageWindowOptions extends ShareWindowOptions {
disableScroll?: boolean // false
usingComponents?: UsingComponents
initialRenderingCache?: 'static' | string
style?: Style
singlePage?: SinglePage
restartStrategy?: RestartStrategy
}
export interface AppWindowOptions extends ShareWindowOptions {
visualEffectInBackground?: 'none' | 'hidden' // none
}
interface SubPackage {
name?: string
root: string
pages: string[]
independent?: boolean
}
interface TabBarItem {
pagePath: string
text: string
iconPath?: string
selectedIconPath?: string
}
export interface TabBar {
color: string
selectedColor: string
backgroundColor: string
borderStyle?: 'black' | 'white' // black
list: TabBarItem[]
position?: 'bottom' | 'top' // bottom
custom?: boolean
}
export interface NetworkTimeout {
request?: number // 60000
requeconnectSocketst?: number // 60000
uploadFile?: number // 60000
downloadFile?: number // 60000
}
interface Plugins {
[name: string]: {
version: string
provider: string
}
}
interface PreloadRule {
[name: string]: {
network: 'wifi' | 'all' // wifi
packages: string[]
}
}
export interface UsingComponents {
[name: string]: string
}
interface Permission {
[name: string]: {
desc: string
}
}
interface UseExtendedLib {
kbone: boolean
weui: boolean
}
interface EntranceDeclare {
locationMessage: {
path: string
query: string
}
}
interface SinglePage {
navigationBarFit?: 'squeezed' | 'float'
}
export interface AppJson {
entryPagePath?: string // mp-weixin
pages: string[]
window?: AppWindowOptions
tabBar?: TabBar
networkTimeout?: NetworkTimeout
debug?: boolean
functionalPages?: boolean
subPackages?: SubPackage[]
workers?: string
requiredBackgroundModes?: string[] // audio,location
plugins?: Plugins
preloadRule?: PreloadRule
resizable?: boolean
usingComponents?: UsingComponents
permission?: Permission
sitemapLocation?: string
style?: Style
useExtendedLib?: UseExtendedLib
entranceDeclare?: EntranceDeclare
darkmode?: boolean
themeLocation?: string
lazyCodeLoading?: 'requiredComponents' | string
singlePage?: SinglePage
restartStrategy?: RestartStrategy
}
import { extend, hasOwn } from '@vue/shared'
import { removePlatformStyle } from '../pages'
import { AppWindowOptions, PageWindowOptions, TabBar } from './types'
function trimJson(json: Record<string, any>) {
delete json.maxWidth
delete json.topWindow
delete json.leftWindow
delete json.rightWindow
if (json.tabBar) {
delete json.tabBar.matchMedia
}
return json
}
function convert(
to: Record<string, any>,
from: Record<string, any>,
map: Record<string, string>
) {
Object.keys(map).forEach((key) => {
if (hasOwn(from, map[key])) {
to[key] = from[map[key]]
}
})
return to
}
export function parseWindowOptions(
style: UniApp.PagesJsonPageStyle,
platform: UniApp.PLATFORM,
windowOptionsMap?: Record<string, string>
): PageWindowOptions | AppWindowOptions {
if (!style) {
return {}
}
const platformStyle = style[platform] || {}
removePlatformStyle(trimJson(style) as any)
const res: PageWindowOptions | AppWindowOptions = {}
if (windowOptionsMap) {
return extend(convert(res, style, windowOptionsMap), platformStyle)
}
return extend(res, style, platformStyle)
}
function trimTabBarJson(tabBar: UniApp.TabBarOptions) {
;(
[
'fontSize',
'height',
'iconWidth',
'midButton',
'selectedIndex',
'spacing',
] as const
).forEach((name) => {
delete tabBar[name]
})
return tabBar
}
export function parseTabBar(
tabBar: UniApp.TabBarOptions,
platform: UniApp.PLATFORM,
tabBarOptionsMap?: Record<string, string>,
tabBarItemOptionsMap?: Record<string, string>
): TabBar {
const platformStyle = (tabBar as any)[platform] || {}
removePlatformStyle(trimTabBarJson(tabBar) as any)
const res = {} as TabBar
if (tabBarOptionsMap) {
if (tabBarItemOptionsMap && tabBar.list) {
tabBar.list = tabBar.list.map((item) => {
return convert(
{},
item,
tabBarItemOptionsMap
) as UniApp.TabBarItemOptions
})
}
convert(res, tabBar, tabBarOptionsMap)
return extend(res, platformStyle)
}
return extend(res, tabBar, platformStyle)
}
...@@ -20,7 +20,15 @@ export const parsePagesJson = ( ...@@ -20,7 +20,15 @@ export const parsePagesJson = (
export const parsePagesJsonOnce = once(parsePagesJson) export const parsePagesJsonOnce = once(parsePagesJson)
export function normalizePagesJson(jsonStr: string, platform: UniApp.PLATFORM) { export function normalizePagesJson(
jsonStr: string,
platform: UniApp.PLATFORM,
{
subpackages,
}: {
subpackages: boolean
} = { subpackages: false }
) {
let pagesJson: UniApp.PagesJson = { let pagesJson: UniApp.PagesJson = {
pages: [], pages: [],
globalStyle: { globalStyle: {
...@@ -35,10 +43,21 @@ export function normalizePagesJson(jsonStr: string, platform: UniApp.PLATFORM) { ...@@ -35,10 +43,21 @@ export function normalizePagesJson(jsonStr: string, platform: UniApp.PLATFORM) {
} }
// pages // pages
validatePages(pagesJson, jsonStr) validatePages(pagesJson, jsonStr)
pagesJson.subPackages = pagesJson.subPackages || pagesJson.subpackages
delete pagesJson.subpackages
// subpackages // subpackages
pagesJson.pages.push( if (pagesJson.subPackages) {
...normalizeSubpackages(pagesJson.subPackages || 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 // pageStyle
normalizePages(pagesJson.pages, platform) normalizePages(pagesJson.pages, platform)
...@@ -64,7 +83,7 @@ export function normalizePagesJson(jsonStr: string, platform: UniApp.PLATFORM) { ...@@ -64,7 +83,7 @@ export function normalizePagesJson(jsonStr: string, platform: UniApp.PLATFORM) {
return pagesJson return pagesJson
} }
function validatePages(pagesJson: Record<string, any>, jsonStr: string) { export function validatePages(pagesJson: Record<string, any>, jsonStr: string) {
if (!Array.isArray(pagesJson.pages)) { if (!Array.isArray(pagesJson.pages)) {
pagesJson.pages = [] pagesJson.pages = []
throw new Error(`[uni-app] Error: pages.json->pages parse failed.`) throw new Error(`[uni-app] Error: pages.json->pages parse failed.`)
...@@ -79,9 +98,8 @@ function normalizePages( ...@@ -79,9 +98,8 @@ function normalizePages(
pages: UniApp.PagesJsonPageOptions[], pages: UniApp.PagesJsonPageOptions[],
platform: UniApp.PLATFORM platform: UniApp.PLATFORM
) { ) {
return pages.filter((page) => { return pages.forEach((page) => {
page.style = normalizePageStyle(page.path, page.style!, platform) page.style = normalizePageStyle(page.path, page.style!, platform)
return true
}) })
} }
...@@ -151,7 +169,8 @@ function normalizePageStyle( ...@@ -151,7 +169,8 @@ function normalizePageStyle(
} }
} }
pageStyle.isNVue = isNVue pageStyle.isNVue = isNVue
return removePlatformStyle(pageStyle) removePlatformStyle(pageStyle)
return pageStyle
} }
return { navigationBar: {}, isNVue } return { navigationBar: {}, isNVue }
} }
...@@ -347,7 +366,7 @@ function normalizeFilepath(filepath: string) { ...@@ -347,7 +366,7 @@ function normalizeFilepath(filepath: string) {
const platforms = ['h5', 'app', 'mp-', 'quickapp'] const platforms = ['h5', 'app', 'mp-', 'quickapp']
function removePlatformStyle(pageStyle: UniApp.PagesJsonPageStyle) { export function removePlatformStyle(pageStyle: Record<string, any>) {
Object.keys(pageStyle).forEach((name) => { Object.keys(pageStyle).forEach((name) => {
if (platforms.find((prefix) => name.startsWith(prefix))) { if (platforms.find((prefix) => name.startsWith(prefix))) {
delete pageStyle[name as UniApp.PLATFORM] delete pageStyle[name as UniApp.PLATFORM]
......
...@@ -28,7 +28,7 @@ export interface UniMiniProgramPluginOptions { ...@@ -28,7 +28,7 @@ export interface UniMiniProgramPluginOptions {
} }
project?: { project?: {
filename: string filename: string
source: Record<string, unknown> source: Record<string, any>
} }
template: { template: {
extname: string extname: string
......
...@@ -4,52 +4,61 @@ import { Plugin } from 'vite' ...@@ -4,52 +4,61 @@ import { Plugin } from 'vite'
import { import {
defineUniManifestJsonPlugin, defineUniManifestJsonPlugin,
getLocaleFiles, getLocaleFiles,
parseMiniProgramProjectJson,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '../plugin' import { UniMiniProgramPluginOptions } from '../plugin'
export function uniManifestJsonPlugin( export function uniManifestJsonPlugin(
options: UniMiniProgramPluginOptions options: UniMiniProgramPluginOptions
): Plugin { ): Plugin {
let projectJson: Record<string, any>
return defineUniManifestJsonPlugin((opts) => { return defineUniManifestJsonPlugin((opts) => {
return { return {
name: 'vite:uni-mp-manifest-json', name: 'vite:uni-mp-manifest-json',
enforce: 'pre', enforce: 'pre',
transform(_code, id) { transform(code, id) {
if (!opts.filter(id)) { if (!opts.filter(id)) {
return return
} }
this.addWatchFile( const inputDir = process.env.UNI_INPUT_DIR
path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json') this.addWatchFile(path.resolve(inputDir, 'manifest.json'))
) getLocaleFiles(path.resolve(inputDir, 'locale')).forEach((filepath) => {
getLocaleFiles(
path.resolve(process.env.UNI_INPUT_DIR, 'locale')
).forEach((filepath) => {
this.addWatchFile(filepath) this.addWatchFile(filepath)
}) })
// manifestJson = normalizeAppManifestJson( if (options.project) {
// parseJson(code), const template = options.project.source
// parsePagesJsonOnce( let projectname = path.basename(inputDir)
// process.env.UNI_INPUT_DIR, if (projectname === 'src') {
// process.env.UNI_PLATFORM projectname = path.basename(path.dirname(inputDir))
// ) }
// ) template.projectname = projectname
// TODO condition
if (process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
if (!template.setting) {
template.setting = {}
}
template.setting.urlCheck = false
}
projectJson = parseMiniProgramProjectJson(
code,
process.env.UNI_PLATFORM,
{ template }
)
}
return { return {
code: '', code: '',
map: this.getCombinedSourcemap(), map: this.getCombinedSourcemap(),
} }
}, },
generateBundle() { generateBundle() {
// 生成一个空的app-config.js,兼容基座已有规范 if (projectJson && options.project) {
// this.emitFile({ this.emitFile({
// fileName: `app-config.js`, fileName: options.project.filename,
// type: 'asset', type: 'asset',
// source: '(function(){})();', source: JSON.stringify(projectJson, null, 2),
// }) })
// this.emitFile({ }
// fileName: `manifest.json`,
// type: 'asset',
// source: JSON.stringify(manifestJson, null, 2),
// })
}, },
} }
}) })
......
import fs from 'fs'
import path from 'path' import path from 'path'
import { Plugin } from 'vite' import { Plugin } from 'vite'
import { import {
AppJson,
defineUniPagesJsonPlugin, defineUniPagesJsonPlugin,
normalizePagesJson,
getLocaleFiles, getLocaleFiles,
normalizePagePath, normalizePagePath,
normalizeNodeModules,
parseManifestJsonOnce,
parseMiniProgramPagesJson,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { virtualPagePath } from './virtual' import { virtualPagePath } from './virtual'
import { UniMiniProgramPluginOptions } from '../plugin' import { UniMiniProgramPluginOptions } from '../plugin'
...@@ -13,7 +17,7 @@ import { UniMiniProgramPluginOptions } from '../plugin' ...@@ -13,7 +17,7 @@ import { UniMiniProgramPluginOptions } from '../plugin'
export function uniPagesJsonPlugin( export function uniPagesJsonPlugin(
options: UniMiniProgramPluginOptions options: UniMiniProgramPluginOptions
): Plugin { ): Plugin {
let pagesJson: UniApp.PagesJson let appJson: AppJson
return defineUniPagesJsonPlugin((opts) => { return defineUniPagesJsonPlugin((opts) => {
return { return {
name: 'vite:uni-mp-pages-json', name: 'vite:uni-mp-pages-json',
...@@ -22,49 +26,61 @@ export function uniPagesJsonPlugin( ...@@ -22,49 +26,61 @@ export function uniPagesJsonPlugin(
if (!opts.filter(id)) { if (!opts.filter(id)) {
return return
} }
this.addWatchFile(path.resolve(process.env.UNI_INPUT_DIR, 'pages.json')) const inputDir = process.env.UNI_INPUT_DIR
getLocaleFiles( this.addWatchFile(path.resolve(inputDir, 'pages.json'))
path.resolve(process.env.UNI_INPUT_DIR, 'locale') getLocaleFiles(path.resolve(inputDir, 'locale')).forEach((filepath) => {
).forEach((filepath) => {
this.addWatchFile(filepath) this.addWatchFile(filepath)
}) })
pagesJson = normalizePagesJson(code, process.env.UNI_PLATFORM) const manifestJson = parseManifestJsonOnce(inputDir)
// TODO subpackages const res = parseMiniProgramPagesJson(code, process.env.UNI_PLATFORM, {
pagesJson.pages.forEach((page) => { debug: !!manifestJson.debug,
this.addWatchFile( darkmode:
path.resolve(process.env.UNI_INPUT_DIR, page.path + '.vue') options.app.darkmode &&
) fs.existsSync(path.resolve(inputDir, 'theme.json')),
networkTimeout: manifestJson.networkTimeout,
subpackages: options.app.subpackages,
})
appJson = res.appJson
Object.keys(res.pageJsons).forEach((name) => {
this.emitFile({
fileName: normalizeNodeModules(name) + '.json',
type: 'asset',
source: JSON.stringify(res.pageJsons[name], null, 2),
})
}) })
return { return {
code: code: `import './manifest.json.js'\n` + importPagesCode(appJson),
`import './manifest.json.js'\n` +
normalizeMiniProgramPagesJson(pagesJson),
map: this.getCombinedSourcemap(), map: this.getCombinedSourcemap(),
} }
}, },
generateBundle() { generateBundle() {
// this.emitFile({ if (appJson) {
// fileName: `app-config-service.js`, this.emitFile({
// type: 'asset', fileName: `app.json`,
// source: normalizeAppConfigService( type: 'asset',
// pagesJson, source: JSON.stringify(appJson, null, 2),
// parseManifestJsonOnce(process.env.UNI_INPUT_DIR) })
// ), }
// })
}, },
} }
}) })
} }
function normalizeMiniProgramPagesJson(pagesJson: Record<string, any>) { function importPagesCode(pagesJson: AppJson) {
const importPagesCode: string[] = [] const importPagesCode: string[] = []
pagesJson.pages.forEach((page: UniApp.PagesJsonPageOptions) => { function importPageCode(pagePath: string) {
const pagePath = page.path
const pagePathWithExtname = normalizePagePath(pagePath, 'app') const pagePathWithExtname = normalizePagePath(pagePath, 'app')
if (pagePathWithExtname) { if (pagePathWithExtname) {
importPagesCode.push(`import('${virtualPagePath(pagePathWithExtname)}')`) importPagesCode.push(`import('${virtualPagePath(pagePathWithExtname)}')`)
} }
}) }
pagesJson.pages.forEach((pagePath) => importPageCode(pagePath))
if (pagesJson.subPackages) {
pagesJson.subPackages.forEach(({ root, pages }) => {
pages &&
pages.forEach((pagePath) => importPageCode(path.join(root, pagePath)))
})
}
return `if(!Math){ return `if(!Math){
${importPagesCode.join('\n')} ${importPagesCode.join('\n')}
}` }`
......
...@@ -54,6 +54,7 @@ export function uniVirtualPlugin({ ...@@ -54,6 +54,7 @@ export function uniVirtualPlugin({
const filepath = normalizePath( const filepath = normalizePath(
path.resolve(inputDir, parseVirtualPagePath(id)) path.resolve(inputDir, parseVirtualPagePath(id))
) )
this.addWatchFile(filepath)
return { return {
code: `import Page from '${filepath}?mpType=page' code: `import Page from '${filepath}?mpType=page'
${global}.createPage(Page)`, ${global}.createPage(Page)`,
...@@ -62,6 +63,7 @@ ${global}.createPage(Page)`, ...@@ -62,6 +63,7 @@ ${global}.createPage(Page)`,
const filepath = normalizePath( const filepath = normalizePath(
path.resolve(inputDir, parseVirtualComponentPath(id)) path.resolve(inputDir, parseVirtualComponentPath(id))
) )
this.addWatchFile(filepath)
return { return {
code: `import Component from '${filepath}' code: `import Component from '${filepath}'
${global}.createComponent(Component)`, ${global}.createComponent(Component)`,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册