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

Inline manifest option #18

Merged
merged 4 commits into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = {
plugins: [
new ChunkManifestPlugin({
filename: "manifest.json",
manifestVariable: "webpackManifest"
manifestVariable: "webpackManifest",
inlineManifest: false
})
]
};
Expand All @@ -42,3 +43,15 @@ Where the manifest will be exported to on bundle compilation. This will be relat
#### `manifestVariable`

What JS variable on the client webpack should refer to when requiring chunks. Default = `"webpackManifest"`

#### `inlineManifest`

Whether or not to write the manifest output into the [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin). Default = `false`

```html
// index.ejs
<body>
<!-- app -->
<%= htmlWebpackPlugin.files.webpackManifest %>
</body>
```
11 changes: 10 additions & 1 deletion lib/ChunkManifestPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ function ChunkManifestPlugin(options) {
options = options || {};
this.manifestFilename = options.filename || "manifest.json";
this.manifestVariable = options.manifestVariable || "webpackManifest";
this.inlineManifest = options.inlineManifest || false;
}
module.exports = ChunkManifestPlugin;

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;
var chunkManifest;

if (filename) {
chunkManifest = [chunk].reduce(function registerChunk(manifest, c) {
Expand Down Expand Up @@ -52,5 +54,12 @@ ChunkManifestPlugin.prototype.apply = function(compiler) {
return _.replace("\"__CHUNK_MANIFEST__\"",
"window[\"" + manifestVariable + "\"][" + chunkIdVar + "]");
});

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);
});
}
});
};