| 1 | [npm]: https://img.shields.io/npm/v/@rollup/plugin-replace
|
|---|
| 2 | [npm-url]: https://www.npmjs.com/package/@rollup/plugin-replace
|
|---|
| 3 | [size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-replace
|
|---|
| 4 | [size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-replace
|
|---|
| 5 |
|
|---|
| 6 | [![npm][npm]][npm-url]
|
|---|
| 7 | [![size][size]][size-url]
|
|---|
| 8 | [](https://liberamanifesto.com)
|
|---|
| 9 |
|
|---|
| 10 | # @rollup/plugin-replace
|
|---|
| 11 |
|
|---|
| 12 | 🍣 A Rollup plugin which replaces targeted strings in files while bundling.
|
|---|
| 13 |
|
|---|
| 14 | ## Requirements
|
|---|
| 15 |
|
|---|
| 16 | This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
|
|---|
| 17 |
|
|---|
| 18 | ## Install
|
|---|
| 19 |
|
|---|
| 20 | Using npm:
|
|---|
| 21 |
|
|---|
| 22 | ```console
|
|---|
| 23 | npm install @rollup/plugin-replace --save-dev
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | ## Usage
|
|---|
| 27 |
|
|---|
| 28 | Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | import replace from '@rollup/plugin-replace';
|
|---|
| 32 |
|
|---|
| 33 | export default {
|
|---|
| 34 | input: 'src/index.js',
|
|---|
| 35 | output: {
|
|---|
| 36 | dir: 'output',
|
|---|
| 37 | format: 'cjs'
|
|---|
| 38 | },
|
|---|
| 39 | plugins: [
|
|---|
| 40 | replace({
|
|---|
| 41 | 'process.env.NODE_ENV': JSON.stringify('production'),
|
|---|
| 42 | __buildDate__: () => JSON.stringify(new Date()),
|
|---|
| 43 | __buildVersion: 15
|
|---|
| 44 | })
|
|---|
| 45 | ]
|
|---|
| 46 | };
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
|
|---|
| 50 |
|
|---|
| 51 | The configuration above will replace every instance of `process.env.NODE_ENV` with `"production"` and `__buildDate__` with the result of the given function in any file included in the build.
|
|---|
| 52 |
|
|---|
| 53 | _Note: Values must be either primitives (e.g. string, number) or `function` that returns a string. For complex values, use `JSON.stringify`. To replace a target with a value that will be evaluated as a string, set the value to a quoted string (e.g. `"test"`) or use `JSON.stringify` to preprocess the target string safely._
|
|---|
| 54 |
|
|---|
| 55 | Typically, `@rollup/plugin-replace` should be placed in `plugins` _before_ other plugins so that they may apply optimizations, such as dead code removal.
|
|---|
| 56 |
|
|---|
| 57 | ## Options
|
|---|
| 58 |
|
|---|
| 59 | In addition to the properties and values specified for replacement, users may also specify the options below.
|
|---|
| 60 |
|
|---|
| 61 | ### `delimiters`
|
|---|
| 62 |
|
|---|
| 63 | Type: `Array[...String, String]`<br>
|
|---|
| 64 | Default: `['\b', '\b']`
|
|---|
| 65 |
|
|---|
| 66 | Specifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html). See [Word Boundaries](#word-boundaries) below for more information.
|
|---|
| 67 |
|
|---|
| 68 | ### `preventAssignment`
|
|---|
| 69 |
|
|---|
| 70 | Type: `Boolean`<br>
|
|---|
| 71 | Default: `false`
|
|---|
| 72 |
|
|---|
| 73 | Prevents replacing strings where they are followed by a single equals sign. For example, where the plugin is called as follows:
|
|---|
| 74 |
|
|---|
| 75 | ```js
|
|---|
| 76 | replace({
|
|---|
| 77 | values: {
|
|---|
| 78 | 'process.env.DEBUG': 'false'
|
|---|
| 79 | }
|
|---|
| 80 | });
|
|---|
| 81 | ```
|
|---|
| 82 |
|
|---|
| 83 | Observe the following code:
|
|---|
| 84 |
|
|---|
| 85 | ```js
|
|---|
| 86 | // Input
|
|---|
| 87 | process.env.DEBUG = false;
|
|---|
| 88 | if (process.env.DEBUG == true) {
|
|---|
| 89 | //
|
|---|
| 90 | }
|
|---|
| 91 | // Without `preventAssignment`
|
|---|
| 92 | false = false; // this throws an error because false cannot be assigned to
|
|---|
| 93 | if (false == true) {
|
|---|
| 94 | //
|
|---|
| 95 | }
|
|---|
| 96 | // With `preventAssignment`
|
|---|
| 97 | process.env.DEBUG = false;
|
|---|
| 98 | if (false == true) {
|
|---|
| 99 | //
|
|---|
| 100 | }
|
|---|
| 101 | ```
|
|---|
| 102 |
|
|---|
| 103 | ### `exclude`
|
|---|
| 104 |
|
|---|
| 105 | Type: `String` | `Array[...String]`<br>
|
|---|
| 106 | Default: `null`
|
|---|
| 107 |
|
|---|
| 108 | A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.
|
|---|
| 109 |
|
|---|
| 110 | ### `include`
|
|---|
| 111 |
|
|---|
| 112 | Type: `String` | `Array[...String]`<br>
|
|---|
| 113 | Default: `null`
|
|---|
| 114 |
|
|---|
| 115 | A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
|
|---|
| 116 |
|
|---|
| 117 | ### `values`
|
|---|
| 118 |
|
|---|
| 119 | Type: `{ [key: String]: Replacement }`, where `Replacement` is either a string or a `function` that returns a string.
|
|---|
| 120 | Default: `{}`
|
|---|
| 121 |
|
|---|
| 122 | To avoid mixing replacement strings with the other options, you can specify replacements in the `values` option. For example, the following signature:
|
|---|
| 123 |
|
|---|
| 124 | ```js
|
|---|
| 125 | replace({
|
|---|
| 126 | include: ['src/**/*.js'],
|
|---|
| 127 | changed: 'replaced'
|
|---|
| 128 | });
|
|---|
| 129 | ```
|
|---|
| 130 |
|
|---|
| 131 | Can be replaced with:
|
|---|
| 132 |
|
|---|
| 133 | ```js
|
|---|
| 134 | replace({
|
|---|
| 135 | include: ['src/**/*.js'],
|
|---|
| 136 | values: {
|
|---|
| 137 | changed: 'replaced'
|
|---|
| 138 | }
|
|---|
| 139 | });
|
|---|
| 140 | ```
|
|---|
| 141 |
|
|---|
| 142 | ## Word Boundaries
|
|---|
| 143 |
|
|---|
| 144 | By default, values will only match if they are surrounded by _word boundaries_.
|
|---|
| 145 |
|
|---|
| 146 | Consider the following options and build file:
|
|---|
| 147 |
|
|---|
| 148 | ```js
|
|---|
| 149 | module.exports = {
|
|---|
| 150 | ...
|
|---|
| 151 | plugins: [replace({ changed: 'replaced' })]
|
|---|
| 152 | };
|
|---|
| 153 | ```
|
|---|
| 154 |
|
|---|
| 155 | ```js
|
|---|
| 156 | // file.js
|
|---|
| 157 | console.log('changed');
|
|---|
| 158 | console.log('unchanged');
|
|---|
| 159 | ```
|
|---|
| 160 |
|
|---|
| 161 | The result would be:
|
|---|
| 162 |
|
|---|
| 163 | ```js
|
|---|
| 164 | // file.js
|
|---|
| 165 | console.log('replaced');
|
|---|
| 166 | console.log('unchanged');
|
|---|
| 167 | ```
|
|---|
| 168 |
|
|---|
| 169 | To ignore word boundaries and replace every instance of the string, wherever it may be, specify empty strings as delimiters:
|
|---|
| 170 |
|
|---|
| 171 | ```js
|
|---|
| 172 | export default {
|
|---|
| 173 | ...
|
|---|
| 174 | plugins: [
|
|---|
| 175 | replace({
|
|---|
| 176 | changed: 'replaced',
|
|---|
| 177 | delimiters: ['', '']
|
|---|
| 178 | })
|
|---|
| 179 | ]
|
|---|
| 180 | };
|
|---|
| 181 | ```
|
|---|
| 182 |
|
|---|
| 183 | ## Meta
|
|---|
| 184 |
|
|---|
| 185 | [CONTRIBUTING](/.github/CONTRIBUTING.md)
|
|---|
| 186 |
|
|---|
| 187 | [LICENSE (MIT)](/LICENSE)
|
|---|