babel-plugin-require-root-rewrite

A Babel plugin for requiring files relative to the project root.

Example

In: /path/to/project/containers/Register/Register.js

const BaseAuthForm = require('~/components/BaseAuthForm');

or

import BaseAuthForm from '~/components/BaseAuthForm';

Out

var BaseAuthForm = require('../../components/BaseAuthForm');

Installation

$ npm install babel-plugin-require-root-rewrite --save-dev

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": [
    ["babel-plugin-require-root-rewrite", {
      "pattern": "^~/",
      "basePath": "lib",
      "overrides": [
        "api",
        "server"
      ]
    }]
  ]
}

Via CLI

$ babel --plugins babel-plugin-require-root-rewrite script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['babel-plugin-require-root-rewrite']
});