Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Runtime source with chunk manifest lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
jouni-kantola committed Oct 26, 2017
1 parent 401e202 commit e6c8dcf
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/chunk-manifest-webpack-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,24 +61,27 @@ class ChunkManifestPlugin {
);
}

return _;
return source;
});
});

compiler.plugin("compilation", function(compilation) {
compilation.mainTemplate.plugin("require-ensure", function(
_,
source,
chunk,
hash,
chunkIdVariableName
) {
if (oldChunkFilename) {
this.outputOptions.chunkFilename = oldChunkFilename;
}
return _.replace(

const updatedSource = source.replace(
/"__CHUNK_MANIFEST__"/,
`window["${manifestVariable}"][${chunkIdVariableName}]`
);

return updatedSource;
});
});
}
Expand Down
69 changes: 69 additions & 0 deletions test/plugin-runtime-replacement-test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit e6c8dcf

Please sign in to comment.