-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
ScriptTransformer.ts
1004 lines (876 loc) · 27.8 KB
/
ScriptTransformer.ts
1
2
3
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
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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {createHash} from 'crypto';
import * as path from 'path';
import {transformSync as babelTransform} from '@babel/core';
// @ts-expect-error: should just be `require.resolve`, but the tests mess that up
import babelPluginIstanbul from 'babel-plugin-istanbul';
import {fromSource as sourcemapFromSource} from 'convert-source-map';
import stableStringify = require('fast-json-stable-stringify');
import * as fs from 'graceful-fs';
import {addHook} from 'pirates';
import slash = require('slash');
import {sync as writeFileAtomic} from 'write-file-atomic';
import type {Config} from '@jest/types';
import HasteMap from 'jest-haste-map';
import {
createDirectory,
isPromise,
requireOrImportModule,
tryRealpath,
} from 'jest-util';
import handlePotentialSyntaxError from './enhanceUnexpectedTokenMessage';
import {
makeInvalidReturnValueError,
makeInvalidSourceMapWarning,
makeInvalidSyncTransformerError,
makeInvalidTransformerError,
} from './runtimeErrorsAndWarnings';
import shouldInstrument from './shouldInstrument';
import type {
Options,
ReducedTransformOptions,
RequireAndTranspileModuleOptions,
StringMap,
SyncTransformer,
TransformOptions,
TransformResult,
TransformedSource,
Transformer,
} from './types';
// Use `require` to avoid TS rootDir
const {version: VERSION} = require('../package.json');
type ProjectCache = {
configString: string;
ignorePatternsRegExp?: RegExp;
transformRegExp?: Array<[RegExp, string, Record<string, unknown>]>;
transformedFiles: Map<string, TransformResult>;
};
// This data structure is used to avoid recalculating some data every time that
// we need to transform a file. Since ScriptTransformer is instantiated for each
// file we need to keep this object in the local scope of this module.
const projectCaches = new Map<string, ProjectCache>();
// To reset the cache for specific changesets (rather than package version).
const CACHE_VERSION = '1';
async function waitForPromiseWithCleanup(
promise: Promise<unknown>,
cleanup: () => void,
) {
try {
await promise;
} finally {
cleanup();
}
}
class ScriptTransformer {
private readonly _cache: ProjectCache;
private readonly _transformCache = new Map<
string,
{transformer: Transformer; transformerConfig: unknown}
>();
private _transformsAreLoaded = false;
constructor(
private readonly _config: Config.ProjectConfig,
private readonly _cacheFS: StringMap,
) {
const configString = stableStringify(this._config);
let projectCache = projectCaches.get(configString);
if (!projectCache) {
projectCache = {
configString,
ignorePatternsRegExp: calcIgnorePatternRegExp(this._config),
transformRegExp: calcTransformRegExp(this._config),
transformedFiles: new Map(),
};
projectCaches.set(configString, projectCache);
}
this._cache = projectCache;
}
private _buildCacheKeyFromFileInfo(
fileData: string,
filename: string,
transformOptions: TransformOptions,
transformerCacheKey: string | undefined,
): string {
if (transformerCacheKey) {
return createHash('md5')
.update(transformerCacheKey)
.update(CACHE_VERSION)
.digest('hex');
}
return createHash('md5')
.update(fileData)
.update(transformOptions.configString)
.update(transformOptions.instrument ? 'instrument' : '')
.update(filename)
.update(CACHE_VERSION)
.digest('hex');
}
private _getCacheKey(
fileData: string,
filename: string,
options: ReducedTransformOptions,
): string {
const configString = this._cache.configString;
const {transformer, transformerConfig = {}} =
this._getTransformer(filename) || {};
let transformerCacheKey = undefined;
const transformOptions: TransformOptions = {
...options,
cacheFS: this._cacheFS,
config: this._config,
configString,
transformerConfig,
};
if (typeof transformer?.getCacheKey === 'function') {
transformerCacheKey = transformer.getCacheKey(
fileData,
filename,
transformOptions,
);
}
return this._buildCacheKeyFromFileInfo(
fileData,
filename,
transformOptions,
transformerCacheKey,
);
}
private async _getCacheKeyAsync(
fileData: string,
filename: string,
options: ReducedTransformOptions,
): Promise<string> {
const configString = this._cache.configString;
const {transformer, transformerConfig = {}} =
this._getTransformer(filename) || {};
let transformerCacheKey = undefined;
const transformOptions: TransformOptions = {
...options,
cacheFS: this._cacheFS,
config: this._config,
configString,
transformerConfig,
};
if (transformer) {
const getCacheKey =
transformer.getCacheKeyAsync || transformer.getCacheKey;
if (typeof getCacheKey === 'function') {
transformerCacheKey = await getCacheKey(
fileData,
filename,
transformOptions,
);
}
}
return this._buildCacheKeyFromFileInfo(
fileData,
filename,
transformOptions,
transformerCacheKey,
);
}
private _createFolderFromCacheKey(
filename: string,
cacheKey: string,
): string {
const HasteMapClass = HasteMap.getStatic(this._config);
const baseCacheDir = HasteMapClass.getCacheFilePath(
this._config.cacheDirectory,
`jest-transform-cache-${this._config.name}`,
VERSION,
);
// Create sub folders based on the cacheKey to avoid creating one
// directory with many files.
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
const cacheFilenamePrefix = path
.basename(filename, path.extname(filename))
.replace(/\W/g, '');
const cachePath = slash(
path.join(cacheDir, `${cacheFilenamePrefix}_${cacheKey}`),
);
createDirectory(cacheDir);
return cachePath;
}
private _getFileCachePath(
filename: string,
content: string,
options: ReducedTransformOptions,
): string {
const cacheKey = this._getCacheKey(content, filename, options);
return this._createFolderFromCacheKey(filename, cacheKey);
}
private async _getFileCachePathAsync(
filename: string,
content: string,
options: ReducedTransformOptions,
): Promise<string> {
const cacheKey = await this._getCacheKeyAsync(content, filename, options);
return this._createFolderFromCacheKey(filename, cacheKey);
}
private _getTransformPath(filename: string) {
const transformRegExp = this._cache.transformRegExp;
if (!transformRegExp) {
return undefined;
}
for (let i = 0; i < transformRegExp.length; i++) {
if (transformRegExp[i][0].test(filename)) {
return transformRegExp[i][1];
}
}
return undefined;
}
async loadTransformers(): Promise<void> {
await Promise.all(
this._config.transform.map(
async ([, transformPath, transformerConfig]) => {
let transformer: Transformer = await requireOrImportModule(
transformPath,
);
if (!transformer) {
throw new Error(makeInvalidTransformerError(transformPath));
}
if (typeof transformer.createTransformer === 'function') {
transformer = transformer.createTransformer(transformerConfig);
}
if (
typeof transformer.process !== 'function' &&
typeof transformer.processAsync !== 'function'
) {
throw new Error(makeInvalidTransformerError(transformPath));
}
const res = {transformer, transformerConfig};
this._transformCache.set(transformPath, res);
},
),
);
this._transformsAreLoaded = true;
}
private _getTransformer(filename: string) {
if (!this._transformsAreLoaded) {
throw new Error(
'Jest: Transformers have not been loaded yet - make sure to run `loadTransformers` and wait for it to complete before starting to transform files',
);
}
if (this._config.transform.length === 0) {
return null;
}
const transformPath = this._getTransformPath(filename);
if (!transformPath) {
return null;
}
const cached = this._transformCache.get(transformPath);
if (cached) {
return cached;
}
throw new Error(
`Jest was unable to load the transformer defined for ${filename}. This is a bug in Jest, please open up an issue`,
);
}
private _instrumentFile(
filename: string,
input: TransformedSource,
canMapToInput: boolean,
options: ReducedTransformOptions,
): TransformedSource {
const inputCode = typeof input === 'string' ? input : input.code;
const inputMap = typeof input === 'string' ? null : input.map;
const result = babelTransform(inputCode, {
auxiliaryCommentBefore: ' istanbul ignore next ',
babelrc: false,
caller: {
name: '@jest/transform',
supportsDynamicImport: options.supportsDynamicImport,
supportsExportNamespaceFrom: options.supportsExportNamespaceFrom,
supportsStaticESM: options.supportsStaticESM,
supportsTopLevelAwait: options.supportsTopLevelAwait,
},
configFile: false,
filename,
plugins: [
[
babelPluginIstanbul,
{
compact: false,
// files outside `cwd` will not be instrumented
cwd: this._config.rootDir,
exclude: [],
extension: false,
inputSourceMap: inputMap,
useInlineSourceMaps: false,
},
],
],
sourceMaps: canMapToInput ? 'both' : false,
});
if (result && result.code) {
return result as TransformResult;
}
return input;
}
private _buildTransformResult(
filename: string,
cacheFilePath: string,
content: string,
transformer: Transformer | undefined,
shouldCallTransform: boolean,
options: ReducedTransformOptions,
processed: TransformedSource | null,
sourceMapPath: string | null,
): TransformResult {
let transformed: TransformedSource = {
code: content,
map: null,
};
if (transformer && shouldCallTransform) {
if (typeof processed === 'string') {
transformed.code = processed;
} else if (processed != null && typeof processed.code === 'string') {
transformed = processed;
} else {
throw new Error(makeInvalidReturnValueError());
}
}
if (!transformed.map) {
try {
//Could be a potential freeze here.
//See: https://github.com/facebook/jest/pull/5177#discussion_r158883570
const inlineSourceMap = sourcemapFromSource(transformed.code);
if (inlineSourceMap) {
transformed.map = inlineSourceMap.toObject();
}
} catch {
const transformPath = this._getTransformPath(filename);
invariant(transformPath);
console.warn(makeInvalidSourceMapWarning(filename, transformPath));
}
}
// That means that the transform has a custom instrumentation
// logic and will handle it based on `config.collectCoverage` option
const transformWillInstrument =
shouldCallTransform && transformer && transformer.canInstrument;
// Apply instrumentation to the code if necessary, keeping the instrumented code and new map
let map = transformed.map;
let code;
if (!transformWillInstrument && options.instrument) {
/**
* We can map the original source code to the instrumented code ONLY if
* - the process of transforming the code produced a source map e.g. ts-jest
* - we did not transform the source code
*
* Otherwise we cannot make any statements about how the instrumented code corresponds to the original code,
* and we should NOT emit any source maps
*
*/
const shouldEmitSourceMaps =
(transformer != null && map != null) || transformer == null;
const instrumented = this._instrumentFile(
filename,
transformed,
shouldEmitSourceMaps,
options,
);
code =
typeof instrumented === 'string' ? instrumented : instrumented.code;
map = typeof instrumented === 'string' ? null : instrumented.map;
} else {
code = transformed.code;
}
if (map) {
const sourceMapContent =
typeof map === 'string' ? map : JSON.stringify(map);
invariant(sourceMapPath, 'We should always have default sourceMapPath');
writeCacheFile(sourceMapPath, sourceMapContent);
} else {
sourceMapPath = null;
}
writeCodeCacheFile(cacheFilePath, code);
return {
code,
originalCode: content,
sourceMapPath,
};
}
transformSource(
filepath: string,
content: string,
options: ReducedTransformOptions,
): TransformResult {
const filename = tryRealpath(filepath);
const {transformer, transformerConfig = {}} =
this._getTransformer(filename) || {};
const cacheFilePath = this._getFileCachePath(filename, content, options);
const sourceMapPath: string = `${cacheFilePath}.map`;
// Ignore cache if `config.cache` is set (--no-cache)
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
if (code) {
// This is broken: we return the code, and a path for the source map
// directly from the cache. But, nothing ensures the source map actually
// matches that source code. They could have gotten out-of-sync in case
// two separate processes write concurrently to the same cache files.
return {
code,
originalCode: content,
sourceMapPath,
};
}
let processed = null;
let shouldCallTransform = false;
if (transformer && this.shouldTransform(filename)) {
shouldCallTransform = true;
assertSyncTransformer(transformer, this._getTransformPath(filename));
processed = transformer.process(content, filename, {
...options,
cacheFS: this._cacheFS,
config: this._config,
configString: this._cache.configString,
transformerConfig,
});
}
return this._buildTransformResult(
filename,
cacheFilePath,
content,
transformer,
shouldCallTransform,
options,
processed,
sourceMapPath,
);
}
async transformSourceAsync(
filepath: string,
content: string,
options: ReducedTransformOptions,
): Promise<TransformResult> {
const filename = tryRealpath(filepath);
const {transformer, transformerConfig = {}} =
this._getTransformer(filename) || {};
const cacheFilePath = await this._getFileCachePathAsync(
filename,
content,
options,
);
const sourceMapPath: string = `${cacheFilePath}.map`;
// Ignore cache if `config.cache` is set (--no-cache)
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
if (code) {
// This is broken: we return the code, and a path for the source map
// directly from the cache. But, nothing ensures the source map actually
// matches that source code. They could have gotten out-of-sync in case
// two separate processes write concurrently to the same cache files.
return {
code,
originalCode: content,
sourceMapPath,
};
}
let processed = null;
let shouldCallTransform = false;
if (transformer && this.shouldTransform(filename)) {
shouldCallTransform = true;
const process = transformer.processAsync || transformer.process;
// This is probably dead code since `_getTransformerAsync` already asserts this
invariant(
typeof process === 'function',
'A transformer must always export either a `process` or `processAsync`',
);
processed = await process(content, filename, {
...options,
cacheFS: this._cacheFS,
config: this._config,
configString: this._cache.configString,
transformerConfig,
});
}
return this._buildTransformResult(
filename,
cacheFilePath,
content,
transformer,
shouldCallTransform,
options,
processed,
sourceMapPath,
);
}
private async _transformAndBuildScriptAsync(
filename: string,
options: Options,
transformOptions: ReducedTransformOptions,
fileSource?: string,
): Promise<TransformResult> {
const {isInternalModule} = options;
let fileContent = fileSource ?? this._cacheFS.get(filename);
if (!fileContent) {
fileContent = fs.readFileSync(filename, 'utf8');
this._cacheFS.set(filename, fileContent);
}
const content = stripShebang(fileContent);
let code = content;
let sourceMapPath: string | null = null;
const willTransform =
!isInternalModule &&
(transformOptions.instrument || this.shouldTransform(filename));
try {
if (willTransform) {
const transformedSource = await this.transformSourceAsync(
filename,
content,
transformOptions,
);
code = transformedSource.code;
sourceMapPath = transformedSource.sourceMapPath;
}
return {
code,
originalCode: content,
sourceMapPath,
};
} catch (e: any) {
throw handlePotentialSyntaxError(e);
}
}
private _transformAndBuildScript(
filename: string,
options: Options,
transformOptions: ReducedTransformOptions,
fileSource?: string,
): TransformResult {
const {isInternalModule} = options;
let fileContent = fileSource ?? this._cacheFS.get(filename);
if (!fileContent) {
fileContent = fs.readFileSync(filename, 'utf8');
this._cacheFS.set(filename, fileContent);
}
const content = stripShebang(fileContent);
let code = content;
let sourceMapPath: string | null = null;
const willTransform =
!isInternalModule &&
(transformOptions.instrument || this.shouldTransform(filename));
try {
if (willTransform) {
const transformedSource = this.transformSource(
filename,
content,
transformOptions,
);
code = transformedSource.code;
sourceMapPath = transformedSource.sourceMapPath;
}
return {
code,
originalCode: content,
sourceMapPath,
};
} catch (e: any) {
throw handlePotentialSyntaxError(e);
}
}
async transformAsync(
filename: string,
options: Options,
fileSource?: string,
): Promise<TransformResult> {
const instrument =
options.coverageProvider === 'babel' &&
shouldInstrument(filename, options, this._config);
const scriptCacheKey = getScriptCacheKey(filename, instrument);
let result = this._cache.transformedFiles.get(scriptCacheKey);
if (result) {
return result;
}
result = await this._transformAndBuildScriptAsync(
filename,
options,
{...options, instrument},
fileSource,
);
if (scriptCacheKey) {
this._cache.transformedFiles.set(scriptCacheKey, result);
}
return result;
}
transform(
filename: string,
options: Options,
fileSource?: string,
): TransformResult {
const instrument =
options.coverageProvider === 'babel' &&
shouldInstrument(filename, options, this._config);
const scriptCacheKey = getScriptCacheKey(filename, instrument);
let result = this._cache.transformedFiles.get(scriptCacheKey);
if (result) {
return result;
}
result = this._transformAndBuildScript(
filename,
options,
{...options, instrument},
fileSource,
);
if (scriptCacheKey) {
this._cache.transformedFiles.set(scriptCacheKey, result);
}
return result;
}
transformJson(
filename: string,
options: Options,
fileSource: string,
): string {
const {isInternalModule} = options;
const willTransform = !isInternalModule && this.shouldTransform(filename);
if (willTransform) {
const {code: transformedJsonSource} = this.transformSource(
filename,
fileSource,
{...options, instrument: false},
);
return transformedJsonSource;
}
return fileSource;
}
async requireAndTranspileModule<ModuleType = unknown>(
moduleName: string,
callback?: (module: ModuleType) => void | Promise<void>,
options: RequireAndTranspileModuleOptions = {
applyInteropRequireDefault: true,
instrument: false,
supportsDynamicImport: false,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
},
): Promise<ModuleType> {
let transforming = false;
const {applyInteropRequireDefault, ...transformOptions} = options;
const revertHook = addHook(
(code, filename) => {
try {
transforming = true;
return (
this.transformSource(filename, code, transformOptions).code || code
);
} finally {
transforming = false;
}
},
{
exts: this._config.moduleFileExtensions.map(ext => `.${ext}`),
ignoreNodeModules: false,
matcher: filename => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return this.shouldTransform(filename);
},
},
);
try {
const module: ModuleType = await requireOrImportModule(
moduleName,
applyInteropRequireDefault,
);
if (!callback) {
revertHook();
return module;
}
const cbResult = callback(module);
if (isPromise(cbResult)) {
return waitForPromiseWithCleanup(cbResult, revertHook).then(
() => module,
);
}
return module;
} finally {
revertHook();
}
}
shouldTransform(filename: string): boolean {
const ignoreRegexp = this._cache.ignorePatternsRegExp;
const isIgnored = ignoreRegexp ? ignoreRegexp.test(filename) : false;
return this._config.transform.length !== 0 && !isIgnored;
}
}
// TODO: do we need to define the generics twice?
export async function createTranspilingRequire(
config: Config.ProjectConfig,
): Promise<
<TModuleType = unknown>(
resolverPath: string,
applyInteropRequireDefault?: boolean,
) => Promise<TModuleType>
> {
const transformer = await createScriptTransformer(config);
return async function requireAndTranspileModule<TModuleType = unknown>(
resolverPath: string,
applyInteropRequireDefault: boolean = false,
) {
const transpiledModule =
await transformer.requireAndTranspileModule<TModuleType>(
resolverPath,
() => {},
{
applyInteropRequireDefault,
instrument: false,
supportsDynamicImport: false, // this might be true, depending on node version.
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
},
);
return transpiledModule;
};
}
const removeFile = (path: string) => {
try {
fs.unlinkSync(path);
} catch {}
};
const stripShebang = (content: string) => {
// If the file data starts with a shebang remove it. Leaves the empty line
// to keep stack trace line numbers correct.
if (content.startsWith('#!')) {
return content.replace(/^#!.*/, '');
} else {
return content;
}
};
/**
* This is like `writeCacheFile` but with an additional sanity checksum. We
* cannot use the same technique for source maps because we expose source map
* cache file paths directly to callsites, with the expectation they can read
* it right away. This is not a great system, because source map cache file
* could get corrupted, out-of-sync, etc.
*/
function writeCodeCacheFile(cachePath: string, code: string) {
const checksum = createHash('md5').update(code).digest('hex');
writeCacheFile(cachePath, `${checksum}\n${code}`);
}
/**
* Read counterpart of `writeCodeCacheFile`. We verify that the content of the
* file matches the checksum, in case some kind of corruption happened. This
* could happen if an older version of `jest-runtime` writes non-atomically to
* the same cache, for example.
*/
function readCodeCacheFile(cachePath: string): string | null {
const content = readCacheFile(cachePath);
if (content == null) {
return null;
}
const code = content.substring(33);
const checksum = createHash('md5').update(code).digest('hex');
if (checksum === content.substring(0, 32)) {
return code;
}
return null;
}
/**
* Writing to the cache atomically relies on 'rename' being atomic on most
* file systems. Doing atomic write reduces the risk of corruption by avoiding
* two processes to write to the same file at the same time. It also reduces
* the risk of reading a file that's being overwritten at the same time.
*/
const writeCacheFile = (cachePath: string, fileData: string) => {
try {
writeFileAtomic(cachePath, fileData, {encoding: 'utf8', fsync: false});
} catch (e: any) {
if (cacheWriteErrorSafeToIgnore(e, cachePath)) {
return;
}
e.message = `jest: failed to cache transform results in: ${cachePath}\nFailure message: ${e.message}`;
removeFile(cachePath);
throw e;
}
};
/**
* On Windows, renames are not atomic, leading to EPERM exceptions when two
* processes attempt to rename to the same target file at the same time.
* If the target file exists we can be reasonably sure another process has
* legitimately won a cache write race and ignore the error.
*/
const cacheWriteErrorSafeToIgnore = (
e: Error & {code: string},
cachePath: string,
) =>
process.platform === 'win32' &&
e.code === 'EPERM' &&
fs.existsSync(cachePath);
const readCacheFile = (cachePath: string): string | null => {
if (!fs.existsSync(cachePath)) {
return null;
}
let fileData;
try {
fileData = fs.readFileSync(cachePath, 'utf8');
} catch (e: any) {
e.message = `jest: failed to read cache file: ${cachePath}\nFailure message: ${e.message}`;
removeFile(cachePath);
throw e;
}
if (fileData == null) {
// We must have somehow created the file but failed to write to it,
// let's delete it and retry.
removeFile(cachePath);
}
return fileData;
};
const getScriptCacheKey = (filename: string, instrument: boolean) => {
const mtime = fs.statSync(filename).mtime;
return `${filename}_${mtime.getTime()}${instrument ? '_instrumented' : ''}`;
};
const calcIgnorePatternRegExp = (config: Config.ProjectConfig) => {
if (
!config.transformIgnorePatterns ||
config.transformIgnorePatterns.length === 0
) {
return undefined;
}
return new RegExp(config.transformIgnorePatterns.join('|'));
};
const calcTransformRegExp = (config: Config.ProjectConfig) => {
if (!config.transform.length) {
return undefined;
}
const transformRegexp: Array<[RegExp, string, Record<string, unknown>]> = [];
for (let i = 0; i < config.transform.length; i++) {
transformRegexp.push([
new RegExp(config.transform[i][0]),
config.transform[i][1],
config.transform[i][2],
]);
}
return transformRegexp;
};
function invariant(condition: unknown, message?: string): asserts condition {
if (!condition) {
throw new Error(message);
}
}
function assertSyncTransformer(
transformer: Transformer,
name: string | undefined,
): asserts transformer is SyncTransformer {
invariant(name);
invariant(
typeof transformer.process === 'function',
makeInvalidSyncTransformerError(name),
);
}
export type TransformerType = ScriptTransformer;
export async function createScriptTransformer(
config: Config.ProjectConfig,
cacheFS: StringMap = new Map(),
): Promise<TransformerType> {
const transformer = new ScriptTransformer(config, cacheFS);