All Projects → Va1 → String Replace Loader

Va1 / String Replace Loader

Licence: mit
Replace loader for Webpack

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to String Replace Loader

Node Addon Loader
A loader for node native addons
Stars: ✭ 17 (-91.71%)
Mutual labels:  webpack, webpack2, webpack-loader
Webpack.js.org
Repository for webpack documentation and more!
Stars: ✭ 2,049 (+899.51%)
Mutual labels:  webpack, webpack2, webpack-loader
Webpack Core Usage
webpack2完整系列课程,欢迎阅读。同时欢迎移步我的react全家桶文章全集: https://github.com/liangklfangl/react-article-bucket
Stars: ✭ 94 (-54.15%)
Mutual labels:  webpack, webpack2, webpack-loader
Markdown Loader
markdown loader for webpack
Stars: ✭ 335 (+63.41%)
Mutual labels:  webpack, webpack2, webpack-loader
Bootstrap Loader
Load Bootstrap styles and scripts in your Webpack bundle
Stars: ✭ 1,038 (+406.34%)
Mutual labels:  webpack, webpack2, webpack-loader
Webpack Conditional Loader
C conditionals directive for JavaScript
Stars: ✭ 93 (-54.63%)
Mutual labels:  webpack, webpack2, webpack-loader
Svg Sprite Loader
Webpack loader for creating SVG sprites.
Stars: ✭ 1,822 (+788.78%)
Mutual labels:  webpack, webpack2, webpack-loader
Angularjs Typescript Webpack
AngularJS 1.7, typescript 3 and webpack 4 starter project based on angular tutorial
Stars: ✭ 150 (-26.83%)
Mutual labels:  webpack, webpack2
File Loader
File Loader
Stars: ✭ 1,846 (+800.49%)
Mutual labels:  webpack, webpack-loader
Webpack2
基于webpack2和vue.js2构建饿了么多页面应用 🌹
Stars: ✭ 165 (-19.51%)
Mutual labels:  webpack, webpack2
Multipage Webpack Plugin
A plugin that makes handling templates and asset distribution for multi-page applications using webpack trivial
Stars: ✭ 168 (-18.05%)
Mutual labels:  webpack, webpack2
Azure Functions Pack
Easily package your Node.js Functions for Azure Functions
Stars: ✭ 149 (-27.32%)
Mutual labels:  webpack, webpack2
Devopenclub Tech Webpack2
Webpack 2 视频教程课程源码
Stars: ✭ 148 (-27.8%)
Mutual labels:  webpack, webpack2
React Redux Styled Hot Universal
react boilerplate used best practices and focus on performance
Stars: ✭ 147 (-28.29%)
Mutual labels:  webpack, webpack2
Bs Loader
📻 Bucklescript loader for Webpack and Jest
Stars: ✭ 146 (-28.78%)
Mutual labels:  webpack, webpack-loader
Grex
A command-line tool and library for generating regular expressions from user-provided test cases
Stars: ✭ 4,847 (+2264.39%)
Mutual labels:  regular-expression, regexp
Html Res Webpack Plugin
plugin for generating html in webpack
Stars: ✭ 170 (-17.07%)
Mutual labels:  webpack, webpack2
Pug As Jsx Loader
Stars: ✭ 168 (-18.05%)
Mutual labels:  webpack, webpack-loader
Webpack2 Lessons
📖《webpack2 包教不包会》
Stars: ✭ 187 (-8.78%)
Mutual labels:  webpack, webpack2
Css Modules Typescript Loader
Webpack loader to create TypeScript declarations for CSS Modules
Stars: ✭ 172 (-16.1%)
Mutual labels:  webpack, webpack-loader

Replace loader for Webpack

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

Install:

$ yarn add --dev string-replace-loader

With release of 2.0.0 the loader is expected to be used in Node v4+ environment. Support for Node v3 and lower was dropped, but you can install and use the loader version of 1.3.0 in older environments.

With release of 3.0.0 the loader is expected to be used with Webpack v5+. Support for Webpack v4 and lower was dropped, but you can install and use the loader version of 2.3.0 in older environments.

Usage:

Loader allows to perform replacements in a way String.prototype.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:

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

RegEx replacement:

To achieve regular expression replacement you should either specify the search option as RegExp instance, either specify it as string and add the flags option (as an empty string if you do not want any flags). In the latter case, search and flags are being passed to the 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:

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

or

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

Multiple replacement:

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

In your webpack.config.js:

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

Callback replacement

You can specify a callback function to have dynamic replacement values.

In your webpack.config.js:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'string-replace-loader',
        options: {
          search: '^Hello, (.*)!$',
          replace: (match, p1, offset, string) => `Bonjour, ${p1.toUpperCase()}!`,
          flags: 'g'
        }
      }
    ]
  }
}

Strict mode replacement:

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:

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

Contributing:

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

Licence:

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].