html-replace-webpack-plugin
Advanced tools
Comparing version 2.5.3 to 2.5.5
@@ -21,3 +21,3 @@ function HtmlReplaceWebpackPlugin(options) { | ||
if (!isPatternValid) 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) { | ||
@@ -42,3 +42,7 @@ var replacement = option.replacement.apply(null, matches) | ||
compiler.hooks.compilation.tap('HtmlReplaceWebpackPlugin', compilation => { | ||
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('html-webpack-plugin-before-html-processing', this.replace) | ||
if (compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing) { | ||
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('HtmlReplaceWebpackPlugin', this.replace) | ||
} else { | ||
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!') | ||
} | ||
}) | ||
@@ -45,0 +49,0 @@ } else { |
{ | ||
"name": "html-replace-webpack-plugin", | ||
"version": "2.5.3", | ||
"version": "2.5.5", | ||
"description": "A Webpack plugin for replace HTML contents with custom pattern string or regex.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# [html-replace-webpack-plugin] | ||
A Webpack plugin for replace HTML contents with custom pattern string or regex. | ||
## Examples | ||
https://github.com/iminif/html-replace-webpack-plugin-howto | ||
## :green_heart: Special Note! :eyes: | ||
This plugin works together with [html-webpack-plugin]! | ||
@@ -12,6 +15,6 @@ | ||
First, install `html-replace-webpack-plugin` as a development dependency: | ||
First of all, you need both `html-webpack-plugin` and `html-replace-webpack-plugin`. | ||
```shell | ||
npm i -D html-replace-webpack-plugin | ||
npm i -D html-webpack-plugin html-replace-webpack-plugin | ||
``` | ||
@@ -22,2 +25,5 @@ | ||
### In your `webpack.config.js` file: | ||
> :green_heart: 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! | ||
```javascript | ||
@@ -29,5 +35,5 @@ var webpack = require('webpack') | ||
const resource = { | ||
js: {'bootstrap': '//cdn/bootstrap/bootstrap.min.js'}, | ||
css: {'bootstrap': '//cdn/bootstrap/bootstrap.min.css'}, | ||
img:{'the-girl': '//cdn/img/the-girl.jpg'} | ||
js: { bootstrap: '//cdn/bootstrap/bootstrap.min.js' }, | ||
css: { bootstrap: '//cdn/bootstrap/bootstrap.min.css' }, | ||
img: { 'the-girl': '//cdn/img/the-girl.jpg' } | ||
} | ||
@@ -44,29 +50,32 @@ | ||
plugin: [ | ||
new HtmlWebpackPlugin({ | ||
/* configs */ | ||
}), | ||
// Replace html contents with string or regex patterns | ||
new HtmlReplaceWebpackPlugin([ | ||
{ | ||
pattern: 'foo', | ||
replacement: '`foo` has been replaced with `bar`' | ||
}, | ||
{ | ||
pattern: '@@title', | ||
replacement: 'html replace webpack plugin' | ||
}, | ||
{ | ||
pattern: /(<!--\s*|@@)(css|js|img):([\w-\/]+)(\s*-->)?/g, | ||
replacement: function(match, $1, type, file, $4, index, input) | ||
{ | ||
// those formal parameters could be: | ||
// match: <-- css:bootstrap--> | ||
// type: css | ||
// file: bootstrap | ||
// Then fetch css link from some resource object | ||
// var url = resources['css']['bootstrap'] | ||
pattern: 'foo', | ||
replacement: '`foo` has been replaced with `bar`' | ||
}, | ||
{ | ||
pattern: '@@title', | ||
replacement: 'html replace webpack plugin' | ||
}, | ||
{ | ||
pattern: /(<!--\s*|@@)(css|js|img):([\w-\/]+)(\s*-->)?/g, | ||
replacement: function(match, $1, type, file, $4, index, input) { | ||
// those formal parameters could be: | ||
// match: <-- css:bootstrap--> | ||
// type: css | ||
// file: bootstrap | ||
// Then fetch css link from some resource object | ||
// var url = resources['css']['bootstrap'] | ||
var url = resource[type][file] | ||
var url = resource[type][file] | ||
// $1==='@@' <--EQ--> $4===undefined | ||
return $4 == undefined ? url : tpl[type].replace('%s', url) | ||
// $1==='@@' <--EQ--> $4===undefined | ||
return $4 == undefined ? url : tpl[type].replace('%s', url) | ||
} | ||
} | ||
}]) | ||
]) | ||
] | ||
@@ -77,2 +86,3 @@ } | ||
#### In your `src/index.html` file: | ||
```html | ||
@@ -94,2 +104,3 @@ <!DOCTYPE html> | ||
#### After replacing, in the `dist/index.html` file: | ||
```html | ||
@@ -110,14 +121,19 @@ <html lang="en"> | ||
## API | ||
html-replace-webpack-plugin can be called with an objects array or an object. | ||
### Options for `html-replace-webpack-plugin` | ||
new HtmlReplaceWebpackPlugin([obj1[, obj2[, obj3[, ...[, objN]]]]] | obj) | ||
#### [obj1[, obj2[, obj3[, ...[, objN]]]]] | obj | ||
Type: `Objects Array` | `Object` | ||
#### obj1, obj2, obj3, ..., objN | obj | ||
Type: `Object` | ||
#### obj.pattern | ||
Type: `String` | `RegExp` | ||
@@ -128,2 +144,3 @@ | ||
#### obj.replacement | ||
Type: `String` | `Function` | ||
@@ -135,3 +152,3 @@ | ||
[html-replace-webpack-plugin]: https://www.npmjs.com/package/html-replace-webpack-plugin | ||
[MDN documentation for RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp | ||
[MDN documentation for String.replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter | ||
[mdn documentation for regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp | ||
[mdn documentation for string.replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter |
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
8212
47
146