Babel macro for transformation of dynamic imports into synchronous requires based on a supplied code string.
Because you want code splitting on the client side, but synchronous imports on the server side.
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.
MIT