diff --git a/packages/uni-cli-shared/lib/theme.js b/packages/uni-cli-shared/lib/theme.js index 13bc0be31d0333ef56e4254536e287244809d394..6b790d4a4b7ff5a6ea3a2767d6b413b122b70418 100644 --- a/packages/uni-cli-shared/lib/theme.js +++ b/packages/uni-cli-shared/lib/theme.js @@ -6,12 +6,17 @@ const { getJson } = require('./json') +let themeConfig = {} +let manifestJsonDarkmode = false + function parseThemeByJsonStr (jsonStr, keys, theme) { if (jsonStr.indexOf('@') === -1) { return jsonStr } keys.forEach(key => { - jsonStr = jsonStr.replace(new RegExp('@' + key, 'g'), theme[key]) + jsonStr = jsonStr.replace(new RegExp('@' + key, 'g'), $1 => { + return theme[key] || $1 + }) }) return jsonStr } @@ -22,11 +27,9 @@ function hasTheme (themeLocation = 'theme.json') { } function darkmode () { - return !!(global.uniPlugin.options || {}).darkmode + return manifestJsonDarkmode && !!(global.uniPlugin.options || {}).darkmode } -let themeConfig = {} - module.exports = { getTheme: () => themeConfig, darkmode, @@ -34,6 +37,7 @@ 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 } diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index 5ed4d5c4186520dbc8afb6d37bf20c7dd1894376..737cb5c264b99aac052efb061eb83f4464b5d8ce 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -76,7 +76,7 @@ module.exports = function (content, map) { } this.addDependency(manifestJsonPath) - const originalPagesJson = parsePagesJson(content, { + const pagesJson = parsePagesJson(content, { addDependency: file => { (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add( normalizePath(file) @@ -85,7 +85,7 @@ module.exports = function (content, map) { } }) - if (!originalPagesJson.pages || originalPagesJson.pages.length === 0) { + if (!pagesJson.pages || pagesJson.pages.length === 0) { console.error(uniI18n.__('pagesLoader.pagesNodeCannotNull')) process.exit(0) } @@ -94,14 +94,13 @@ module.exports = function (content, map) { const queryParam = loaderUtils.parseQuery(this.resourceQuery) if (queryParam) { if (queryParam.type === 'origin-pages-json') { - return `export default ${JSON.stringify(originalPagesJson)}` + return `export default ${JSON.stringify(pagesJson)}` } } } const platformManifestJson = manifestJson[process.env.UNI_PLATFORM] || {} - const pagesJson = parseTheme(originalPagesJson) if (global.uniPlugin.defaultTheme) { this.addDependency( path.resolve( @@ -124,11 +123,16 @@ module.exports = function (content, map) { process.UNI_TRANSFORM_PX = true } + if (process.env.VUE_APP_DARK_MODE !== 'true') { + const { pages, globalStyle, tabBar } = parseTheme(pagesJson) + Object.assign(pagesJson, { pages, globalStyle, tabBar }) + } + if (process.env.UNI_PLATFORM === 'h5') { return this.callback( null, require('./platforms/h5')( - process.env.VUE_APP_DARK_MODE === 'true' ? originalPagesJson : pagesJson, + pagesJson, manifestJson, this ), @@ -178,9 +182,7 @@ module.exports = function (content, map) { } const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)( - process.env.UNI_PLATFORM === 'app-plus' && process.env.VUE_APP_DARK_MODE === 'true' - ? originalPagesJson - : pagesJson, + pagesJson, manifestJson, isAppView ) diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js index 471f39c948642f5f03913c8d16bea2f91bc6fea7..ed98999ca874a96da667345f9f91410d4f352ee0 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js @@ -112,7 +112,7 @@ function updateFileFlag (appJson) { function _initTheme (appJson, userManifestJson) { const manifestJson = userManifestJson[process.env.UNI_PLATFORM] || {} appJson.darkmode = manifestJson.darkmode || false - const themeLocation = manifestJson.themeLocation || '' + const themeLocation = manifestJson.themeLocation || 'theme.json' if (themeLocation && hasTheme(themeLocation)) { appJson.themeConfig = getTheme() } diff --git a/src/shared/theme.js b/src/shared/theme.js index a99a5621651b12e76e139e28181fef3d890a5322..5cbd336645347049c7fd8bac75387a2cf9375ff7 100644 --- a/src/shared/theme.js +++ b/src/shared/theme.js @@ -12,7 +12,7 @@ export function normalizeTabBarStyles (borderStyle) { return borderStyle } -export function normallizeStyles (pageStyle, themeConfig, mode = 'light') { +export function normallizeStyles (pageStyle, themeConfig = {}, mode = 'light') { const modeStyle = themeConfig[mode] const styles = {} if (!modeStyle) { @@ -29,7 +29,7 @@ export function normallizeStyles (pageStyle, themeConfig, mode = 'light') { : item) } else if (isStr(styleItem) && styleItem.startsWith('@')) { const _key = styleItem.replace('@', '') - let _styleItem = modeStyle[_key] + let _styleItem = modeStyle[_key] || styleItem switch (key) { case 'borderStyle': _styleItem = normalizeTabBarStyles(_styleItem)