Skip to content

Provide an option to transform dynamic import to Promise.resolve(() => require(...)) #1084

@SamChou19815

Description

@SamChou19815

ESBuild fixes #1029 by always transform import(...) to Promise.resolve(() => require(...)). However, this is not always desired.

Old Node Versions

Dynamic import is not supported in earlier node version (<13).
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility.
Therefore, it should at least emit using Promise.resolve when having something like --target=node12.

Static Site Generation

During static site generation, we might want to produce a single bundled js that contains all the async imported modules.

e.g. Suppose we have the following source

// a.js
// some react code

// b.js
// some react code

// entry-point.js
const LazyA = import('./a');
const LazyB = import('./b');
// ...

If dynamic import syntax is preserved, then webpack will create a chunk for both a and b. However, this is not what we want for static site generation, since there is no concern for bundle size during build time, and we only want one single js for convenience.

If the dynamic import syntax is transformed into Promise.resolve(() => require(...)), then we won't have the problem anymore.

I encountered this problem when I am trying to adopt esbuild for docusaurus: facebook/docusaurus#4532

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions