babel-plugin-import-node
v1.0.2
Published
Babel plugin to transpile import() to requireAsync()
Downloads
8
Maintainers
Readme
babel-plugin-import-node
Babel plugin to transpile import()
to requireAsync()
.
NOTE: Babylon >= v6.12.0 is required to correct parse dynamic imports.
Installation
$ npm install babel-plugin-import-node --save-dev
Example
In:
const asyncRoutes = [
import('./root').then(module => module.default),
import(`./named_${subRoute}`),
]
Out:
var _requireAsync = function requireAsync(modulePath) {
return Promise.resolve().then(function() {
return require(modulePath);
});
};
const asyncRoutes = [
_requireAsync('./root').then(module => module.default),
_requireAsync(`./named_${subRoute}`),
]
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["import-node"]
}
Via CLI
$ babel --plugins import-node script.js
Via Node API
require('babel-core').transform('code', {
plugins: ['import-node']
});