Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
1755 commits
Select commit Hold shift + click to select a range
a6e8a40
refactor: add tencent.txt
ruanyf Feb 23, 2020
b0b10a3
refactor: add tencent.txt
ruanyf Feb 23, 2020
717f4e4
docs: fix #952
ruanyf Feb 23, 2020
296159a
修正一处小地方
EggOxygen Feb 25, 2020
57701fc
docs(class): fix typo
ruanyf Feb 27, 2020
2e1c107
Merge pull request #954 from EggOxygen/patch-1
ruanyf Feb 27, 2020
b4ec056
fix: 使语句更通顺
anhongpeng Feb 29, 2020
518f311
refactor: update disqus url to HTTPS
ruanyf Mar 3, 2020
6800796
docs(async): 如何处理数组的异步操作
ruanyf Mar 3, 2020
3405f13
Merge pull request #955 from AnHongpeng/gh-pages
ruanyf Mar 4, 2020
6069e3f
使用core-js替代过时的@babel/polyfill
Mookiepiece Mar 7, 2020
130cdee
Merge pull request #958 from Mookiepiece/master
ruanyf Mar 10, 2020
428a748
docs(intro): delete Babel API #958
ruanyf Mar 10, 2020
0f65697
perf: remove unnecessary closure
MingCheHarden Mar 13, 2020
21e3cfb
Merge pull request #960 from yaoguoli/patch-1
ruanyf Mar 16, 2020
e692306
fix: arraybuffer.md add `new`
hou-pu Mar 18, 2020
74acd64
Merge pull request #961 from wangzongxu/patch-1
ruanyf Mar 18, 2020
f18bd46
修复表述错误
linmii Mar 21, 2020
2061ad9
修正 `exec()` 拼写
kidonng Mar 22, 2020
f330662
Merge pull request #964 from kidonng/patch-1
ruanyf Mar 23, 2020
f71a2ce
docs(number): fix #965
ruanyf Mar 23, 2020
491f6f0
Merge pull request #962 from linmii/patch-1
ruanyf Mar 23, 2020
cd0310c
docs(object): 修改扩展运算符自动执行取值函数 get 的例子
ruanyf Mar 23, 2020
31ab7b1
去除多余文字
linmii Mar 24, 2020
fe72377
refactor: edit sponsor banner
ruanyf Mar 24, 2020
0068aa5
refactor: edit sponsor banner
ruanyf Mar 25, 2020
f0b8e22
Merge pull request #967 from linmii/patch-2
ruanyf Mar 26, 2020
8b4db43
docs: edit object-methods
ruanyf Mar 30, 2020
7332021
统一“抽象操作的运行流程”的描述
baooab Mar 31, 2020
09d2e0b
Merge pull request #970 from baooab/patch-5
ruanyf Apr 1, 2020
03c9e34
docs(module): update es2020
ruanyf Apr 3, 2020
0849a77
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Apr 3, 2020
ac50325
docs: edit Promise
ruanyf Apr 6, 2020
1474f82
fix(proposals): shell exec style
waitingsong Apr 7, 2020
7076dd5
docs(math): fix #973
ruanyf Apr 9, 2020
38963f7
Merge pull request #971 from waitingsong/shell-exec
ruanyf Apr 9, 2020
c3dd90d
refactor: edit ditto/support
ruanyf Apr 13, 2020
f810124
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Apr 13, 2020
e11dc14
Update module.md
suzhongping Apr 24, 2020
d65a547
Merge pull request #979 from suzhongping/patch-1
ruanyf Apr 25, 2020
2f908a5
完善Reflect.ownkeys()方法的描述
chenweitech May 8, 2020
6f59bc3
docs(proxy): edit defineProperty
ruanyf May 13, 2020
3832b6a
refactor: adjust support date
ruanyf May 13, 2020
18e372d
Merge pull request #982 from chenweitech/patch-1
ruanyf May 14, 2020
55cbbb8
fix: li tag click bug #984
ruanyf May 14, 2020
323e962
docs(set): fix #991
ruanyf May 31, 2020
f0a92d5
docs(regex): fixed matchall() #992
ruanyf Jun 12, 2020
0219f20
docs(object): edit 链判断运算符 ?.
ruanyf Jun 21, 2020
d59b301
docs(number): edit BigInt
ruanyf Jul 6, 2020
4425866
docs(object): edit null 判断运算符
ruanyf Jul 7, 2020
064ee65
docs(object-methods): edit Object.assign()
ruanyf Jul 7, 2020
e785137
docs(let): fix #1001
ruanyf Aug 6, 2020
9461a92
docs: edit README
ruanyf Aug 14, 2020
266199c
docs(module-loader): edit Node.js 的模块加载
ruanyf Aug 19, 2020
09accf9
docs(module-loader): fix typo
ruanyf Aug 19, 2020
7b8c063
docs(module-loader): edit 同时支持两种格式的模块
ruanyf Aug 20, 2020
a841acd
docs(module-loader): fix typo
ruanyf Aug 20, 2020
2613635
docs(module-loader): fix typo
ruanyf Aug 20, 2020
dfe3373
feat: add change theme
Aug 21, 2020
e33a668
upt: delete note
Aug 25, 2020
adb4964
docs(module-loader): fix typo
ruanyf Aug 29, 2020
dd892f8
Merge pull request #1002 from zhidaoliu/gh-pages
ruanyf Aug 29, 2020
4e14b40
docs(object): fix #1004
ruanyf Sep 1, 2020
2f76a9f
(docs): fix typo
hehe1111 Sep 9, 2020
76fc255
docs(promise): edit Promise.reject()
ruanyf Sep 10, 2020
04ff592
Merge pull request #1006 from hehe1111/patch-1
ruanyf Sep 10, 2020
d4952d6
docs(module-loader): fix typo
ruanyf Sep 10, 2020
50b25c1
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Sep 10, 2020
6dc250f
docs(decorator): 修改函数提升时代码实际执行顺序
careteenL Sep 12, 2020
c432253
优化了 async 函数内部报错会导致 Promise 对象变为`reject`状态的例子
jacty Sep 12, 2020
f81d202
docs(proxy): fix constrct()
ruanyf Sep 15, 2020
0f1acd3
docs(iterable): fix typo
ruanyf Sep 15, 2020
841eebb
Merge pull request #1007 from careteenL/patch-1
ruanyf Sep 15, 2020
8a5b570
Merge pull request #1008 from jacty/patch-1
ruanyf Sep 15, 2020
10defdb
docs(async): fix typo
ruanyf Sep 15, 2020
9f1f925
Update proxy.md
chua1989 Sep 15, 2020
48e318b
Update proxy.md
chua1989 Sep 15, 2020
c025fc9
Merge pull request #1009 from chua1989/gh-pages
ruanyf Sep 16, 2020
746a918
docs(proxy): edit construct()
ruanyf Sep 16, 2020
6ff7cb8
docs(class): fix private property
ruanyf Sep 17, 2020
c2d1792
docs(set-map): edit WeakMap
ruanyf Sep 22, 2020
4bd4d77
docs(function/arrow fucntion): edit #150
ruanyf Oct 2, 2020
2e082a3
docs(module): fix module suffix
ruanyf Oct 23, 2020
8f852c0
docs(Atomics): Atomics.wake() rename to Atomics.notify()
ruanyf Oct 24, 2020
d1f96fe
docs(proxy): fix this #1014
ruanyf Oct 24, 2020
356b0db
docs(string): add replaceAll()
ruanyf Oct 25, 2020
492fb25
docs(string-methods): add replaceAll()
ruanyf Oct 27, 2020
2ed3a6b
docs(promise): edit Promise.any()
ruanyf Oct 31, 2020
04dddb2
docs: fix wangdoc-es6 #4
ruanyf Oct 31, 2020
861dd41
docs(arraybuffer): edit Atomics.load()
ruanyf Nov 6, 2020
f817164
docs(string-methods): edit replaceAll(), fixed #1018
ruanyf Nov 17, 2020
a326217
docs(string): fix typo
ruanyf Nov 17, 2020
ff47784
docs(style): fixed #1020
ruanyf Nov 24, 2020
584d5d9
docs(generator): edit style
ruanyf Nov 24, 2020
201eebe
docs(async): fixed #1023
ruanyf Nov 29, 2020
bc0ba76
docs(async): edit reduce()
ruanyf Nov 29, 2020
038a080
refactor: edit support banner
ruanyf Dec 2, 2020
761d107
refactor: edit support banner
ruanyf Dec 2, 2020
56d15b3
refactor: edit support banner
ruanyf Dec 3, 2020
d041893
docs(class): edit class intro
ruanyf Dec 9, 2020
cc2420e
docs(function): edit arrow function
ruanyf Dec 17, 2020
1c99572
docs(promise): fix #1028
ruanyf Dec 22, 2020
1d556be
Update promise.md
Geekhyt Dec 22, 2020
78972be
Merge pull request #1029 from Geekhyt/patch-2
ruanyf Dec 22, 2020
4309e6a
docs(regex): edit regex
ruanyf Jan 6, 2021
802ee5e
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
ruanyf Jan 6, 2021
b45161a
docs(proxy): fix #1032
ruanyf Jan 22, 2021
9461630
refactor: edit support js
ruanyf Feb 5, 2021
543678f
docs(array): fix #1037
ruanyf Feb 11, 2021
ec9db66
refactor: edit support banner
ruanyf Feb 20, 2021
631a66e
refactor: edit support banner
ruanyf Feb 20, 2021
3929f4f
refactor: edit support banner
ruanyf Feb 23, 2021
e9fd28b
fix: remove 'else'
roc-an Apr 18, 2021
d09058f
fix: 为输出补充示例
roc-an Apr 20, 2021
d39c55d
Merge pull request #1045 from roc-an/patch-2
ruanyf Apr 23, 2021
95f57f8
Merge pull request #1044 from roc-an/patch-1
ruanyf Apr 23, 2021
fa03d29
fix: 强迫症犯了想去掉多余空格好看一些
roc-an Apr 24, 2021
59146b4
docs(proxy): fix set return value #1047
ruanyf Apr 26, 2021
0163114
Merge pull request #1048 from roc-an/patch-3
ruanyf Apr 26, 2021
85b8174
docs(set-map): 改进 WeakSet 和 WeakMap 文档
codeudan May 8, 2021
71af30d
docs(function): fixed 箭头函数的this #1050
ruanyf May 11, 2021
e6b0210
Merge pull request #1052 from codeudan/gh-pages
ruanyf May 12, 2021
ee1b51e
docs(set-map): edit text
ruanyf May 12, 2021
d41bf83
docs(function): 调整不合适的函数描述
codeudan May 14, 2021
53cb4d3
docs: 修正已知的 this 术语错误
codeudan May 15, 2021
4f2353a
Merge pull request #1054 from codeudan/gh-pages
ruanyf May 18, 2021
0503136
Merge pull request #1055 from codeudan/fix/this
ruanyf May 18, 2021
89370af
docs(class): 添加 in 运算符
ruanyf May 24, 2021
8e236cb
refactor: edit support banner
ruanyf Jun 2, 2021
fdac6cc
docs: add ES2021
ruanyf Jul 10, 2021
9181f78
refactor: update chapters
ruanyf Jul 10, 2021
145e840
Update async.md
yonghui-wang Jul 14, 2021
2139df3
Merge pull request #1063 from yonghui-wang/patch-2
ruanyf Jul 14, 2021
6a972df
docs(operator): fix WeakRef typo
ruanyf Jul 14, 2021
9d51292
Update let.md
wgm7512 Jul 22, 2021
864c2c0
docs(proposals): JSON 模块
ruanyf Jul 22, 2021
12ce67f
Merge pull request #1064 from ThroughTheNight/patch-1
ruanyf Jul 22, 2021
4ead6a0
docs(class): fix typo #1065
ruanyf Jul 27, 2021
1f57596
一处笔误
MrCare Jul 31, 2021
5eb76ed
Merge pull request #1066 from MrCare/patch-1
ruanyf Aug 1, 2021
9cf2cca
refactor: edit support banner
ruanyf Aug 4, 2021
562c688
Update function.md
wgm7512 Aug 6, 2021
0549983
fix: BigInt 不是构造函数
roc-an Aug 6, 2021
a802e37
Update operator.md
newbeman Aug 7, 2021
de8fad9
docs(promise): edit Promise.allSettled()
ruanyf Aug 9, 2021
25f95e0
Merge pull request #1071 from ThroughTheNight/patch-2
ruanyf Aug 9, 2021
f56ca0a
Merge pull request #1072 from roc-an/patch-4
ruanyf Aug 9, 2021
4d9b22c
Merge pull request #1073 from newbeman/patch-1
ruanyf Aug 9, 2021
47dec55
docs(class): 加入 static block
ruanyf Sep 2, 2021
d50868c
refactor: edit support banner
ruanyf Sep 2, 2021
84dba56
docs(style): fixed #1076
ruanyf Sep 8, 2021
248b51a
fix typo: 或 -> 会
imba-tjd Sep 8, 2021
daa455f
use https for some links
imba-tjd Sep 9, 2021
c43e5ca
Merge pull request #1077 from imba-tjd/patch-1
ruanyf Sep 9, 2021
9b210c0
Merge pull request #1078 from imba-tjd/patch-2
ruanyf Sep 9, 2021
9388b33
Update intro.md
Lohoyo Sep 17, 2021
a687c39
Merge pull request #1081 from Lohoyo/patch-1
ruanyf Sep 17, 2021
1fdd34e
docs(object): add AggregateError
ruanyf Sep 19, 2021
e17a581
docs(object): edit AggregateError
ruanyf Sep 19, 2021
180c700
docs(style): edit module style suggestion
ruanyf Oct 4, 2021
de76869
docs(string): fixed #1086
ruanyf Oct 19, 2021
5b57360
Update function.md
Lohoyo Oct 19, 2021
cee42e6
Merge pull request #1088 from Lohoyo/patch-2
ruanyf Oct 21, 2021
728b545
Add script suffix to prevent error
vinzid Oct 31, 2021
a82ed10
Merge pull request #1089 from vinzid/patch-1
ruanyf Nov 1, 2021
1a0eaa5
docs(iterator): fixed #1093
ruanyf Nov 13, 2021
79e871c
docs(array): add at()
ruanyf Nov 14, 2021
2215773
Update string-methods.md
ConradZhangcong Nov 16, 2021
c039b6f
Merge pull request #1094 from ConradZhangcong/gh-pages
ruanyf Nov 22, 2021
74de293
Update array.md
ConradZhangcong Nov 23, 2021
f4030e1
Merge pull request #1097 from ConradZhangcong/gh-pages
ruanyf Nov 23, 2021
a066b9a
docs(module-loader): fix typo
ruanyf Nov 23, 2021
a0265ef
refactor: adjust support code
ruanyf Nov 30, 2021
4781407
refactor: adjust support code
ruanyf Nov 30, 2021
664f685
docs(class): fixed #1099
ruanyf Dec 2, 2021
fd3eb89
docs(proxy): fixed #1100
ruanyf Dec 2, 2021
d9ec0db
refact: edit support script
ruanyf Dec 29, 2021
cddf4b8
refactor: edit support banner
ruanyf Dec 31, 2021
255eedb
docs(reg): edit Unicode 属性类
ruanyf Jan 6, 2022
7fa7ee1
docs(class-extends): edit text
ruanyf Jan 24, 2022
e064886
Update class-extends.md
liuxuan1-1 Feb 5, 2022
b270280
Merge pull request #1108 from liuxuan1-1/patch-2
ruanyf Feb 5, 2022
b2a122e
docs(symbol): fixed #1107
ruanyf Feb 6, 2022
bcfe5ee
refactor: add support banner
ruanyf Feb 7, 2022
8d81fef
refactor: add support banner
ruanyf Feb 7, 2022
9218ba8
refactor: add support banner
ruanyf Feb 8, 2022
ce7fd81
Update object.md
5unnyWind Mar 2, 2022
1fad026
Merge pull request #1110 from 5unnyWind/patch-1
ruanyf Mar 8, 2022
8a214a6
docs(array): edit Array.from()
ruanyf Mar 31, 2022
e856f9e
docs(function): edit 函数参数的解构赋值
ruanyf Apr 9, 2022
95e2e79
docs(object): edit 对象的扩展运算符
ruanyf Apr 9, 2022
ef70334
docs(module): edit import()
ruanyf May 29, 2022
8b9f192
docs(promise): edit Promise.any()
Mookiepiece Jun 4, 2022
b5fa350
Merge pull request #1118 from Mookiepiece/gh-pages
ruanyf Jun 7, 2022
e0772de
docs(promise): edit AggregateError
ruanyf Jun 7, 2022
a837d8f
docs: update ES2022
ruanyf Jun 25, 2022
6880b36
docs(class): fix typo
ruanyf Jun 25, 2022
41970f7
docs: update latest proposals
ruanyf Jun 26, 2022
fbd7faf
docs(decorator): add stage 3 proposal content
ruanyf Jun 27, 2022
f6bb1b9
docs(decorator): edit editor notes
ruanyf Jun 27, 2022
f45f032
docs(regex): fix es2022 indices
xt1995726 Jun 30, 2022
d4d888e
typo: match -> match()
xt1995726 Jun 30, 2022
a090a27
docs(async): fix typo
condorheroblog Jul 1, 2022
aab3314
Merge pull request #1121 from xt1995726/gh-pages
ruanyf Jul 6, 2022
a0c47d5
docs(regex): edit d 修饰符
ruanyf Jul 6, 2022
7d22df8
Merge pull request #1122 from condorheroblog/patch-1
ruanyf Jul 6, 2022
7d030c6
docs(async): edit top-level await
ruanyf Jul 6, 2022
4c0201c
docs(class): fixed #1125 private class field
ruanyf Jul 13, 2022
9348fd2
refactor: edit support banner
ruanyf Jul 23, 2022
010de73
docs(set-map): fix typo
nicong622 Jul 24, 2022
829920e
Merge pull request #1128 from nicong622/patch-1
ruanyf Jul 28, 2022
6bf9155
refactor: edit support banner
ruanyf Jul 31, 2022
686fcff
refactor: edit support banner
ruanyf Jul 31, 2022
21edbe4
docs(symbol): edit text
ruanyf Aug 8, 2022
2b448b6
docs(clss-extends): add inheritance of private property
ruanyf Aug 13, 2022
cf4c947
docs(proposals): edit links
ruanyf Aug 31, 2022
eba1ada
docs(proposals): edit Math.signbit()
ruanyf Aug 31, 2022
5730890
fix: line feed causes HTML rendering error view
condorheroblog Sep 1, 2022
6288fc1
Merge pull request #1135 from condorheroblog/patch-2
ruanyf Sep 1, 2022
e808d04
Update class-extends.md
w-l-l Sep 18, 2022
cded3bc
Merge pull request #1137 from w-l-l/patch-1
ruanyf Sep 18, 2022
1a8f46c
docs(class): fix typo
w-l-l Sep 20, 2022
0eaf2d8
Merge pull request #1140 from w-l-l/patch-3
ruanyf Sep 21, 2022
36b55b9
docs(class): 静态块
ruanyf Sep 21, 2022
ef5e3a2
docs(class): fix typo
ruanyf Oct 18, 2022
7f1acfe
docs(module): move import.meta from proposals to module
ruanyf Oct 23, 2022
406ba6d
docs(module): fix import.meta typo
ruanyf Oct 23, 2022
ef2f878
docs(decorator): edit text
ruanyf Oct 23, 2022
add0bba
docs(decorator): fix typo
w-l-l Dec 4, 2022
1623fdc
Merge pull request #1144 from w-l-l/patch-4
ruanyf Dec 7, 2022
90c0ea7
docs(class-extends): add new materials into super()
ruanyf Dec 23, 2022
f9ef8fd
refactor: add wwads banner
ruanyf Jan 10, 2023
c1a81dc
refactor: adjust wwads banner
ruanyf Jan 10, 2023
536ac22
refactor: adjust wwads banner
ruanyf Jan 10, 2023
010daf6
refactor: adjust wwads banner
ruanyf Jan 10, 2023
09a4657
refactor: adjust wwads banner
ruanyf Jan 10, 2023
b9c8b3d
refactor: adjust wwads banner
ruanyf Jan 10, 2023
b638fb0
refactor: adjust wwads banner
ruanyf Jan 10, 2023
9007f1f
refactor: prism添加对typescript的支持
likeke1997 Jan 16, 2023
6d244f6
Merge pull request #1145 from likeke1997/gh-pages
ruanyf Jan 19, 2023
7fc248d
Update arraybuffer.md
peterwangfeng Feb 1, 2023
07e9c15
Merge pull request #1146 from peterwangfeng/patch-1
ruanyf Feb 2, 2023
7523f55
docs(arraybuffer): update DataView methods
ruanyf Feb 2, 2023
60e128c
module-loader: remove `--experimental-conditional-exports`
islishude Feb 12, 2023
f7b84f2
Merge pull request #1148 from islishude/patch-1
ruanyf Feb 12, 2023
b20e38b
docs(set): fix delete fn
Hencky Mar 2, 2023
badabbf
Merge pull request #1149 from Hencky/gh-pages
ruanyf Mar 2, 2023
0928b9c
docs(set-map): WeakSet methods
Hencky Mar 6, 2023
b68c36d
Merge pull request #1150 from Hencky/gh-pages
ruanyf Mar 6, 2023
50e9ced
docs(operator): #! 命令
ruanyf Apr 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs(module-loader): edit Node.js 的模块加载
  • Loading branch information
