rollup-plugin-replace
Replace strings in files while bundling them.
Installation
npm install --save-dev rollup-plugin-replace
Usage
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.
import replace from 'rollup-plugin-replace';
export default {
plugins: [
replace({
ENVIRONMENT: JSON.stringify('production')
})
]
};
Options
{
include: 'config.js',
exclude: 'node_modules/**',
delimiters: ['<@', '@>'],
VERSION: '1.0.0',
ENVIRONMENT: JSON.stringify('development'),
__dirname: (id) => `'${path.dirname(id)}'`,
values: {
VERSION: '1.0.0',
ENVIRONMENT: JSON.stringify('development')
}
}
Word boundaries
By default, values will only match if they are surrounded by word boundaries — i.e. with options like this...
{
changed: 'replaced'
}
...and code like this...
console.log('changed');
console.log('unchanged');
...the result will be this:
console.log('replaced');
console.log('unchanged');
If that's not what you want, specify empty strings as delimiters:
{
changed: 'replaced',
delimiters: ['', '']
}
License
MIT