html-replace-webpack-plugin
Advanced tools
Comparing version 2.5.6 to 2.6.0
35
index.js
@@ -10,24 +10,17 @@ function HtmlReplaceWebpackPlugin(options) { | ||
this.replace = function(htmlPluginData, callback) { | ||
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 | ||
throw new Error('Invalid `pattern` option provided, it must be a valid regex.') | ||
} | ||
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 | ||
htmlPluginData.html = htmlPluginData.html.replace(matches[0], replacement) | ||
} | ||
htmlPluginData.html = htmlPluginData.html.replace(option.pattern, option.replacement) | ||
} else { | ||
if (option.pattern instanceof RegExp) | ||
htmlPluginData.html = htmlPluginData.html.replace(option.pattern, option.replacement) | ||
else htmlPluginData.html = htmlPluginData.html.split(option.pattern).join(option.replacement) | ||
htmlPluginData.html = htmlPluginData.html.replace(option.pattern, option.replacement) | ||
else | ||
htmlPluginData.html = htmlPluginData.html.split(option.pattern).join(option.replacement) | ||
} | ||
@@ -40,7 +33,10 @@ }) | ||
HtmlReplaceWebpackPlugin.prototype.apply = function(compiler) { | ||
HtmlReplaceWebpackPlugin.prototype.apply = function (compiler) { | ||
if (compiler.hooks) { | ||
compiler.hooks.compilation.tap('HtmlReplaceWebpackPlugin', compilation => { | ||
if (compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) { | ||
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlReplaceWebpackPlugin', this.replace) | ||
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync( | ||
'HtmlReplaceWebpackPlugin', | ||
this.replace | ||
) | ||
} else { | ||
@@ -50,6 +46,11 @@ var HtmlWebpackPlugin = require('html-webpack-plugin') | ||
if (!HtmlWebpackPlugin) { | ||
throw new Error('Please ensure that `html-webpack-plugin` was placed before `html-replace-webpack-plugin` in your Webpack config if you were working with Webpack 4.x!') | ||
throw new Error( | ||
'Please ensure that `html-webpack-plugin` was placed before `html-replace-webpack-plugin` in your Webpack config if you were working with Webpack 4.x!' | ||
) | ||
} | ||
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync('HtmlReplaceWebpackPlugin', this.replace) | ||
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync( | ||
'HtmlReplaceWebpackPlugin', | ||
this.replace | ||
) | ||
} | ||
@@ -56,0 +57,0 @@ }) |
{ | ||
"name": "html-replace-webpack-plugin", | ||
"version": "2.5.6", | ||
"version": "2.6.0", | ||
"description": "A Webpack plugin for replace HTML contents with custom pattern string or regex.", | ||
@@ -29,3 +29,6 @@ "main": "index.js", | ||
"2.2.7": "unpublished@2017-09-15 11:22" | ||
}, | ||
"devDependencies": { | ||
"html-webpack-plugin": "^4.5.0" | ||
} | ||
} |
5
54
8677
1