Replace loader for Webpack
npm install @clayne/string-replace-loaderPerform replacements (plain and regular expression) in the contents loaded by the loader.
``bash`
$ npm install --save-dev string-replace-loader
In general, loader allows to perform replacements in a way String.prototype.replace() does (loader uses it internally). For instance, it means that if you want to replace all occurences, you should use RegExp in query.search with g flag in query.flags, etc.
In your webpack.config.js:
`javascript`
module.exports = {
// ...
module: {
loaders: [
{
test: /fileInWhichJQueryIsUndefined\.js$/,
loader: 'string-replace',
query: {
search: 'jQuery',
replace: 'window.$'
}
}
]
}
}
To achieve regular expression replacement you should specify the flags query paramsearch
(as an empty string if you do not want any flags). In this case, and flags are being
passed to the RegExp constructor.
In your webpack.config.js:
`javascript`
module.exports = {
// ...
module: {
loaders: [
{
test: /fileInWhichJQueryIsUndefined\.js$/,
loader: 'string-replace',
query: {
search: 'jquery',
replace: 'window.$',
flags: 'i'
}
}
]
}
}
Also, you can pass an array of search-replace pairs this way:
In your webpack.config.js:
`javascript``
module.exports = {
// ...
module: {
loaders: [
{
test: /\.js$/,
loader: 'string-replace',
query: {
multiple: [
{ search: 'jQuery', replace: 'window.$' },
{ search: '_', replace: 'window.lodash' }
]
}
}
]
}
}
Feel free to open issues to propose stuff and participate. Pull requests are also welcome.