babel-plugin-short-import
Small Babel plugin allowing to use shorthand imports.
It generates import's identifier from file name (without extension), so all line can be written in shorter version as import "path/to/the/file"
. It also works with NPM modules.
Plugin requires Babel 7 and it may cause conflicts with Webpack's methods of including styles and other files.
Installation
$ npm install --save-dev babel-plugin-short-import
Usage
.babelrc
(Recommended)
Via .babelrc
Via CLI
$ babel --plugins babel-plugin-short-import script.js
Via Node API
;
Parameters
-
importAsNamespace
If
false
(default) it will behave likeimport file from "./path/file.js"
.Otherwise it will import all namespace -
import * as file from "./path/file.js"
. -
useCamelCase
If
false
(default) it will replace invalid characters in file name with underscore ( _ ) likeimport my_file from "./path/my-file.js
.Otherwise it will apply camel case -
import * as myFile from "./path/my-file.js"
.
Examples
path/to/the/file.js or @vendor/some-module
const test1 = "Test 1"; const test2 = test: 2 ; "Default test";
Default importing
Configured with { "importAsNamespace": false }
Import in this example is an equivalent of import file from "path/to/the/file.js"
; console; // undefinedconsole; // undefinedconsole; // "Default test"
Namespace importing
Configured with { "importAsNamespace": true }
Import in this example is an equivalent of import * as file from "path/to/the/file.js"
; console; // "Test 1"console; // { "test": 2 }console; // { "test1": "Test 1", "test2": { "test": 2 }, "default": "Default test" }
NPM modules importing
Configured with { "importAsNamespace": true, "useCamelCase": true }
; console; // "Test 1"console; // { "test": 2 }console; // "Default test"