Babel plugin to transpile import()
to require
, for AMD module loaders
NOTE: Babylon v6.12.0 is required to correctly parse dynamic imports.
$ npm install babel-plugin-dynamic-import-amd --save-dev
.babelrc
(Recommended).babelrc
{
"plugins": ["dynamic-import-amd"]
}
$ babel --plugins dynamic-import-amd script.js
require("babel-core").transform("code", {
plugins: ["dynamic-import-amd"]
});
Now just use syntax from the proposal for adding a "function-like" import() module loading syntactic form to JavaScript. It is currently in stage 3 of the TC39 process.
import('module-name');
let name = '-name';
import(`module-${name}`);
Many credits go to AirBNB's package babel-plugin-dynamic-import-webpack. This package contains the same tests as that library. And it formed the base for the actual dynamic import implementation.