From 70aaee2bee4c29134089fbbcb244a3b5c2aa50e4 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 24 Jun 2020 18:35:04 +0800 Subject: [PATCH] feat(cli): support theme.json #1828 --- packages/uni-app-plus/dist/index.js | 3 +- packages/uni-cli-shared/lib/theme.js | 59 +++++++++++++++++++ packages/uni-mp-alipay/dist/index.js | 3 +- packages/uni-mp-baidu/dist/index.js | 3 +- packages/uni-mp-qq/dist/index.js | 3 +- packages/uni-mp-toutiao/dist/index.js | 3 +- packages/uni-mp-weixin/dist/index.js | 3 +- packages/uni-mp-weixin/lib/uni.config.js | 6 +- packages/uni-quickapp-webview/dist/index.js | 3 +- .../packages/mp-vue/dist/mp.runtime.esm.js | 2 + .../webpack-uni-pages-loader/lib/index-new.js | 14 ++++- .../lib/platforms/mp.js | 12 +++- packages/webpack-uni-pages-loader/lib/util.js | 8 ++- src/core/service/plugins/lifecycle.js | 3 +- .../runtime/wrapper/app-base-parser.js | 3 +- 15 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 packages/uni-cli-shared/lib/theme.js diff --git a/packages/uni-app-plus/dist/index.js b/packages/uni-app-plus/dist/index.js index d42f41bbb..c868527bc 100644 --- a/packages/uni-app-plus/dist/index.js +++ b/packages/uni-app-plus/dist/index.js @@ -1156,7 +1156,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/uni-cli-shared/lib/theme.js b/packages/uni-cli-shared/lib/theme.js new file mode 100644 index 000000000..809c5555f --- /dev/null +++ b/packages/uni-cli-shared/lib/theme.js @@ -0,0 +1,59 @@ +const fs = require('fs') +const path = require('path') + +const { + getJson +} = require('./json') + +function parseThemeByJsonStr (jsonStr, keys, theme) { + if (jsonStr.indexOf('@') === -1) { + return jsonStr + } + keys.forEach(key => { + jsonStr = jsonStr.replace(new RegExp('@' + key, 'g'), theme[key]) + }) + return jsonStr +} + +const themeJsonPath = path.join(process.env.UNI_INPUT_DIR, 'theme.json') + +function hasTheme () { + return fs.existsSync(themeJsonPath) +} + +function darkmode () { + return !!(global.uniPlugin.options || {}).darkmode +} + +module.exports = { + darkmode, + hasTheme, + initTheme () { + if (!hasTheme()) { + return + } + if (darkmode()) { + return + } + try { + const theme = getJson('theme.json', true) + global.uniPlugin.defaultTheme = theme.light + } catch (e) { + console.error(e) + } + }, + parseTheme (json) { + const theme = global.uniPlugin.defaultTheme + if (!theme) { + return json + } + const keys = Object.keys(theme) + if (!keys.length) { + return json + } + if (typeof json === 'string') { + return parseThemeByJsonStr(json, keys, theme) + } + return JSON.parse(parseThemeByJsonStr(JSON.stringify(json), keys, theme)) + } +} diff --git a/packages/uni-mp-alipay/dist/index.js b/packages/uni-mp-alipay/dist/index.js index 9894a8e41..7a497e8d2 100644 --- a/packages/uni-mp-alipay/dist/index.js +++ b/packages/uni-mp-alipay/dist/index.js @@ -1633,7 +1633,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/uni-mp-baidu/dist/index.js b/packages/uni-mp-baidu/dist/index.js index 7b53c18b7..d534b60cf 100644 --- a/packages/uni-mp-baidu/dist/index.js +++ b/packages/uni-mp-baidu/dist/index.js @@ -1298,7 +1298,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/uni-mp-qq/dist/index.js b/packages/uni-mp-qq/dist/index.js index be9a43ac9..1af6867cb 100644 --- a/packages/uni-mp-qq/dist/index.js +++ b/packages/uni-mp-qq/dist/index.js @@ -1214,7 +1214,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/uni-mp-toutiao/dist/index.js b/packages/uni-mp-toutiao/dist/index.js index 817ba29cd..c62d77aa7 100644 --- a/packages/uni-mp-toutiao/dist/index.js +++ b/packages/uni-mp-toutiao/dist/index.js @@ -1306,7 +1306,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index 630ee9c45..b40686e65 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -1171,7 +1171,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/uni-mp-weixin/lib/uni.config.js b/packages/uni-mp-weixin/lib/uni.config.js index c806a821b..6a67b8a82 100644 --- a/packages/uni-mp-weixin/lib/uni.config.js +++ b/packages/uni-mp-weixin/lib/uni.config.js @@ -17,10 +17,12 @@ module.exports = { }, filterTag: 'wxs', project: 'project.config.json', - subPackages: true + subPackages: true, + darkmode: true }, copyWebpackOptions (platformOptions, vueOptions) { - const copyOptions = [ + const copyOptions = [ + 'theme.json', 'sitemap.json', 'ext.json', 'custom-tab-bar' diff --git a/packages/uni-quickapp-webview/dist/index.js b/packages/uni-quickapp-webview/dist/index.js index c190f161d..383f4cf29 100644 --- a/packages/uni-quickapp-webview/dist/index.js +++ b/packages/uni-quickapp-webview/dist/index.js @@ -1156,7 +1156,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ]; function parseBaseApp (vm, { diff --git a/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js b/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js index b6bf7cad5..6eb11a532 100644 --- a/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js +++ b/packages/vue-cli-plugin-uni/packages/mp-vue/dist/mp.runtime.esm.js @@ -5952,6 +5952,8 @@ var LIFECYCLE_HOOKS$1 = [ 'onShow', 'onHide', 'onUniNViewMessage', + 'onPageNotFound', + 'onThemeChange', 'onError', //Page 'onLoad', diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index b061af11b..72b63c3f6 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -16,6 +16,11 @@ const { updateProjectJson } = require('@dcloudio/uni-cli-shared/lib/cache') +const { + initTheme, + parseTheme +} = require('@dcloudio/uni-cli-shared/lib/theme') + const { // pagesJsonJsFileName, initAutoImportComponents @@ -35,6 +40,8 @@ function renameUsingComponents (jsonObj) { module.exports = function (content, map) { this.cacheable && this.cacheable() + initTheme() + let isAppView = false if (this.resourceQuery) { const params = loaderUtils.parseQuery(this.resourceQuery) @@ -48,13 +55,18 @@ module.exports = function (content, map) { // this.addDependency(pagesJsonJsPath) this.addDependency(manifestJsonPath) - const pagesJson = parsePagesJson(content, { + let pagesJson = parsePagesJson(content, { addDependency: (file) => { (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(normalizePath(file)) this.addDependency(file) } }) + if (global.uniPlugin.defaultTheme) { + pagesJson = parseTheme(pagesJson) + this.addDependency(path.resolve(process.env.UNI_INPUT_DIR, 'theme.json')) + } + // 组件自动导入配置 process.UNI_AUTO_SCAN_COMPONENTS = !(pagesJson.easycom && pagesJson.easycom.autoscan === false) initAutoImportComponents(pagesJson.easycom) diff --git a/packages/webpack-uni-pages-loader/lib/platforms/mp.js b/packages/webpack-uni-pages-loader/lib/platforms/mp.js index 1eb16d012..0ee26bc9f 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/mp.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/mp.js @@ -12,6 +12,11 @@ const { updateAppJsonUsingComponents } = require('@dcloudio/uni-cli-shared/lib/cache') +const { + darkmode, + hasTheme +} = require('@dcloudio/uni-cli-shared/lib/theme') + const { hasOwn, parseStyle @@ -208,6 +213,11 @@ module.exports = function (pagesJson, manifestJson, project = {}) { updateAppJsonUsingComponents(app.usingComponents) } + if (darkmode() && hasTheme()) { + app.darkmode = true + app.themeLocation = 'theme.json' + } + const projectName = getPlatformProject() const projectPath = projectName && path.resolve(process.env.VUE_CLI_CONTEXT || process.cwd(), projectName) @@ -309,4 +319,4 @@ module.exports = function (pagesJson, manifestJson, project = {}) { } } } -} +} diff --git a/packages/webpack-uni-pages-loader/lib/util.js b/packages/webpack-uni-pages-loader/lib/util.js index 8598d2c4b..47cd573f9 100644 --- a/packages/webpack-uni-pages-loader/lib/util.js +++ b/packages/webpack-uni-pages-loader/lib/util.js @@ -74,7 +74,13 @@ function parseStyle (style = {}, root = '') { return Object.assign(windowOptions, platformStyle) } - if (style.navigationBarTextStyle && style.navigationBarTextStyle !== 'black') { + if ( + style.navigationBarTextStyle && + ( + style.navigationBarTextStyle !== 'black' && + style.navigationBarTextStyle.indexOf('@') !== 0 + ) + ) { style.navigationBarTextStyle = 'white' } diff --git a/src/core/service/plugins/lifecycle.js b/src/core/service/plugins/lifecycle.js index 575959515..d25121771 100644 --- a/src/core/service/plugins/lifecycle.js +++ b/src/core/service/plugins/lifecycle.js @@ -6,7 +6,8 @@ const LIFECYCLE_HOOKS = [ 'onShow', 'onHide', 'onUniNViewMessage', - 'onPageNotFound', + 'onPageNotFound', + 'onThemeChange', 'onError', // Page 'onLoad', diff --git a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js index f0fea7f28..faaecadfb 100644 --- a/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js +++ b/src/platforms/mp-weixin/runtime/wrapper/app-base-parser.js @@ -9,7 +9,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onThemeChange' ] export default function parseBaseApp (vm, { -- GitLab