UNPKG

2.09 kBMarkdownView Raw
1# rollup-plugin-replace
2[![](https://img.shields.io/npm/v/rollup-plugin-replace.svg?style=flat)](https://www.npmjs.com/package/rollup-plugin-replace)
3
4Replace strings in files while bundling them.
5
6
7## Installation
8
9```bash
10npm install --save-dev rollup-plugin-replace
11```
12
13
14## Usage
15
16Generally, you need to ensure that rollup-plugin-replace goes *before* other things (like rollup-plugin-commonjs) in your `plugins` array, so that those plugins can apply any optimisations such as dead code removal.
17
18
19```js
20// rollup.config.js
21import replace from 'rollup-plugin-replace';
22
23export default {
24 // ...
25 plugins: [
26 replace({
27 ENVIRONMENT: JSON.stringify('production')
28 })
29 ]
30};
31```
32
33
34## Options
35
36```js
37{
38 // a minimatch pattern, or array of patterns, of files that
39 // should be processed by this plugin (if omitted, all files
40 // are included by default)...
41 include: 'config.js',
42
43 // ...and those that shouldn't, if `include` is otherwise
44 // too permissive
45 exclude: 'node_modules/**',
46
47 // To replace every occurrence of `<@foo@>` instead of every
48 // occurrence of `foo`, supply delimiters
49 delimiters: ['<@', '@>'],
50
51 // All other options are treated as `string: replacement`
52 // replacers...
53 VERSION: '1.0.0',
54 ENVIRONMENT: JSON.stringify('development'),
55
56 // or `string: (id) => replacement` functions...
57 __dirname: (id) => `'${path.dirname(id)}'`,
58
59 // ...unless you want to be careful about separating
60 // values from other options, in which case you can:
61 values: {
62 VERSION: '1.0.0',
63 ENVIRONMENT: JSON.stringify('development')
64 }
65}
66```
67
68
69## Word boundaries
70
71By default, values will only match if they are surrounded by *word boundaries* — i.e. with options like this...
72
73```js
74{
75 changed: 'replaced'
76}
77```
78
79...and code like this...
80
81```js
82console.log('changed');
83console.log('unchanged');
84```
85
86...the result will be this:
87
88```js
89console.log('replaced');
90console.log('unchanged');
91```
92
93If that's not what you want, specify empty strings as delimiters:
94
95```js
96{
97 changed: 'replaced',
98 delimiters: ['', '']
99}
100```
101
102
103
104## License
105
106MIT