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

fix(mini-runner): 修复external module导致的错误 (#9862) #9881

Merged
merged 2 commits into from
Jul 26, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../../css/sub-vendors.css'
import '../../css/sub-common.css'
import subCommonStyles from '../../css/sub-common.module.css'
import vendorsStyles from '../../css/sub-vendors.module.css'
import _ from 'lodash'

export default class Detail extends Component {
componentDidMount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import consoleLogSubCommon from '../../utils/consoleLogSubCommon'
import testExcludeFunction from '../../utils/testExcludeFunction'
import '../../css/sub-common.css'
import subCommonStyles from '../../css/sub-common.module.css'
import _ from 'lodash'

export default class My extends Component {
componentDidMount () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe('mini-split-chunks', () => {
test('should process mini-split-chunks', async () => {
const appName = 'mini-split-chunks'
const { stats, config } = await compile(appName, {
webpackChain (chain) {
chain.merge({
externals: [
'lodash'
]
})
},
optimizeMainPackage: {
enable: true,
exclude: [
Expand Down
50 changes: 39 additions & 11 deletions packages/taro-mini-runner/src/plugins/MiniSplitChunksPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as webpack from 'webpack'
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 { resolveMainFilePath, readConfig, promoteRelativePath, normalizePath } from '@tarojs/helper'
import { isString, isFunction, isArray } from '@tarojs/shared'

const PLUGIN_NAME = 'MiniSplitChunkPlugin'
Expand Down Expand Up @@ -74,6 +74,10 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {

compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation: any) => {
compilation.hooks.optimizeChunks.tap(PLUGIN_NAME, (chunks: webpack.compilation.Chunk[]) => {
const splitChunksOriginConfig = {
...compiler?.options?.optimization?.splitChunks
}

this.subCommonDeps = new Map()
this.chunkSubCommons = new Map()
this.subPackagesVendors = new Map()
Expand All @@ -84,13 +88,22 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
const subChunks = chunks.filter(chunk => this.isSubChunk(chunk))

if (subChunks.length === 0) {
this.options = SplitChunksPlugin.normalizeOptions(splitChunksOriginConfig)
return
}

subChunks.forEach((subChunk: webpack.compilation.Chunk) => {
const modules = Array.from(subChunk.modulesIterable)

modules.map((module: any) => {
if (this.isExternalModule(module)) {
return
}

if (!this.hasModuleId(module)) {
return
}

if (this.hasExclude() && this.isExcludeModule(module)) {
return
}
Expand Down Expand Up @@ -141,9 +154,9 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
* 用新的option配置生成新的cacheGroups配置
*/
this.options = SplitChunksPlugin.normalizeOptions({
...compiler?.options?.optimization?.splitChunks,
...splitChunksOriginConfig,
cacheGroups: {
...compiler?.options?.optimization?.splitChunks?.cacheGroups,
...splitChunksOriginConfig?.cacheGroups,
...this.getSubPackageVendorsCacheGroup(),
...this.getSubCommonCacheGroup()
}
Expand Down Expand Up @@ -231,7 +244,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
const subVendorsWxssPath = path.join(subRoot, `${SUB_VENDORS_NAME}${FileExtsMap.STYLE}`)
const source = new ConcatSource()

if (assets[this.formatSystemPath(subVendorsWxssPath)]) {
if (assets[normalizePath(subVendorsWxssPath)]) {
const subVendorsAbsolutePath = path.resolve(this.distPath, subVendorsWxssPath)
const relativePath = this.getRealRelativePath(wxssAbsulutePath, subVendorsAbsolutePath)

Expand All @@ -243,7 +256,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
const wxssFileName = `${moduleName}${FileExtsMap.STYLE}`
const wxssFilePath = path.join(SUB_COMMON_DIR, wxssFileName)

if (assets[this.formatSystemPath(wxssFilePath)]) {
if (assets[normalizePath(wxssFilePath)]) {
const moduleAbsulutePath = path.resolve(this.distPath, subRoot, SUB_COMMON_DIR, wxssFileName)
const relativePath = this.getRealRelativePath(wxssAbsulutePath, moduleAbsulutePath)

Expand Down Expand Up @@ -375,7 +388,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
* match *\/sub-vendors
*/
matchSubVendors (chunk: webpack.compilation.Chunk): boolean {
const subVendorsRegExps = this.subRoots.map(subRoot => new RegExp(`^${this.formatSystemPath(path.join(subRoot, SUB_VENDORS_NAME))}$`))
const subVendorsRegExps = this.subRoots.map(subRoot => new RegExp(`^${normalizePath(path.join(subRoot, SUB_VENDORS_NAME))}$`))
const isSubVendors = subVendorsRegExps.find(subVendorsRegExp => subVendorsRegExp.test(chunk.name))

return !!isSubVendors
Expand Down Expand Up @@ -440,7 +453,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {

return chunks.every(chunk => new RegExp(`^${subRoot}\\/`).test(chunk.name))
},
name: this.formatSystemPath(path.join(subRoot, SUB_VENDORS_NAME)),
name: normalizePath(path.join(subRoot, SUB_VENDORS_NAME)),
minChunks: 2,
priority: 10000
}
Expand All @@ -455,7 +468,7 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
const subCommonCacheGroup = {}

this.subCommonDeps.forEach((depInfo: DepInfo, depName: string) => {
const cacheGroupName = this.formatSystemPath(path.join(SUB_COMMON_DIR, depName))
const cacheGroupName = normalizePath(path.join(SUB_COMMON_DIR, depName))

subCommonCacheGroup[cacheGroupName] = {
name: cacheGroupName,
Expand Down Expand Up @@ -520,9 +533,24 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
}

/**
* 将window系统下的路径分隔符转成/
* 判断module为external module
*/
formatSystemPath (p) {
return p.replace(/\\/g, '/')
isExternalModule (module: any) {
return !!module.external
}

/**
* 判断是否存在resource和_identifier
*/
hasModuleId (module: any) {
if (module.resource) {
return true
}

if (module._identifier) {
return true
}

return false
}
}