Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SamChou19815 opened this issue Mar 29, 2021 · 3 comments

Comments

@SamChou19815
Copy link

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

@evanw evanw closed this as completed in 1aaaec2 Mar 29, 2021
@marvinhagemeister
Copy link

marvinhagemeister commented Mar 30, 2021

The data on MDN is incorrect. Just had to test the support level for that for a project at work last week and can confirm that node 12 does indeed support dynamic imports. Here is a simple repo to try that out locally https://github.com/marvinhagemeister/node-12-dynamic-import . Node versions can be easily switched locally via nvm or volta.

Node version Release Date Dynamic import support
12.16.3 2020-05-26 Not supported
12.17.0 2020-05-26 Supported, but marked as experimental
>=12.20.0 2020-11-24 Fully supported (nodejs/node#31974)
13.0.0 2019-10-22 Not supported
>=13.2.0 2019-11-21 Fully supported

I recall there at the time being a huge discussion on twitter about the LTS version not supporting ESM and thereby blocking the transition away from CommonJS. The decision was made to backport ESM from newer release lines to 12.x.

So node >=12.20.0 does support dynamic imports completely and the data on MDN is wrong. Submitted and issue on their end: https://github.com/mdn/content/issues/3665

cc @evanw

@evanw
Copy link
Owner

evanw commented Mar 30, 2021

The current environment version mechanism doesn't support a feature becoming unsupported and re-supported as the version number increases. So I'm planning to keep the supported version at node 13.2 for esbuild. Hopefully that's ok because node version 12 is no longer active anyway.

@marvinhagemeister
Copy link

Fair enough 👍

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 a pull request may close this issue.

3 participants