Skip to content

Commit dcc1918

Browse files
Mark1626SimenB
authored andcommitted
refactor: Extract transform code to @jest-transform (#8756)
1 parent da05da4 commit dcc1918

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `[jest-validate]` Allow `maxWorkers` as part of the `jest.config.js` ([#8565](https://github.com/facebook/jest/pull/8565))
1818
- `[jest-runtime]` Allow passing configuration objects to transformers ([#7288](https://github.com/facebook/jest/pull/7288))
1919
- `[@jest/core, @jest/test-sequencer]` Support async sort in custom `testSequencer` ([#8642](https://github.com/facebook/jest/pull/8642))
20+
- `[@jest-transform]` Extract transforming require logic within `jest-core` into `@jest-transform` ([#8756](https://github.com/facebook/jest/pull/8756))
2021

2122
### Fixes
2223

packages/jest-core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"jest-watcher": "^24.8.0",
3030
"micromatch": "^3.1.10",
3131
"p-each-series": "^1.0.0",
32-
"pirates": "^4.0.1",
3332
"realpath-native": "^1.1.0",
3433
"rimraf": "^2.5.4",
3534
"slash": "^2.0.0",

packages/jest-core/src/runGlobalHook.ts

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {extname} from 'path';
98
import pEachSeries from 'p-each-series';
10-
import {addHook} from 'pirates';
119
import {Config} from '@jest/types';
1210
import {Test} from 'jest-runner';
1311
import {ScriptTransformer} from '@jest/transform';
@@ -47,46 +45,17 @@ export default async ({
4745

4846
const transformer = new ScriptTransformer(projectConfig);
4947

50-
// Load the transformer to avoid a cycle where we need to load a
51-
// transformer in order to transform it in the require hooks
52-
transformer.preloadTransformer(modulePath);
48+
await transformer.requireAndTranspileModule(modulePath, async m => {
49+
const globalModule = interopRequireDefault(m).default;
5350

54-
let transforming = false;
55-
const revertHook = addHook(
56-
(code, filename) => {
57-
try {
58-
transforming = true;
59-
return (
60-
transformer.transformSource(filename, code, false).code || code
61-
);
62-
} finally {
63-
transforming = false;
64-
}
65-
},
66-
{
67-
exts: [extname(modulePath)],
68-
ignoreNodeModules: false,
69-
matcher: (...args) => {
70-
if (transforming) {
71-
// Don't transform any dependency required by the transformer itself
72-
return false;
73-
}
74-
return transformer.shouldTransform(...args);
75-
},
76-
},
77-
);
78-
79-
const globalModule = interopRequireDefault(require(modulePath)).default;
80-
81-
if (typeof globalModule !== 'function') {
82-
throw new TypeError(
83-
`${moduleName} file must export a function at ${modulePath}`,
84-
);
85-
}
86-
87-
await globalModule(globalConfig);
51+
if (typeof globalModule !== 'function') {
52+
throw new TypeError(
53+
`${moduleName} file must export a function at ${modulePath}`,
54+
);
55+
}
8856

89-
revertHook();
57+
await globalModule(globalConfig);
58+
});
9059
});
9160
}
9261

packages/jest-transform/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"jest-regex-util": "^24.3.0",
2121
"jest-util": "^24.8.0",
2222
"micromatch": "^3.1.10",
23+
"pirates": "^4.0.1",
2324
"realpath-native": "^1.1.0",
2425
"slash": "^2.0.0",
2526
"source-map": "^0.6.1",

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import stableStringify from 'fast-json-stable-stringify';
2020
import slash from 'slash';
2121
import writeFileAtomic from 'write-file-atomic';
2222
import {sync as realpath} from 'realpath-native';
23+
import {addHook} from 'pirates';
2324
import {
2425
Options,
2526
Transformer,
@@ -434,6 +435,49 @@ export default class ScriptTransformer {
434435
return fileSource;
435436
}
436437

438+
async requireAndTranspileModule<ModuleType = unknown>(
439+
moduleName: string,
440+
callback?: (module: ModuleType) => void | Promise<void>,
441+
): Promise<ModuleType> {
442+
// Load the transformer to avoid a cycle where we need to load a
443+
// transformer in order to transform it in the require hooks
444+
this.preloadTransformer(moduleName);
445+
446+
let transforming = false;
447+
const revertHook = addHook(
448+
(code, filename) => {
449+
try {
450+
transforming = true;
451+
return this.transformSource(filename, code, false).code || code;
452+
} finally {
453+
transforming = false;
454+
}
455+
},
456+
{
457+
exts: [path.extname(moduleName)],
458+
ignoreNodeModules: false,
459+
matcher: filename => {
460+
if (transforming) {
461+
// Don't transform any dependency required by the transformer itself
462+
return false;
463+
}
464+
return this.shouldTransform(filename);
465+
},
466+
},
467+
);
468+
const module: ModuleType = require(moduleName);
469+
470+
try {
471+
if (callback) {
472+
await callback(module);
473+
}
474+
} finally {
475+
revertHook();
476+
}
477+
478+
return module;
479+
}
480+
437481
/**
438482
* @deprecated use `this.shouldTransform` instead
439483
*/

0 commit comments

Comments
 (0)