| [9af201e] | 1 | /**
|
|---|
| 2 | * Copyright (c) 2015-present, Facebook, Inc.
|
|---|
| 3 | *
|
|---|
| 4 | * This source code is licensed under the MIT license found in the
|
|---|
| 5 | * LICENSE file in the root directory of this source tree.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | // This webpack plugin lets us interpolate custom variables into `index.html`.
|
|---|
| 9 | // Usage: `new InterpolateHtmlPlugin(HtmlWebpackPlugin, { 'MY_VARIABLE': 42 })`
|
|---|
| 10 | // Then, you can use %MY_VARIABLE% in your `index.html`.
|
|---|
| 11 |
|
|---|
| 12 | // It works in tandem with HtmlWebpackPlugin.
|
|---|
| 13 | // Learn more about creating plugins like this:
|
|---|
| 14 | // https://github.com/ampedandwired/html-webpack-plugin#events
|
|---|
| 15 |
|
|---|
| 16 | 'use strict';
|
|---|
| 17 | const escapeStringRegexp = require('escape-string-regexp');
|
|---|
| 18 |
|
|---|
| 19 | class InterpolateHtmlPlugin {
|
|---|
| 20 | constructor(htmlWebpackPlugin, replacements) {
|
|---|
| 21 | this.htmlWebpackPlugin = htmlWebpackPlugin;
|
|---|
| 22 | this.replacements = replacements;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | apply(compiler) {
|
|---|
| 26 | compiler.hooks.compilation.tap('InterpolateHtmlPlugin', compilation => {
|
|---|
| 27 | this.htmlWebpackPlugin
|
|---|
| 28 | .getHooks(compilation)
|
|---|
| 29 | .afterTemplateExecution.tap('InterpolateHtmlPlugin', data => {
|
|---|
| 30 | // Run HTML through a series of user-specified string replacements.
|
|---|
| 31 | Object.keys(this.replacements).forEach(key => {
|
|---|
| 32 | const value = this.replacements[key];
|
|---|
| 33 | data.html = data.html.replace(
|
|---|
| 34 | new RegExp('%' + escapeStringRegexp(key) + '%', 'g'),
|
|---|
| 35 | value
|
|---|
| 36 | );
|
|---|
| 37 | });
|
|---|
| 38 | });
|
|---|
| 39 | });
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | module.exports = InterpolateHtmlPlugin;
|
|---|