Gathering detailed insights and metrics for @rollup/plugin-replace
Gathering detailed insights and metrics for @rollup/plugin-replace
Gathering detailed insights and metrics for @rollup/plugin-replace
Gathering detailed insights and metrics for @rollup/plugin-replace
rollup-plugin-replace
[](https://www.npmjs.com/package/rollup-plugin-replace)
rollup-plugin-replace-shebang
一个自动替换shebang的rollup插件
rollup-plugin-re
rollup replace plugin
rollup-plugin-search-and-replace
A rollup plugin that searches for and replaces text in one or more files. It works synchronously and makes a single replacement or several replacements at once.
🍣 The one-stop shop for official Rollup plugins
npm install @rollup/plugin-replace
Typescript
Module System
Min. Node Version
Node Version
NPM Version
92.7
Supply Chain
99.5
Quality
83
Maintenance
100
Vulnerability
99.6
License
JavaScript (75.16%)
TypeScript (24.83%)
Shell (0.01%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
3,713 Stars
1,228 Commits
604 Forks
35 Watchers
5 Branches
270 Contributors
Updated on Jul 10, 2025
Latest Version
6.0.2
Package Id
@rollup/plugin-replace@6.0.2
Unpacked Size
23.06 kB
Size
6.18 kB
File Count
8
NPM Version
10.8.2
Node Version
20.18.1
Published on
Dec 15, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
1
🍣 A Rollup plugin which replaces targeted strings in files while bundling.
This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Using npm:
1npm install @rollup/plugin-replace --save-dev
Create a rollup.config.js
configuration file and import the plugin:
1import replace from '@rollup/plugin-replace'; 2 3export default { 4 input: 'src/index.js', 5 output: { 6 dir: 'output', 7 format: 'cjs' 8 }, 9 plugins: [ 10 replace({ 11 'process.env.NODE_ENV': JSON.stringify('production'), 12 __buildDate__: () => JSON.stringify(new Date()), 13 __buildVersion: 15 14 }) 15 ] 16};
Then call rollup
either via the CLI or the API.
The 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.
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.
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 and also prevent replacements of instances with nested access. See Word Boundaries below for more information.
For example, if you pass typeof window
in values
to-be-replaced, then you could expect the following scenarios:
typeof window
will be replacedtypeof window.document
will not be replaced due to (?!\.)
boundarytypeof windowSmth
will not be replaced due to a \b
boundaryDelimiters will be used to build a Regexp
. To match special characters (any of .*+?^${}()|[]\
), be sure to escape them.
objectGuards
Type: Boolean
Default: false
When replacing dot-separated object properties like process.env.NODE_ENV
, will also replace typeof process
object guard
checks against the objects with the string "object"
.
For example:
1replace({ 2 values: { 3 'process.env.NODE_ENV': '"production"' 4 } 5});
1// Input 2if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { 3 console.log('production'); 4} 5// Without `objectGuards` 6if (typeof process !== 'undefined' && 'production' === 'production') { 7 console.log('production'); 8} 9// With `objectGuards` 10if ('object' !== 'undefined' && 'production' === 'production') { 11 console.log('production'); 12}
preventAssignment
Type: Boolean
Default: false
Prevents replacing strings where they are followed by a single equals sign. For example, where the plugin is called as follows:
1replace({ 2 values: { 3 'process.env.DEBUG': 'false' 4 } 5});
Observe the following code:
1// Input 2process.env.DEBUG = false; 3if (process.env.DEBUG == true) { 4 // 5} 6// Without `preventAssignment` 7false = false; // this throws an error because false cannot be assigned to 8if (false == true) { 9 // 10} 11// With `preventAssignment` 12process.env.DEBUG = false; 13if (false == true) { 14 // 15}
exclude
Type: String
| Array[...String]
Default: null
A picomatch 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 picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on. By default all files are targeted.
sourceMap
or sourcemap
Type: Boolean
Default: false
Enables generating sourcemaps for the bundled code. For example, where the plugin is called as follows:
1replace({ 2 sourcemap: true 3});
values
Type: { [key: String]: Replacement }
, where Replacement
is either a string or a function
that returns a string.
Default: {}
To avoid mixing replacement strings with the other options, you can specify replacements in the values
option. For example, the following signature:
1replace({ 2 include: ['src/**/*.js'], 3 changed: 'replaced' 4});
Can be replaced with:
1replace({ 2 include: ['src/**/*.js'], 3 values: { 4 changed: 'replaced' 5 } 6});
By default, values will only match if they are surrounded by word boundaries.
Consider the following options and build file:
1module.exports = { 2 ... 3 plugins: [replace({ changed: 'replaced' })] 4};
1// file.js 2console.log('changed'); 3console.log('unchanged');
The result would be:
1// file.js 2console.log('replaced'); 3console.log('unchanged');
To ignore word boundaries and replace every instance of the string, wherever it may be, specify empty strings as delimiters:
1export default { 2 ... 3 plugins: [ 4 replace({ 5 changed: 'replaced', 6 delimiters: ['', ''] 7 }) 8 ] 9};
No vulnerabilities found.
Reason
12 commit(s) and 7 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
binaries present in source code
Details
Reason
Found 14/30 approved changesets -- score normalized to 4
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
20 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More