Nuxt/packages/webpack/src/plugins/chunk.ts
Daniel Roe e207f7e993
feat(rspack,webpack): add rspack builder (#29142)
Co-authored-by: "yangjian.fe" <yangjian.fe@bytedance.com>
Co-authored-by: underfin <likui6666666@gmail.com>
2024-10-09 15:59:32 +02:00

29 lines
823 B
TypeScript

import type { Compiler } from 'webpack'
import { webpack } from '#builder'
const pluginName = 'ChunkErrorPlugin'
export class ChunkErrorPlugin {
script = `
if (typeof ${webpack.RuntimeGlobals.require} !== "undefined") {
var _ensureChunk = ${webpack.RuntimeGlobals.ensureChunk};
${webpack.RuntimeGlobals.ensureChunk} = function (chunkId) {
return Promise.resolve(_ensureChunk(chunkId)).catch(error => {
const e = new Event('nuxt:preloadError', { cancelable: true })
e.payload = error
window.dispatchEvent(e)
throw error
});
};
};`
apply (compiler: Compiler) {
compiler.hooks.thisCompilation.tap(pluginName, compilation =>
compilation.mainTemplate.hooks.localVars.tap(
{ name: pluginName, stage: 1 },
source => source + this.script,
),
)
}
}