danbucholtz / string-replace-loader

Replace loader for Webpack

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Replace loader for Webpack

Perform replacements (plain and regular expression) in the contents loaded by the loader.

Install:

$ npm install --save-dev string-replace-loader

Usage:

Plain replacement:

In your webpack.config.js:

module.exports = {
  // ...
  module: {
    loaders: [
      {
        test: /fileInWhichJQueryIsUndefined\.js$/,
        loader: 'string-replace',
        query: {
          search: 'jQuery',
          replace: 'window.$'
        }
      }
    ]
  }
}

RegEx replacement:

To achieve regular expression replacement you should specify the flags query param (as an empty string if you do not want any flags). In this case, search and flags are being passed to the RegExp constructor.

In your webpack.config.js:

module.exports = {
  // ...
  module: {
    loaders: [
      {
        test: /fileInWhichJQueryIsUndefined\.js$/,
        loader: 'string-replace',
        query: {
          search: 'jquery',
          replace: 'window.$',
          flags: 'i'
        }
      }
    ]
  }
}

Multiple replacement:

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

In your webpack.config.js:

module.exports = {
  // ...
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'string-replace',
        query: {
          multiple: [
             { search: 'jQuery', replace: 'window.$' },
             { search: '_', replace: 'window.lodash' }
          ]
        }
      }
    ]
  }
}

Contributing:

Feel free to open issues to propose stuff and participate. Pull requests are also welcome.

Licence:

MIT

About

Replace loader for Webpack

License:MIT License


Languages

Language:JavaScript 100.0%