# webpack-plugin-replace

> Replace content while bundling.

Latest version **1.2.0** (published 2019-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install webpack-plugin-replace
pnpm add webpack-plugin-replace
yarn add webpack-plugin-replace
bun add webpack-plugin-replace
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2019-01-25 |
| First published | 2017-06-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Luke Edwards |
| Maintainers | lukeed |
| Keywords | bundle, content, plugin, replace, string, text, webpack, webpack-plugin |

## Links

- npm: https://www.npmjs.com/package/webpack-plugin-replace
- npm.io page: https://npm.io/package/webpack-plugin-replace

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2019-01-25
- 1.1.1 — 2017-06-05
- 1.1.0 — 2017-06-04
- 1.0.1 — 2017-06-04
- 1.0.0 — 2017-06-04
- 0.0.0 — 2017-06-02

## README

# webpack-plugin-replace [![Build Status](https://travis-ci.org/lukeed/webpack-plugin-replace.svg?branch=master)](https://travis-ci.org/lukeed/webpack-plugin-replace)

> Replace content while bundling.


## Install

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


## Usage

```js
// 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`<br>
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`<br>
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`<br>
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`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter).

#### options.values

Type: `Object`<br>
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:

```js
{
  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](https://lukeed.com)

---
_Source: https://npm.io/package/webpack-plugin-replace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
