UNPKG

912 BJavaScriptView Raw
1// copy from [react-dev-utils](https://github.com/facebookincubator/create-react-app/tree/master/packages/react-dev-utils)
2
3const escapeStringRegexp = require('escape-string-regexp');
4
5class 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 // Run HTML through a series of user-specified string replacements.
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
29module.exports = InterpolateHtmlPlugin;
\No newline at end of file