Skip to content

Latest commit

 

History

History
45 lines (28 loc) · 1.25 KB

README.md

File metadata and controls

45 lines (28 loc) · 1.25 KB

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