ruanyf committed Aug 19, 2020
commit 266199c2d2db82e0ae32134b71ab194485b43b9d
2 changes: 2 additions & 0 deletions docs/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,8 @@ setTimeout(() => console.log(outputPlusValue(100), 1000);

这时,模块的加载会等待依赖模块(上例是`awaiting.js`)的异步操作完成,才执行后面的代码,有点像暂停在那里。所以,它总是会得到正确的`output`,不会因为加载时机的不同,而得到不一样的值。

注意,顶层`await`只能用在 ES6 模块,不能用在 CommonJS 模块。这是因为 CommonJS 模块的`require()`是同步加载,如果有顶层`await`,就没法处理加载了。

下面是顶层`await`的一些使用场景。

```javascript
Expand Down
93 changes: 51 additions & 42 deletions docs/module-loader.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ const isNotModuleScript = this !== undefined;

讨论 Node.js 加载 ES6 模块之前,必须了解 ES6 模块与 CommonJS 模块完全不同。

它们有两个重大差异
它们有三个重大差异

- CommonJS 模块输出的是一个值的拷贝,ES6 模块输出的是值的引用。
- CommonJS 模块是运行时加载,ES6 模块是编译时输出接口。
- CommonJS 模块的`require()`是同步加载模块,ES6 模块的`import`命令是异步加载,有一个独立模块依赖的解析阶段。

第二个差异是因为 CommonJS 加载的是一个对象(即`module.exports`属性),该对象只有在脚本运行完才会生成。而 ES6 模块不是对象,它的对外接口只是一种静态定义,在代码静态解析阶段就会生成。

Expand Down Expand Up @@ -271,11 +272,15 @@ $ babel-node main.js

这就证明了`x.js`和`y.js`加载的都是`C`的同一个实例。

## Node.js 加载
## Node.js 的模块加载方法

### 概述

Node.js 对 ES6 模块的处理比较麻烦,因为它有自己的 CommonJS 模块格式,与 ES6 模块格式是不兼容的。目前的解决方案是,将两者分开,ES6 模块和 CommonJS 采用各自的加载方案。从 v13.2 版本开始,Node.js 已经默认打开了 ES6 模块支持。
JavaScript 现在有两种模块。一种是 ES6 模块,简称 ESM;另一种是 CommonJS 模块,简称 CJS。

CommonJS 模块是 Node.js 专用的,与 ES6 模块不兼容。语法上面,两者最明显的差异是,CommonJS 模块使用`require()`和`module.exports`,ES6 模块使用`import`和`export`。

它们采用不同的加载方案。从 Node.js v13.2 版本开始,Node.js 已经默认打开了 ES6 模块支持。

Node.js 要求 ES6 模块采用`.mjs`后缀文件名。也就是说,只要脚本文件里面使用`import`或者`export`命令,那么就必须采用`.mjs`后缀名。Node.js 遇到`.mjs`文件,就认为它是 ES6 模块,默认启用严格模式,不必在每个模块文件顶部指定`"use strict"`。

Expand All @@ -300,7 +305,7 @@ $ node my-app.js

注意,ES6 模块与 CommonJS 模块尽量不要混用。`require`命令不能加载`.mjs`文件,会报错,只有`import`命令才可以加载`.mjs`文件。反过来,`.mjs`文件里面也不能使用`require`命令,必须使用`import`。

### main 字段
### package.json 的 main 字段

`package.json`文件有两个字段可以指定模块的入口文件:`main`和`exports`。比较简单的模块,可以只使用`main`字段,指定模块加载的入口文件。

Expand All @@ -327,7 +332,7 @@ import { something } from 'es-module-package';

这时,如果用 CommonJS 模块的`require()`命令去加载`es-module-package`模块会报错,因为 CommonJS 模块不能处理`export`命令。

### exports 字段
### package.json 的 exports 字段

`exports`字段的优先级高于`main`字段。它有多种用法。

Expand Down Expand Up @@ -427,7 +432,7 @@ import submodule from './node_modules/es-module-package/private-module.js';

```javascript
{
"exports": {
"exports": {package.json 的
"require": "./main.cjs",
"default": "./main.js"
}
Expand All @@ -447,40 +452,23 @@ import submodule from './node_modules/es-module-package/private-module.js';
}
```

### ES6 模块加载 CommonJS 模块

目前,一个模块同时支持 ES6 和 CommonJS 两种格式的常见方法是,`package.json`文件的`main`字段指定 CommonJS 入口,给 Node.js 使用;`module`字段指定 ES6 模块入口,给打包工具使用,因为 Node.js 不认识`module`字段。
### CommonJS 模块加载 ES6 模块

有了上一节的条件加载以后,Node.js 本身就可以同时处理两种模块
CommonJS 的`require()`命令不能加载 ES6 模块,会报错,只能使用`import()`这个方法加载

```javascript
// ./node_modules/pkg/package.json
{
"type": "module",
"main": "./index.cjs",
"exports": {
"require": "./index.cjs",
"default": "./wrapper.mjs"
}
}
(async () => {
await import('./my-app.mjs');
})();
```

上面代码指定了 CommonJS 入口文件`index.cjs`,下面是这个文件的代码。

```javascript
// ./node_modules/pkg/index.cjs
exports.name = 'value';
```
上面代码可以在 CommonJS 模块中运行。

然后,ES6 模块可以加载这个文件
`require()`不支持 ES6 模块的一个原因是,它是同步加载,而 ES6 模块内部可以使用顶层`await`命令,导致无法被同步加载

```javascript
// ./node_modules/pkg/wrapper.mjs
import cjsModule from './index.cjs';
export const name = cjsModule.name;
```
### ES6 模块加载 CommonJS 模块

注意,`import`命令加载 CommonJS 模块,只能整体加载,不能只加载单一的输出项。
ES6 模块的`import`命令可以加载 CommonJS 模块,但是只能整体加载,不能只加载单一的输出项。

```javascript
// 正确
Expand All @@ -490,6 +478,15 @@ import packageMain from 'commonjs-package';
import { method } from 'commonjs-package';
```

这是因为 ES6 模块需要支持静态代码分析,而 CommonJS 模块的输出接口是`module.exports`,无法被静态分析,所以只能整体加载。

加载单一的输出项,可以写成下面这样。

```bash
import packageMain from 'commonjs-package';
const { method } = packageMain;:w
```

还有一种变通的加载方法,就是使用 Node.js 内置的`module.createRequire()`方法。

```javascript
Expand All @@ -505,19 +502,33 @@ const cjs = require('./cjs.cjs');
cjs === 'cjs'; // true
```

上面代码中,ES6 模块通过`module.createRequire()`方法可以加载 CommonJS 模块
上面代码中,ES6 模块通过`module.createRequire()`方法可以加载 CommonJS 模块。但是,这种写法等于将 ES6 和 CommonJS 混在一起了,所以不建议使用。

### CommonJS 模块加载 ES6 模块
## 同时支持两种格式的模块

一个模块同时要支持 CommonJS 和 ES6 两种格式,也很容易。

CommonJS 的`require`命令不能加载 ES6 模块,会报错,只能使用`import()`这个方法加载。
如果原始模块是 ES6 格式,那么需要给出一个整体输出接口,比如`export default obj`,使得 CommonJS 可以用`import()`进行加载。

如果原始模块是 CommonJS 格式,那么可以加一个包装层。

```javascript
(async () => {
await import('./my-app.mjs');
})();
import cjsModule from '../index.js';
export const foo = cjsModule.foo;
```

上面代码可以在 CommonJS 模块中运行。
上面代码先整体输入 CommonJS 模块,然后再根据需要输出具名接口。最后,可以将它放在一个子目录,再放一个单独的`package.json`文件,指明`{ module: "type" }`。

另一种做法是在`package.json`文件的`exports`字段,指明两种格式模块各自的加载入口。

```bash
"exports":{
"require": "./index.js",
"import": "./esm/wrapper.js"
}
```

上面代码指定`require()`和`import`,加载该模块会自动切换到不一样的入口文件。

### Node.js 的内置模块

Expand Down Expand Up @@ -558,11 +569,9 @@ import './foo.mjs?query=1'; // 加载 ./foo 传入参数 ?query=1

目前,Node.js 的`import`命令只支持加载本地模块(`file:`协议)和`data:`协议,不支持加载远程模块。另外,脚本路径只支持相对路径,不支持绝对路径(即以`/`或`//`开头的路径)。

最后,Node 的`import`命令是异步加载,这一点与浏览器的处理方法相同。

### 内部变量

ES6 模块应该是通用的,同一个模块不用修改,就可以用在浏览器环境和服务器环境。为了达到这个目标,Node 规定 ES6 模块之中不能使用 CommonJS 模块的特有的一些内部变量。
ES6 模块应该是通用的,同一个模块不用修改,就可以用在浏览器环境和服务器环境。为了达到这个目标,Node.js 规定 ES6 模块之中不能使用 CommonJS 模块的特有的一些内部变量。

首先,就是`this`关键字。ES6 模块之中,顶层的`this`指向`undefined`;CommonJS 模块的顶层`this`指向当前模块,这是两者的一个重大差异。

Expand Down