From 43a129a942aefa36b712fea57f01016a4056706c Mon Sep 17 00:00:00 2001 From: luckyadam Date: Wed, 26 Jun 2019 21:35:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(mini-runner):=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=BC=96=E8=AF=91=20tabBar=20=E4=B8=8A=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E8=B5=84=E6=BA=90=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/MiniPlugin.ts | 42 +++++++++++++++---- .../src/plugins/removeStyleImport.ts | 3 -- .../taro-mini-runner/src/utils/constants.ts | 3 +- packages/taro-mini-runner/src/utils/index.ts | 12 ++++++ 4 files changed, 47 insertions(+), 13 deletions(-) delete mode 100644 packages/taro-mini-runner/src/plugins/removeStyleImport.ts diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 07d367e84f10..ea5a68930da8 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -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' @@ -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( @@ -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 } @@ -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 { @@ -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)) } } }, @@ -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)) } }) } @@ -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) { @@ -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() @@ -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, @@ -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) diff --git a/packages/taro-mini-runner/src/plugins/removeStyleImport.ts b/packages/taro-mini-runner/src/plugins/removeStyleImport.ts deleted file mode 100644 index d64f0c68c051..000000000000 --- a/packages/taro-mini-runner/src/plugins/removeStyleImport.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default class RemoveStyleImportPlugin { - -} diff --git a/packages/taro-mini-runner/src/utils/constants.ts b/packages/taro-mini-runner/src/utils/constants.ts index 2dcdedbba06e..093dd79a439d 100644 --- a/packages/taro-mini-runner/src/utils/constants.ts +++ b/packages/taro-mini-runner/src/utils/constants.ts @@ -201,5 +201,6 @@ export enum PARSE_AST_TYPE { ENTRY = 'ENTRY', PAGE = 'PAGE', COMPONENT = 'COMPONENT', - NORMAL = 'NORMAL' + NORMAL = 'NORMAL', + STATIC = 'STATIC' } diff --git a/packages/taro-mini-runner/src/utils/index.ts b/packages/taro-mini-runner/src/utils/index.ts index 4f55a21a9b88..3bc7ffe8d849 100644 --- a/packages/taro-mini-runner/src/utils/index.ts +++ b/packages/taro-mini-runner/src/utils/index.ts @@ -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