Skip to content

Commit

Permalink
version 2.1.1; readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Va1 committed Feb 25, 2018
1 parent a6555e1 commit bb1fd01
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@ Support for Node v3 and lower was dropped, but you can install and use the loade

## Usage:

In general, loader allows to perform replacements in a way [String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) does (loader uses it internally).
For instance, it means that if you want to replace all occurrences, you should use RegExp-like string in `query.search` with `g` flag in `query.flags`, etc.
Loader allows to perform replacements in a way [String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) does (loader uses it internally).
It means that if you want to replace all occurrences, you should use RegExp-like string in `options.search` with `g` flag in `options.flags`, etc.

### Plain replacement:

Plain string replacement, no need to escape RegEx special characters.

In your `webpack.config.js`:

```javascript
module.exports = {
// ...
module: {
loaders: [
rules: [
{
test: /fileInWhichJQueryIsUndefined\.js$/,
loader: 'string-replace-loader',
query: {
search: 'jQuery',
replace: 'window.$'
options: {
search: '$',
replace: 'window.jQuery',
}
}
]
Expand All @@ -40,23 +42,24 @@ module.exports = {

### RegEx replacement:

To achieve regular expression replacement you should specify the `flags` query param
To achieve regular expression replacement you should specify the `flags` option
(as an empty string if you do not want any flags). In this case, `search` and `flags` are being
passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor.
passed to the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor
and this means that you should escape RegEx special characters in `search` if you want it to be replaced as a string.

In your `webpack.config.js`:

```javascript
module.exports = {
// ...
module: {
loaders: [
rules: [
{
test: /fileInWhichJQueryIsUndefined\.js$/,
loader: 'string-replace-loader',
query: {
search: 'jquery',
replace: 'window.$',
options: {
search: '\$',
replace: 'window.jQuery',
flags: 'i'
}
}
Expand All @@ -75,11 +78,11 @@ In your `webpack.config.js`:
module.exports = {
// ...
module: {
loaders: [
rules: [
{
test: /\.js$/,
loader: 'string-replace-loader',
query: {
options: {
multiple: [
{ search: 'jQuery', replace: 'window.$' },
{ search: '_', replace: 'window.lodash' }
Expand All @@ -93,19 +96,20 @@ module.exports = {

### Strict mode replacement:

You can set strict mode to ensure that the replacement was done:
You can enable strict mode to ensure that the replacement was performed.
Loader will throw exception if nothing was replaced or if `search` or `replace` options were not specified.

In your `webpack.config.js`:

```javascript
module.exports = {
// ...
module: {
loaders: [
rules: [
{
test: /fileInWhichJQueryIsUndefined\.js$/,
loader: 'string-replace-loader',
query: {
options: {
search: 'jQuery',
replace: 'window.$',
strict: true
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-replace-loader",
"version": "2.1.0",
"version": "2.1.1",
"description": "Replace loader for Webpack",
"keywords": [
"webpack",
Expand Down

0 comments on commit bb1fd01

Please sign in to comment.