https://github.com/jean-smaug/babel-plugin-search-and-replace
A small babel plugin to search strings and replace them
https://github.com/jean-smaug/babel-plugin-search-and-replace
babel babel-plugin search-and-replace
Last synced: 4 months ago
JSON representation
A small babel plugin to search strings and replace them
- Host: GitHub
- URL: https://github.com/jean-smaug/babel-plugin-search-and-replace
- Owner: jean-smaug
- Created: 2017-11-01T20:44:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-10-18T04:09:58.000Z (over 2 years ago)
- Last Synced: 2025-09-28T08:28:47.190Z (9 months ago)
- Topics: babel, babel-plugin, search-and-replace
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/babel-plugin-search-and-replace
- Size: 338 KB
- Stars: 28
- Watchers: 0
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# babel-plugin-search-and-replace
## What it does
It replaces specified string literals
## Install
- `yarn add babel-plugin-search-and-replace`
- `npm i babel-plugin-search-and-replace`
- `pnpm add babel-plugin-search-and-replace`
## How to use it
Add the following lines in your .babelrc.
```json
{
"plugins": [
[
"search-and-replace",
{
"rules": [
{
"search": "searchedString",
"searchTemplateStrings": true,
"replace": "replacement"
},
{
"search": /myRegex/,
"replace": "replacement"
}
]
}
]
]
}
```
If the `search` key is a string it will search exactly this string.
For example with this config
```json
"rules": [{
"search": "foo",
"replace": "baz",
}]
```
`str1` will be replaced but not str 2
```
const str1 = "foo"
const str2 = "foo don't match"
```
If you want `str2` to be replaced, use the regex syntax
```json
"rules": [{
"search": /foo/,
"replace": "baz",
}]
```
## Babel < 7
For Babel < 7 use babel-plugin-search-and-replace@0.3.0 (does not support template strings)
```json
{
"plugins": [
[
"search-and-replace",
[
{
"search": "searchedString",
"replace": "replacement"
},
{
"search": /myRegex/,
"replace": "replacement"
}
]
]
]
}
```