text-replace-html-webpack-plugin
Version:
This package is intended to be used along with html-webpack-plugin. It can be used to replace text in the html file created by the html-webpack-plugin during the webpack build process. Works only wth webpack@^4
68 lines (52 loc) • 3.02 kB
JavaScript
var _lodash = _interopRequireDefault(require("lodash.isregexp"));
var _lodash2 = _interopRequireDefault(require("lodash.isstring"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var TextReplaceHtmlWebpackPlugin =
/*#__PURE__*/
function () {
function TextReplaceHtmlWebpackPlugin(options) {
_classCallCheck(this, TextReplaceHtmlWebpackPlugin);
this.options = options;
}
_createClass(TextReplaceHtmlWebpackPlugin, [{
key: "apply",
value: function apply(compiler) {
var _this = this;
compiler.hooks.compilation.tap('TextReplaceHtmlWebpackPlugin', function (compilation) {
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('TextReplaceHtmlWebpackPlugin', function (data, cb) {
var replacementArray = _this.options.replacementArray;
if (Array.isArray(replacementArray)) {
replacementArray.forEach(function (pattern) {
var searchString = pattern.searchString,
replace = pattern.replace,
regex = pattern.regex;
if (searchString && regex) {
compilation.warnings.push(new Error("TextReplaceHtmlWebpackPlugin => Provide either searchString (".concat(searchString, ") or regex (").concat(regex, "). If both are provided, regex will take precedence.")));
}
if (regex) {
if ((0, _lodash.default)(regex)) {
data.html = data.html.replace(regex, replace);
} else {
compilation.warnings.push(new Error("TextReplaceHtmlWebpackPlugin => regex : ".concat(regex, " : Invalid regex supplied.")));
}
} else if (searchString) {
if ((0, _lodash2.default)(searchString)) {
data.html = data.html.replace(new RegExp(searchString, "g"), replace);
} else {
compilation.warnings.push(new Error("TextReplaceHtmlWebpackPlugin => searchString : ".concat(searchString, " : Invalid searchString supplied.")));
}
}
});
}
cb(null, data);
});
});
}
}]);
return TextReplaceHtmlWebpackPlugin;
}();
module.exports = TextReplaceHtmlWebpackPlugin;
;