Skip to content

Latest commit

 

History

History
73 lines (54 loc) · 1.5 KB

File metadata and controls

73 lines (54 loc) · 1.5 KB

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