Skip to content

Commit

Permalink
Merge pull request #18 from arundo/inline-manifest-option
Browse files Browse the repository at this point in the history
Inline manifest option
  • Loading branch information
spadgos authored Apr 21, 2017
2 parents 05d5890 + f19f911 commit bd40246
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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);
});
}
});
};

0 comments on commit bd40246

Please sign in to comment.