babel-plugin-replace-dynamic-import-runtime

1.0.2 • Public • Published

Replace dynamic import() with require() at runtime

babel-plugin-replace-dynamic-import-runtime

Babel plugin to replace import(...) with require(...) at runtime, for node (also works with await import(...))

Use case

This has a very narrow use case: when you want to parse the same file for both browser and node (using babel/webpack) and still be able to use webpack's code splitting ability.

This is basically a utility to help reduce code duplication.

So statements like:

...
const someDynamicImport = import('../path/to/your/module');
...

into a require statement that node can understand:

const someDynamicImport = require('../path/to/your/module');

NOTE: Babylon >= v6.12.0 is required to correct parse dynamic imports.

Installation

yarn add babel-plugin-replace-dynamic-import-runtime --dev

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["replace-dynamic-import-runtime"]
}

Via CLI

$ babel --plugins replace-dynamic-import-runtime script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['replace-dynamic-import-runtime']
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.2
    8,454
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.2
    8,454
  • 1.0.1
    1
  • 1.0.0
    1

Package Sidebar

Install

npm i babel-plugin-replace-dynamic-import-runtime

Weekly Downloads

8,456

Version

1.0.2

License

MIT

Last publish

Collaborators

  • rdig