提交 70aaee2b 编写于 作者: fxy060608's avatar fxy060608

feat(cli): support theme.json #1828

上级 94893771
......@@ -1156,7 +1156,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
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))
}
}
......@@ -1633,7 +1633,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
......@@ -1298,7 +1298,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
......@@ -1214,7 +1214,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
......@@ -1306,7 +1306,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
......@@ -1171,7 +1171,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
......@@ -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'
......
......@@ -1156,7 +1156,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
];
function parseBaseApp (vm, {
......
......@@ -5952,6 +5952,8 @@ var LIFECYCLE_HOOKS$1 = [
'onShow',
'onHide',
'onUniNViewMessage',
'onPageNotFound',
'onThemeChange',
'onError',
//Page
'onLoad',
......
......@@ -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)
......
......@@ -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 = {}) {
}
}
}
}
}
......@@ -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'
}
......
......@@ -6,7 +6,8 @@ const LIFECYCLE_HOOKS = [
'onShow',
'onHide',
'onUniNViewMessage',
'onPageNotFound',
'onPageNotFound',
'onThemeChange',
'onError',
// Page
'onLoad',
......
......@@ -9,7 +9,8 @@ const hooks = [
'onShow',
'onHide',
'onError',
'onPageNotFound'
'onPageNotFound',
'onThemeChange'
]
export default function parseBaseApp (vm, {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册