Skip to content

Commit

Permalink
fix(webpack5-runner): 修复使用 sentry-webpack-plugin 时报错,fix #13988 (#14111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj authored Jul 5, 2023
1 parent 856ce32 commit 09ac60c
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export default class TaroMiniPlugin {
}
})

const { PROCESS_ASSETS_STAGE_ADDITIONAL } = compiler.webpack.Compilation
const { PROCESS_ASSETS_STAGE_ADDITIONAL, PROCESS_ASSETS_STAGE_OPTIMIZE } = compiler.webpack.Compilation
compilation.hooks.processAssets.tapAsync(
{
name: PLUGIN_NAME,
Expand All @@ -298,6 +298,17 @@ export default class TaroMiniPlugin {
await this.generateMiniFiles(compilation, compiler)
})
)
compilation.hooks.processAssets.tapAsync(
{
name: PLUGIN_NAME,
// 删除 assets 的相关操作放在触发时机较后的 Stage,避免过早删除出现的一些问题,#13988
// Stage 触发顺序:https://webpack.js.org/api/compilation-hooks/#list-of-asset-processing-stages
stage: PROCESS_ASSETS_STAGE_OPTIMIZE
},
this.tryAsync<any>(async () => {
await this.optimizeMiniFiles(compilation, compiler)
})
)
})

compiler.hooks.afterEmit.tapAsync(
Expand Down Expand Up @@ -889,17 +900,10 @@ export default class TaroMiniPlugin {
*/
compilation.getAssets().forEach(({ name: assetPath }) => {
const styleExt = this.options.fileType.style
const templExt = this.options.fileType.templ
if (new RegExp(`(\\${styleExt}|\\${templExt})\\.js(\\.map){0,1}$`).test(assetPath)) {
delete compilation.assets[assetPath]
} else if (new RegExp(`${styleExt}${styleExt}$`).test(assetPath)) {
if (new RegExp(`${styleExt}${styleExt}$`).test(assetPath)) {
const assetObj = compilation.assets[assetPath]
const newAssetPath = assetPath.replace(styleExt, '')
compilation.assets[newAssetPath] = assetObj
delete compilation.assets[assetPath]
}
if (!isUsingCustomWrapper && assetPath === 'custom-wrapper.js') {
delete compilation.assets[assetPath]
}
})

Expand Down Expand Up @@ -1030,6 +1034,26 @@ export default class TaroMiniPlugin {
}
}

async optimizeMiniFiles (compilation: Compilation, _compiler: Compiler) {
const isUsingCustomWrapper = componentConfig.thirdPartyComponents.has('custom-wrapper')

/**
* 与原生小程序混写时解析模板与样式
*/
compilation.getAssets().forEach(({ name: assetPath }) => {
const styleExt = this.options.fileType.style
const templExt = this.options.fileType.templ
if (new RegExp(`(\\${styleExt}|\\${templExt})\\.js(\\.map){0,1}$`).test(assetPath)) {
delete compilation.assets[assetPath]
} else if (new RegExp(`${styleExt}${styleExt}$`).test(assetPath)) {
delete compilation.assets[assetPath]
}
if (!isUsingCustomWrapper && assetPath === 'custom-wrapper.js') {
delete compilation.assets[assetPath]
}
})
}

generateConfigFile (compilation: Compilation, compiler: Compiler, filePath: string, config: Config & { component?: boolean }) {
const { RawSource } = compiler.webpack.sources
const fileConfigName = this.getConfigPath(this.getComponentName(filePath))
Expand Down

0 comments on commit 09ac60c

Please sign in to comment.