diff --git a/src/chunk-manifest-webpack-plugin.js b/src/chunk-manifest-webpack-plugin.js index fdd8770..e649780 100644 --- a/src/chunk-manifest-webpack-plugin.js +++ b/src/chunk-manifest-webpack-plugin.js @@ -24,7 +24,7 @@ class ChunkManifestPlugin { compiler.plugin("this-compilation", function(compilation) { const mainTemplate = compilation.mainTemplate; - mainTemplate.plugin("require-ensure", function(_, chunk, hash) { + mainTemplate.plugin("require-ensure", function(source, chunk, hash) { const filename = this.outputOptions.chunkFilename || this.outputOptions.filename; @@ -61,13 +61,13 @@ class ChunkManifestPlugin { ); } - return _; + return source; }); }); compiler.plugin("compilation", function(compilation) { compilation.mainTemplate.plugin("require-ensure", function( - _, + source, chunk, hash, chunkIdVariableName @@ -75,10 +75,13 @@ class ChunkManifestPlugin { if (oldChunkFilename) { this.outputOptions.chunkFilename = oldChunkFilename; } - return _.replace( + + const updatedSource = source.replace( /"__CHUNK_MANIFEST__"/, `window["${manifestVariable}"][${chunkIdVariableName}]` ); + + return updatedSource; }); }); } diff --git a/test/plugin-runtime-replacement-test.js b/test/plugin-runtime-replacement-test.js new file mode 100644 index 0000000..42da678 --- /dev/null +++ b/test/plugin-runtime-replacement-test.js @@ -0,0 +1,69 @@ +import test from "ava"; + +import ChunkManifestPlugin from "../src/chunk-manifest-webpack-plugin"; + +/*compiler.plugin("compilation", function(compilation) { + compilation.mainTemplate.plugin("require-ensure", function( + source, + chunk, + hash, + chunkIdVariableName + ) { + if (oldChunkFilename) { + this.outputOptions.chunkFilename = oldChunkFilename; + } + return source.replace( + /"__CHUNK_MANIFEST__"/, + `window["${manifestVariable}"][${chunkIdVariableName}]` + ); + }); + });*/ + +test.cb("replace runtime with lookup", t => { + const chunkIdVariableName = "a-chunk-id-variable"; + const manifestVariable = "manifest-variable"; + const placeholder = `"__CHUNK_MANIFEST__"`; + + const runtimeSource = [ + "runtime-source-start", + placeholder, + ";", + "runtime-source-end" + ]; + + let expected = [...runtimeSource]; + expected[1] = `window["${manifestVariable}"][${chunkIdVariableName}]`; + + const compilationPluginEvent = (compilationEvent, ensure) => { + if (compilationEvent === "require-ensure") { + const updatedSource = ensure( + runtimeSource.join(""), + undefined, + undefined, + chunkIdVariableName + ); + t.is(updatedSource, expected.join("")); + t.end(); + } + }; + + const pluginEvent = (event, compile) => { + if (event === "compilation") { + const compilation = { + mainTemplate: { + plugin: compilationPluginEvent + } + }; + compile(compilation); + } + }; + + const fakeCompiler = { plugin: pluginEvent }; + + const plugin = new ChunkManifestPlugin({ + manifestVariable + }); + + plugin.plugins = [{ apply: () => {} }]; + plugin.apply(fakeCompiler); +});