/babel-plugin-shortcut-import

Babel plugin for shortcut imports

Primary LanguageJavaScriptMIT LicenseMIT

Shortcut import

npm downloads dependencies devdependencies Codacy Badge Build Status Coverage Status Greenkeeper badge

Dependency-free, lightweight babel plugin that allows using shortcuts in import and require declarations.

Installation

npm install babel-plugin-shortcut-import --save-dev

Configuration

In .babelrc write:

{
  "plugins": [
    ["babel-plugin-shortcut-import"]
  ]
}

Then you can define your own shortcuts for directiories. For example:

{
  "plugins": [
    ["babel-plugin-shortcut-import", [
        {
          "pathPrefix": "#",
          "pathSuffix": "src/"
        },
        {
          "pathPrefix": "~",
          "pathSuffix": "src/shared/"
        }
    ]]
  ]
}

Now every shortcutted import like:

import Test from '~test.js';

Will be translated by babel to:

import Test from './shared/test.js';

Translated path is relative, so it may be different depending on the location of the source file.

It works for require function also.

Inspired by babel-plugin-root-import