Skip to content

laat/babel-plugin-transform-rename-import

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

babel-plugin-transform-rename-import npm

replace import sources

Install

$ npm install --save babel-plugin-transform-rename-import

babelrc

{
  "plugins": [["transform-rename-import", { original: 'assert', replacement: 'power-assert' }]]
}

or multiple replacements:

{
  "plugins": [
    ["transform-rename-import", {
      replacements: [
        { original: 'replace-me', replacement: 'replaced' },
        { original: 'replace-me2', replacement: 'replaced2' }
      ]
    }
  ]]
}

RegExp:

{
  "plugins": [["transform-rename-import", { original: '^(.+?)\\.less$', replacement: '$1.css' }]]
}

Programatic Usage

import plugin from "babel-plugin-transform-rename-import";
import { transform } from "@babel/core";

function replace(code, original, replacement) {
  return transform(code, {
    babelrc: false,
    plugins: [[plugin, { original, replacement }]]
  }).code;
}

replace("require('foo')", "foo", "bar");
//=> 'require("bar");'

replace("import foo from 'foo'", "foo", "bar");
//=> 'import foo from "bar";'

replace("require('foo/thingy')", "foo", "bar");
//=> 'require("bar/thingy");'

replace("require('foo/thingy.less')", "^(.+?)\\.less$", "$1.css");
//=> 'require("foo/thingy.css");'

License

MIT © Sigurd Fosseng

About

rename imports and requires

Resources

License

Stars

Watchers

Forks

Packages

No packages published