Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(taro-mini-runner): 提供小程序端 dark mode 配置文件打包支持 #7295

Merged
merged 14 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default class TaroMiniPlugin {
prerenderPages: Set<string>
dependencies = new Map<string, TaroSingleEntryDependency>()
loadChunksPlugin: TaroLoadChunksPlugin
themeLocation: string

constructor (options = {}) {
this.options = Object.assign({
Expand Down Expand Up @@ -313,6 +314,7 @@ export default class TaroMiniPlugin {
this.appConfig = this.getAppConfig()
this.getPages()
this.getPagesConfig()
this.getDarkMode()
this.getConfigFiles(compiler)
this.addEntries()
}
Expand Down Expand Up @@ -546,6 +548,17 @@ export default class TaroMiniPlugin {
}
}

/**
* 收集 dark mode 配置中的文件
*/
getDarkMode () {
const themeLocation = this.appConfig.themeLocation
const darkMode = this.appConfig.darkmode
if (darkMode && themeLocation && typeof themeLocation === 'string') {
this.themeLocation = themeLocation
}
}

/**
* 搜集 tabbar icon 图标路径
* 收集自定义 tabbar 组件
Expand Down Expand Up @@ -646,6 +659,9 @@ export default class TaroMiniPlugin {
})
this.generateTabBarFiles(compilation)
this.injectCommonStyles(compilation)
if (this.themeLocation) {
this.generateDarkModeFile(compilation)
}
if (typeof modifyBuildAssets === 'function') {
await modifyBuildAssets(compilation.assets)
}
Expand Down Expand Up @@ -731,6 +747,22 @@ export default class TaroMiniPlugin {
return filePath + targetExtname
}

/**
* 输出 themeLocation 文件
* @param compilation
*/
generateDarkModeFile (compilation: webpack.compilation.Compilation) {
const themeLocationPath = path.resolve(this.options.sourceDir, this.themeLocation)
if (fs.existsSync(themeLocationPath)) {
const themeLocationStat = fs.statSync(themeLocationPath)
const themeLocationSource = fs.readFileSync(themeLocationPath)
compilation.assets[this.themeLocation] = {
size: () => themeLocationStat.size,
source: () => themeLocationSource
}
}
}

/**
* 输出 tabbar icons 文件
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/taro/types/taro.config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ declare namespace Taro {
* @since 2.8.0
*/
style?: 'v2'
/**
* 配置 darkmode 为 true,即表示当前小程序已适配 DarkMode
* @since 2.11.0
*/
darkmode?: boolean
/**
* 指定 darkmode 变量配置文件 theme.json 路径
* @since 2.11.0
*/
themeLocation?: string
}

interface Config extends PageConfig, AppConfig {
Expand Down