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