forked from szrenwei/inline-manifest-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (48 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const sourceMappingURL = require('source-map-url');
function InlineManifestPlugin(options) {
this.options = extend({
name: 'webpackManifest'
}, options || {})
}
InlineManifestPlugin.prototype.apply = function (compiler) {
const me = this;
compiler.hooks.compilation.tap('webpack-inline-manifest-plugin', function (compilation) {
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tap('webpack-inline-manifest-plugin', function (htmlPluginData) {
const name = me.options.name;
// HtmlWebpackPlugin use the 'manifest' name as HTML5's app cache manifest
// so we can't use the same name
if (name === 'manifest') {
throw new Error('[WebpackInlineManifestPlugin]: name can\'t be "manifest".')
}
const webpackManifest = [];
const assets = htmlPluginData.assets;
const manifestPath = (compilation.chunks.filter(function (chunk) {
return chunk.name === 'manifest'
})[0] || {files: []}).files[0];
if (manifestPath) {
webpackManifest.push('<script>');
webpackManifest.push(sourceMappingURL.removeFrom(compilation.assets[manifestPath].source()));
webpackManifest.push('</script>');
const manifestIndex = assets.js.indexOf(assets.publicPath + manifestPath);
if (manifestIndex >= 0) {
assets.js.splice(manifestIndex, 1);
delete assets.chunks.manifest
}
}
assets[name] = webpackManifest.join('')
})
})
};
function extend(base) {
const len = arguments.length;
for (let i = 1; i < len; i++) {
const obj = arguments[i];
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
base[key] = obj[key]
}
}
}
return base
}
module.exports = InlineManifestPlugin;