Commit f9dc1ea
module: add __esModule to require()'d ESM
Tooling in the ecosystem have been using the __esModule property to
recognize transpiled ESM in consuming code. For example, a 'log'
package written in ESM:
export function log(val) { console.log(val); }
Can be transpiled as:
exports.__esModule = true;
exports.default = function log(val) { console.log(val); }
The consuming code may be written like this in ESM:
import log from 'log'
Which gets transpiled to:
const _mod = require('log');
const log = _mod.__esModule ? _mod.default : _mod;
So to allow transpiled consuming code to recognize require()'d real ESM
as ESM and pick up the default exports, we add a __esModule property by
building a source text module facade for any module that has a default
export and add .__esModule = true to the exports. We don't do this to
modules that don't have default exports to avoid the unnecessary
overhead. This maintains the enumerability of the re-exported names
and the live binding of the exports.
The source of the facade is defined as a constant per-isolate property
required_module_facade_source_string, which looks like this
export * from 'original';
export { default } from 'original';
export const __esModule = true;
And the 'original' module request is always resolved by
createRequiredModuleFacade() to wrap which is a ModuleWrap wrapping
over the original module.
PR-URL: #52166
Backport-PR-URL: #56927
Refs: #52134
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Refs: #526971 parent 18593b7 commit f9dc1eaCopy full SHA for f9dc1ea
File tree
Expand file treeCollapse file tree
30 files changed
+316
-55
lines changedOpen diff view settings
Filter options
- doc/api
- lib/internal/modules
- cjs
- esm
- src
- test
- common
- es-module
- fixtures/es-modules
- transpiled-cjs-require-module
- dist
- node_modules/logger
- src
- parallel
Expand file treeCollapse file tree
30 files changed
+316
-55
lines changedOpen diff view settings
Collapse file
+27-9Lines changed: 27 additions & 9 deletions
- Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
195 | 195 | |
196 | 196 | |
197 | 197 | |
198 | | - |
| 198 | + |
199 | 199 | |
200 | 200 | |
201 | 201 | |
| 202 | + |
| 203 | + |
202 | 204 | |
203 | | - |
| 205 | + |
204 | 206 | |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
205 | 211 | |
206 | 212 | |
207 | 213 | |
208 | 214 | |
209 | 215 | |
210 | 216 | |
| 217 | + |
| 218 | + |
211 | 219 | |
212 | | - |
| 220 | + |
| 221 | + |
213 | 222 | |
214 | | - |
215 | 223 | |
216 | 224 | |
217 | | - |
218 | 225 | |
219 | | - |
220 | | - |
221 | | - |
222 | | - |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
223 | 232 | |
224 | 233 | |
| 234 | + |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
225 | 243 | |
226 | 244 | |
227 | 245 | |
|
Collapse file
lib/internal/modules/cjs/loader.js
Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js+51-4Lines changed: 51 additions & 4 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
41 | 41 | |
42 | 42 | |
43 | 43 | |
| 44 | + |
44 | 45 | |
45 | 46 | |
46 | 47 | |
| ||
71 | 72 | |
72 | 73 | |
73 | 74 | |
74 | | - |
| 75 | + |
75 | 76 | |
76 | 77 | |
77 | 78 | |
| ||
1340 | 1341 | |
1341 | 1342 | |
1342 | 1343 | |
1343 | | - |
1344 | | - |
1345 | | - |
| 1344 | + |
| 1345 | + |
| 1346 | + |
| 1347 | + |
| 1348 | + |
| 1349 | + |
| 1350 | + |
| 1351 | + |
| 1352 | + |
| 1353 | + |
| 1354 | + |
| 1355 | + |
| 1356 | + |
| 1357 | + |
| 1358 | + |
| 1359 | + |
| 1360 | + |
| 1361 | + |
| 1362 | + |
| 1363 | + |
| 1364 | + |
| 1365 | + |
| 1366 | + |
| 1367 | + |
| 1368 | + |
| 1369 | + |
| 1370 | + |
| 1371 | + |
| 1372 | + |
| 1373 | + |
| 1374 | + |
| 1375 | + |
| 1376 | + |
| 1377 | + |
| 1378 | + |
| 1379 | + |
| 1380 | + |
| 1381 | + |
| 1382 | + |
| 1383 | + |
| 1384 | + |
| 1385 | + |
| 1386 | + |
| 1387 | + |
| 1388 | + |
| 1389 | + |
| 1390 | + |
| 1391 | + |
| 1392 | + |
1346 | 1393 | |
1347 | 1394 | |
1348 | 1395 | |
|
Collapse file
lib/internal/modules/esm/loader.js
Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js+3-3Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
269 | 269 | |
270 | 270 | |
271 | 271 | |
272 | | - |
| 272 | + |
273 | 273 | |
274 | 274 | |
275 | 275 | |
| ||
294 | 294 | |
295 | 295 | |
296 | 296 | |
297 | | - |
| 297 | + |
298 | 298 | |
299 | 299 | |
300 | 300 | |
| ||
306 | 306 | |
307 | 307 | |
308 | 308 | |
309 | | - |
| 309 | + |
310 | 310 | |
311 | 311 | |
312 | 312 | |
|
Collapse file
+2Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1061 | 1061 | |
1062 | 1062 | |
1063 | 1063 | |
| 1064 | + |
| 1065 | + |
1064 | 1066 | |
1065 | 1067 | |
1066 | 1068 | |
|
Collapse file
+6Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
252 | 252 | |
253 | 253 | |
254 | 254 | |
| 255 | + |
255 | 256 | |
256 | 257 | |
257 | 258 | |
| ||
285 | 286 | |
286 | 287 | |
287 | 288 | |
| 289 | + |
| 290 | + |
| 291 | + |
| 292 | + |
| 293 | + |
288 | 294 | |
289 | 295 | |
290 | 296 | |
|
Collapse file
+70Lines changed: 70 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
966 | 966 | |
967 | 967 | |
968 | 968 | |
| 969 | + |
| 970 | + |
| 971 | + |
| 972 | + |
| 973 | + |
| 974 | + |
| 975 | + |
| 976 | + |
| 977 | + |
| 978 | + |
| 979 | + |
| 980 | + |
| 981 | + |
| 982 | + |
| 983 | + |
| 984 | + |
| 985 | + |
| 986 | + |
| 987 | + |
| 988 | + |
| 989 | + |
| 990 | + |
| 991 | + |
| 992 | + |
| 993 | + |
| 994 | + |
| 995 | + |
| 996 | + |
| 997 | + |
| 998 | + |
| 999 | + |
| 1000 | + |
| 1001 | + |
| 1002 | + |
| 1003 | + |
| 1004 | + |
| 1005 | + |
| 1006 | + |
| 1007 | + |
| 1008 | + |
| 1009 | + |
| 1010 | + |
| 1011 | + |
| 1012 | + |
| 1013 | + |
| 1014 | + |
| 1015 | + |
| 1016 | + |
| 1017 | + |
| 1018 | + |
| 1019 | + |
| 1020 | + |
| 1021 | + |
| 1022 | + |
| 1023 | + |
| 1024 | + |
| 1025 | + |
| 1026 | + |
| 1027 | + |
| 1028 | + |
| 1029 | + |
| 1030 | + |
| 1031 | + |
| 1032 | + |
969 | 1033 | |
970 | 1034 | |
971 | 1035 | |
| ||
998 | 1062 | |
999 | 1063 | |
1000 | 1064 | |
| 1065 | + |
| 1066 | + |
| 1067 | + |
| 1068 | + |
1001 | 1069 | |
1002 | 1070 | |
1003 | 1071 | |
| ||
1038 | 1106 | |
1039 | 1107 | |
1040 | 1108 | |
| 1109 | + |
| 1110 | + |
1041 | 1111 | |
1042 | 1112 | |
1043 | 1113 | |
|
Collapse file
+3Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
87 | 87 | |
88 | 88 | |
89 | 89 | |
| 90 | + |
| 91 | + |
| 92 | + |
90 | 93 | |
91 | 94 | |
92 | 95 | |
|
Collapse file
+7-2Lines changed: 7 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
927 | 927 | |
928 | 928 | |
929 | 929 | |
930 | | - |
| 930 | + |
| 931 | + |
| 932 | + |
| 933 | + |
| 934 | + |
| 935 | + |
931 | 936 | |
932 | | - |
| 937 | + |
933 | 938 | |
934 | 939 | |
935 | 940 | |
|
Collapse file
test/es-module/test-require-module-default-extension.js
Copy file name to clipboardExpand all lines: test/es-module/test-require-module-default-extension.js+2-4Lines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1 | 1 | |
2 | 2 | |
3 | 3 | |
4 | | - |
| 4 | + |
5 | 5 | |
6 | | - |
7 | 6 | |
8 | 7 | |
9 | | - |
10 | | - |
| 8 | + |
11 | 9 | |
12 | 10 | |
13 | 11 | |
|
Collapse file
test/es-module/test-require-module-defined-esmodule.js
Copy file name to clipboard+23Lines changed: 23 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
0 commit comments