@scelar/nodepod 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__sw__.js +642 -642
- package/dist/__tests__/bench/integration.bench.d.ts +1 -0
- package/dist/__tests__/bench/memory-volume.bench.d.ts +1 -0
- package/dist/__tests__/bench/polyfills.bench.d.ts +1 -0
- package/dist/__tests__/bench/script-engine.bench.d.ts +1 -0
- package/dist/__tests__/bench/shell.bench.d.ts +1 -0
- package/dist/__tests__/bench/syntax-transforms.bench.d.ts +1 -0
- package/dist/__tests__/bench/version-resolver.bench.d.ts +1 -0
- package/dist/__tests__/buffer.test.d.ts +1 -0
- package/dist/__tests__/byte-encoding.test.d.ts +1 -0
- package/dist/__tests__/digest.test.d.ts +1 -0
- package/dist/__tests__/events.test.d.ts +1 -0
- package/dist/__tests__/memory-volume.test.d.ts +1 -0
- package/dist/__tests__/path.test.d.ts +1 -0
- package/dist/__tests__/process.test.d.ts +1 -0
- package/dist/__tests__/script-engine.test.d.ts +1 -0
- package/dist/__tests__/shell-builtins.test.d.ts +1 -0
- package/dist/__tests__/shell-interpreter.test.d.ts +1 -0
- package/dist/__tests__/shell-parser.test.d.ts +1 -0
- package/dist/__tests__/stream.test.d.ts +1 -0
- package/dist/__tests__/syntax-transforms.test.d.ts +1 -0
- package/dist/__tests__/version-resolver.test.d.ts +1 -0
- package/dist/{child_process-Dopvyd-E.js → child_process-53fMkug_.js} +4 -4
- package/dist/child_process-53fMkug_.js.map +1 -0
- package/dist/{child_process-B38qoN6R.cjs → child_process-lxSKECHq.cjs} +5 -5
- package/dist/child_process-lxSKECHq.cjs.map +1 -0
- package/dist/{index--Qr8LVpQ.js → index-B8lyh_ti.js} +1316 -559
- package/dist/index-B8lyh_ti.js.map +1 -0
- package/dist/{index-cnitc68U.cjs → index-C-TQIrdG.cjs} +1422 -612
- package/dist/index-C-TQIrdG.cjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/memory-volume.d.ts +1 -1
- package/dist/polyfills/wasi.d.ts +45 -4
- package/dist/script-engine.d.ts +2 -0
- package/dist/sdk/nodepod.d.ts +4 -3
- package/dist/sdk/types.d.ts +6 -0
- package/dist/syntax-transforms.d.ts +1 -0
- package/dist/threading/process-manager.d.ts +1 -1
- package/dist/threading/worker-protocol.d.ts +1 -1
- package/package.json +5 -3
- package/src/__tests__/bench/integration.bench.ts +117 -0
- package/src/__tests__/bench/memory-volume.bench.ts +115 -0
- package/src/__tests__/bench/polyfills.bench.ts +147 -0
- package/src/__tests__/bench/script-engine.bench.ts +104 -0
- package/src/__tests__/bench/shell.bench.ts +101 -0
- package/src/__tests__/bench/syntax-transforms.bench.ts +82 -0
- package/src/__tests__/bench/version-resolver.bench.ts +95 -0
- package/src/__tests__/buffer.test.ts +273 -0
- package/src/__tests__/byte-encoding.test.ts +98 -0
- package/src/__tests__/digest.test.ts +44 -0
- package/src/__tests__/events.test.ts +245 -0
- package/src/__tests__/memory-volume.test.ts +443 -0
- package/src/__tests__/path.test.ts +181 -0
- package/src/__tests__/process.test.ts +129 -0
- package/src/__tests__/script-engine.test.ts +229 -0
- package/src/__tests__/shell-builtins.test.ts +357 -0
- package/src/__tests__/shell-interpreter.test.ts +157 -0
- package/src/__tests__/shell-parser.test.ts +204 -0
- package/src/__tests__/stream.test.ts +142 -0
- package/src/__tests__/syntax-transforms.test.ts +158 -0
- package/src/__tests__/version-resolver.test.ts +184 -0
- package/src/constants/cdn-urls.ts +18 -18
- package/src/helpers/byte-encoding.ts +51 -39
- package/src/index.ts +192 -192
- package/src/memory-volume.ts +968 -941
- package/src/module-transformer.ts +368 -368
- package/src/packages/installer.ts +396 -396
- package/src/packages/version-resolver.ts +12 -2
- package/src/polyfills/buffer.ts +633 -628
- package/src/polyfills/child_process.ts +2288 -2288
- package/src/polyfills/esbuild.ts +854 -854
- package/src/polyfills/events.ts +282 -276
- package/src/polyfills/fs.ts +2888 -2888
- package/src/polyfills/http.ts +1450 -1449
- package/src/polyfills/process.ts +721 -690
- package/src/polyfills/readline.ts +692 -692
- package/src/polyfills/stream.ts +1620 -1620
- package/src/polyfills/tty.ts +71 -71
- package/src/polyfills/wasi.ts +1284 -22
- package/src/request-proxy.ts +716 -716
- package/src/script-engine.ts +465 -146
- package/src/sdk/nodepod.ts +525 -509
- package/src/sdk/types.ts +7 -0
- package/src/syntax-transforms.ts +543 -561
- package/src/threading/offload-worker.ts +383 -383
- package/src/threading/offload.ts +271 -271
- package/src/threading/process-manager.ts +956 -956
- package/src/threading/process-worker-entry.ts +858 -854
- package/src/threading/worker-protocol.ts +1 -1
- package/dist/child_process-B38qoN6R.cjs.map +0 -1
- package/dist/child_process-Dopvyd-E.js.map +0 -1
- package/dist/index--Qr8LVpQ.js.map +0 -1
- package/dist/index-cnitc68U.cjs.map +0 -1
package/src/syntax-transforms.ts
CHANGED
|
@@ -1,561 +1,543 @@
|
|
|
1
|
-
// ESM-to-CJS conversion via acorn AST, with regex fallback
|
|
2
|
-
|
|
3
|
-
import * as acorn from "acorn";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
if (
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
)
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const
|
|
389
|
-
const
|
|
390
|
-
const
|
|
391
|
-
if (
|
|
392
|
-
const absStart =
|
|
393
|
-
let absEnd = absStart + 5;
|
|
394
|
-
while (absEnd < code.length && code[absEnd] === " ") absEnd++;
|
|
395
|
-
patches.push([absStart, absEnd, ""]);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
if (
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
out = out.replace(
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
);
|
|
492
|
-
out = out.replace(
|
|
493
|
-
|
|
494
|
-
(_m, specs, src) => {
|
|
495
|
-
const fixed = specs.replace(
|
|
496
|
-
return `const {${fixed}} = require("${src}");`;
|
|
497
|
-
},
|
|
498
|
-
);
|
|
499
|
-
out = out.replace(
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
);
|
|
503
|
-
|
|
504
|
-
out = out.replace(
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
);
|
|
522
|
-
out = out.replace(
|
|
523
|
-
|
|
524
|
-
(_m, specs
|
|
525
|
-
const binds = specs
|
|
526
|
-
.split(",")
|
|
527
|
-
.map((s: string) => {
|
|
528
|
-
const parts = s.trim().split(
|
|
529
|
-
const local = parts[0].trim();
|
|
530
|
-
const exported = parts.length > 1 ? parts[1].trim() : local;
|
|
531
|
-
return `exports.${exported} =
|
|
532
|
-
})
|
|
533
|
-
.join("; ");
|
|
534
|
-
return binds + ";";
|
|
535
|
-
},
|
|
536
|
-
);
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
const local = parts[0].trim();
|
|
545
|
-
const exported = parts.length > 1 ? parts[1].trim() : local;
|
|
546
|
-
return `exports.${exported} = ${local}`;
|
|
547
|
-
})
|
|
548
|
-
.join("; ");
|
|
549
|
-
return binds + ";";
|
|
550
|
-
},
|
|
551
|
-
);
|
|
552
|
-
// named exports
|
|
553
|
-
out = out.replace(
|
|
554
|
-
/export\s+async\s+function\s+(\w+)/g,
|
|
555
|
-
"exports.$1 = async function $1",
|
|
556
|
-
);
|
|
557
|
-
out = out.replace(/export\s+function\s+(\w+)/g, "exports.$1 = function $1");
|
|
558
|
-
out = out.replace(/export\s+class\s+(\w+)/g, "exports.$1 = class $1");
|
|
559
|
-
out = out.replace(/export\s+(?:const|let|var)\s+(\w+)\s*=/g, "exports.$1 =");
|
|
560
|
-
return out;
|
|
561
|
-
}
|
|
1
|
+
// ESM-to-CJS conversion via acorn AST, with regex fallback
|
|
2
|
+
|
|
3
|
+
import * as acorn from "acorn";
|
|
4
|
+
|
|
5
|
+
// Pre-compiled regex patterns for fallback paths (avoid per-call compilation)
|
|
6
|
+
const RE_AWAIT_QUICK = /\bawait\b/;
|
|
7
|
+
const RE_ASYNC_QUICK = /\basync\b/;
|
|
8
|
+
const RE_AWAIT_LOOKAHEAD = /(?<![.\w])await\s+(?=[\w$("'\[`!~+\-/])/;
|
|
9
|
+
const RE_AWAIT_LOOKAHEAD_G = /(?<![.\w])await\s+(?=[\w$("'\[`!~+\-/])/g;
|
|
10
|
+
const RE_ASYNC_FN_G = /(?<![.\w])async\s+(?=function[\s*(])/g;
|
|
11
|
+
const RE_ASYNC_PAREN_G = /(?<![.\w])async\s+(?=\()/g;
|
|
12
|
+
const RE_ASYNC_ARROW_G = /(?<![.\w])async\s+(?=\w+\s*=>)/g;
|
|
13
|
+
const RE_TYPE_IMPORT_BRACES = /import\s+type\s+\{[^}]*\}\s+from\s+['"][^'"]+['"]\s*;?/g;
|
|
14
|
+
const RE_TYPE_IMPORT_DEFAULT = /import\s+type\s+\w+\s+from\s+['"][^'"]+['"]\s*;?/g;
|
|
15
|
+
const RE_TYPE_IMPORT_STAR = /import\s+type\s+\*\s+as\s+\w+\s+from\s+['"][^'"]+['"]\s*;?/g;
|
|
16
|
+
const RE_MIXED_TYPE_IMPORT = /import\s+\{([^}]*\btype\s+\w+[^}]*)\}\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
17
|
+
const RE_TYPE_EXPORT_FROM = /export\s+type\s+\{[^}]*\}\s+from\s+['"][^'"]+['"]\s*;?/g;
|
|
18
|
+
const RE_TYPE_EXPORT = /export\s+type\s+\{[^}]*\}\s*;?/g;
|
|
19
|
+
const RE_IMPORT_STAR = /import\s+\*\s+as\s+(\w+)\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
20
|
+
const RE_IMPORT_DEFAULT_NAMED = /import\s+(\w+)\s*,\s*\{([^}]+)\}\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
21
|
+
const RE_IMPORT_DEFAULT = /import\s+(\w+)\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
22
|
+
const RE_IMPORT_NAMED = /import\s+\{([^}]+)\}\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
23
|
+
const RE_IMPORT_SIDE_EFFECT = /import\s+['"]([^'"]+)['"]\s*;?/g;
|
|
24
|
+
const RE_EXPORT_DEFAULT_CLASS = /export\s+default\s+class\s+(\w+)/g;
|
|
25
|
+
const RE_EXPORT_DEFAULT_FN_NAMED = /export\s+default\s+function\s+(\w+)/g;
|
|
26
|
+
const RE_EXPORT_DEFAULT_FN_ANON = /export\s+default\s+function\s*\(/g;
|
|
27
|
+
const RE_EXPORT_DEFAULT = /export\s+default\s+/g;
|
|
28
|
+
const RE_EXPORT_STAR = /export\s+\*\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
29
|
+
const RE_EXPORT_NAMED_FROM = /export\s+\{([^}]+)\}\s+from\s+['"]([^'"]+)['"]\s*;?/g;
|
|
30
|
+
const RE_EXPORT_NAMED = /export\s+\{([^}]+)\}\s*;?/g;
|
|
31
|
+
const RE_EXPORT_ASYNC_FN = /export\s+async\s+function\s+(\w+)/g;
|
|
32
|
+
const RE_EXPORT_FN = /export\s+function\s+(\w+)/g;
|
|
33
|
+
const RE_EXPORT_CLASS = /export\s+class\s+(\w+)/g;
|
|
34
|
+
const RE_EXPORT_VAR = /export\s+(?:const|let|var)\s+(\w+)\s*=/g;
|
|
35
|
+
const RE_AS_RENAME = /(\w+)\s+as\s+(\w+)/g;
|
|
36
|
+
const RE_TYPE_SPEC = /^\s*type\s+\w+/;
|
|
37
|
+
const RE_AS_SPLIT = /\s+as\s+/;
|
|
38
|
+
|
|
39
|
+
export function esmToCjs(code: string): string {
|
|
40
|
+
try {
|
|
41
|
+
return esmToCjsViaAst(code);
|
|
42
|
+
} catch {
|
|
43
|
+
return esmToCjsViaRegex(code);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// collect ESM→CJS patches from a pre-parsed AST, pushes into the patches array
|
|
48
|
+
export function collectEsmCjsPatches(
|
|
49
|
+
ast: any,
|
|
50
|
+
code: string,
|
|
51
|
+
patches: Array<[number, number, string]>,
|
|
52
|
+
): void {
|
|
53
|
+
const hasDefaultExport = ast.body.some(
|
|
54
|
+
(n: any) => n.type === "ExportDefaultDeclaration",
|
|
55
|
+
);
|
|
56
|
+
const hasNamedExport = ast.body.some(
|
|
57
|
+
(n: any) => n.type === "ExportNamedDeclaration",
|
|
58
|
+
);
|
|
59
|
+
const mixedExports = hasDefaultExport && hasNamedExport;
|
|
60
|
+
|
|
61
|
+
for (const node of ast.body) {
|
|
62
|
+
if (node.type === "ImportDeclaration") {
|
|
63
|
+
const src = node.source.value;
|
|
64
|
+
const specs = node.specifiers;
|
|
65
|
+
|
|
66
|
+
if (specs.length === 0) {
|
|
67
|
+
patches.push([
|
|
68
|
+
node.start,
|
|
69
|
+
node.end,
|
|
70
|
+
`require(${JSON.stringify(src)});`,
|
|
71
|
+
]);
|
|
72
|
+
} else {
|
|
73
|
+
const defSpec = specs.find(
|
|
74
|
+
(s: any) => s.type === "ImportDefaultSpecifier",
|
|
75
|
+
);
|
|
76
|
+
const nsSpec = specs.find(
|
|
77
|
+
(s: any) => s.type === "ImportNamespaceSpecifier",
|
|
78
|
+
);
|
|
79
|
+
const namedSpecs = specs.filter(
|
|
80
|
+
(s: any) => s.type === "ImportSpecifier",
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const lines: string[] = [];
|
|
84
|
+
const tmpVar = `__import_${node.start}`;
|
|
85
|
+
const needsTmp = defSpec && (namedSpecs.length > 0 || nsSpec);
|
|
86
|
+
|
|
87
|
+
if (needsTmp) {
|
|
88
|
+
lines.push(`const ${tmpVar} = require(${JSON.stringify(src)})`);
|
|
89
|
+
lines.push(
|
|
90
|
+
`const ${defSpec.local.name} = ${tmpVar}.__esModule ? ${tmpVar}.default : ${tmpVar}`,
|
|
91
|
+
);
|
|
92
|
+
} else if (defSpec) {
|
|
93
|
+
lines.push(
|
|
94
|
+
`const ${defSpec.local.name} = (function(m) { return m.__esModule ? m.default : m; })(require(${JSON.stringify(src)}))`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (nsSpec) {
|
|
99
|
+
if (!needsTmp) {
|
|
100
|
+
lines.push(
|
|
101
|
+
`const ${nsSpec.local.name} = require(${JSON.stringify(src)})`,
|
|
102
|
+
);
|
|
103
|
+
} else {
|
|
104
|
+
lines.push(`const ${nsSpec.local.name} = ${tmpVar}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (namedSpecs.length > 0) {
|
|
109
|
+
const binds = namedSpecs
|
|
110
|
+
.map((s: any) =>
|
|
111
|
+
s.imported.name === s.local.name
|
|
112
|
+
? s.local.name
|
|
113
|
+
: `${s.imported.name}: ${s.local.name}`,
|
|
114
|
+
)
|
|
115
|
+
.join(", ");
|
|
116
|
+
if (needsTmp) {
|
|
117
|
+
lines.push(`const { ${binds} } = ${tmpVar}`);
|
|
118
|
+
} else {
|
|
119
|
+
lines.push(`const { ${binds} } = require(${JSON.stringify(src)})`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
patches.push([node.start, node.end, lines.join(";\n") + ";"]);
|
|
123
|
+
}
|
|
124
|
+
} else if (node.type === "ExportDefaultDeclaration") {
|
|
125
|
+
const decl = node.declaration;
|
|
126
|
+
const exportTarget = mixedExports ? "exports.default" : "module.exports";
|
|
127
|
+
|
|
128
|
+
if (
|
|
129
|
+
(decl.type === "FunctionDeclaration" ||
|
|
130
|
+
decl.type === "ClassDeclaration") &&
|
|
131
|
+
decl.id?.name
|
|
132
|
+
) {
|
|
133
|
+
// Non-overlapping: remove "export default ", append binding
|
|
134
|
+
patches.push([node.start, decl.start, ""]);
|
|
135
|
+
patches.push([
|
|
136
|
+
node.end,
|
|
137
|
+
node.end,
|
|
138
|
+
`;\n${exportTarget} = ${decl.id.name};`,
|
|
139
|
+
]);
|
|
140
|
+
} else {
|
|
141
|
+
// Replace "export default " with assignment target
|
|
142
|
+
patches.push([node.start, decl.start, `${exportTarget} = `]);
|
|
143
|
+
}
|
|
144
|
+
} else if (node.type === "ExportNamedDeclaration") {
|
|
145
|
+
if (node.declaration) {
|
|
146
|
+
const decl = node.declaration;
|
|
147
|
+
if (
|
|
148
|
+
decl.type === "FunctionDeclaration" ||
|
|
149
|
+
decl.type === "ClassDeclaration"
|
|
150
|
+
) {
|
|
151
|
+
const name = decl.id.name;
|
|
152
|
+
// Non-overlapping patches: remove "export " prefix, append binding
|
|
153
|
+
patches.push([node.start, decl.start, ""]);
|
|
154
|
+
patches.push([
|
|
155
|
+
node.end,
|
|
156
|
+
node.end,
|
|
157
|
+
`;\nexports.${name} = ${name};`,
|
|
158
|
+
]);
|
|
159
|
+
} else if (decl.type === "VariableDeclaration") {
|
|
160
|
+
const needsLiveBinding = decl.kind === "let" || decl.kind === "var";
|
|
161
|
+
const hasDestructuring = decl.declarations.some(
|
|
162
|
+
(d: any) =>
|
|
163
|
+
d.id.type === "ObjectPattern" || d.id.type === "ArrayPattern",
|
|
164
|
+
);
|
|
165
|
+
// Remove "export " prefix
|
|
166
|
+
patches.push([node.start, decl.start, ""]);
|
|
167
|
+
// Append export bindings after declaration
|
|
168
|
+
const bindings: string[] = [];
|
|
169
|
+
if (hasDestructuring) {
|
|
170
|
+
for (const d of decl.declarations) {
|
|
171
|
+
for (const name of extractBindingNames(d.id)) {
|
|
172
|
+
if (needsLiveBinding) {
|
|
173
|
+
bindings.push(
|
|
174
|
+
`Object.defineProperty(exports, ${JSON.stringify(name)}, { get() { return ${name}; }, enumerable: true })`,
|
|
175
|
+
);
|
|
176
|
+
} else {
|
|
177
|
+
bindings.push(`exports.${name} = ${name}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
for (const d of decl.declarations) {
|
|
183
|
+
if (needsLiveBinding) {
|
|
184
|
+
bindings.push(
|
|
185
|
+
`Object.defineProperty(exports, ${JSON.stringify(d.id.name)}, { get() { return ${d.id.name}; }, enumerable: true })`,
|
|
186
|
+
);
|
|
187
|
+
} else {
|
|
188
|
+
bindings.push(`exports.${d.id.name} = ${d.id.name}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
patches.push([
|
|
193
|
+
node.end,
|
|
194
|
+
node.end,
|
|
195
|
+
"\n" + bindings.join(";\n") + ";",
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
} else if (node.source) {
|
|
199
|
+
const src = node.source.value;
|
|
200
|
+
const tmp = `__reexport_${node.start}`;
|
|
201
|
+
const lines = [`const ${tmp} = require(${JSON.stringify(src)})`];
|
|
202
|
+
for (const spec of node.specifiers) {
|
|
203
|
+
if (spec.local.name === "default") {
|
|
204
|
+
lines.push(
|
|
205
|
+
`exports.${spec.exported.name} = ${tmp}.__esModule ? ${tmp}.default : ${tmp}`,
|
|
206
|
+
);
|
|
207
|
+
} else {
|
|
208
|
+
lines.push(
|
|
209
|
+
`exports.${spec.exported.name} = ${tmp}.${spec.local.name}`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
patches.push([node.start, node.end, lines.join(";\n") + ";"]);
|
|
214
|
+
} else {
|
|
215
|
+
const lines = node.specifiers.map(
|
|
216
|
+
(s: any) => `exports.${s.exported.name} = ${s.local.name}`,
|
|
217
|
+
);
|
|
218
|
+
patches.push([node.start, node.end, lines.join(";\n") + ";"]);
|
|
219
|
+
}
|
|
220
|
+
} else if (node.type === "ExportAllDeclaration") {
|
|
221
|
+
const src = node.source.value;
|
|
222
|
+
patches.push([
|
|
223
|
+
node.start,
|
|
224
|
+
node.end,
|
|
225
|
+
`Object.assign(exports, require(${JSON.stringify(src)}))`,
|
|
226
|
+
]);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function esmToCjsViaAst(code: string): string {
|
|
232
|
+
const ast = acorn.parse(code, {
|
|
233
|
+
ecmaVersion: "latest",
|
|
234
|
+
sourceType: "module",
|
|
235
|
+
});
|
|
236
|
+
const patches: Array<[number, number, string]> = [];
|
|
237
|
+
collectEsmCjsPatches(ast as any, code, patches);
|
|
238
|
+
|
|
239
|
+
let output = code;
|
|
240
|
+
patches.sort((a, b) => b[0] - a[0] || b[1] - a[1]);
|
|
241
|
+
for (const [s, e, r] of patches)
|
|
242
|
+
output = output.slice(0, s) + r + output.slice(e);
|
|
243
|
+
return output;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// extract all bound names from a destructuring pattern or identifier
|
|
247
|
+
function extractBindingNames(pattern: any): string[] {
|
|
248
|
+
if (pattern.type === "Identifier") {
|
|
249
|
+
return [pattern.name];
|
|
250
|
+
}
|
|
251
|
+
if (pattern.type === "ObjectPattern") {
|
|
252
|
+
const names: string[] = [];
|
|
253
|
+
for (const prop of pattern.properties) {
|
|
254
|
+
if (prop.type === "RestElement") {
|
|
255
|
+
names.push(...extractBindingNames(prop.argument));
|
|
256
|
+
} else {
|
|
257
|
+
names.push(...extractBindingNames(prop.value));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return names;
|
|
261
|
+
}
|
|
262
|
+
if (pattern.type === "ArrayPattern") {
|
|
263
|
+
const names: string[] = [];
|
|
264
|
+
for (const elem of pattern.elements) {
|
|
265
|
+
if (elem) {
|
|
266
|
+
if (elem.type === "RestElement") {
|
|
267
|
+
names.push(...extractBindingNames(elem.argument));
|
|
268
|
+
} else {
|
|
269
|
+
names.push(...extractBindingNames(elem));
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return names;
|
|
274
|
+
}
|
|
275
|
+
if (pattern.type === "AssignmentPattern") {
|
|
276
|
+
return extractBindingNames(pattern.left);
|
|
277
|
+
}
|
|
278
|
+
return [];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function hasTopLevelAwait(code: string): boolean {
|
|
282
|
+
if (!RE_AWAIT_QUICK.test(code)) return false;
|
|
283
|
+
|
|
284
|
+
try {
|
|
285
|
+
let ast: any;
|
|
286
|
+
try {
|
|
287
|
+
ast = acorn.parse(code, {
|
|
288
|
+
ecmaVersion: "latest",
|
|
289
|
+
sourceType: "script",
|
|
290
|
+
allowAwaitOutsideFunction: true,
|
|
291
|
+
});
|
|
292
|
+
} catch {
|
|
293
|
+
ast = acorn.parse(code, {
|
|
294
|
+
ecmaVersion: "latest",
|
|
295
|
+
sourceType: "module",
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
let found = false;
|
|
300
|
+
let insideAsync = 0;
|
|
301
|
+
|
|
302
|
+
function walk(node: any): void {
|
|
303
|
+
if (found || !node || typeof node !== "object") return;
|
|
304
|
+
if (Array.isArray(node)) {
|
|
305
|
+
for (const child of node) walk(child);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (typeof node.type !== "string") return;
|
|
309
|
+
|
|
310
|
+
const isAsyncFn =
|
|
311
|
+
(node.type === "FunctionDeclaration" ||
|
|
312
|
+
node.type === "FunctionExpression" ||
|
|
313
|
+
node.type === "ArrowFunctionExpression") &&
|
|
314
|
+
node.async;
|
|
315
|
+
|
|
316
|
+
if (isAsyncFn) insideAsync++;
|
|
317
|
+
if (node.type === "AwaitExpression" && insideAsync === 0) {
|
|
318
|
+
found = true;
|
|
319
|
+
}
|
|
320
|
+
if (node.type === "ForOfStatement" && node.await && insideAsync === 0) {
|
|
321
|
+
found = true;
|
|
322
|
+
}
|
|
323
|
+
if (!found) {
|
|
324
|
+
for (const key in node) {
|
|
325
|
+
if (key === "type" || key === "start" || key === "end") continue;
|
|
326
|
+
const val = node[key];
|
|
327
|
+
if (val && typeof val === "object") walk(val);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (isAsyncFn) insideAsync--;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
walk(ast);
|
|
334
|
+
return found;
|
|
335
|
+
} catch {
|
|
336
|
+
return RE_AWAIT_LOOKAHEAD.test(code);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export function stripTopLevelAwait(
|
|
341
|
+
code: string,
|
|
342
|
+
mode: "topLevelOnly" | "full" = "topLevelOnly",
|
|
343
|
+
): string {
|
|
344
|
+
const full = mode === "full";
|
|
345
|
+
if (!full && !RE_AWAIT_QUICK.test(code)) return code;
|
|
346
|
+
if (full && !RE_AWAIT_QUICK.test(code) && !RE_ASYNC_QUICK.test(code)) return code;
|
|
347
|
+
|
|
348
|
+
try {
|
|
349
|
+
let ast: any;
|
|
350
|
+
try {
|
|
351
|
+
ast = acorn.parse(code, {
|
|
352
|
+
ecmaVersion: "latest",
|
|
353
|
+
sourceType: "script",
|
|
354
|
+
allowAwaitOutsideFunction: true,
|
|
355
|
+
});
|
|
356
|
+
} catch {
|
|
357
|
+
ast = acorn.parse(code, {
|
|
358
|
+
ecmaVersion: "latest",
|
|
359
|
+
sourceType: "module",
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const patches: Array<[number, number, string]> = [];
|
|
364
|
+
let insideAsync = 0;
|
|
365
|
+
|
|
366
|
+
function walk(node: any) {
|
|
367
|
+
if (!node || typeof node !== "object") return;
|
|
368
|
+
if (Array.isArray(node)) {
|
|
369
|
+
for (const child of node) walk(child);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (typeof node.type !== "string") return;
|
|
373
|
+
|
|
374
|
+
const isAsyncFn =
|
|
375
|
+
(node.type === "FunctionDeclaration" ||
|
|
376
|
+
node.type === "FunctionExpression" ||
|
|
377
|
+
node.type === "ArrowFunctionExpression") &&
|
|
378
|
+
node.async;
|
|
379
|
+
|
|
380
|
+
if (isAsyncFn) insideAsync++;
|
|
381
|
+
|
|
382
|
+
if (full && isAsyncFn && !node.generator) {
|
|
383
|
+
if (code.slice(node.start, node.start + 5) === "async") {
|
|
384
|
+
let end = node.start + 5;
|
|
385
|
+
while (end < code.length && (code[end] === " " || code[end] === "\t")) end++;
|
|
386
|
+
patches.push([node.start, end, ""]);
|
|
387
|
+
} else {
|
|
388
|
+
const searchStart = Math.max(0, node.start - 30);
|
|
389
|
+
const region = code.slice(searchStart, node.start);
|
|
390
|
+
const asyncIdx = region.lastIndexOf("async");
|
|
391
|
+
if (asyncIdx >= 0) {
|
|
392
|
+
const absStart = searchStart + asyncIdx;
|
|
393
|
+
let absEnd = absStart + 5;
|
|
394
|
+
while (absEnd < code.length && (code[absEnd] === " " || code[absEnd] === "\t")) absEnd++;
|
|
395
|
+
patches.push([absStart, absEnd, ""]);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (node.type === "AwaitExpression") {
|
|
401
|
+
if (full || insideAsync === 0) {
|
|
402
|
+
let awaitEnd = node.start + 5;
|
|
403
|
+
while (
|
|
404
|
+
awaitEnd < node.argument.start &&
|
|
405
|
+
(code[awaitEnd] === " " || code[awaitEnd] === "\t" || code[awaitEnd] === "\n" || code[awaitEnd] === "\r")
|
|
406
|
+
) {
|
|
407
|
+
awaitEnd++;
|
|
408
|
+
}
|
|
409
|
+
patches.push([node.start, awaitEnd, "__syncAwait("]);
|
|
410
|
+
patches.push([node.end, node.end, ")"]);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (node.type === "ForOfStatement" && node.await) {
|
|
415
|
+
if (full || insideAsync === 0) {
|
|
416
|
+
const forEnd = node.start + 3;
|
|
417
|
+
const snippet = code.slice(forEnd, node.left.start);
|
|
418
|
+
const awIdx = snippet.indexOf("await");
|
|
419
|
+
if (awIdx >= 0) {
|
|
420
|
+
const absStart = forEnd + awIdx;
|
|
421
|
+
let absEnd = absStart + 5;
|
|
422
|
+
while (absEnd < code.length && code[absEnd] === " ") absEnd++;
|
|
423
|
+
patches.push([absStart, absEnd, ""]);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
for (const key in node) {
|
|
429
|
+
if (key === "type" || key === "start" || key === "end") continue;
|
|
430
|
+
const val = node[key];
|
|
431
|
+
if (val && typeof val === "object") walk(val);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (isAsyncFn) insideAsync--;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
walk(ast);
|
|
438
|
+
|
|
439
|
+
if (patches.length === 0) return code;
|
|
440
|
+
|
|
441
|
+
let output = code;
|
|
442
|
+
patches.sort((a, b) => b[0] - a[0] || b[1] - a[1]);
|
|
443
|
+
for (const [start, end, replacement] of patches) {
|
|
444
|
+
output = output.slice(0, start) + replacement + output.slice(end);
|
|
445
|
+
}
|
|
446
|
+
return output;
|
|
447
|
+
} catch {
|
|
448
|
+
if (full) {
|
|
449
|
+
let out = code.replace(RE_AWAIT_LOOKAHEAD_G, "");
|
|
450
|
+
out = out.replace(RE_ASYNC_FN_G, "");
|
|
451
|
+
out = out.replace(RE_ASYNC_PAREN_G, "");
|
|
452
|
+
out = out.replace(RE_ASYNC_ARROW_G, "");
|
|
453
|
+
return out;
|
|
454
|
+
}
|
|
455
|
+
return code.replace(RE_AWAIT_LOOKAHEAD_G, "");
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function esmToCjsViaRegex(code: string): string {
|
|
460
|
+
let out = code;
|
|
461
|
+
// strip TS type-only imports
|
|
462
|
+
out = out.replace(RE_TYPE_IMPORT_BRACES, "");
|
|
463
|
+
out = out.replace(RE_TYPE_IMPORT_DEFAULT, "");
|
|
464
|
+
out = out.replace(RE_TYPE_IMPORT_STAR, "");
|
|
465
|
+
// remove inline type specifiers from mixed imports
|
|
466
|
+
out = out.replace(
|
|
467
|
+
RE_MIXED_TYPE_IMPORT,
|
|
468
|
+
(_m, specs: string, src: string) => {
|
|
469
|
+
const kept = specs
|
|
470
|
+
.split(",")
|
|
471
|
+
.filter((s: string) => !RE_TYPE_SPEC.test(s))
|
|
472
|
+
.map((s: string) => s.trim())
|
|
473
|
+
.filter(Boolean);
|
|
474
|
+
if (kept.length === 0) return "";
|
|
475
|
+
const fixed = kept.join(", ").replace(RE_AS_RENAME, "$1: $2");
|
|
476
|
+
return `const {${fixed}} = require("${src}");`;
|
|
477
|
+
},
|
|
478
|
+
);
|
|
479
|
+
// strip TS type-only exports
|
|
480
|
+
out = out.replace(RE_TYPE_EXPORT_FROM, "");
|
|
481
|
+
out = out.replace(RE_TYPE_EXPORT, "");
|
|
482
|
+
out = out.replace(RE_IMPORT_STAR, 'const $1 = require("$2");');
|
|
483
|
+
out = out.replace(
|
|
484
|
+
RE_IMPORT_DEFAULT_NAMED,
|
|
485
|
+
(_m, def, named, src) => {
|
|
486
|
+
const tmp = `__import_${def}`;
|
|
487
|
+
const fixed = named.replace(RE_AS_RENAME, "$1: $2");
|
|
488
|
+
return `const ${tmp} = require("${src}"); const ${def} = ${tmp}.__esModule ? ${tmp}.default : ${tmp}; const {${fixed}} = ${tmp};`;
|
|
489
|
+
},
|
|
490
|
+
);
|
|
491
|
+
out = out.replace(RE_IMPORT_DEFAULT, 'const $1 = require("$2");');
|
|
492
|
+
out = out.replace(
|
|
493
|
+
RE_IMPORT_NAMED,
|
|
494
|
+
(_m, specs, src) => {
|
|
495
|
+
const fixed = specs.replace(RE_AS_RENAME, "$1: $2");
|
|
496
|
+
return `const {${fixed}} = require("${src}");`;
|
|
497
|
+
},
|
|
498
|
+
);
|
|
499
|
+
out = out.replace(RE_IMPORT_SIDE_EFFECT, 'require("$1");');
|
|
500
|
+
// export default
|
|
501
|
+
out = out.replace(RE_EXPORT_DEFAULT_CLASS, "module.exports = class $1");
|
|
502
|
+
out = out.replace(RE_EXPORT_DEFAULT_FN_NAMED, "module.exports = function $1");
|
|
503
|
+
out = out.replace(RE_EXPORT_DEFAULT_FN_ANON, "module.exports = function(");
|
|
504
|
+
out = out.replace(RE_EXPORT_DEFAULT, "module.exports = ");
|
|
505
|
+
// re-exports
|
|
506
|
+
out = out.replace(RE_EXPORT_STAR, 'Object.assign(exports, require("$1"));');
|
|
507
|
+
out = out.replace(
|
|
508
|
+
RE_EXPORT_NAMED_FROM,
|
|
509
|
+
(_m, specs, src) => {
|
|
510
|
+
const binds = specs
|
|
511
|
+
.split(",")
|
|
512
|
+
.map((s: string) => {
|
|
513
|
+
const parts = s.trim().split(RE_AS_SPLIT);
|
|
514
|
+
const local = parts[0].trim();
|
|
515
|
+
const exported = parts.length > 1 ? parts[1].trim() : local;
|
|
516
|
+
return `exports.${exported} = require("${src}").${local}`;
|
|
517
|
+
})
|
|
518
|
+
.join("; ");
|
|
519
|
+
return binds + ";";
|
|
520
|
+
},
|
|
521
|
+
);
|
|
522
|
+
out = out.replace(
|
|
523
|
+
RE_EXPORT_NAMED,
|
|
524
|
+
(_m, specs) => {
|
|
525
|
+
const binds = specs
|
|
526
|
+
.split(",")
|
|
527
|
+
.map((s: string) => {
|
|
528
|
+
const parts = s.trim().split(RE_AS_SPLIT);
|
|
529
|
+
const local = parts[0].trim();
|
|
530
|
+
const exported = parts.length > 1 ? parts[1].trim() : local;
|
|
531
|
+
return `exports.${exported} = ${local}`;
|
|
532
|
+
})
|
|
533
|
+
.join("; ");
|
|
534
|
+
return binds + ";";
|
|
535
|
+
},
|
|
536
|
+
);
|
|
537
|
+
// named exports
|
|
538
|
+
out = out.replace(RE_EXPORT_ASYNC_FN, "exports.$1 = async function $1");
|
|
539
|
+
out = out.replace(RE_EXPORT_FN, "exports.$1 = function $1");
|
|
540
|
+
out = out.replace(RE_EXPORT_CLASS, "exports.$1 = class $1");
|
|
541
|
+
out = out.replace(RE_EXPORT_VAR, "exports.$1 =");
|
|
542
|
+
return out;
|
|
543
|
+
}
|