Skip to content

Commit 24804f5

Browse files
Amour1688Single-Dancer
authored andcommitted
refactor(runner): preserve MiniSplitChunksPlugin hooks and declare tapable in tests
1 parent b110d41 commit 24804f5

4 files changed

Lines changed: 84 additions & 6 deletions

File tree

‎packages/taro-webpack5-runner/src/plugins/MiniSplitChunksPlugin.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,29 @@ export default class MiniSplitChunksPlugin extends SplitChunksPlugin {
494494
}
495495
}
496496

497+
/**
498+
* 根据 webpack entry 配置获取入口文件路径
499+
*/
500+
getAppEntry (compiler: Compiler) {
501+
return extractMiniAppEntryPath(compiler.options.entry)
502+
}
503+
504+
/**
505+
* 获取分包配置
506+
*/
497507
getSubpackageConfig (compiler: Compiler): SubPackage[] {
498-
const appEntry = extractMiniAppEntryPath(compiler.options.entry)
499-
if (!appEntry) {
500-
return []
501-
}
502-
const appConfigPath = resolveMainFilePath(`${appEntry.replace(path.extname(appEntry), '')}.config`)
508+
const appEntry = this.getAppEntry(compiler)
509+
const appConfigPath = this.getConfigFilePath(appEntry)
503510
const appConfig: AppConfig = readConfig(appConfigPath, this.combination.config)
504-
return appConfig?.subPackages || appConfig?.subpackages || []
511+
512+
return appConfig.subPackages || appConfig.subpackages || []
513+
}
514+
515+
/**
516+
* 根据 app、页面、组件的路径获取对应的 config 配置文件的路径
517+
*/
518+
getConfigFilePath (filePath: string): string {
519+
return resolveMainFilePath(`${filePath.replace(path.extname(filePath), '')}.config`)
505520
}
506521

507522
isExcludeModule (module: any): boolean {

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/__tests__/runner-shared-bundler-compat.spec.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,63 @@ describe('shared bundler compatibility', () => {
167167
superApplySpy.mockRestore()
168168
}
169169
})
170+
171+
test('MiniSplitChunksPlugin honors subclass getAppEntry and getConfigFilePath overrides', () => {
172+
withTempDir('taro-mini-split-chunks-hooks-', tmpDir => {
173+
const customEntry = path.join(tmpDir, 'custom-app.ts')
174+
const customEntryConfig = path.join(tmpDir, 'custom-app.config.js')
175+
const customConfigPath = path.join(tmpDir, 'override.config.json')
176+
177+
fs.writeFileSync(customEntryConfig, `module.exports = ${JSON.stringify({
178+
subPackages: [
179+
{ root: 'pkgFromEntry/', pages: ['a/index'] },
180+
{ root: 'pkgIndie', pages: ['index'], independent: true },
181+
],
182+
})}`)
183+
fs.writeFileSync(customConfigPath, JSON.stringify({
184+
subpackages: [
185+
{ root: 'pkgFromConfig/', pages: ['b/index'] },
186+
],
187+
}))
188+
189+
class CustomEntryPlugin extends MiniSplitChunksPlugin {
190+
getAppEntry () {
191+
return customEntry
192+
}
193+
}
194+
195+
class CustomConfigPathPlugin extends MiniSplitChunksPlugin {
196+
getConfigFilePath () {
197+
return customConfigPath
198+
}
199+
}
200+
201+
const fileType = { style: '.wxss', config: '.json', script: '.js', templ: '.wxml', xs: '.wxs' }
202+
const entryPlugin = new CustomEntryPlugin({ combination: { config: {} } as any, fileType })
203+
const configPlugin = new CustomConfigPathPlugin({ combination: { config: {} } as any, fileType })
204+
const superApplySpy = jest
205+
.spyOn(Object.getPrototypeOf(MiniSplitChunksPlugin.prototype), 'apply')
206+
.mockImplementation(() => {})
207+
208+
try {
209+
entryPlugin.apply({
210+
webpack,
211+
context: tmpDir,
212+
options: { entry: {}, output: { path: path.join(tmpDir, 'dist') }, mode: 'production', optimization: {} },
213+
hooks: { thisCompilation: new SyncHook(['compilation']) },
214+
} as any)
215+
expect(entryPlugin.subRoots).toEqual(['pkgFromEntry'])
216+
217+
configPlugin.apply({
218+
webpack,
219+
context: tmpDir,
220+
options: { entry: () => ({}), output: { path: path.join(tmpDir, 'dist') }, mode: 'production', optimization: {} },
221+
hooks: { thisCompilation: new SyncHook(['compilation']) },
222+
} as any)
223+
expect(configPlugin.subRoots).toEqual(['pkgFromConfig'])
224+
} finally {
225+
superApplySpy.mockRestore()
226+
}
227+
})
228+
})
170229
})

‎tests/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@babel/core": "^7.24.4",
3737
"acorn": "^8.11.3",
3838
"acorn-walk": "^8.3.2",
39+
"tapable": "^2.2.1",
3940
"vue-loader": "^17.0.0",
4041
"webpack-chain": "^6.5.1",
4142
"webpack": "5.104.1",

0 commit comments

Comments
 (0)