diff --git a/packages/uni-app-plus/dist/index.js b/packages/uni-app-plus/dist/index.js index d42f41bbbe2c03407f1920426b143f2fb4423a6e..c868527bc85a9a81c7477cac953e21c257ac220b 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 0000000000000000000000000000000000000000..809c5555f6bee63a11bdc1243439dd797fc63bc5 --- /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 9894a8e41e0cfb9e02608c3a9fb524922bb5507b..7a497e8d2e9dfd9fe1ab96577a775ee551601385 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 7b53c18b7d8d6848c3956a34c25b4a092a997079..d534b60cf873483b67c461fe681da9c63f81a4e0 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 be9a43ac9da8b4cf693b09e60e00484290d2d69a..1af6867cb4f745337f88baa527d0f9f5be6a8db9 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 817ba29cd75fb92aca2a572358dd08c87c5625ad..c62d77aa74f614d7c771eae7cd1abedba1369569 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 630ee9c45681053ebaa59f789853422ce2e53604..b40686e6557309ddd4b2ed8b46733d1fb2de1264 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 c806a821ba4102b41b30f89301b1fe597bfc37e3..6a67b8a8279f804a64857665019dd7f425dbbd0e 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 c190f161d3edbe0ee1fb458b2bbced592611a9c9..383f4cf29ea16b7cf41233f85ab4e11da902d435 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 b6bf7cad5734d4b380e2e4895b8c3948fba916d3..6eb11a5321caaef333111687f110a060eec5cb30 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 b061af11b3a0d3dbf43064908a8a7816155450b8..72b63c3f6ac997a2d08f675aed8bf7678b8fe4fc 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 1eb16d01242a56e41381c8895811e51b629f52e8..0ee26bc9fdf3c8c759179cbe18656063fe49b7c3 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 8598d2c4b30d969d58982b7dbcb1b0036439b147..47cd573f9a58ac2a187a85b202cc1db9516bc128 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 575959515ce1b3dfdf2bae017b393f4b5e10a280..d2512177102d57ab9653620be3fdf23395d3d2af 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 f0fea7f2879fa9b3b5964b77264f96c250572c7f..faaecadfb84952f5f28f723fd254f2f360d0808d 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, {