Skip to content

✂ Babel macro for transformation of dynamic imports into synchronous requires based on a supplied code string.

License

Notifications You must be signed in to change notification settings

wafflepie/universal-import.macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

universal-import.macro

Babel Macro

Babel macro for transformation of dynamic imports into synchronous requires based on a supplied code string.

Why?

Because you want code splitting on the client side, but synchronous imports on the server side.

How?

Add universal-import.macro to your dependencies. Replace import() with universalImport() from this package.

// Before
import(`./assets/${name}.svg`);
// After
import universalImport from "universal-import.macro";

universalImport(`./assets/${name}.svg`, `!!process.env.NO_DYNAMIC_IMPORTS`);

Depending on the !!process.env.NO_DYNAMIC_IMPORTS expression, Webpack will see a different import method.

// process.env.NO_DYNAMIC_IMPORTS = false
import(`./assets/${name}.svg`);

// process.env.NO_DYNAMIC_IMPORTS = true
require(`./assets/${name}.svg`);

Replace !!process.env.NO_DYNAMIC_IMPORTS with some other code string which evaluates into a boolean at build-time. Since it can be any expression, you can use an IIFE for more complex conditions.

Tests?

Yes, some.

License?

MIT

About

✂ Babel macro for transformation of dynamic imports into synchronous requires based on a supplied code string.

Topics

Resources

License

Stars

Watchers

Forks