1.0.3 • Published 3 years ago

improved-pattern-replace-loader v1.0.3

Weekly downloads
160
License
MIT
Repository
github
Last release
3 years ago

🔬 pattern-replace-loader

Pattern replace loader for webpack

Perform plain string and regular expressions.

Install:

$ npm install --save-dev improved-pattern-replace-loader

Usage:

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.

Plain replacement:

In your webpack.config.js:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /filename\.js$/,
        loader: 'improved-pattern-replace-loader',
        options: {
          search: '[variable]',
          replace: 'Hello'
        }
      }
    ]
  }
}

RegExp replacement:

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:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /filename\.js$/,
        loader: 'improved-pattern-replace-loader',
        options: {
          search: '[variable]',
          replace: 'Hello',
          flags: 'gi'
        }
      }
    ]
  }
}

Multiple replacement:

Also, you can pass an array of objects of search/replace pairs this way:

In your webpack.config.js:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'improved-pattern-replace-loader',
        options: {
          multiple: [
             { search: '[variable1]', replace: 'Hello' },
             { search: '[variable2]', replace: 'Bye!' }
          ]
        }
      }
    ]
  }
}

Verbose output:

You can enable verbose output to check which files have been processed by the replace loader. In your webpack.config.js:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /filename\.js$/,
        loader: 'improved-pattern-replace-loader',
        options: {
          verbose: true,
          search: '[variable]',
          replace: 'Hello'
        }
      }
    ]
  }
}

Contributing:

Feel free to open issues to propose stuff and participate.

Pull requests are also welcome.

Licence:

MIT

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago