From 581d0eb86cf5ab9d15b74cec57849bd7820010a7 Mon Sep 17 00:00:00 2001 From: Jouni Kantola Date: Fri, 27 Oct 2017 01:31:41 +0200 Subject: [PATCH] Replace runtime's chunk manifest with lookup --- src/chunk-manifest-webpack-plugin.js | 11 ++++-- test/plugin-runtime-replacement-test.js | 52 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 test/plugin-runtime-replacement-test.js 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..371104b --- /dev/null +++ b/test/plugin-runtime-replacement-test.js @@ -0,0 +1,52 @@ +import test from "ava"; + +import ChunkManifestPlugin from "../src/chunk-manifest-webpack-plugin"; + +test.cb("replace runtime's chunk manifest 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); +});