Skip to content

Commit

Permalink
feat(mini-runner/mini-split-chunks): optimizeMainPackage配置改成对象,添加excl…
Browse files Browse the repository at this point in the history
…ude参数,exclude的module不走分包提取的逻辑
  • Loading branch information
huangcj99 committed Dec 3, 2020
1 parent 8315494 commit 137a29e
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 68 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React, { Component } from 'react'
import { View, Text } from '@tarojs/components'
import consoleLogMain from '../../utils/consoleLogMain'
import consoleLogSubVendors from '../../utils/consoleLogSubVendors'
import testExcludeString from '../../utils/testExcludeString'
import './index.css'

export default class Detail extends Component {
componentDidMount() {
consoleLogMain()
consoleLogSubVendors()
testExcludeString()
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { View, Text } from '@tarojs/components'
import consoleLogMain from '../../utils/consoleLogMain'
import consoleLogSubVendors from '../../utils/consoleLogSubVendors'
import consoleLogSubCommon from '../../utils/consoleLogSubCommon'
import testExcludeString from '../../utils/testExcludeString'
import testExcludeFunction from '../../utils/testExcludeFunction'

export default class My extends Component {
componentDidMount () {
consoleLogMain()
consoleLogSubVendors()
consoleLogSubCommon('packageA')
testExcludeString()
testExcludeFunction()
}

render () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import React, { Component } from 'react'
import { View, Text } from '@tarojs/components'
import consoleLogMain from '../../utils/consoleLogMain'
import consoleLogSubCommon from '../../utils/consoleLogSubCommon'
import testExcludeFunction from '../../utils/testExcludeFunction'

export default class My extends Component {
componentDidMount () {
consoleLogMain()
consoleLogSubCommon('packageB')
testExcludeFunction()
}

render () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log('testExcludeFunction')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
console.log('testExcludeString')
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs-extra'
import * as path from 'path'

let compile
let getOutput
Expand All @@ -18,7 +19,13 @@ describe('mini-split-chunks', () => {

test('should process mini-split-chunks', async () => {
const { stats, config } = await compile('mini-split-chunks', {
optimizeMainPackage: true
optimizeMainPackage: {
enable: true,
exclude: [
path.resolve(__dirname, './fixtures/mini-split-chunks/src/utils/testExcludeString.js'),
(module) => module.resource.indexOf('testExcludeFunction') >= 0
]
}
})

const assets = stats.toJson().assets || []
Expand Down
50 changes: 49 additions & 1 deletion packages/taro-mini-runner/src/plugins/MiniSplitChunksPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ import * as SplitChunksPlugin from 'webpack/lib/optimize/SplitChunksPlugin'
import { ConcatSource } from 'webpack-sources'
import { AppConfig, SubPackage } from '@tarojs/taro'
import { resolveMainFilePath, readConfig, promoteRelativePath } from '@tarojs/helper'
import { isString, isFunction, isArray } from '@tarojs/shared'

const PLUGIN_NAME = 'MiniSplitChunkPlugin'
const SUB_COMMON_DIR = 'sub-common'
const SUB_VENDORS_NAME = 'vendors'
const CSS_MINI_EXTRACT = 'css/mini-extract'

interface MiniSplitChunksPluginOption {
exclude?: Array<string | ExcludeFunctionItem>
}

interface ExcludeFunctionItem {
(module: webpack.compilation.Module): boolean
}

interface DepInfo {
resource: string
chunks: Set<string>
Expand All @@ -25,18 +34,20 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
subPackagesVendors: webpack.compilation.Chunk[]
context: string
distPath: string
exclude: Array<string | ExcludeFunctionItem>
isDevMode: boolean
subPackages: SubPackage[]
subRoots: string[]
subRootRegExps: RegExp[]

constructor () {
constructor (options: MiniSplitChunksPluginOption) {
super()
this.options = null
this.subCommonDeps = new Map()
this.chunkSubCommons = new Map()
this.subPackagesVendors = []
this.distPath = ''
this.exclude = options.exclude || []
}

apply (compiler: any) {
Expand Down Expand Up @@ -364,6 +375,14 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
return false
}

if (!module.resource) {
return false
}

if (this.hasExclude() && this.isExcludeModule(module)) {
return false
}

return chunks.every(chunk => new RegExp(`^${subRoot}/`).test(chunk.name))
},
name: `${subRoot}/${SUB_VENDORS_NAME}`,
Expand All @@ -384,6 +403,14 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
subCommonCacheGroup[`${SUB_COMMON_DIR}/${depName}`] = {
name: `${SUB_COMMON_DIR}/${depName}`,
test: module => {
if (!module.resource) {
return false
}

if (this.hasExclude() && this.isExcludeModule(module)) {
return false
}

return module.resource === depInfo.resource
},
priority: 1000
Expand All @@ -392,6 +419,27 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
return subCommonCacheGroup
}

hasExclude (): boolean {
return isArray(this.exclude) && this.exclude.length > 0
}

isExcludeModule (module: any): boolean {
const moduleResource = module.resource

for (let i = 0; i < this.exclude.length; i++) {
const excludeItem = this.exclude[i]

if (isString(excludeItem) && excludeItem === moduleResource) {
return true
}

if (isFunction(excludeItem) && excludeItem(module)) {
return true
}
}
return false
}

setChunkSubCommons (subCommonDeps: Map<string, DepInfo>) {
const chunkSubCommons: Map<string, Set<string>> = new Map()

Expand Down
13 changes: 8 additions & 5 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
terser,
commonChunks,
addChunkPages,
optimizeMainPackage = false,
optimizeMainPackage = {
enable: false
},

modifyMiniConfigs,
modifyBuildAssets
Expand Down Expand Up @@ -134,8 +136,10 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
}
plugin.definePlugin = getDefinePlugin([constantsReplaceList])
/** 需要在miniPlugin前,否则无法获取entry地址 */
if (optimizeMainPackage) {
plugin.miniSplitChunksPlugin = getMiniSplitChunksPlugin()
if (optimizeMainPackage.enable) {
plugin.miniSplitChunksPlugin = getMiniSplitChunksPlugin({
exclude: optimizeMainPackage.exclude
})
}
plugin.miniPlugin = getMiniPlugin({
sourceDir,
Expand All @@ -156,8 +160,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
addChunkPages,
modifyMiniConfigs,
modifyBuildAssets,
minifyXML,
optimizeMainPackage
minifyXML
})

plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-mini-runner/src/webpack/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export const getMiniPlugin = args => {
return partial(getPlugin, MiniPlugin)([args])
}

export const getMiniSplitChunksPlugin = () => {
return partial(getPlugin, MiniSplitChunksPlugin)([])
export const getMiniSplitChunksPlugin = (args) => {
return partial(getPlugin, MiniSplitChunksPlugin)([args])
}

export const getProviderPlugin = args => {
Expand Down
5 changes: 4 additions & 1 deletion packages/taro/types/compile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ export interface IMiniAppConfig {
customFilesTypes?: IMINI_APP_FILE_TYPE,
commonChunks?: string[] | ((commonChunks: string[]) => string[]),
addChunkPages?: ((pages: Map<string, string[]>, pagesNames?: string[]) => void),
optimizeMainPackage: boolean,
optimizeMainPackage?: {
enable?: boolean,
exclude?: any[]
},

compile?: {
exclude?: any[],
Expand Down

0 comments on commit 137a29e

Please sign in to comment.