-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (70 loc) · 2.48 KB
/
index.js
File metadata and controls
72 lines (70 loc) · 2.48 KB
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
"use strict";
const _ = require("lodash");
module.exports = class Html4ServerWebpackPlugin {
constructor(options) {
this.options = _.extend({}, options || {});
}
apply(compiler) {
// Hook into the html-webpack-plugin processing
// webpack 4+ comes with a new plugin system
if (compiler.hooks) {
compiler.hooks.compilation.tap('Html4ServerWebpackPlugin', compilation => {
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
"Html4ServerWebpackPlugin",
(htmlPluginData, callback) => {
this.writeAssetToDisk(
compilation,
htmlPluginData.plugin.options,
htmlPluginData,
callback
);
}
);
});
} else {
compiler.plugin("compilation", compilation => {
compilation.plugin(
"html-webpack-plugin-after-html-processing",
(htmlPluginData, callback) => {
this.writeAssetToDisk(
compilation,
htmlPluginData.plugin.options,
htmlPluginData,
callback
);
}
);
});
}
}
writeAssetToDisk(
compilation,
htmlWebpackPluginOptions,
htmlPluginData,
callback
) {
const { outputName, html } = htmlPluginData;
if (htmlWebpackPluginOptions.html4ServerPlugin) {
const {
templateData,
filename
} = htmlWebpackPluginOptions.html4ServerPlugin;
const assetsName = filename;
const tmlFunction = _.template(html);
const newHtml = tmlFunction(templateData);
const source = "const serialize = require('serialize-javascript'); module.exports = " + tmlFunction;
compilation.assets[assetsName] = {
source: function() {
return source;
},
size: function() {
return Buffer.byteLength(source, "utf8");
}
};
htmlPluginData.html = newHtml;
callback(null, htmlPluginData);
} else {
return callback(null);
}
}
};