A babel plugin that unrolls wildcard imports.
Installation
$ npm install --save-dev babel-plugin-import-recursive
or with yarn
$ yarn add --dev babel-plugin-import-recursive
Example
With the following folder structure:
|- index.js
|- actions
|- action.a.js
|- action_b.js
|- sub_dir
|- actionC.js
and with the following JS:
;
will be compiled to:
const _dirImport = {};;;_dirImportactionA = _actionA;_dirImportactionB = _actionB;const actions = _dirImport;
You can also import files recursively using double asterisk
like this:
;
will be compiled to:
const _dirImport = {};;;;_dirImportactionA = _actionA;_dirImportactionB = _actionB;_dirImportactionC = _actionC;const actions = _dirImport;
And import without specifiers
;
will be compiled to:
;;;
You can also import all the methods directly, using a single asterisk
.
the following JS:
;
will be compiled to:
const _dirImport = {};;;for let key in _actionA _dirImportkey === 'default' ? 'actionA' : key = _actionAkey; for let key in _actionB _dirImportkey === 'default' ? 'actionB' : key = _actionBkey;const actions = _dirImport;
And you can use both, double and single asterisk
, like this:
;
will be compiled to:
const _dirImport = {};;;;for let key in _actionA _dirImportkey === 'default' ? 'actionA' : key = _actionAkey; for let key in _actionB _dirImportkey === 'default' ? 'actionB' : key = _actionBkey; for let key in _actionC _dirImportkey === 'default' ? 'actionC' : key = _actionCkey;const actions = _dirImport;
Usage
Just add it to your .babelrc file
Options
exts
By default, the files with the following extensions: ["js", "mjs", "jsx"]
, will be imported. You can change this using:
snakeCase
By default, the variables name would be in camelCase. You can change this using:
result: action_a
, action_b
and action_c
nostrip
By default, the file extension will be removed in the generated import statements, you can change this using:
listTransform
Callback to transform the resolved file list. You should use .babelrc.js
or babel.config.js
to define the function.
You can sort imported modules by depth, for example:
moduleexports = plugins: 'import-recursive' { return alength - blength; } ;
Forked from babel-plugin-import-directory that was forked from babel-plugin-wildcard 🦔