Skip to content

Babel plugin to transpile import() to a require(), for node. Best solution for SSR with react-router

License

Notifications You must be signed in to change notification settings

Ridestore/babel-plugin-dynamic-import-node-sync

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-dynamic-import-node-sync

Babel 6 plugin to transpile async import() to sync a require(), for node. Matches the proposed spec.

I am using it for server-side rendering.

Difference from babel-plugin-dynamic-import-node

babel-plugin-dynamic-import-node-sync

import(SOURCE) => () => { const r=require(SOURCE);r.then(cb=>cb(r));return r; }()

babel-plugin-dynamic-import-node

import(SOURCE) => Promise.resolve().then(() => require(SOURCE))

Installation

$ npm install @ridestore/babel-plugin-dynamic-import-node-sync --save-dev

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": [
    ["@ridestore/babel-plugin-dynamic-import-node-sync", { "target": "sync" }]
  ]
}

Options

opts.target

Script is looking for import() with comment provided in target field, example:

// .babelrc
{
  "plugins": [
    ["@ridestore/babel-plugin-dynamic-import-node-sync", { "target": "sync" }]
  ]
}

// index.js
const component = import(/* sync */ './path/to/component').then(module => module.MyComponent);
// will be replaced with
const component = (()=>{const r=__webpack_require__(666);r.then=cb=>cb(r);return r;})().then(module => module.MyComponent);

Otherwise, if you initiate plugin without target option, it replaces all import() calls

About

Babel plugin to transpile import() to a require(), for node. Best solution for SSR with react-router

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%