Product
Introducing Socket Optimize
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.
@rollup/plugin-replace
Advanced tools
The @rollup/plugin-replace npm package allows you to replace strings in files while bundling them with Rollup. This can be particularly useful for inserting environment variables, toggling between development and production modes, or any scenario where you need to dynamically alter your code at build time.
Environment Variable Replacement
This feature allows you to replace occurrences of 'process.env.NODE_ENV' with 'production' in your code. It's particularly useful for setting the environment mode.
import replace from '@rollup/plugin-replace';
export default {
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production')
})
]
};
Conditional Code Inclusion/Exclusion
This feature enables you to conditionally include or exclude code blocks based on the replacement. In this example, all blocks guarded by 'if (process.env.DEBUG)' will always execute as if the condition is true.
import replace from '@rollup/plugin-replace';
export default {
plugins: [
replace({
'if (process.env.DEBUG)': 'if (true)'
})
]
};
This Babel plugin allows you to replace specified nodes in your Babel AST with arbitrary values, similar to how @rollup/plugin-replace works. The difference lies in the approach: babel-plugin-transform-define operates on the AST level during the transpilation step, rather than during the bundling process.
🍣 A Rollup plugin which replaces strings in files while bundling.
This plugin requires an LTS Node version (v8.0.0+) and Rollup v1.20.0+.
Using npm:
npm install @rollup/plugin-replace --save-dev
Create a rollup.config.js
configuration file and import the plugin:
import replace from '@rollup/plugin-replace';
export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
},
plugins: [
replace({
__buildEnv__: 'production',
__buildDate__: () => new Date(),
__buildVersion: 15
})
]
};
Then call rollup
either via the CLI or the API.
The configuration above will replace every instance of __buildEnv__
with 'production'
and __buildDate__
with the result of the given function in any file included in the build. Note: Values have to be either primitives or functions that return a string. For complex values, use JSON.stringify
.
Typically, @rollup/plugin-replace
should be placed in plugins
before other plugins so that they may apply optimizations, such as dead code removal.
In addition to the properties and values specified for replacement, users may also specify the options below.
delimiters
Type: Array[...String, String]
Default: ['\b', '\b']
Specifies the boundaries around which strings will be replaced. By default, delimiters are word boundaries. See Word Boundaries below for more information.
exclude
Type: String
| Array[...String]
Default: null
A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore. By default no files are ignored.
include
Type: String
| Array[...String]
Default: null
A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
By default, values will only match if they are surrounded by word boundaries.
Consider the following options and build file:
module.exports = {
...
plugins: [replace({ changed: 'replaced' })]
};
// file.js
console.log('changed');
console.log('unchanged');
The result would be:
// file.js
console.log('replaced');
console.log('unchanged');
To ignore word boundaries and replace every instance of the string, wherever it may be, specify empty strings as delimiters:
export default {
...
plugins: [
replace({
changed: 'replaced',
delimiters: ['', '']
})
]
};
FAQs
Replace strings in files while bundling
The npm package @rollup/plugin-replace receives a total of 3,738,509 weekly downloads. As such, @rollup/plugin-replace popularity was classified as popular.
We found that @rollup/plugin-replace demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.