Deferred imports evaluation ("import defer")
- Category
- JavaScript
- Type
- Chromium catches up
- Status
- Proposed (Chrome Proposed)
- Intent stage
- None
WPT details: Test262 tests: - https://github.com/tc39/test262/tree/main/test/language/import/import-defer - https://github.com/tc39/test262/tree/main/test/language/expressions/dynamic-import/import-defer
Summary
`import defer` allows lazily evaluating ES modules, to reduce the startup impact of modules that are not actually needed for the first JavaScript execution. This provides an easier-to-use alternative to dynamic import. While dynamic import provides more benefits, as it allows deferring loading of modules rather than just evaluation, it has a lot of friction due to its asynchronous nature, and thus it's not always usable. `import defer` follows the same syntax as regular imports, but it only works with namespace and dynamic imports: ``` import defer * as namespace from "./some-module.js" const namespace2 = await import.defer("./some-module.js"); ``` The module will be synchronously executed on property access on the module namespace object. To allow synchronous execution, not all of `./some-module.js`'s dependency tree will be deferred. Modules that contain top-level await will be eagerly executed, leaving only the synchronous parts of the tree for later.
Standards & signals
- Specification: https://tc39.es/proposal-defer-import-eval
- Firefox: Positive — Mozilla was originally championing the proposal
- Safari: Shipped/Shipping
- Web developers: No signals
- Tracking bug: https://issues.chromium.org/issues/398218423
Ecosystem
- Web Platform Tests: https://wpt.fyi/results/?q=test%2Flanguage%2Fimport%2Fimport-defer
- Chrome Platform Showcase: https://chrome-platform-showcase.paulkinlan-ea.deno.net/features
- GenDN reference: https://gendn.paulkinlan-ea.deno.net/features
- chrome.dev demos search: https://www.google.com/search?q=site%3Achrome.dev+Deferred%20imports%20evaluation%20(%22import%20defer%22)
- MDN Web Docs: https://developer.mozilla.org/en-US/search?q=Deferred%20imports%20evaluation%20(%22import%20defer%22)
- Baseline status: https://webstatus.dev/?q=Deferred%20imports%20evaluation%20(%22import%20defer%22)
Explainers: https://github.com/tc39/proposal-defer-import-eval