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

Commit

Permalink
Update for Webpack 4
Browse files Browse the repository at this point in the history
  • Loading branch information
ruszki committed Mar 21, 2018
1 parent 4a7711b commit 0628028
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
40 changes: 23 additions & 17 deletions src/chunk-manifest-webpack-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This dependency plugin is a fork of:
* This dependency plugin is a fork of:
* chunk-manifest-webpack-plugin (https://github.com/soundcloud/chunk-manifest-webpack-plugin)
*
*
* inline-chunk-manifest-html-webpack-plugin already enables inlining webpack's chunk manifest,
* and therefor has been extracted.
*/
Expand All @@ -22,15 +22,15 @@ class ChunkManifestPlugin {
const manifestVariable = this.manifestVariable;
let chunkFilename;

compiler.plugin("this-compilation", compilation => {
compiler.hooks.thisCompilation.tap("ChunkManifestPlugin", compilation => {
const mainTemplate = compilation.mainTemplate;
mainTemplate.plugin("require-ensure", function(
mainTemplate.hooks.requireEnsure.tap("ChunkManifestPlugin", (
source,
chunk,
hash
/*, chunkIdVariableName */
) {
chunkFilename = this.outputOptions.chunkFilename;
) => {
chunkFilename = compilation.outputOptions.chunkFilename;

if (chunkFilename) {
const chunkManifest = [chunk].reduce(function registerChunk(
Expand All @@ -42,23 +42,29 @@ class ChunkManifestPlugin {
if (c.hasRuntime()) {
manifest[c.id] = undefined;
} else {
const assetFilename = mainTemplate.applyPluginsWaterfall(
"asset-path",
const assetFilename = mainTemplate.getAssetPath(
chunkFilename,
{
hash,
hash: hash,
chunk: c
}
);

manifest[c.id] = assetFilename;
}

return c.chunks.reduce(registerChunk, manifest);
},
{});
const cGroups = Array.from(c.groupsIterable);
const cGroupsChildren = cGroups.map(group => group.chunks);
const unsortedChunks = cGroupsChildren.reduce(
(chunksArray, childrens) => chunksArray.concat(childrens),
[]);

this.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";
const chunks = Array.from(new Set(unsortedChunks));

return chunks.reduce(registerChunk, manifest);
}, {});

compilation.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";

compilation.assets[manifestFilename] = new RawSource(
JSON.stringify(chunkManifest)
Expand All @@ -69,16 +75,16 @@ class ChunkManifestPlugin {
});
});

compiler.plugin("compilation", compilation => {
compiler.hooks.compilation.tap("ChunkManifestPlugin", compilation => {
const mainTemplate = compilation.mainTemplate;
mainTemplate.plugin("require-ensure", function(
mainTemplate.hooks.requireEnsure.tap("ChunkManifestPlugin", (
source,
chunk,
hash,
chunkIdVariableName
) {
) => {
if (chunkFilename) {
this.outputOptions.chunkFilename = chunkFilename;
compilation.outputOptions.chunkFilename = chunkFilename;
}

const updatedSource = source.replace(
Expand Down
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ class InlineChunkManifestHtmlWebpackPlugin {
const chunkManifestVariable = this.chunkManifestVariable;
const dropAsset = this.dropAsset;

compiler.plugin("emit", (compilation, callback) => {
compiler.hooks.emit.tapAsync("InlineChunkManifestHtmlWebpackPlugin", (compilation, callback) => {
if (dropAsset) {
delete compilation.assets[manifestFilename];
}

callback();
});

compiler.plugin("compilation", compilation => {
compilation.plugin(
"html-webpack-plugin-alter-asset-tags",
compiler.hooks.compilation.tap("InlineChunkManifestHtmlWebpackPlugin", compilation => {
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync("InlineChunkManifestHtmlWebpackPlugin",
(htmlPluginData, callback) => {
const asset = compilation.assets[manifestFilename];

Expand All @@ -82,8 +81,7 @@ class InlineChunkManifestHtmlWebpackPlugin {
}
);

compilation.plugin(
"html-webpack-plugin-before-html-generation",
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync("InlineChunkManifestHtmlWebpackPlugin",
(htmlPluginData, callback) => {
const asset = compilation.assets[manifestFilename];

Expand Down

0 comments on commit 0628028

Please sign in to comment.