pattern replace loader for webpack 2
npm install pattern-replace-loaderPerform plain string and regular expressions.
``bash`
$ npm install --save-dev pattern-replace-loader
or using yarn
`bash`
$ yarn add pattern-replace-loader --dev
Plain: It uses String.prototype.replace() to perform replaces in file contents.
Regex: It will go and look for all the occurrences of what you've specified in options.search with g flag in options.flags, etc.
In your webpack.config.js:
`javascript`
module.exports = {
// ...
module: {
rules: [
{
test: /filename\.js$/,
loader: 'pattern-replace-loader',
options: {
search: '[variable]',
replace: 'Hello'
}
}
]
}
}
To be able to use RegExp in yuor replacement you should specify flags in the options param. In this case, search and flags are being
passed to the RegExp constructor.
In your webpack.config.js:
`javascript`
module.exports = {
// ...
module: {
rules: [
{
test: /filename\.js$/,
loader: 'pattern-replace-loader',
options: {
search: '[variable]',
replace: 'Hello',
flags: 'gi'
}
}
]
}
}
Also, you can pass an array of objects of search/replace pairs this way:
In your webpack.config.js:
`javascript`
module.exports = {
// ...
module: {
rules: [
{
test: /\.js$/,
loader: 'pattern-replace-loader',
options: {
multiple: [
{ search: '[variable1]', replace: 'Hello' },
{ search: '[variable2]', replace: 'Bye!' }
]
}
}
]
}
}
:`javascript
module.exports = {
// ...
module: {
rules: [
{
test: /filename\.js$/,
loader: 'pattern-replace-loader',
options: {
verbose: true,
search: '[variable]',
replace: 'Hello'
}
}
]
}
}
``Feel free to open issues to propose stuff and participate.
Pull requests are also welcome.
Thanks goes to these wonderful people (emoji key):
Alejandro Garcia Anglada 💻 🤔 | mathiasscheffe 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!