From fcaefce120d6bf8777eb23c9f7f4ae61af762eb2 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Fri, 6 Mar 2020 17:09:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(mini-runner):=20=E4=BD=BF=E7=94=A8=20addChu?= =?UTF-8?q?nkPages=20=E6=97=B6=EF=BC=8C=E9=A1=B5=E9=9D=A2=E5=BC=95?= =?UTF-8?q?=E5=85=A5=E7=9A=84=E7=BB=84=E4=BB=B6=E4=B9=9F=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E6=8A=BD=E7=A6=BB=E7=9A=84=20chunks=EF=BC=8C?= =?UTF-8?q?close=20#5580?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/MiniPlugin.ts | 45 +++++++------ .../src/plugins/TaroLoadChunksPlugin.ts | 66 +++++++++++++++---- packages/taro-mini-runner/src/utils/types.ts | 8 +++ 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 6be51e391e3a..e144d1720b25 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -14,10 +14,9 @@ import * as t from 'babel-types' import traverse from 'babel-traverse' import { Config as IConfig, PageConfig } from '@tarojs/taro' import * as _ from 'lodash' -import { SyncHook } from 'tapable' import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP, taroJsFramework, taroJsQuickAppComponents, REG_SCRIPTS, processTypeEnum } from '../utils/constants' -import { IComponentObj, AddPageChunks } from '../utils/types' +import { IComponentObj, AddPageChunks, IComponent } from '../utils/types' import { resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync, isEmptyObject, promoteRelativePath, printLog, isAliasPath, replaceAliasPath } from '../utils' import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency' import { getTaroJsQuickAppComponentsPath, generateQuickAppUx, getImportTaroSelfComponents, getImportCustomComponents, generateQuickAppManifest } from '../utils/helper' @@ -56,14 +55,6 @@ export interface ITaroFileInfo { } } -interface IComponent { - name: string, - path: string, - isNative: boolean, - stylePath?: string, - templatePath?: string -} - const PLUGIN_NAME = 'MiniPlugin' let taroFileTypeMap: ITaroFileInfo = {} @@ -161,6 +152,7 @@ export default class MiniPlugin { errors: any[] changedFileType: PARSE_AST_TYPE | undefined addedComponents: Set + pageComponentsDependenciesMap: Map> constructor (options = {}) { this.options = defaults(options || {}, { @@ -184,6 +176,7 @@ export default class MiniPlugin { this.isWatch = false this.errors = [] this.addedComponents = new Set() + this.pageComponentsDependenciesMap = new Map() } tryAsync = fn => async (arg, callback) => { @@ -198,11 +191,19 @@ export default class MiniPlugin { apply (compiler) { this.context = compiler.context this.appEntry = this.getAppEntry(compiler) - compiler.hooks.getPages = new SyncHook(['pages']) compiler.hooks.run.tapAsync( PLUGIN_NAME, this.tryAsync(async (compiler: webpack.Compiler) => { - await this.run(compiler) + await this.run(compiler) + new TaroLoadChunksPlugin({ + commonChunks: this.options.commonChunks, + buildAdapter: this.options.buildAdapter, + isBuildPlugin: this.options.isBuildPlugin, + addChunkPages: this.options.addChunkPages, + pages: this.pages, + depsMap: this.pageComponentsDependenciesMap, + sourceDir: this.sourceDir + }).apply(compiler) }) ) @@ -215,6 +216,15 @@ export default class MiniPlugin { } else { await this.watchRun(compiler, changedFiles) } + new TaroLoadChunksPlugin({ + commonChunks: this.options.commonChunks, + buildAdapter: this.options.buildAdapter, + isBuildPlugin: this.options.isBuildPlugin, + addChunkPages: this.options.addChunkPages, + pages: this.pages, + depsMap: this.pageComponentsDependenciesMap, + sourceDir: this.sourceDir + }).apply(compiler) }) ) @@ -256,13 +266,6 @@ export default class MiniPlugin { }) ) - new TaroLoadChunksPlugin({ - commonChunks: this.options.commonChunks, - buildAdapter: this.options.buildAdapter, - isBuildPlugin: this.options.isBuildPlugin, - addChunkPages: this.options.addChunkPages - }).apply(compiler) - new TaroNormalModulesPlugin().apply(compiler) } @@ -530,7 +533,6 @@ export default class MiniPlugin { } }) ]) - ;(compiler.hooks as any).getPages.call(this.pages) } catch (error) { if (error.codeFrame) { this.errors.push(new Error(error.message + '\n' + error.codeFrame)) @@ -771,6 +773,7 @@ export default class MiniPlugin { })) } if (depComponents && depComponents.length) { + const componentsList = new Set() depComponents.forEach(item => { const componentPath = resolveScriptPath(path.resolve(path.dirname(file.path), item.path)) if (fs.existsSync(componentPath) && !Array.from(this.components).some(item => item.path === componentPath)) { @@ -786,6 +789,8 @@ export default class MiniPlugin { } this.components.add(componentObj) this.addedComponents.add(componentObj) + componentsList.add(componentObj) + this.pageComponentsDependenciesMap.set(file.path, componentsList) this.getComponents(compiler, new Set([componentObj]), false) } }) diff --git a/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts b/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts index c9b2069deda0..6ab4da367dbf 100644 --- a/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/TaroLoadChunksPlugin.ts @@ -6,7 +6,7 @@ import { urlToRequest } from 'loader-utils' import { PARSE_AST_TYPE, REG_STYLE, BUILD_TYPES } from '../utils/constants' import { promoteRelativePath } from '../utils' -import { AddPageChunks } from '../utils/types' +import { AddPageChunks, IComponent, IComponentObj } from '../utils/types' const PLUGIN_NAME = 'TaroLoadChunksPlugin' @@ -14,7 +14,10 @@ interface IOptions { commonChunks: string[], buildAdapter: BUILD_TYPES, isBuildPlugin: boolean, - addChunkPages?: AddPageChunks + addChunkPages?: AddPageChunks, + pages: Set, + depsMap: Map> + sourceDir: string } export default class TaroLoadChunksPlugin { @@ -22,24 +25,49 @@ export default class TaroLoadChunksPlugin { buildAdapter: BUILD_TYPES isBuildPlugin: boolean addChunkPages?: AddPageChunks + pages: Set, + depsMap: Map> + sourceDir: string constructor (options: IOptions) { this.commonChunks = options.commonChunks this.buildAdapter = options.buildAdapter this.isBuildPlugin = options.isBuildPlugin this.addChunkPages = options.addChunkPages + this.pages = options.pages + this.depsMap = options.depsMap + this.sourceDir = options.sourceDir } apply (compiler: webpack.Compiler) { - let pagesList - const addChunkPagesList = new Map(); - (compiler.hooks as any).getPages.tap(PLUGIN_NAME, pages => { - pagesList = pages - }) + const pagesList = this.pages + const addChunkPagesList = new Map() + const depsMap = this.depsMap compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation: any) => { let commonChunks + let fileChunks = new Map() + let allDepsComponents = new Set() compilation.hooks.afterOptimizeChunks.tap(PLUGIN_NAME, (chunks: compilation.Chunk[]) => { commonChunks = chunks.filter(chunk => this.commonChunks.includes(chunk.name)).reverse() + this.addChunkPages(addChunkPagesList, Array.from(pagesList).map((item: any) => item.name)) + chunks.forEach(chunk => { + const id = getIdOrName(chunk) + addChunkPagesList.forEach((v, k) => { + if (k === id) { + const depChunks = v.map(v => ({ name: v })) + fileChunks.set(id, depChunks) + let entryModule = chunk.entryModule.rootModule ? chunk.entryModule.rootModule : chunk.entryModule + if (entryModule) { + const depsComponents = getAllDepComponents(entryModule.resource, depsMap) + depsComponents.forEach(component => { + const id = component.path.replace(this.sourceDir + path.sep, '').replace(path.extname(component.path), '').replace(/\\{1,}/g, '/') + const oriDep = fileChunks.get(id) || [] + fileChunks.set(id, Array.from(new Set([...oriDep, ...depChunks]))) + }) + } + } + }) + }) }) compilation.chunkTemplate.hooks.renderWithEntry.tap(PLUGIN_NAME, (modules, chunk) => { if (chunk.entryModule) { @@ -77,13 +105,14 @@ export default class TaroLoadChunksPlugin { entryModule.miniType === PARSE_AST_TYPE.COMPONENT)) { return addRequireToSource(getIdOrName(chunk), modules, commonChunks) } - if (typeof this.addChunkPages === 'function' && entryModule.miniType === PARSE_AST_TYPE.PAGE) { - const id = getIdOrName(chunk) + if (fileChunks.size + && (entryModule.miniType === PARSE_AST_TYPE.PAGE + || entryModule.miniType === PARSE_AST_TYPE.COMPONENT)) { let source - this.addChunkPages(addChunkPagesList, Array.from(pagesList).map((item: any) => item.name)) - addChunkPagesList.forEach((v, k) => { + const id = getIdOrName(chunk) + fileChunks.forEach((v, k) => { if (k === id) { - source = addRequireToSource(id, modules, v.map(v => ({ name: v }))) + source = addRequireToSource(id, modules, v) } }) return source @@ -101,6 +130,19 @@ function getIdOrName (chunk) { return chunk.name } +function getAllDepComponents (filePath, depsMap) { + let componentsList = new Set() + depsMap.forEach((value, key) => { + if (filePath === key) { + componentsList = new Set([...componentsList, ...value]) + value.forEach(item => { + componentsList = new Set([...componentsList, ...getAllDepComponents(item.path, depsMap)]) + }) + } + }) + return componentsList +} + function addRequireToSource (id, modules, commonChunks) { const source = new ConcatSource() commonChunks.forEach(chunkItem => { diff --git a/packages/taro-mini-runner/src/utils/types.ts b/packages/taro-mini-runner/src/utils/types.ts index b90c3c1692f1..9846aa723b2a 100644 --- a/packages/taro-mini-runner/src/utils/types.ts +++ b/packages/taro-mini-runner/src/utils/types.ts @@ -15,6 +15,14 @@ export interface IComponentObj { type?: string } +export interface IComponent { + name: string, + path: string, + isNative: boolean, + stylePath?: string, + templatePath?: string +} + export interface IChain { [key: string]: any }