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

[Bug]: @modern-js/module-tools 输出 cjs 时 转换了 await import #5795

Closed
dingff opened this issue Jun 2, 2024 · 10 comments
Closed

[Bug]: @modern-js/module-tools 输出 cjs 时 转换了 await import #5795

dingff opened this issue Jun 2, 2024 · 10 comments

Comments

@dingff
Copy link

dingff commented Jun 2, 2024

版本信息

System:
    OS: macOS 14.4.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 19.54 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 125.0.6422.113
    Safari: 17.4.1

问题详情

cjs

image

esm

image

复现链接

none

复现步骤

none

@dingff dingff added the bug Something isn't working label Jun 2, 2024
@fi3ework
Copy link
Member

fi3ework commented Jun 5, 2024

@modern-js/module-tools 底层是 esbuild,所以这本质上是 esbuild 的行为。

举个例子,如果你的目标环境不支持 import(),esbuild 会自动将你源码中的 import() 转换为 require,是符合预期的行为。

如果你希望强制保留 import(),可以使用 https://esbuild.github.io/api/#supporteddemo),修改 @modern-js/module-tools 配置文件中的 https://modernjs.dev/module-tools/api/config/build-config.html#esbuildoptions 即可。

如果有更多的问题,请提供复现链接及复现步骤帮助我们更好得排查(「请提供最小重现」)。

@fi3ework fi3ework closed this as completed Jun 5, 2024
@fi3ework fi3ework removed the bug Something isn't working label Jun 5, 2024
@dingff
Copy link
Author

dingff commented Jun 6, 2024

'dynamic-import': buildType === 'bundle' || format !== 'cjs',

请问这里为什么要这样做呀?

@dingff
Copy link
Author

dingff commented Jun 6, 2024

@fi3ework

@fi3ework
Copy link
Member

fi3ework commented Jun 6, 2024

你指的是前面的还是后面的判断条件

@dingff
Copy link
Author

dingff commented Jun 6, 2024

你指的是前面的还是后面的判断条件

cjs为什么要关闭dynamic-import?

@dingff
Copy link
Author

dingff commented Jun 7, 2024

@fi3ework

@fi3ework
Copy link
Member

fi3ework commented Jun 7, 2024

因为 CJS 在低版本 node 的兼容性更好,而且 Node 对 code splitting 没有什么强的诉求

@dingff
Copy link
Author

dingff commented Jun 7, 2024

因为 CJS 在低版本 node 的兼容性更好,而且 Node 对 code splitting 没有什么强的诉求

但是这样在format为cjs时,动态导入的esm依赖就会报错,因为import被转换成了require,这个问题怎么解呢 @fi3ework

@fi3ework
Copy link
Member

fi3ework commented Jun 7, 2024

我最开始的回复已经回答了这个问题。

@dingff
Copy link
Author

dingff commented Jun 7, 2024

我最开始的回复已经回答了这个问题。

好的,感谢耐心解答

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

No branches or pull requests

2 participants