Giter Site home page Giter Site logo

string-replace-loader's Introduction

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:

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.

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' }
          ]
        }
      }
    ]
  }
}

Strict mode replacement:

You can set strict mode to ensure that the replacement was done:

In your webpack.config.js:

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

Contributing:

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

Licence:

MIT

string-replace-loader's People

Contributors

donaldpipowitch avatar kennydude avatar sashless avatar valeryvs avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.