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

Commit caf9aa3

Browse files
committed
Override chunk manifest plugin(s)
1 parent 7db493a commit caf9aa3

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const inlineChunkManifestConfig = {
4141
filename: 'manifest.json', // manifest.json is default
4242
manifestVariable: 'webpackManifest', // webpackManifest is default
4343
chunkManifestVariable: 'webpackChunkManifest', // webpackChunkManifest is default; use in html-webpack-plugin template
44-
dropAsset: true // false is default; use to skip output of the chunk manifest asset (removes manifest.json)
44+
dropAsset: true, // false is default; use to skip output of the chunk manifest asset (removes manifest.json)
45+
manifestPlugins: [/* override default chunk manifest plugin(s) */]
4546
};
4647

4748
new InlineChunkManifestHtmlWebpackPlugin(inlineChunkManifestConfig)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "inline-chunk-manifest-html-webpack-plugin",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Extension plugin for html-webpack-plugin to inline webpack chunk manifest. Default inlines in head tag.",
55
"main": "./src/index.js",
66
"repository": {

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ class InlineChunkManifestHtmlWebpackPlugin {
1212
options.chunkManifestVariable || "webpackChunkManifest";
1313
this.dropAsset = options.dropAsset || false;
1414

15-
this.plugins = [
15+
const manifestPlugins = options.manifestPlugins;
16+
17+
if (manifestPlugins && !Array.isArray(manifestPlugins)) {
18+
throw new TypeError(
19+
"Overriden manifest plugin(s) must be specified as array; [new Plugin1(), new Plugin1(), ...]"
20+
);
21+
}
22+
23+
const defaultManifestPlugins = [
1624
new ChunkManifestPlugin({
1725
filename: this.manifestFilename,
1826
manifestVariable: this.manifestVariable
1927
})
2028
];
29+
30+
this.plugins = manifestPlugins && manifestPlugins.length
31+
? manifestPlugins
32+
: defaultManifestPlugins;
2133
}
2234

2335
apply(compiler) {

test/plugin-apply-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,28 @@ test.cb("dependency plugins are applied", t => {
3131
plugin.plugins = [dependencyPlugin, anotherDependencyPlugin];
3232
plugin.apply(fakeCompiler);
3333
});
34+
35+
test.cb("overridden manifest plugins applied", t => {
36+
t.plan(2);
37+
38+
const fakeCompiler = { plugin: () => {} };
39+
40+
const dependencyPlugin = {
41+
apply: compiler => {
42+
t.is(compiler, fakeCompiler);
43+
}
44+
};
45+
46+
const anotherDependencyPlugin = {
47+
apply: compiler => {
48+
t.is(compiler, fakeCompiler);
49+
t.end();
50+
}
51+
};
52+
53+
const plugin = new InlineChunkManifestHtmlWebpackPlugin({
54+
manifestPlugins: [dependencyPlugin, anotherDependencyPlugin]
55+
});
56+
57+
plugin.apply(fakeCompiler);
58+
});

test/plugin-init-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,36 @@ test("override chunk manifest variable", t => {
5353

5454
t.is(plugin.chunkManifestVariable, "another-variable");
5555
});
56+
57+
test("fallback to default chunk manifest plugin", t => {
58+
const plugin = new InlineChunkManifestHtmlWebpackPlugin({
59+
manifestPlugins: []
60+
});
61+
62+
t.is(plugin.plugins.length, 1);
63+
t.true(plugin.plugins[0] instanceof ChunkManifestPlugin);
64+
});
65+
66+
test("ensure overriden plugins handle apply", t => {
67+
const manifestPlugin = { override: true, apply: () => {} };
68+
const anotherManifestPlugin = { override: true, apply: () => {} };
69+
70+
const plugin = new InlineChunkManifestHtmlWebpackPlugin({
71+
manifestPlugins: [manifestPlugin, anotherManifestPlugin]
72+
});
73+
74+
t.deepEqual(plugin.plugins, [manifestPlugin, anotherManifestPlugin]);
75+
});
76+
77+
test("array of plugins required", t => {
78+
const error = t.throws(() => {
79+
const plugin = new InlineChunkManifestHtmlWebpackPlugin({
80+
manifestPlugins: 1
81+
});
82+
}, TypeError);
83+
84+
t.is(
85+
error.message,
86+
"Overriden manifest plugin(s) must be specified as array; [new Plugin1(), new Plugin1(), ...]"
87+
);
88+
});

0 commit comments

Comments
 (0)