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

Commit 781b763

Browse files
committed
Forked chunk-manifest-webpack-plugin
1 parent 98c41d1 commit 781b763

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "inline-chunk-manifest-html-webpack-plugin",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"description": "Extension plugin for html-webpack-plugin to inline webpack chunk manifest. Default inlines in head tag.",
55
"main": "./src/index.js",
66
"repository": {
@@ -29,9 +29,7 @@
2929
"nyc": "^10.2.0",
3030
"prettier": "^1.1.0"
3131
},
32-
"dependencies": {
33-
"chunk-manifest-webpack-plugin": "~1.0.0"
34-
},
32+
"dependencies": {},
3533
"scripts": {
3634
"lint": "node_modules/.bin/eslint ./src",
3735
"test": "ava test/*-test.js",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var RawSource = require("webpack-core/lib/RawSource");
2+
3+
function ChunkManifestPlugin(options) {
4+
options = options || {};
5+
this.manifestFilename = options.filename || "manifest.json";
6+
this.manifestVariable = options.manifestVariable || "webpackManifest";
7+
this.inlineManifest = options.inlineManifest || false;
8+
}
9+
module.exports = ChunkManifestPlugin;
10+
11+
ChunkManifestPlugin.prototype.constructor = ChunkManifestPlugin;
12+
ChunkManifestPlugin.prototype.apply = function(compiler) {
13+
var manifestFilename = this.manifestFilename;
14+
var manifestVariable = this.manifestVariable;
15+
var inlineManifest = this.inlineManifest;
16+
var oldChunkFilename;
17+
var chunkManifest;
18+
19+
compiler.plugin("this-compilation", function(compilation) {
20+
var mainTemplate = compilation.mainTemplate;
21+
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
22+
var filename =
23+
this.outputOptions.chunkFilename || this.outputOptions.filename;
24+
25+
if (filename) {
26+
chunkManifest = [chunk].reduce(function registerChunk(manifest, c) {
27+
if (c.id in manifest) return manifest;
28+
var hasRuntime = typeof c.hasRuntime === "function"
29+
? c.hasRuntime()
30+
: c.entry;
31+
if (hasRuntime) {
32+
manifest[c.id] = undefined;
33+
} else {
34+
manifest[
35+
c.id
36+
] = mainTemplate.applyPluginsWaterfall("asset-path", filename, {
37+
hash: hash,
38+
chunk: c
39+
});
40+
}
41+
return c.chunks.reduce(registerChunk, manifest);
42+
}, {});
43+
oldChunkFilename = this.outputOptions.chunkFilename;
44+
this.outputOptions.chunkFilename = "__CHUNK_MANIFEST__";
45+
// mark as asset for emitting
46+
compilation.assets[manifestFilename] = new RawSource(
47+
JSON.stringify(chunkManifest)
48+
);
49+
chunk.files.push(manifestFilename);
50+
}
51+
52+
return _;
53+
});
54+
});
55+
56+
compiler.plugin("compilation", function(compilation) {
57+
compilation.mainTemplate.plugin("require-ensure", function(
58+
_,
59+
chunk,
60+
hash,
61+
chunkIdVar
62+
) {
63+
if (oldChunkFilename) {
64+
this.outputOptions.chunkFilename = oldChunkFilename;
65+
}
66+
67+
return _.replace(
68+
'"__CHUNK_MANIFEST__"',
69+
'window["' + manifestVariable + '"][' + chunkIdVar + "]"
70+
);
71+
});
72+
73+
if (inlineManifest) {
74+
compilation.plugin("html-webpack-plugin-before-html-generation", function(
75+
data,
76+
callback
77+
) {
78+
var manifestHtml =
79+
"<script>window." +
80+
manifestVariable +
81+
"=" +
82+
JSON.stringify(chunkManifest) +
83+
"</script>";
84+
callback(null, (data.assets[manifestVariable] = manifestHtml));
85+
});
86+
}
87+
});
88+
};

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
const ChunkManifestPlugin = require("chunk-manifest-webpack-plugin");
3+
const ChunkManifestPlugin = require("./chunk-manifest-webpack-plugin");
44

55
class InlineChunkManifestHtmlWebpackPlugin {
66
constructor(options) {

0 commit comments

Comments
 (0)