From 33d31247becd64ea4152d15b31f5dd3258f87d5b Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Fri, 11 Nov 2022 16:06:10 +0800 Subject: [PATCH] chore: mp theme.json copy --- packages/uni-cli-shared/lib/theme.js | 23 ++++++++----------- .../webpack-uni-pages-loader/lib/index-new.js | 10 +++++--- .../lib/platforms/mp.js | 8 +++---- src/platforms/h5/components/app/observable.js | 2 +- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/uni-cli-shared/lib/theme.js b/packages/uni-cli-shared/lib/theme.js index 6b790d4a4..83b60fdf0 100644 --- a/packages/uni-cli-shared/lib/theme.js +++ b/packages/uni-cli-shared/lib/theme.js @@ -1,13 +1,8 @@ const fs = require('fs') const path = require('path') -const { parseJson } = require('./json') - -const { - getJson -} = require('./json') +const { parseJson, getJson } = require('./json') let themeConfig = {} -let manifestJsonDarkmode = false function parseThemeByJsonStr (jsonStr, keys, theme) { if (jsonStr.indexOf('@') === -1) { @@ -27,7 +22,7 @@ function hasTheme (themeLocation = 'theme.json') { } function darkmode () { - return manifestJsonDarkmode && !!(global.uniPlugin.options || {}).darkmode + return !!(global.uniPlugin.options || {}).darkmode } module.exports = { @@ -37,7 +32,6 @@ module.exports = { initTheme (manifestJson = {}) { const platform = process.env.UNI_PLATFORM const themeLocation = (manifestJson[platform] || {}).themeLocation || 'theme.json' - manifestJsonDarkmode = (manifestJson[platform] || {}).darkmode || false if (!hasTheme(themeLocation)) { return } @@ -66,11 +60,14 @@ module.exports = { return JSON.parse(parseThemeByJsonStr(JSON.stringify(json), keys, theme)) }, copyMiniProgramThemeJson (platformOptions, vueOptions) { - platformOptions.themeLocation || (platformOptions.themeLocation = 'theme.json') - return { - from: path.resolve(process.env.UNI_INPUT_DIR, platformOptions.themeLocation), - to: path.resolve(process.env.UNI_OUTPUT_DIR, platformOptions.themeLocation), - transform: content => JSON.stringify(parseJson(content.toString(), true)) + const themeLocation = platformOptions.themeLocation || 'theme.json' + if (hasTheme(themeLocation)) { + platformOptions.themeLocation = themeLocation + return { + from: path.resolve(process.env.UNI_INPUT_DIR, platformOptions.themeLocation), + to: path.resolve(process.env.UNI_OUTPUT_DIR, platformOptions.themeLocation), + transform: content => JSON.stringify(parseJson(content.toString(), true)) + } } } } diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index 737cb5c26..817cb459d 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -18,7 +18,8 @@ const { const { initTheme, - parseTheme + parseTheme, + darkmode } = require('@dcloudio/uni-cli-shared/lib/theme') const { @@ -123,9 +124,12 @@ module.exports = function (content, map) { process.UNI_TRANSFORM_PX = true } - if (process.env.VUE_APP_DARK_MODE !== 'true') { + if ( + (process.env.UNI_PLATFORM.indexOf('mp') !== -1 && !darkmode()) || + process.env.VUE_APP_DARK_MODE !== 'true' + ) { const { pages, globalStyle, tabBar } = parseTheme(pagesJson) - Object.assign(pagesJson, { pages, globalStyle, tabBar }) + Object.assign(pagesJson, JSON.parse(JSON.stringify({ pages, globalStyle, tabBar }))) } if (process.env.UNI_PLATFORM === 'h5') { diff --git a/packages/webpack-uni-pages-loader/lib/platforms/mp.js b/packages/webpack-uni-pages-loader/lib/platforms/mp.js index f948fe017..27b1fd7a5 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/mp.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/mp.js @@ -258,9 +258,9 @@ module.exports = function (pagesJson, manifestJson, project = {}) { updateAppJsonUsingComponents(app.usingComponents) } - if (darkmode() && hasTheme()) { - app.darkmode = true - app.themeLocation = 'theme.json' + const themeLocation = (manifestJson[process.env.UNI_PLATFORM] || {}).themeLocation + if (darkmode() && hasTheme(themeLocation)) { + app.themeLocation = themeLocation || 'theme.json' } const projectName = getPlatformProject() @@ -376,4 +376,4 @@ module.exports = function (pagesJson, manifestJson, project = {}) { } } } -} +} diff --git a/src/platforms/h5/components/app/observable.js b/src/platforms/h5/components/app/observable.js index cd576ae36..f67a87111 100644 --- a/src/platforms/h5/components/app/observable.js +++ b/src/platforms/h5/components/app/observable.js @@ -13,7 +13,7 @@ onThemeChange(() => { __uniConfig.tabBar.color = tabBarStyle.color __uniConfig.tabBar.selectedColor = tabBarStyle.selectedColor __uniConfig.tabBar.blurEffect = tabBarStyle.blurEffect - if (tabBarStyle.list.length && __uniConfig.tabBar.list.length) { + if (tabBarStyle.list && tabBarStyle.list.length && __uniConfig.tabBar.list.length) { tabBarStyle.list.forEach((item, index) => { __uniConfig.tabBar.list[index].iconPath = item.iconPath __uniConfig.tabBar.list[index].selectedIconPath = item.selectedIconPath -- GitLab