https://github.com/unplugin/unplugin-replace
A universal bundler plugin which replaces targeted strings in files.
https://github.com/unplugin/unplugin-replace
Last synced: 5 days ago
JSON representation
A universal bundler plugin which replaces targeted strings in files.
- Host: GitHub
- URL: https://github.com/unplugin/unplugin-replace
- Owner: unplugin
- License: mit
- Created: 2024-03-02T16:26:33.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-16T08:05:02.000Z (10 days ago)
- Last Synced: 2025-06-16T09:25:54.820Z (10 days ago)
- Language: TypeScript
- Homepage: https://jsr.io/@unplugin/replace
- Size: 905 KB
- Stars: 20
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# unplugin-replace [](https://npmjs.com/package/unplugin-replace) [](https://jsr.io/@unplugin/replace)
[](https://github.com/unplugin/unplugin-replace/actions/workflows/unit-test.yml)
🍣 A universal bundler plugin which replaces targeted strings in files, based on [@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace).
## Installation
```bash
# npm
npm i -D unplugin-replace# jsr
npx jsr add -D @unplugin/replace
```Vite
```ts
// vite.config.ts
import Replace from 'unplugin-replace/vite'export default defineConfig({
plugins: [Replace()],
})
```
Rollup
```ts
// rollup.config.js
import Replace from 'unplugin-replace/rollup'export default {
plugins: [Replace()],
}
```
esbuild
```ts
// esbuild.config.js
import { build } from 'esbuild'build({
plugins: [require('unplugin-replace/esbuild')()],
})
```
Webpack
```ts
// webpack.config.js
module.exports = {
/* ... */
plugins: [require('unplugin-replace/webpack')()],
}
```
## Usage
### Options
For all options please refer to [docs](https://github.com/rollup/plugins/tree/master/packages/replace#options).
This plugin accepts all [@rollup/plugin-replace](https://github.com/rollup/plugins/tree/master/packages/replace#options) options, and some extra options that are specific to this plugin.
### `options.values`
- Type: `{ [find: string]: Replacement } | ReplaceItem[]`
- Default: `[]````ts
type ReplaceItem = {
/** Supply a string or RegExp to find what you are looking for. */
find: string | RegExp/**
* Can be a string or a function.
* - If it's a string, it will replace the substring matched by pattern. A number of special replacement patterns are supported
* - If it's a function, it will be invoked for every match and its return value is used as the replacement text.
*/
replacement: Replacement
}
type Replacement = string | ((id: string, match: RegExpExecArray) => string)
```Comparing with `@rollup/plugin-replace`, `find` option supports regex pattern.
**Example:**
```ts
Replace({
values: [
{
find: /apples/gi,
replacement: 'oranges',
},
{
find: 'process.env.NODE_ENV',
replacement: '"production"',
},
],
})
```## Sponsors
## License
[MIT](./LICENSE) License © 2024-PRESENT [三咲智子](https://github.com/sxzz)