Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP ported code to webpack 4 #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 90 additions & 57 deletions lib/ChunkManifestPlugin.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,99 @@
var RawSource = require("webpack-core/lib/RawSource");
const pluginName = 'ChunkManifestPlugin';

function ChunkManifestPlugin(options) {
options = options || {};
this.manifestFilename = options.filename || "manifest.json";
this.manifestVariable = options.manifestVariable || "webpackManifest";
this.inlineManifest = options.inlineManifest || false;
}
module.exports = ChunkManifestPlugin;
class ChunkManifestPlugin {
constructor(options) {
this.options = {
filename: 'manifest.json',
manifestVariable: 'webpackManifest',
inlineManifest: false,
...options,
};
}

apply(compiler) {
const chunkManifestFilename = '__CHUNK_MANIFEST__';
let chunkManifest;
let oldChunkFilename;

compiler.hooks.thisCompilation.tap(pluginName, compilation => {
const {mainTemplate} = compilation;

mainTemplate.hooks.requireEnsure.tap(
pluginName,
(source, chunk, hash) => {
const {outputOptions} = mainTemplate;
const filename =
outputOptions.chunkFilename || outputOptions.filename;

ChunkManifestPlugin.prototype.constructor = ChunkManifestPlugin;
ChunkManifestPlugin.prototype.apply = function(compiler) {
var manifestFilename = this.manifestFilename;
var manifestVariable = this.manifestVariable;
var inlineManifest = this.inlineManifest;
var oldChunkFilename;
var chunkManifest;

compiler.plugin("this-compilation", function(compilation) {
var mainTemplate = compilation.mainTemplate;
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
var filename = this.outputOptions.chunkFilename || this.outputOptions.filename;

if (filename) {
chunkManifest = [chunk].reduce(function registerChunk(manifest, c) {
if(c.id in manifest) return manifest;
var hasRuntime = typeof c.hasRuntime === 'function' ? c.hasRuntime() : c.entry;
if(hasRuntime) {
manifest[c.id] = undefined;
} else {
manifest[c.id] = mainTemplate.applyPluginsWaterfall("asset-path", filename, {
hash: hash,
chunk: c
});
if (filename) {
const registerChunk = (manifest, chunk) => {
if (chunk.id in manifest) {
return manifest;
}

if (chunk.hasRuntime()) {
manifest[chunk.id] = undefined;
} else {
manifest[chunk.id] = mainTemplate.getAssetPath(filename, {
hash,
chunk,
});
}

return Array.from(chunk.getAllAsyncChunks()).reduce(
registerChunk,
manifest,
);
};
chunkManifest = registerChunk({}, chunk);

oldChunkFilename = outputOptions.chunkFilename;
outputOptions.chunkFilename = chunkManifestFilename;

const source = JSON.stringify(chunkManifest, null, true);
compilation.assets[this.options.filename] = {
source: () => source,
size: () => source.length,
};
chunk.files.push(this.options.filename);
}
return c.chunks.reduce(registerChunk, manifest);
}, {});
oldChunkFilename = this.outputOptions.chunkFilename;
this.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";
// mark as asset for emitting
compilation.assets[manifestFilename] = new RawSource(JSON.stringify(chunkManifest));
chunk.files.push(manifestFilename);
}

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

compiler.plugin("compilation", function(compilation) {
compilation.mainTemplate.plugin("require-ensure", function(_, chunk, hash, chunkIdVar) {
if (oldChunkFilename) {
this.outputOptions.chunkFilename = oldChunkFilename;
}
compiler.hooks.compilation.tap(pluginName, compilation => {
const {mainTemplate} = compilation;

mainTemplate.hooks.requireEnsure.tap(
pluginName,
(source, chunk, hash, chunkId) => {
if (oldChunkFilename) {
mainTemplate.outputOptions.chunkFilename = oldChunkFilename;

return source.replace(
`"${chunkManifestFilename}"`,
`window['${this.options.manifestVariable}']['${chunkId}']`,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should chunkId be quoted here? I had to unquote it because I think it's referring to the name variable in scope. (I've never done Webpack hooks stuff, so I might be completely wrong here)

Copy link
Author

@malectro malectro Apr 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, my bad.

edit: actually it looks like it's quoted on L 56 in the master so probably should be as is.

);
}
},
);

if (this.options.inlineManifest) {
const {htmlWebpackPluginBeforeHtmlGeneration} = compilation.hooks;

return _.replace("\"__CHUNK_MANIFEST__\"",
"window[\"" + manifestVariable + "\"][" + chunkIdVar + "]");
if (htmlWebpackPluginBeforeHtmlGeneration) {
const {manifestVariable} = this.options;

htmlWebpackPluginBeforeHtmlGeneration.tap(pluginName, data => {
const manifestHtml = `<script>window.${manifestVariable}=${JSON.stringify(chunkManifest)}</script>`;
return data.assets[manifestVariable] = manifestHtml;
});
}
}
});
}
}

if (inlineManifest){
compilation.plugin("html-webpack-plugin-before-html-generation", function (data, callback) {
var manifestHtml = "<script>window." + manifestVariable + "=" + JSON.stringify(chunkManifest) + "</script>";
callback(null, data.assets[manifestVariable] = manifestHtml);
});
}
});
};
module.exports = ChunkManifestPlugin;