0 votes
in JavaScript by
What are the use cases for dynamic imports?

1 Answer

0 votes
by

Below are some of the use cases of using dynamic imports over static imports,

  1. Import a module on-demand or conditionally. For example, if you want to load a polyfill on legacy browser
if (isLegacyBrowser()) {
    import(···)
    .then(···);
}
  1. Compute the module specifier at runtime. For example, you can use it for internationalization.
import(`messages_${getLocale()}.js`).then(···);
  1. Import a module from within a regular script instead a module.
...