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

ESM 和 CJS 模块规范区别 #33

Open
Inchill opened this issue Aug 12, 2022 · 0 comments
Open

ESM 和 CJS 模块规范区别 #33

Inchill opened this issue Aug 12, 2022 · 0 comments

Comments

@Inchill
Copy link
Owner

Inchill commented Aug 12, 2022

  • 两者的模块导入导出语法不同,CommonJs 是通过 module.exports,exports 导出,require 导入;ESModule 则是 export 导出,import 导入。
  • CommonJs 是运行时加载模块,ESModule 是在静态编译期间就确定模块的依赖。
  • ESModule 在编译期间会将所有 import 提升到顶部,CommonJs 不会提升 require。
  • CommonJs 导出的是一个值拷贝,会对加载结果进行缓存,一旦内部再修改这个值,则不会同步到外部。ESModule 是导出的一个引用,内部修改可以同步到外部。
  • CommonJs 中顶层的 this 指向这个模块本身,而 ESModule 中顶层 this 指向 undefined。
  • CommonJS 加载的是整个模块,将所有的接口全部加载进来,ESModule 可以单独加载其中的某个接口。
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

1 participant