Skip to content

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Mar 22, 2025

This allows lazily loading statically analyzable dynamic imports when not type checking:

// main.ts
console.error("Here");
await import("jsr:@david/code-block-writer");
console.error("Done");

Before:

> deno run main.ts
Download https://jsr.io/@david/code-block-writer/meta.json
Download https://jsr.io/@david/code-block-writer/13.0.3_meta.json
Download https://jsr.io/@david/code-block-writer/13.0.3/mod.ts
Download https://jsr.io/@david/code-block-writer/13.0.3/utils/string_utils.ts
Here
Done

After:

> deno run --unstable-lazy-dynamic-imports main.ts
Here
Download https://jsr.io/@david/code-block-writer/meta.json
Download https://jsr.io/@david/code-block-writer/13.0.3_meta.json
Download https://jsr.io/@david/code-block-writer/13.0.3/mod.ts
Download https://jsr.io/@david/code-block-writer/13.0.3/utils/string_utils.ts
Done

Enablement

Use --unstable-lazy-dynamic-imports or set "unstable": ["lazy-dynamic-imports"] in a deno.json.

Notes

  1. Not loading them ahead of time means semver resolution may resolve differently because the order the dynamic imports get executed may differ from the order that's used without this flag.
  2. deno run --check still loads these dynamic imports ahead of time because the information might be used for type checking.
  3. This means your app may startup fine, but possibly fail loading a dynamic import due to a transient network error.

Closes #28581
Closes #23231 (this solution is easier than this and there is not much demand for that)
Ref #23149

Copy link
Member

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dsherret dsherret merged commit 31f6c05 into denoland:main Mar 25, 2025
18 checks passed
@dsherret dsherret deleted the feat_unstable_lazy_dynamic_imports branch March 25, 2025 15:38
littledivy pushed a commit that referenced this pull request Mar 28, 2025
…mic imports (#28593)

This allows lazily loading statically analyzable dynamic imports when
not type checking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lazy-loading dynamic imports Consider asynchronously caching statically analyzable dynamic imports in the background for deno run instead of up front
2 participants