Skip to content

Commit

Permalink
feat(mini-runner): 支持编译 tabBar 上引用的资源编译
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent 975efa2 commit 43a129a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
42 changes: 33 additions & 9 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import * as t from 'babel-types'
import traverse from 'babel-traverse'
import { Config as IConfig } from '@tarojs/taro'

import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG } from '../utils/constants'
import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP } from '../utils/constants'
import { IComponentObj } from '../utils/types'
import { traverseObjectNode, resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync } from '../utils'
import { traverseObjectNode, resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync, isEmptyObject } from '../utils'
import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency'

import TaroLoadChunksPlugin from './TaroLoadChunksPlugin'
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class MiniPlugin {
}
}

apply (compiler: webpack.Compiler) {
apply (compiler) {
this.context = compiler.context
this.appEntry = this.getAppEntry(compiler)
compiler.hooks.run.tapAsync(
Expand Down Expand Up @@ -251,7 +251,7 @@ export default class MiniPlugin {
getNpmComponentRealPath (code: string, component: IComponentObj, adapter: BUILD_TYPES): string | null {
let componentRealPath: string | null = null
let importExportName
const { isTaroComponent, transformResult } = isFileToBeTaroComponent(code, component.path, adapter)
const { isTaroComponent, transformResult } = isFileToBeTaroComponent(code, component.path as string, adapter)
if (isTaroComponent) {
return component.path
}
Expand All @@ -265,7 +265,7 @@ export default class MiniPlugin {
specifiers.forEach(specifier => {
const exported = specifier.exported
if (kebabCase(exported.name) === component.name) {
componentRealPath = resolveScriptPath(path.resolve(path.dirname(component.path), source.value))
componentRealPath = resolveScriptPath(path.resolve(path.dirname(component.path as string), source.value))
}
})
} else {
Expand All @@ -290,7 +290,7 @@ export default class MiniPlugin {
if (astPath.get('callee').isIdentifier({ name: 'require' })) {
const arg = astPath.get('arguments')[0]
if (t.isStringLiteral(arg.node)) {
componentRealPath = resolveScriptPath(path.resolve(path.dirname(component.path), arg.node.value))
componentRealPath = resolveScriptPath(path.resolve(path.dirname(component.path as string), arg.node.value))
}
}
},
Expand All @@ -306,7 +306,7 @@ export default class MiniPlugin {
specifiers.forEach(specifier => {
const local = specifier.local
if (local.name === importExportName) {
componentRealPath = resolveScriptPath(path.resolve(path.dirname(component.path), source.value))
componentRealPath = resolveScriptPath(path.resolve(path.dirname(component.path as string), source.value))
}
})
}
Expand Down Expand Up @@ -337,6 +337,29 @@ export default class MiniPlugin {
})
}

generateTabBarFiles (compiler, appConfig) {
const tabBar = appConfig.tabBar
const { buildAdapter } = this.options
if (tabBar && typeof tabBar === 'object' && !isEmptyObject(tabBar)) {
const {
list: listConfig,
iconPath: pathConfig,
selectedIconPath: selectedPathConfig
} = CONFIG_MAP[buildAdapter]

const list = tabBar[listConfig] || []
let tabBarIcons: string[] = []
list.forEach(item => {
item[pathConfig] && tabBarIcons.push(item[pathConfig])
item[selectedPathConfig] && tabBarIcons.push(item[selectedPathConfig])
})
tabBarIcons.map(item => {
const itemPath = path.resolve(this.sourceDir, item)
this.addEntry(compiler, itemPath, item, PARSE_AST_TYPE.STATIC)
})
}
}

getSubPackages (appConfig) {
const subPackages = appConfig.subPackages || appConfig['subpackages']
if (subPackages && subPackages.length) {
Expand Down Expand Up @@ -364,7 +387,7 @@ export default class MiniPlugin {
}
}

getPages () {
getPages (compiler) {
const { buildAdapter } = this.options
const appEntry = this.appEntry
const code = fs.readFileSync(appEntry).toString()
Expand All @@ -381,6 +404,7 @@ export default class MiniPlugin {
throw new Error('缺少页面')
}
this.getSubPackages(configObj)
this.generateTabBarFiles(compiler, configObj)
taroFileTypeMap[this.appEntry] = {
type: PARSE_AST_TYPE.ENTRY,
config: configObj,
Expand Down Expand Up @@ -492,7 +516,7 @@ export default class MiniPlugin {
}

run (compiler: webpack.Compiler) {
this.getPages()
this.getPages(compiler)
this.getComponents(this.pages, true)
this.addEntries(compiler)
this.transferFileContent(compiler)
Expand Down
3 changes: 0 additions & 3 deletions packages/taro-mini-runner/src/plugins/removeStyleImport.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/taro-mini-runner/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,6 @@ export enum PARSE_AST_TYPE {
ENTRY = 'ENTRY',
PAGE = 'PAGE',
COMPONENT = 'COMPONENT',
NORMAL = 'NORMAL'
NORMAL = 'NORMAL',
STATIC = 'STATIC'
}
12 changes: 12 additions & 0 deletions packages/taro-mini-runner/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export function isQuickAppPkg (name: string): boolean {
return /@system\./.test(name)
}

export function isEmptyObject (obj: any): boolean {
if (obj == null) {
return true
}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
return false
}
}
return true
}

export function traverseObjectNode (node, buildAdapter: string, parentKey?: string) {
if (node.type === 'ClassProperty' || node.type === 'ObjectProperty') {
const properties = node.value.properties
Expand Down

0 comments on commit 43a129a

Please sign in to comment.