This repository was archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (110 loc) · 5.82 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var assert = require('assert');
var PLUGIN_NAME = 'HtmlWebpackEventPlugin';
function HtmlWebpackEventPlugin(options) {
assert.equal(options, undefined, PLUGIN_NAME + ' does not accept any options');
}
HtmlWebpackEventPlugin.prototype.apply = function (compiler) {
var self = this;
// webpack 4
if (compiler.hooks) {
compiler.hooks.compilation.tap(PLUGIN_NAME, function (compilation) {
// Let plugins alter the chunks and the chunk sorting
compilation.hooks.htmlWebpackPluginAlterChunks.tap(PLUGIN_NAME, function (chunks, htmlPluginData) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.alterChunks : null;
var ret = self.callEventFun(eventFun, htmlPluginData, chunks);
return ret.data;
});
// Allow plugins to make changes to the assets before invoking the template
// This only makes sense to use if `inject` is `false`
compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync(PLUGIN_NAME, function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.beforeHtmlGeneration : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Allow plugins to change the html before assets are injected
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(PLUGIN_NAME, function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.beforeHtmlProcessing : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Allow plugins to change the assetTag definitions
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(PLUGIN_NAME, function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.alterAssetTags : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Allow plugins to change the html after assets are injected
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(PLUGIN_NAME, function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.afterHtmlProcessing : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Let other plugins know that we are done:
compilation.hooks.htmlWebpackPluginAfterEmit.tapAsync(PLUGIN_NAME, function (htmlPluginData, callback) {
// htmlPluginData === undefined
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.afterEmit : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
});
}
else {
compiler.plugin('compilation', function (compilation) {
// Let plugins alter the chunks and the chunk sorting
compilation.plugin('html-webpack-plugin-alter-chunks', function (chunks, htmlPluginData) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.alterChunks : null;
var ret = self.callEventFun(eventFun, htmlPluginData, chunks);
return ret.data;
});
// Allow plugins to make changes to the assets before invoking the template
// This only makes sense to use if `inject` is `false`
compilation.plugin('html-webpack-plugin-before-html-generation', function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.beforeHtmlGeneration : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Allow plugins to change the html before assets are injected
compilation.plugin('html-webpack-plugin-before-html-processing', function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.beforeHtmlProcessing : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Allow plugins to change the assetTag definitions
compilation.plugin('html-webpack-plugin-alter-asset-tags', function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.alterAssetTags : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Allow plugins to change the html after assets are injected
compilation.plugin('html-webpack-plugin-after-html-processing', function (htmlPluginData, callback) {
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.afterHtmlProcessing : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
// Let other plugins know that we are done:
compilation.plugin('html-webpack-plugin-after-emit', function (htmlPluginData, callback) {
// htmlPluginData === undefined
var eventFun = htmlPluginData ? htmlPluginData.plugin.options.afterEmit : null;
var ret = self.callEventFun(eventFun, htmlPluginData, htmlPluginData);
callback(ret.error, ret.data);
});
});
}
};
HtmlWebpackEventPlugin.prototype.callEventFun = function (eventFun, htmlPluginData, defaultData) {
var ret = {
error: null,
data: null
};
if (typeof eventFun === 'function') {
try {
ret.data = eventFun(htmlPluginData, defaultData);
} catch (error) {
ret.error = error;
}
} else {
ret.data = defaultData;
}
return ret;
};
module.exports = HtmlWebpackEventPlugin;