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/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js index b82875fde8ed353a5b6dab6a06360140823e4d5a..11add830b291bbb680a7524abcba5b0f9e932f27 100644 --- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js @@ -251,10 +251,10 @@ describe('mp:compiler-extra', () => { ) }) - it('generate events inside v-for', () => { - assertCodegen( - '', - '' + it('generate events inside v-for', () => { + assertCodegen( + '', + '' ) // TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异 assertCodegen( @@ -310,23 +310,23 @@ describe('mp:compiler-extra', () => { assertCodegen( '', '' - ) - assertCodegen( - ` - - - - `, - '' - ) - assertCodegen( - '', - '' - ) - assertCodegen( - '', - '' - ) + ) + assertCodegen( + ` + + + + `, + '' + ) + assertCodegen( + '', + '' + ) + assertCodegen( + '', + '' + ) }) it('generate class binding', () => { @@ -586,6 +586,17 @@ describe('mp:compiler-extra', () => { '{{item.g0}}', 'with(this){var l0=__map(list,function(item,i){var g0=item.split("").join(" ");return{$orig:__get_orig(item),g0:g0}});$mp.data=Object.assign({},{$root:{l0:l0}})}' ) + assertCodegen( + ` + + + {{formatIt(item2.id)}} + + + `, + '{{\'\'+item2.m0+\'\'}}', + 'with(this){var l1=__map(tabList,function(item,index){var l0=__map(list[item.key],function(item2,index2){var m0=formatIt(item2.id);return{$orig:__get_orig(item2),m0:m0}});return{$orig:__get_orig(item),l0:l0}});$mp.data=Object.assign({},{$root:{l1:l1}})}' + ) }) it('generate TemplateLiteral ', () => { @@ -600,11 +611,11 @@ describe('mp:compiler-extra', () => { '' ) }) - it('generate event ', () => { - assertCodegen( - '', - '' - ) + it('generate event ', () => { + assertCodegen( + '', + '' + ) assertCodegen( '{{item.title}}', @@ -654,15 +665,15 @@ describe('mp:compiler-extra', () => { '{{ item }}', '{{item}}' ) - }) - it('generate bool attr', () => { - assertCodegen( - '