Giter VIP home page Giter VIP logo

babel-plugin-search-and-replace's Introduction

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.

{
  "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

"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

"rules": [{
  "search": /foo/,
  "replace": "baz",
}]

Babel < 7

For Babel < 7 use [email protected] (does not support template strings)

{
  "plugins": [
    [
      "search-and-replace",
      [
        {
          "search": "searchedString",
          "replace": "replacement"
        },
        {
          "search": /myRegex/,
          "replace": "replacement"
        }
      ]
    ]
  ]
}

babel-plugin-search-and-replace's People

Contributors

benmccann avatar dependabot[bot] avatar flasd avatar jean-smaug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

babel-plugin-search-and-replace's Issues

Update README

Hello! First of all thanks for making this plugin, it really helped us in our project. However, it is quite hard to use because you don't provide any documentation for it. For instance, it took me 2 days and a good look into your source code to figure out that if you use search: "string", it only replaces string literals that match the whole search value.

For example, with this configuration:

rules: [{
  search: 'foo',
  replace: 'baz',
}]

this string is successfully replaced:

string = "foo"

but not this string:

string = "foo bar"

You have to use the regex variant for this, i. e.:

rules: [{
  search: /foo/,
  replace: 'baz',
}]

I don't know if this is a bug, but if it isn't, you should at least mention this in the README file. Otherwise, I spend a lot of hours wondering why the plugin does not replace all of my strings.

Broken, regex doesn't work.

Broken, regex doesn't work.

ConfigError: Error while parsing config - JSON5: invalid character '.' at 19:18

/something/

is not valid JSON value, it must be string "something" so for regex it would make sense to have "/something/" instad

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.