mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-23 17:10:07 +00:00
Co-authored-by: "yangjian.fe" <yangjian.fe@bytedance.com> Co-authored-by: underfin <likui6666666@gmail.com>
29 lines
823 B
TypeScript
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,
|
|
),
|
|
)
|
|
}
|
|
}
|