dynamic-import-support
v1.0.0
Published
transform dynamic imports for node.js
Downloads
3
Maintainers
Readme
dynamic-import-support
enable dynamic imports in node.js
import('./whatever').then(function (exports) {
console.log(exports)
})
Install
npm install dynamic-import-support
Usage
Use dynamic-import-support/register
to enable import()
in all modules loaded after the current one:
require('dynamic-import-support/register')
Use dynamic-import-support
to transform some source code containing import()
calls:
var dynamicImport = require('dynamic-import-support')
dynamicImport(`
import('./whatever').then(function (exports) {
console.log(exports)
})
`) === `
function _import(p){return Promise.resolve().then(function(){return require(p)})}
_import('./whatever').then(function (exports) {
console.log(exports)
})
`
It uses js-tokens instead of a full parser, so it's very quick.
Some patterns aren't supported though.
For example, import()
inside a template string won't be transformed.
If this is a problem, please open an issue and we'll figure out how to make it work :)