1 |
|
2 |
|
3 | const escapeStringRegexp = require('escape-string-regexp');
|
4 |
|
5 | class InterpolateHtmlPlugin {
|
6 | constructor(replacements) {
|
7 | this.replacements = replacements;
|
8 | }
|
9 |
|
10 | apply(compiler) {
|
11 | compiler.plugin('compilation', compilation => {
|
12 | compilation.plugin('html-webpack-plugin-before-html-processing',
|
13 | (data, callback) => {
|
14 |
|
15 | Object.keys(this.replacements).forEach(key => {
|
16 | const value = this.replacements[key];
|
17 | data.html = data.html.replace(
|
18 | new RegExp('%' + escapeStringRegexp(key) + '%', 'g'),
|
19 | value
|
20 | );
|
21 | });
|
22 | callback(null, data);
|
23 | }
|
24 | );
|
25 | });
|
26 | }
|
27 | }
|
28 |
|
29 | module.exports = InterpolateHtmlPlugin; |
\ | No newline at end of file |