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

Commit 05911da

Browse files
author
ruszki
committed
Update for Webpack 4
1 parent 4a7711b commit 05911da

9 files changed

+324
-230
lines changed

src/chunk-manifest-webpack-plugin.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
2-
* This dependency plugin is a fork of:
2+
* This dependency plugin is a fork of:
33
* chunk-manifest-webpack-plugin (https://github.com/soundcloud/chunk-manifest-webpack-plugin)
4-
*
4+
*
55
* inline-chunk-manifest-html-webpack-plugin already enables inlining webpack's chunk manifest,
66
* and therefor has been extracted.
77
*/
@@ -22,15 +22,15 @@ class ChunkManifestPlugin {
2222
const manifestVariable = this.manifestVariable;
2323
let chunkFilename;
2424

25-
compiler.plugin("this-compilation", compilation => {
25+
compiler.hooks.thisCompilation.tap("ChunkManifestPlugin", compilation => {
2626
const mainTemplate = compilation.mainTemplate;
27-
mainTemplate.plugin("require-ensure", function(
27+
mainTemplate.hooks.requireEnsure.tap("ChunkManifestPlugin", (
2828
source,
2929
chunk,
3030
hash
3131
/*, chunkIdVariableName */
32-
) {
33-
chunkFilename = this.outputOptions.chunkFilename;
32+
) => {
33+
chunkFilename = compilation.outputOptions.chunkFilename;
3434

3535
if (chunkFilename) {
3636
const chunkManifest = [chunk].reduce(function registerChunk(
@@ -42,23 +42,28 @@ class ChunkManifestPlugin {
4242
if (c.hasRuntime()) {
4343
manifest[c.id] = undefined;
4444
} else {
45-
const assetFilename = mainTemplate.applyPluginsWaterfall(
46-
"asset-path",
47-
chunkFilename,
48-
{
49-
hash,
50-
chunk: c
51-
}
52-
);
45+
const assetFilename = mainTemplate.getAssetPath(chunkFilename, {
46+
hash: hash,
47+
chunk: c
48+
});
5349

5450
manifest[c.id] = assetFilename;
5551
}
5652

57-
return c.chunks.reduce(registerChunk, manifest);
53+
const cGroups = Array.from(c.groupsIterable);
54+
const cGroupsChildren = cGroups.map(group => group.chunks);
55+
const unsortedChunks = cGroupsChildren.reduce(
56+
(chunksArray, childrens) => chunksArray.concat(childrens),
57+
[]
58+
);
59+
60+
const chunks = Array.from(new Set(unsortedChunks));
61+
62+
return chunks.reduce(registerChunk, manifest);
5863
},
5964
{});
6065

61-
this.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";
66+
compilation.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";
6267

6368
compilation.assets[manifestFilename] = new RawSource(
6469
JSON.stringify(chunkManifest)
@@ -69,25 +74,23 @@ class ChunkManifestPlugin {
6974
});
7075
});
7176

72-
compiler.plugin("compilation", compilation => {
77+
compiler.hooks.compilation.tap("ChunkManifestPlugin", compilation => {
7378
const mainTemplate = compilation.mainTemplate;
74-
mainTemplate.plugin("require-ensure", function(
75-
source,
76-
chunk,
77-
hash,
78-
chunkIdVariableName
79-
) {
80-
if (chunkFilename) {
81-
this.outputOptions.chunkFilename = chunkFilename;
82-
}
83-
84-
const updatedSource = source.replace(
85-
/"__CHUNK_MANIFEST__"/,
86-
`window["${manifestVariable}"][${chunkIdVariableName}]`
87-
);
79+
mainTemplate.hooks.requireEnsure.tap(
80+
"ChunkManifestPlugin",
81+
(source, chunk, hash, chunkIdVariableName) => {
82+
if (chunkFilename) {
83+
compilation.outputOptions.chunkFilename = chunkFilename;
84+
}
85+
86+
const updatedSource = source.replace(
87+
/"__CHUNK_MANIFEST__"/,
88+
`window["${manifestVariable}"][${chunkIdVariableName}]`
89+
);
8890

89-
return updatedSource;
90-
});
91+
return updatedSource;
92+
}
93+
);
9194
});
9295
}
9396
}

src/index.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,52 +51,58 @@ class InlineChunkManifestHtmlWebpackPlugin {
5151
const chunkManifestVariable = this.chunkManifestVariable;
5252
const dropAsset = this.dropAsset;
5353

54-
compiler.plugin("emit", (compilation, callback) => {
55-
if (dropAsset) {
56-
delete compilation.assets[manifestFilename];
57-
}
54+
compiler.hooks.emit.tapAsync(
55+
"InlineChunkManifestHtmlWebpackPlugin",
56+
(compilation, callback) => {
57+
if (dropAsset) {
58+
delete compilation.assets[manifestFilename];
59+
}
5860

59-
callback();
60-
});
61-
62-
compiler.plugin("compilation", compilation => {
63-
compilation.plugin(
64-
"html-webpack-plugin-alter-asset-tags",
65-
(htmlPluginData, callback) => {
66-
const asset = compilation.assets[manifestFilename];
67-
68-
if (asset) {
69-
const newTag = {
70-
tagName: "script",
71-
closeTag: true,
72-
attributes: {
73-
type: "text/javascript"
74-
},
75-
innerHTML: `window.${manifestVariable}=${asset.source()}`
76-
};
77-
78-
htmlPluginData.head.unshift(newTag);
61+
callback();
62+
}
63+
);
64+
65+
compiler.hooks.compilation.tap(
66+
"InlineChunkManifestHtmlWebpackPlugin",
67+
compilation => {
68+
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(
69+
"InlineChunkManifestHtmlWebpackPlugin",
70+
(htmlPluginData, callback) => {
71+
const asset = compilation.assets[manifestFilename];
72+
73+
if (asset) {
74+
const newTag = {
75+
tagName: "script",
76+
closeTag: true,
77+
attributes: {
78+
type: "text/javascript"
79+
},
80+
innerHTML: `window.${manifestVariable}=${asset.source()}`
81+
};
82+
83+
htmlPluginData.head.unshift(newTag);
84+
}
85+
86+
callback(null, htmlPluginData);
7987
}
88+
);
8089

81-
callback(null, htmlPluginData);
82-
}
83-
);
90+
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync(
91+
"InlineChunkManifestHtmlWebpackPlugin",
92+
(htmlPluginData, callback) => {
93+
const asset = compilation.assets[manifestFilename];
8494

85-
compilation.plugin(
86-
"html-webpack-plugin-before-html-generation",
87-
(htmlPluginData, callback) => {
88-
const asset = compilation.assets[manifestFilename];
95+
if (asset) {
96+
htmlPluginData.assets[
97+
chunkManifestVariable
98+
] = `<script type="text/javascript">window.${manifestVariable}=${asset.source()}</script>`;
99+
}
89100

90-
if (asset) {
91-
htmlPluginData.assets[
92-
chunkManifestVariable
93-
] = `<script type="text/javascript">window.${manifestVariable}=${asset.source()}</script>`;
101+
callback(null, htmlPluginData);
94102
}
95-
96-
callback(null, htmlPluginData);
97-
}
98-
);
99-
});
103+
);
104+
}
105+
);
100106
}
101107

102108
applyDependencyPlugins(compiler) {

test/plugin-apply-test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ test("plugin and dependency plugins has apply", t => {
1212
test.cb("dependency plugins are applied", t => {
1313
t.plan(2);
1414

15-
const fakeCompiler = { plugin: () => {} };
15+
const fakeCompiler = {
16+
hooks: {
17+
emit: {
18+
tapAsync: (name, handler) => {}
19+
},
20+
compilation: {
21+
tap: (name, handler) => {}
22+
}
23+
}
24+
};
1625

1726
const dependencyPlugin = {
1827
apply: compiler => {
@@ -35,7 +44,16 @@ test.cb("dependency plugins are applied", t => {
3544
test.cb("overridden manifest plugins applied", t => {
3645
t.plan(2);
3746

38-
const fakeCompiler = { plugin: () => {} };
47+
const fakeCompiler = {
48+
hooks: {
49+
emit: {
50+
tapAsync: (name, handler) => {}
51+
},
52+
compilation: {
53+
tap: (name, handler) => {}
54+
}
55+
}
56+
};
3957

4058
const dependencyPlugin = {
4159
apply: compiler => {

test/plugin-create-asset-test.js

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,54 @@ test.cb("create asset", t => {
77
const manifestVariable = "manifest-variable";
88
const chunkManifestVariable = "chunk-manifest-variable";
99

10-
const compilationPluginEvent = (compilationEvent, alterAssets) => {
11-
if (compilationEvent === "html-webpack-plugin-before-html-generation") {
12-
const htmlPluginData = {
13-
assets: {}
14-
};
15-
16-
alterAssets(htmlPluginData, (_, result) => {
17-
const asset = htmlPluginData.assets[chunkManifestVariable];
18-
19-
t.is(
20-
asset,
21-
`<script type="text/javascript">window.${manifestVariable}=${manifestFileContent}</script>`
22-
);
23-
24-
t.end();
25-
});
26-
}
10+
const compilationPluginEvent = (name, alterAssets) => {
11+
const htmlPluginData = {
12+
assets: {}
13+
};
14+
15+
alterAssets(htmlPluginData, (_, result) => {
16+
const asset = htmlPluginData.assets[chunkManifestVariable];
17+
18+
t.is(
19+
asset,
20+
`<script type="text/javascript">window.${manifestVariable}=${manifestFileContent}</script>`
21+
);
22+
23+
t.end();
24+
});
2725
};
2826

29-
const pluginEvent = (event, compile) => {
30-
if (event === "compilation") {
31-
const assets = {};
32-
assets[manifestFilename] = {
33-
source: () => manifestFileContent
34-
};
27+
const pluginEvent = (name, compile) => {
28+
const assets = {};
29+
assets[manifestFilename] = {
30+
source: () => manifestFileContent
31+
};
3532

36-
const compilation = {
37-
plugin: compilationPluginEvent,
38-
assets: assets
39-
};
33+
const compilation = {
34+
hooks: {
35+
htmlWebpackPluginAlterAssetTags: {
36+
tapAsync: () => {}
37+
},
38+
htmlWebpackPluginBeforeHtmlGeneration: {
39+
tapAsync: compilationPluginEvent
40+
}
41+
},
42+
assets: assets
43+
};
4044

41-
compile(compilation);
42-
}
45+
compile(compilation);
4346
};
4447

45-
const fakeCompiler = { plugin: pluginEvent };
48+
const fakeCompiler = {
49+
hooks: {
50+
emit: {
51+
tapAsync: (name, handler) => {}
52+
},
53+
compilation: {
54+
tap: pluginEvent
55+
}
56+
}
57+
};
4658

4759
const plugin = new InlineChunkManifestHtmlWebpackPlugin({
4860
filename: manifestFilename,

test/plugin-drop-asset-test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ function isDropped(dropAsset, callback) {
2727
source: () => manifestFileContent
2828
};
2929

30-
const pluginEvent = (event, emit) => {
31-
if (event === "emit") {
32-
emit(compilation, () => {
33-
const asset = compilation.assets[manifestFilename];
34-
callback(asset);
35-
});
30+
const fakeCompiler = {
31+
hooks: {
32+
emit: {
33+
tapAsync: (name, handler) => {
34+
handler(compilation, () => {
35+
const asset = compilation.assets[manifestFilename];
36+
callback(asset);
37+
});
38+
}
39+
},
40+
compilation: {
41+
tap: (name, handler) => {}
42+
}
3643
}
3744
};
3845

39-
const fakeCompiler = { plugin: pluginEvent };
40-
4146
const plugin = new InlineChunkManifestHtmlWebpackPlugin({
4247
filename: manifestFilename,
4348
manifestVariable: manifestVariable,

0 commit comments

Comments
 (0)