UNPKG

5.2 kBMarkdownView Raw
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[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](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
16This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+.
17
18## Install
19
20Using npm:
21
22```console
23npm install @rollup/plugin-replace --save-dev
24```
25
26## Usage
27
28Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
29
30```js
31import replace from '@rollup/plugin-replace';
32
33export 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
49Then 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
51The 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
55Typically, `@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
59In addition to the properties and values specified for replacement, users may also specify the options below.
60
61### `delimiters`
62
63Type: `Array[...String, String]`<br>
64Default: `['\b', '\b(?!\.)']`
65
66Specifies the boundaries around which strings will be replaced. By default, delimiters are [word boundaries](https://www.regular-expressions.info/wordboundaries.html) and also prevent replacements of instances with nested access. See [Word Boundaries](#word-boundaries) below for more information.
67For example, if you pass `typeof window` in `values` to-be-replaced, then you could expect the following scenarios:
68
69- `typeof window` **will** be replaced
70- `typeof window.document` **will not** be replaced due to `(?!\.)` boundary
71- `typeof windowSmth` **will not** be replaced due to a `\b` boundary
72
73### `preventAssignment`
74
75Type: `Boolean`<br>
76Default: `false`
77
78Prevents replacing strings where they are followed by a single equals sign. For example, where the plugin is called as follows:
79
80```js
81replace({
82 values: {
83 'process.env.DEBUG': 'false'
84 }
85});
86```
87
88Observe the following code:
89
90```js
91// Input
92process.env.DEBUG = false;
93if (process.env.DEBUG == true) {
94 //
95}
96// Without `preventAssignment`
97false = false; // this throws an error because false cannot be assigned to
98if (false == true) {
99 //
100}
101// With `preventAssignment`
102process.env.DEBUG = false;
103if (false == true) {
104 //
105}
106```
107
108### `exclude`
109
110Type: `String` | `Array[...String]`<br>
111Default: `null`
112
113A [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.
114
115### `include`
116
117Type: `String` | `Array[...String]`<br>
118Default: `null`
119
120A [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.
121
122### `values`
123
124Type: `{ [key: String]: Replacement }`, where `Replacement` is either a string or a `function` that returns a string.
125Default: `{}`
126
127To avoid mixing replacement strings with the other options, you can specify replacements in the `values` option. For example, the following signature:
128
129```js
130replace({
131 include: ['src/**/*.js'],
132 changed: 'replaced'
133});
134```
135
136Can be replaced with:
137
138```js
139replace({
140 include: ['src/**/*.js'],
141 values: {
142 changed: 'replaced'
143 }
144});
145```
146
147## Word Boundaries
148
149By default, values will only match if they are surrounded by _word boundaries_.
150
151Consider the following options and build file:
152
153```js
154module.exports = {
155 ...
156 plugins: [replace({ changed: 'replaced' })]
157};
158```
159
160```js
161// file.js
162console.log('changed');
163console.log('unchanged');
164```
165
166The result would be:
167
168```js
169// file.js
170console.log('replaced');
171console.log('unchanged');
172```
173
174To ignore word boundaries and replace every instance of the string, wherever it may be, specify empty strings as delimiters:
175
176```js
177export default {
178 ...
179 plugins: [
180 replace({
181 changed: 'replaced',
182 delimiters: ['', '']
183 })
184 ]
185};
186```
187
188## Meta
189
190[CONTRIBUTING](/.github/CONTRIBUTING.md)
191
192[LICENSE (MIT)](/LICENSE)