@@ -26,6 +26,7 @@ import {sync as writeFileAtomic} from 'write-file-atomic';
26
26
import { addHook } from 'pirates' ;
27
27
import type {
28
28
Options ,
29
+ TransformOptions ,
29
30
TransformResult ,
30
31
TransformedSource ,
31
32
Transformer ,
@@ -62,6 +63,14 @@ async function waitForPromiseWithCleanup(
62
63
}
63
64
}
64
65
66
+ // https://github.com/DefinitelyTyped/DefinitelyTyped/pull/49267
67
+ declare module '@babel/core' {
68
+ interface TransformCaller {
69
+ supportsExportNamespaceFrom ?: boolean ;
70
+ supportsTopLevelAwait ?: boolean ;
71
+ }
72
+ }
73
+
65
74
export default class ScriptTransformer {
66
75
private _cache : ProjectCache ;
67
76
private _config : Config . ProjectConfig ;
@@ -93,9 +102,7 @@ export default class ScriptTransformer {
93
102
private _getCacheKey (
94
103
fileData : string ,
95
104
filename : Config . Path ,
96
- instrument : boolean ,
97
- supportsDynamicImport : boolean ,
98
- supportsStaticESM : boolean ,
105
+ options : TransformOptions ,
99
106
) : string {
100
107
const configString = this . _cache . configString ;
101
108
const transformer = this . _getTransformer ( filename ) ;
@@ -105,10 +112,12 @@ export default class ScriptTransformer {
105
112
. update (
106
113
transformer . getCacheKey ( fileData , filename , configString , {
107
114
config : this . _config ,
108
- instrument,
115
+ instrument : options . instrument ,
109
116
rootDir : this . _config . rootDir ,
110
- supportsDynamicImport,
111
- supportsStaticESM,
117
+ supportsDynamicImport : options . supportsDynamicImport ,
118
+ supportsExportNamespaceFrom : options . supportsExportNamespaceFrom ,
119
+ supportsStaticESM : options . supportsStaticESM ,
120
+ supportsTopLevelAwait : options . supportsTopLevelAwait ,
112
121
} ) ,
113
122
)
114
123
. update ( CACHE_VERSION )
@@ -117,7 +126,7 @@ export default class ScriptTransformer {
117
126
return createHash ( 'md5' )
118
127
. update ( fileData )
119
128
. update ( configString )
120
- . update ( instrument ? 'instrument' : '' )
129
+ . update ( options . instrument ? 'instrument' : '' )
121
130
. update ( filename )
122
131
. update ( CACHE_VERSION )
123
132
. digest ( 'hex' ) ;
@@ -127,22 +136,14 @@ export default class ScriptTransformer {
127
136
private _getFileCachePath (
128
137
filename : Config . Path ,
129
138
content : string ,
130
- instrument : boolean ,
131
- supportsDynamicImport : boolean ,
132
- supportsStaticESM : boolean ,
139
+ options : TransformOptions ,
133
140
) : Config . Path {
134
141
const baseCacheDir = HasteMap . getCacheFilePath (
135
142
this . _config . cacheDirectory ,
136
143
'jest-transform-cache-' + this . _config . name ,
137
144
VERSION ,
138
145
) ;
139
- const cacheKey = this . _getCacheKey (
140
- content ,
141
- filename ,
142
- instrument ,
143
- supportsDynamicImport ,
144
- supportsStaticESM ,
145
- ) ;
146
+ const cacheKey = this . _getCacheKey ( content , filename , options ) ;
146
147
// Create sub folders based on the cacheKey to avoid creating one
147
148
// directory with many files.
148
149
const cacheDir = path . join ( baseCacheDir , cacheKey [ 0 ] + cacheKey [ 1 ] ) ;
@@ -213,9 +214,8 @@ export default class ScriptTransformer {
213
214
private _instrumentFile (
214
215
filename : Config . Path ,
215
216
input : TransformedSource ,
216
- supportsDynamicImport : boolean ,
217
- supportsStaticESM : boolean ,
218
217
canMapToInput : boolean ,
218
+ options : TransformOptions ,
219
219
) : TransformedSource {
220
220
const inputCode = typeof input === 'string' ? input : input . code ;
221
221
const inputMap = typeof input === 'string' ? null : input . map ;
@@ -225,8 +225,10 @@ export default class ScriptTransformer {
225
225
babelrc : false ,
226
226
caller : {
227
227
name : '@jest/transform' ,
228
- supportsDynamicImport,
229
- supportsStaticESM,
228
+ supportsDynamicImport : options . supportsDynamicImport ,
229
+ supportsExportNamespaceFrom : options . supportsExportNamespaceFrom ,
230
+ supportsStaticESM : options . supportsStaticESM ,
231
+ supportsTopLevelAwait : options . supportsTopLevelAwait ,
230
232
} ,
231
233
configFile : false ,
232
234
filename,
@@ -260,23 +262,14 @@ export default class ScriptTransformer {
260
262
this . _getTransformer ( filepath ) ;
261
263
}
262
264
263
- // TODO: replace third argument with TransformOptions in Jest 26
264
265
transformSource (
265
266
filepath : Config . Path ,
266
267
content : string ,
267
- instrument : boolean ,
268
- supportsDynamicImport = false ,
269
- supportsStaticESM = false ,
268
+ options : TransformOptions ,
270
269
) : TransformResult {
271
270
const filename = tryRealpath ( filepath ) ;
272
271
const transform = this . _getTransformer ( filename ) ;
273
- const cacheFilePath = this . _getFileCachePath (
274
- filename ,
275
- content ,
276
- instrument ,
277
- supportsDynamicImport ,
278
- supportsStaticESM ,
279
- ) ;
272
+ const cacheFilePath = this . _getFileCachePath ( filename , content , options ) ;
280
273
let sourceMapPath : Config . Path | null = cacheFilePath + '.map' ;
281
274
// Ignore cache if `config.cache` is set (--no-cache)
282
275
let code = this . _config . cache ? readCodeCacheFile ( cacheFilePath ) : null ;
@@ -306,11 +299,12 @@ export default class ScriptTransformer {
306
299
} ;
307
300
308
301
if ( transform && shouldCallTransform ) {
309
- const processed = transform . process ( content , filename , this . _config , {
310
- instrument,
311
- supportsDynamicImport,
312
- supportsStaticESM,
313
- } ) ;
302
+ const processed = transform . process (
303
+ content ,
304
+ filename ,
305
+ this . _config ,
306
+ options ,
307
+ ) ;
314
308
315
309
if ( typeof processed === 'string' ) {
316
310
transformed . code = processed ;
@@ -344,7 +338,7 @@ export default class ScriptTransformer {
344
338
345
339
// Apply instrumentation to the code if necessary, keeping the instrumented code and new map
346
340
let map = transformed . map ;
347
- if ( ! transformWillInstrument && instrument ) {
341
+ if ( ! transformWillInstrument && options . instrument ) {
348
342
/**
349
343
* We can map the original source code to the instrumented code ONLY if
350
344
* - the process of transforming the code produced a source map e.g. ts-jest
@@ -360,9 +354,8 @@ export default class ScriptTransformer {
360
354
const instrumented = this . _instrumentFile (
361
355
filename ,
362
356
transformed ,
363
- supportsDynamicImport ,
364
- supportsStaticESM ,
365
357
shouldEmitSourceMaps ,
358
+ options ,
366
359
) ;
367
360
368
361
code =
@@ -392,15 +385,10 @@ export default class ScriptTransformer {
392
385
private _transformAndBuildScript (
393
386
filename : Config . Path ,
394
387
options : Options ,
395
- instrument : boolean ,
388
+ transformOptions : TransformOptions ,
396
389
fileSource ?: string ,
397
390
) : TransformResult {
398
- const {
399
- isCoreModule,
400
- isInternalModule,
401
- supportsDynamicImport,
402
- supportsStaticESM,
403
- } = options ;
391
+ const { isCoreModule, isInternalModule} = options ;
404
392
const content = stripShebang (
405
393
fileSource || fs . readFileSync ( filename , 'utf8' ) ,
406
394
) ;
@@ -411,16 +399,14 @@ export default class ScriptTransformer {
411
399
const willTransform =
412
400
! isInternalModule &&
413
401
! isCoreModule &&
414
- ( this . shouldTransform ( filename ) || instrument ) ;
402
+ ( this . shouldTransform ( filename ) || transformOptions . instrument ) ;
415
403
416
404
try {
417
405
if ( willTransform ) {
418
406
const transformedSource = this . transformSource (
419
407
filename ,
420
408
content ,
421
- instrument ,
422
- supportsDynamicImport ,
423
- supportsStaticESM ,
409
+ transformOptions ,
424
410
) ;
425
411
426
412
code = transformedSource . code ;
@@ -459,7 +445,7 @@ export default class ScriptTransformer {
459
445
const result = this . _transformAndBuildScript (
460
446
filename ,
461
447
options ,
462
- instrument ,
448
+ { ... options , instrument} ,
463
449
fileSource ,
464
450
) ;
465
451
@@ -475,22 +461,15 @@ export default class ScriptTransformer {
475
461
options : Options ,
476
462
fileSource : string ,
477
463
) : string {
478
- const {
479
- isCoreModule,
480
- isInternalModule,
481
- supportsDynamicImport,
482
- supportsStaticESM,
483
- } = options ;
464
+ const { isCoreModule, isInternalModule} = options ;
484
465
const willTransform =
485
466
! isInternalModule && ! isCoreModule && this . shouldTransform ( filename ) ;
486
467
487
468
if ( willTransform ) {
488
469
const { code : transformedJsonSource } = this . transformSource (
489
470
filename ,
490
471
fileSource ,
491
- false ,
492
- supportsDynamicImport ,
493
- supportsStaticESM ,
472
+ { ...options , instrument : false } ,
494
473
) ;
495
474
return transformedJsonSource ;
496
475
}
@@ -501,14 +480,17 @@ export default class ScriptTransformer {
501
480
requireAndTranspileModule < ModuleType = unknown > (
502
481
moduleName : string ,
503
482
callback ?: ( module : ModuleType ) => void ,
483
+ transformOptions ?: TransformOptions ,
504
484
) : ModuleType ;
505
485
requireAndTranspileModule < ModuleType = unknown > (
506
486
moduleName : string ,
507
487
callback ?: ( module : ModuleType ) => Promise < void > ,
488
+ transformOptions ?: TransformOptions ,
508
489
) : Promise < ModuleType > ;
509
490
requireAndTranspileModule < ModuleType = unknown > (
510
491
moduleName : string ,
511
492
callback ?: ( module : ModuleType ) => void | Promise < void > ,
493
+ transformOptions : TransformOptions = { instrument : false } ,
512
494
) : ModuleType | Promise < ModuleType > {
513
495
// Load the transformer to avoid a cycle where we need to load a
514
496
// transformer in order to transform it in the require hooks
@@ -520,9 +502,7 @@ export default class ScriptTransformer {
520
502
try {
521
503
transforming = true ;
522
504
return (
523
- // we might wanna do `supportsDynamicImport` at some point
524
- this . transformSource ( filename , code , false , false , false ) . code ||
525
- code
505
+ this . transformSource ( filename , code , transformOptions ) . code || code
526
506
) ;
527
507
} finally {
528
508
transforming = false ;
0 commit comments