From ede722662612ecda7d134e704d78d6f6cb5c4a85 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 9 Mar 2020 16:14:19 +0800 Subject: [PATCH] feat(qa): add manifest.json --- .../uni-quickapp/lib/configure-webpack.js | 37 ++++++----- packages/uni-quickapp/lib/env.js | 3 - .../uni-quickapp/lib/manifest/base-parser.js | 28 ++++++++ .../lib/manifest/display-parser.js | 65 +++++++++++++++++++ packages/uni-quickapp/lib/manifest/index.js | 16 +++++ .../uni-quickapp/lib/manifest/pages-parser.js | 15 +++++ packages/uni-quickapp/lib/parse-entry.js | 3 - packages/uni-quickapp/lib/vue.config.js | 2 +- packages/vue-cli-plugin-uni/index.js | 3 +- packages/vue-cli-plugin-uni/lib/env.js | 43 ++++++------ .../webpack-uni-pages-loader/lib/index-new.js | 3 + .../webpack-uni-pages-loader/lib/index.js | 6 +- .../lib/platforms/quickapp.js | 3 + 13 files changed, 183 insertions(+), 44 deletions(-) delete mode 100644 packages/uni-quickapp/lib/env.js create mode 100644 packages/uni-quickapp/lib/manifest/base-parser.js create mode 100644 packages/uni-quickapp/lib/manifest/display-parser.js create mode 100644 packages/uni-quickapp/lib/manifest/index.js create mode 100644 packages/uni-quickapp/lib/manifest/pages-parser.js delete mode 100644 packages/uni-quickapp/lib/parse-entry.js diff --git a/packages/uni-quickapp/lib/configure-webpack.js b/packages/uni-quickapp/lib/configure-webpack.js index 31cf5355..d5265553 100644 --- a/packages/uni-quickapp/lib/configure-webpack.js +++ b/packages/uni-quickapp/lib/configure-webpack.js @@ -4,7 +4,7 @@ const webpack = require('webpack') const CopyPlugin = require('copy-webpack-plugin') const HandlerPlugin = require('@hap-toolkit/packager/lib/plugin/handler-plugin') -const ZipPlugin = require('@hap-toolkit/packager/lib/plugin/zip-plugin') +const ZipPlugin = require('@hap-toolkit/packager/lib/plugin/zip-plugin') const NotifyPlugin = require('@hap-toolkit/packager/lib/plugin/notify-plugin') const Css2jsonPlugin = require('@hap-toolkit/dsl-vue/lib/plugin/css2json-plugin') @@ -17,15 +17,23 @@ const env = { NODE_PHASE: process.env.NODE_PHASE } -const dslFilename = 'vue.' + (process.env.NODE_ENV === 'production' ? 'prod' : 'dev') + '.js' -const manifest = global.framework.manifest +const dslFilename = ('vue.' + (process.env.NODE_ENV === 'production' ? 'prod' : 'dev') + '.js') -function genPriorities(e) { +const manifest = process.UNI_MANIFEST.quickapp || {} +const entryPagePath = process.UNI_PAGES.pages[0].path + +const versionCode = parseInt(manifest.versionCode || process.UNI_MANIFEST.versionCode) || 1 + +if (!manifest.package) { + console.error(`maniest.json quickapp 节点缺少 package 配置`) + process.exit(0) +} + +function genPriorities(entryPagePath) { const o = [/^i18n\/.+\.json$/i, 'manifest.json', 'app.js', /^common\//i]; - if (e && e.router && e.router.entry) { - const n = e.router.entry; - o.splice(3, 0, new RegExp(`^${n}/$`), new RegExp(`^${n}/.+`)) - } else colorconsole.error('manifest.json 中未配置入口页面 router.entry'); + if (entryPagePath) { + o.splice(3, 0, new RegExp(`^${entryPagePath}/$`), new RegExp(`^${entryPagePath}/.+`)) + } else console.error('未配置入口页面'); return o } @@ -50,23 +58,22 @@ module.exports = { new CopyPlugin([{ from: path.resolve(__dirname, '../dist/' + dslFilename), to: 'dsl.js' - }, { - from: path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json') }]), new HandlerPlugin({}), new Css2jsonPlugin(), new InstVuePlugin(), + // TODO 目前 manifest,entryPagePath 是启动时读取一次 new ZipPlugin({ name: manifest.package, icon: manifest.icon, - versionCode: manifest.versionCode, - output: path.resolve(process.env.UNI_OUTPUT_DIR), - pathBuild: path.resolve(process.env.UNI_OUTPUT_DIR, 'build'), + versionCode, + output: path.resolve(process.env.UNI_OUTPUT_DIR, '..'), + pathBuild: process.env.UNI_OUTPUT_DIR, pathSignFolder: './sign', sign: process.env.NODE_ENV === 'production' ? 'release' : 'debug', - priorities: genPriorities(manifest), + priorities: genPriorities(entryPagePath), subpackages: undefined, - comment: '{"toolkit":"0.6.13","timeStamp":"2020-03-08T13:22:31.014Z","node":"v12.15.0","platform":"darwin","arch":"x64","extends":null}', + comment: '', cwd: process.env.UNI_INPUT_DIR, disableStreamPack: undefined, disableSubpackages: undefined diff --git a/packages/uni-quickapp/lib/env.js b/packages/uni-quickapp/lib/env.js deleted file mode 100644 index 23a1105c..00000000 --- a/packages/uni-quickapp/lib/env.js +++ /dev/null @@ -1,3 +0,0 @@ - -global.framework = {} -global.framework.manifest = require('/Users/fxy/Documents/demo/my-qa-project/src/manifest.json') diff --git a/packages/uni-quickapp/lib/manifest/base-parser.js b/packages/uni-quickapp/lib/manifest/base-parser.js new file mode 100644 index 00000000..da5dd0d7 --- /dev/null +++ b/packages/uni-quickapp/lib/manifest/base-parser.js @@ -0,0 +1,28 @@ +const ATTRS = { + 'name': 'name', + 'versionName': 'versionName', + 'versionCode': 'versionCode' +} + +function merge(to, from) { + Object.keys(ATTRS).forEach(name => { + if (!to[name]) { + to[name] = from[name] + } + }) +} + +module.exports = function parseBase(manifest, manifestJson) { + merge(manifest, manifestJson) + manifest.versionCode = parseInt(manifest.versionCode) || 1 + + if (!manifest.config) { + manifest.config = {} + } + if (!manifest.config.dsl) { + manifest.config.dsl = {} + } + manifest.config.dsl.name = 'vue' + + return manifest +} diff --git a/packages/uni-quickapp/lib/manifest/display-parser.js b/packages/uni-quickapp/lib/manifest/display-parser.js new file mode 100644 index 00000000..088434a5 --- /dev/null +++ b/packages/uni-quickapp/lib/manifest/display-parser.js @@ -0,0 +1,65 @@ +const PLATFORMS = [ + 'h5', + 'app-plus', + 'mp-weixin', + 'mp-qq', + 'mp-baidu', + 'mp-alipay', + 'mp-toutiao', + 'quickapp' +] + +const DISPLAY = { + 'navigationBarBackgroundColor': 'titleBarBackgroundColor', + 'navigationBarTextStyle': (style, val) => { + if (val === 'black') { + style.titleBarTextColor = '#000000' + style.statusBarTextStyle = 'dark' + } else { + style.titleBarTextColor = '#ffffff' + style.statusBarTextStyle = 'light' + } + }, + 'navigationBarTitleText': 'titleBarText', + 'navigationStyle': (style, val) => { + if (val === 'custom') { + style.titleBar = false + } + } +} + +function parseStyle(style = {}) { + const ret = {} + Object.keys(style).forEach(name => { + if (!PLATFORMS.includes(name)) { + const transform = DISPLAY[name] + if (transform) { + if (typeof transform === 'string') { + ret[transform] = style[name] + } else if (typeof transform === 'function') { + transform(ret, style[name]) + } + } else { + ret[name] = style[name] + } + } + }) + if (style.quickapp) { + Object.assign(ret, style.quickapp) + } + return ret +} + +module.exports = function parseDisplay(manifest, pages, globalStyle = {}) { + const display = { + pages: {} + } + // globalStyle + Object.assign(display, parseStyle(globalStyle)) + + pages.forEach(page => { + const key = page.path.substr(0, page.path.lastIndexOf('/')) + display.pages[key] = parseStyle(page.style) + }) + manifest.display = display +} diff --git a/packages/uni-quickapp/lib/manifest/index.js b/packages/uni-quickapp/lib/manifest/index.js new file mode 100644 index 00000000..4eb25245 --- /dev/null +++ b/packages/uni-quickapp/lib/manifest/index.js @@ -0,0 +1,16 @@ +const parseBase = require('./base-parser') +const parsePages = require('./pages-parser') +const parseDisplay = require('./display-parser') + +module.exports = function(pagesJson, manifestJson, loader) { + const manifest = manifestJson.quickapp || {} + parseBase(manifest, manifestJson) + parsePages(manifest, pagesJson.pages) + parseDisplay(manifest, pagesJson.pages, pagesJson.globalStyle) + + global.framework.manifest = manifest + + loader.emitFile('manifest.json', JSON.stringify(manifest)) + + return '' +} diff --git a/packages/uni-quickapp/lib/manifest/pages-parser.js b/packages/uni-quickapp/lib/manifest/pages-parser.js new file mode 100644 index 00000000..15dac61b --- /dev/null +++ b/packages/uni-quickapp/lib/manifest/pages-parser.js @@ -0,0 +1,15 @@ +module.exports = function parsePages(manifest, pages) { + const entryPagePath = pages[0].path + const router = { + entry: entryPagePath.substr(0, entryPagePath.lastIndexOf('/')), + pages: {} + } + pages.forEach(page => { + const lastIndex = page.path.lastIndexOf('/') + const key = page.path.substr(0, lastIndex) + router.pages[key] = { + component: page.path.substr(lastIndex + 1) + } + }) + manifest.router = router +} diff --git a/packages/uni-quickapp/lib/parse-entry.js b/packages/uni-quickapp/lib/parse-entry.js deleted file mode 100644 index 42f8dec1..00000000 --- a/packages/uni-quickapp/lib/parse-entry.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = function parseEntry() { - -} diff --git a/packages/uni-quickapp/lib/vue.config.js b/packages/uni-quickapp/lib/vue.config.js index b86cb305..d4bab4ef 100644 --- a/packages/uni-quickapp/lib/vue.config.js +++ b/packages/uni-quickapp/lib/vue.config.js @@ -1,4 +1,4 @@ -require('./env') +global.framework = {} const chainWebpack = require('./chain-webpack') const configureWebpack = require('./configure-webpack') diff --git a/packages/vue-cli-plugin-uni/index.js b/packages/vue-cli-plugin-uni/index.js index 6266ee21..f3cdd4b6 100644 --- a/packages/vue-cli-plugin-uni/index.js +++ b/packages/vue-cli-plugin-uni/index.js @@ -20,9 +20,10 @@ module.exports = (api, options) => { initBuildCommand(api, options) if (process.env.UNI_PLATFORM === 'quickapp') { + process.env.UNI_OUTPUT_DIR = path.resolve(process.env.UNI_OUTPUT_DIR, 'build') Object.assign(options, { assetsDir, - outputDir: path.resolve(process.env.UNI_OUTPUT_DIR, 'build') + outputDir: process.env.UNI_OUTPUT_DIR }) require('./lib/options')(options) const platformOptions = { diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 2fd28fe7..ddfb9809 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -18,27 +18,27 @@ if (process.env.UNI_CLOUD_SPACES) { if (spaces.length === 1) { const space = spaces[0] console.log(`本项目的uniCloud使用的默认服务空间spaceId为:${space.id}`) - } - process.env.UNI_CLOUD_PROVIDER = JSON.stringify(spaces.map(space => { - if (space.clientSecret) { - return { - provider: 'aliyun', - spaceName: space.name, - spaceId: space.id, - clientSecret: space.clientSecret, - endpoint: space.apiEndpoint - } - } else { - return { - provider: 'tencent', - spaceName: space.name, - spaceId: space.id - } - } + } + process.env.UNI_CLOUD_PROVIDER = JSON.stringify(spaces.map(space => { + if (space.clientSecret) { + return { + provider: 'aliyun', + spaceName: space.name, + spaceId: space.id, + clientSecret: space.clientSecret, + endpoint: space.apiEndpoint + } + } else { + return { + provider: 'tencent', + spaceName: space.name, + spaceId: space.id + } + } })) } } catch (e) {} -} +} if ( process.UNI_CLOUD && @@ -88,7 +88,7 @@ if (process.env.NODE_ENV === 'production') { // 发行模式,不启用 cache delete process.env.UNI_USING_CACHE } -const { +const { normalizePath, isSupportSubPackages, runByHBuilderX, @@ -120,6 +120,9 @@ if (Array.isArray(pagesJsonObj.subPackages)) { const manifestJsonObj = getManifestJson() const platformOptions = manifestJsonObj[process.env.UNI_PLATFORM] || {} +process.UNI_PAGES = pagesJsonObj +process.UNI_MANIFEST = manifestJsonObj + if (manifestJsonObj.debug) { process.env.VUE_APP_DEBUG = true } @@ -212,7 +215,7 @@ if (process.env.UNI_PLATFORM === 'app-plus') { normalizePath(path.resolve(process.env.UNI_INPUT_DIR, filepath)) ) } - } + } } else { // 其他平台,待确认配置方案 if ( manifestJsonObj['app-plus'] && diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index 64c9716b..b123e123 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -69,6 +69,9 @@ module.exports = function (content) { if (process.env.UNI_PLATFORM === 'h5') { return require('./platforms/h5')(pagesJson, manifestJson) } + if (process.env.UNI_PLATFORM === 'quickapp') { + return require('./platforms/quickapp')(pagesJson, manifestJson, this) + } if (!process.env.UNI_USING_V3) { parsePages(pagesJson, function (page) { diff --git a/packages/webpack-uni-pages-loader/lib/index.js b/packages/webpack-uni-pages-loader/lib/index.js index 83d27c4c..33a71b3b 100644 --- a/packages/webpack-uni-pages-loader/lib/index.js +++ b/packages/webpack-uni-pages-loader/lib/index.js @@ -41,7 +41,11 @@ module.exports = function (content) { } } - if (process.env.UNI_USING_COMPONENTS || process.env.UNI_PLATFORM === 'h5') { + if ( + process.env.UNI_USING_COMPONENTS || + process.env.UNI_PLATFORM === 'h5' || + process.env.UNI_PLATFORM === 'quickapp' + ) { return require('./index-new').call(this, content) } diff --git a/packages/webpack-uni-pages-loader/lib/platforms/quickapp.js b/packages/webpack-uni-pages-loader/lib/platforms/quickapp.js index e69de29b..e83a9c0c 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/quickapp.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/quickapp.js @@ -0,0 +1,3 @@ +module.exports = function (pagesJson, manifestJson, loader) { + return require('@dcloudio/uni-quickapp/lib/manifest')(pagesJson, manifestJson, loader) +} -- GitLab