html-replace-webpack-plugin
Advanced tools
Comparing version 2.2.8 to 2.5.0
74
index.js
@@ -1,10 +0,6 @@ | ||
function HtmlReplaceWebpackPlugin(options) | ||
{ | ||
function HtmlReplaceWebpackPlugin(options) { | ||
options = Array.isArray(options) ? options : [options] | ||
options.forEach(function(option) | ||
{ | ||
if(typeof option.pattern == 'undefined' || | ||
typeof option.replacement == 'undefined') | ||
{ | ||
options.forEach(option => { | ||
if (typeof option.pattern == 'undefined' || typeof option.replacement == 'undefined') { | ||
throw new Error('Both `pattern` and `replacement` options must be defined!') | ||
@@ -14,50 +10,42 @@ } | ||
this.replace = function(htmlData) | ||
{ | ||
options.forEach(function(option) | ||
{ | ||
if(typeof option.replacement === 'function') | ||
{ | ||
var matches = null; | ||
var isPatternValid = true; | ||
try { | ||
new RegExp(option.pattern); | ||
} catch(e) { | ||
isPatternValid = false; | ||
} | ||
this.replace = function(htmlPluginData, callback) { | ||
options.forEach(option => { | ||
if (typeof option.replacement === 'function') { | ||
var matches = null | ||
var isPatternValid = true | ||
try { | ||
new RegExp(option.pattern) | ||
} catch (e) { | ||
isPatternValid = false | ||
} | ||
if(!isPatternValid) throw new Error("Invalid `pattern` option provided, it must be a valid regex."); | ||
while((matches = option.pattern.exec(htmlData)) != null) | ||
{ | ||
if (!isPatternValid) throw new Error("Invalid `pattern` option provided, it must be a valid regex.") | ||
while ((matches = option.pattern.exec(htmlPluginData.html)) != null) { | ||
var replacement = option.replacement.apply(null, matches) | ||
// matches[0]: matching content string | ||
htmlData = htmlData.replace(matches[0], replacement) | ||
htmlPluginData.html = htmlPluginData.html.replace(matches[0], replacement) | ||
} | ||
} else { | ||
// htmlPluginData.html.replace(option.pattern, option.replacement) | ||
htmlPluginData.html = htmlPluginData.html.split(option.pattern).join(option.replacement) | ||
} | ||
else | ||
{ | ||
// htmlData = htmlData.replace(option.pattern, option.replacement) | ||
htmlData = htmlData.split(option.pattern).join(option.replacement) | ||
} | ||
}) | ||
return htmlData | ||
callback(null, htmlPluginData) | ||
} | ||
} | ||
HtmlReplaceWebpackPlugin.prototype.apply = function(compiler) | ||
{ | ||
var _this = this | ||
compiler.plugin('compilation', function(compilation) | ||
{ | ||
// console.log('The compiler is starting a new compilation...') | ||
compilation.plugin('html-webpack-plugin-before-html-processing', | ||
function(htmlPluginData, callback) | ||
{ | ||
htmlPluginData.html = _this.replace(htmlPluginData.html) | ||
callback(null, htmlPluginData) | ||
}) | ||
}) | ||
HtmlReplaceWebpackPlugin.prototype.apply = function(compiler) { | ||
if (compiler.hooks) { | ||
compiler.hooks.compilation.tap('HtmlReplaceWebpackPlugin', compilation => { | ||
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('html-webpack-plugin-before-html-processing', this.replace) | ||
}) | ||
} else { | ||
compiler.plugin('compilation', compilation => { | ||
compilation.plugin('html-webpack-plugin-before-html-processing', this.replace) | ||
}) | ||
} | ||
} | ||
module.exports = HtmlReplaceWebpackPlugin |
{ | ||
"name": "html-replace-webpack-plugin", | ||
"version": "2.2.8", | ||
"version": "2.5.0", | ||
"description": "A Webpack plugin for replace HTML contents with custom pattern string or regex.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7425
43