Code Monkey home page Code Monkey logo

webpack-plugin-replace's Introduction

webpack-plugin-replace Build Status

Replace content while bundling.

Install

$ npm install --save-dev webpack-plugin-replace

Usage

// webpack.config.js
const ReplacePlugin = require('webpack-plugin-replace');

module.exports = {
  // ...
  plugins: [
    new ReplacePlugin({
      exclude: [
        'foo.js',
        /node_modules/,
        filepath => filepath.includes('ignore')
      ],
      patterns: [{
        regex: /throw\s+(new\s+)?(Type|Reference)?Error\s*\(/g,
        value: 'return;('
      }],
      values: {
        'process.env.NODE_ENV': JSON.stringify('production'),
        'FOO_BAR': '"hello world"',
        'DEV_MODE': false,
      }
    })
  ]
};

API

webpack-plugin-replace(options)

options.exclude

Type: Array|String|Function|RegExp
Default: false

If multiple conditions are provided, matching any condition will exclude the filepath, which prevents any alterations.

Note: By default, nothing is excluded!

options.include

Type: Array|String|Function|RegExp
Default: true

If multiple conditions are provided, matching any condition will include & scan the filepath for eligible replacements.

Note: By default, all filepaths are included!

options.patterns

Type: Array
Default: []

An array of RegExp pattern definitions. Each definition is an object with regex <RegExp> and value <String|Function> keys. If value is a function, it takes the same arguments as String.prototype.relace.

options.values

Type: Object
Default: {}

An object whose keys are strings that should be replaced and whose values are strings the replacements.

If desired, you may forgo the values key & declare your key:value pairs directly to the main configuration. For example:

{
  exclude: /node_modules/,
  values: {
    'process.env.NODE_ENV': JSON.stringify('production'),
  }
}

// is the same as:

{
  exclude: /node_modules/,
  'process.env.NODE_ENV': JSON.stringify('production'),
}

License

MIT © Luke Edwards

webpack-plugin-replace's People

Contributors

lukeed 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.