Skip to content

Commit edf8c0a

Browse files
authored
feat: pass ESM options to transformers (#9597)
1 parent 8b8f90a commit edf8c0a

3 files changed

Lines changed: 76 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- `[babel-jest]` Support passing `supportsDynamicImport` and `supportsStaticESM` ([#9766](https://github.com/facebook/jest/pull/9766))
6+
- `[jest-runtime, @jest/transformer]` Support passing `supportsDynamicImport` and `supportsStaticESM` ([#9597](https://github.com/facebook/jest/pull/9597))
67

78
### Fixes
89

packages/jest-runtime/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ type HasteMapOptions = {
5252

5353
type InternalModuleOptions = {
5454
isInternalModule: boolean;
55+
supportsDynamicImport: boolean;
56+
supportsStaticESM: boolean;
57+
};
58+
59+
const defaultTransformOptions: InternalModuleOptions = {
60+
isInternalModule: false,
61+
supportsDynamicImport: false,
62+
supportsStaticESM: false,
5563
};
5664

5765
type InitialModule = Partial<Module> &
@@ -368,7 +376,11 @@ class Runtime {
368376
}
369377

370378
requireInternalModule<T = unknown>(from: Config.Path, to?: string): T {
371-
return this.requireModule(from, to, {isInternalModule: true});
379+
return this.requireModule(from, to, {
380+
isInternalModule: true,
381+
supportsDynamicImport: false,
382+
supportsStaticESM: false,
383+
});
372384
}
373385

374386
requireActual<T = unknown>(from: Config.Path, moduleName: string): T {
@@ -493,7 +505,7 @@ class Runtime {
493505
}
494506

495507
private _getFullTransformationOptions(
496-
options: InternalModuleOptions | undefined,
508+
options: InternalModuleOptions = defaultTransformOptions,
497509
): TransformationOptions {
498510
return {
499511
...options,

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export default class ScriptTransformer {
9090
fileData: string,
9191
filename: Config.Path,
9292
instrument: boolean,
93+
supportsDynamicImport: boolean,
94+
supportsStaticESM: boolean,
9395
): string {
9496
const configString = this._cache.configString;
9597
const transformer = this._getTransformer(filename);
@@ -101,8 +103,8 @@ export default class ScriptTransformer {
101103
config: this._config,
102104
instrument,
103105
rootDir: this._config.rootDir,
104-
supportsDynamicImport: false,
105-
supportsStaticESM: false,
106+
supportsDynamicImport,
107+
supportsStaticESM,
106108
}),
107109
)
108110
.update(CACHE_VERSION)
@@ -122,13 +124,21 @@ export default class ScriptTransformer {
122124
filename: Config.Path,
123125
content: string,
124126
instrument: boolean,
127+
supportsDynamicImport: boolean,
128+
supportsStaticESM: boolean,
125129
): Config.Path {
126130
const baseCacheDir = HasteMap.getCacheFilePath(
127131
this._config.cacheDirectory,
128132
'jest-transform-cache-' + this._config.name,
129133
VERSION,
130134
);
131-
const cacheKey = this._getCacheKey(content, filename, instrument);
135+
const cacheKey = this._getCacheKey(
136+
content,
137+
filename,
138+
instrument,
139+
supportsDynamicImport,
140+
supportsStaticESM,
141+
);
132142
// Create sub folders based on the cacheKey to avoid creating one
133143
// directory with many files.
134144
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
@@ -193,13 +203,19 @@ export default class ScriptTransformer {
193203
return transform;
194204
}
195205

196-
private _instrumentFile(filename: Config.Path, content: string): string {
206+
private _instrumentFile(
207+
filename: Config.Path,
208+
content: string,
209+
supportsDynamicImport: boolean,
210+
supportsStaticESM: boolean,
211+
): string {
197212
const result = babelTransform(content, {
198213
auxiliaryCommentBefore: ' istanbul ignore next ',
199214
babelrc: false,
200215
caller: {
201216
name: '@jest/transform',
202-
supportsStaticESM: false,
217+
supportsDynamicImport,
218+
supportsStaticESM,
203219
},
204220
configFile: false,
205221
filename,
@@ -243,14 +259,23 @@ export default class ScriptTransformer {
243259
this._getTransformer(filepath);
244260
}
245261

262+
// TODO: replace third argument with TransformOptions in Jest 26
246263
transformSource(
247264
filepath: Config.Path,
248265
content: string,
249266
instrument: boolean,
267+
supportsDynamicImport = false,
268+
supportsStaticESM = false,
250269
): TransformResult {
251270
const filename = this._getRealPath(filepath);
252271
const transform = this._getTransformer(filename);
253-
const cacheFilePath = this._getFileCachePath(filename, content, instrument);
272+
const cacheFilePath = this._getFileCachePath(
273+
filename,
274+
content,
275+
instrument,
276+
supportsDynamicImport,
277+
supportsStaticESM,
278+
);
254279
let sourceMapPath: Config.Path | null = cacheFilePath + '.map';
255280
// Ignore cache if `config.cache` is set (--no-cache)
256281
let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
@@ -287,8 +312,8 @@ export default class ScriptTransformer {
287312
if (transform && shouldCallTransform) {
288313
const processed = transform.process(content, filename, this._config, {
289314
instrument,
290-
supportsDynamicImport: false,
291-
supportsStaticESM: false,
315+
supportsDynamicImport,
316+
supportsStaticESM,
292317
});
293318

294319
if (typeof processed === 'string') {
@@ -323,7 +348,12 @@ export default class ScriptTransformer {
323348
}
324349

325350
if (!transformWillInstrument && instrument) {
326-
code = this._instrumentFile(filename, transformed.code);
351+
code = this._instrumentFile(
352+
filename,
353+
transformed.code,
354+
supportsDynamicImport,
355+
supportsStaticESM,
356+
);
327357
} else {
328358
code = transformed.code;
329359
}
@@ -350,12 +380,16 @@ export default class ScriptTransformer {
350380

351381
private _transformAndBuildScript(
352382
filename: Config.Path,
353-
options: Options | null,
383+
options: Options,
354384
instrument: boolean,
355385
fileSource?: string,
356386
): TransformResult {
357-
const isInternalModule = !!(options && options.isInternalModule);
358-
const isCoreModule = !!(options && options.isCoreModule);
387+
const {
388+
isCoreModule,
389+
isInternalModule,
390+
supportsDynamicImport,
391+
supportsStaticESM,
392+
} = options;
359393
const content = stripShebang(
360394
fileSource || fs.readFileSync(filename, 'utf8'),
361395
);
@@ -375,6 +409,8 @@ export default class ScriptTransformer {
375409
filename,
376410
content,
377411
instrument,
412+
supportsDynamicImport,
413+
supportsStaticESM,
378414
);
379415

380416
code = transformedSource.code;
@@ -431,8 +467,12 @@ export default class ScriptTransformer {
431467
options: Options,
432468
fileSource: string,
433469
): string {
434-
const isInternalModule = options.isInternalModule;
435-
const isCoreModule = options.isCoreModule;
470+
const {
471+
isCoreModule,
472+
isInternalModule,
473+
supportsDynamicImport,
474+
supportsStaticESM,
475+
} = options;
436476
const willTransform =
437477
!isInternalModule && !isCoreModule && this.shouldTransform(filename);
438478

@@ -441,6 +481,8 @@ export default class ScriptTransformer {
441481
filename,
442482
fileSource,
443483
false,
484+
supportsDynamicImport,
485+
supportsStaticESM,
444486
);
445487
return transformedJsonSource;
446488
}
@@ -469,7 +511,11 @@ export default class ScriptTransformer {
469511
(code, filename) => {
470512
try {
471513
transforming = true;
472-
return this.transformSource(filename, code, false).code || code;
514+
return (
515+
// we might wanna do `supportsDynamicImport` at some point
516+
this.transformSource(filename, code, false, false, false).code ||
517+
code
518+
);
473519
} finally {
474520
transforming = false;
475521
}

0 commit comments

Comments
 (0)