Skip to content

Commit

Permalink
refactor: Extract transform code to @jest-transform (#8756)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 authored and SimenB committed Jul 27, 2019
1 parent da05da4 commit dcc1918
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@
- `[jest-validate]` Allow `maxWorkers` as part of the `jest.config.js` ([#8565](https://github.com/facebook/jest/pull/8565))
- `[jest-runtime]` Allow passing configuration objects to transformers ([#7288](https://github.com/facebook/jest/pull/7288))
- `[@jest/core, @jest/test-sequencer]` Support async sort in custom `testSequencer` ([#8642](https://github.com/facebook/jest/pull/8642))
- `[@jest-transform]` Extract transforming require logic within `jest-core` into `@jest-transform` ([#8756](https://github.com/facebook/jest/pull/8756))

### Fixes

Expand Down
1 change: 0 additions & 1 deletion packages/jest-core/package.json
Expand Up @@ -29,7 +29,6 @@
"jest-watcher": "^24.8.0",
"micromatch": "^3.1.10",
"p-each-series": "^1.0.0",
"pirates": "^4.0.1",
"realpath-native": "^1.1.0",
"rimraf": "^2.5.4",
"slash": "^2.0.0",
Expand Down
49 changes: 9 additions & 40 deletions packages/jest-core/src/runGlobalHook.ts
Expand Up @@ -5,9 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {extname} from 'path';
import pEachSeries from 'p-each-series';
import {addHook} from 'pirates';
import {Config} from '@jest/types';
import {Test} from 'jest-runner';
import {ScriptTransformer} from '@jest/transform';
Expand Down Expand Up @@ -47,46 +45,17 @@ export default async ({

const transformer = new ScriptTransformer(projectConfig);

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

let transforming = false;
const revertHook = addHook(
(code, filename) => {
try {
transforming = true;
return (
transformer.transformSource(filename, code, false).code || code
);
} finally {
transforming = false;
}
},
{
exts: [extname(modulePath)],
ignoreNodeModules: false,
matcher: (...args) => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return transformer.shouldTransform(...args);
},
},
);

const globalModule = interopRequireDefault(require(modulePath)).default;

if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}

await globalModule(globalConfig);
if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}

revertHook();
await globalModule(globalConfig);
});
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-transform/package.json
Expand Up @@ -20,6 +20,7 @@
"jest-regex-util": "^24.3.0",
"jest-util": "^24.8.0",
"micromatch": "^3.1.10",
"pirates": "^4.0.1",
"realpath-native": "^1.1.0",
"slash": "^2.0.0",
"source-map": "^0.6.1",
Expand Down
44 changes: 44 additions & 0 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -20,6 +20,7 @@ import stableStringify from 'fast-json-stable-stringify';
import slash from 'slash';
import writeFileAtomic from 'write-file-atomic';
import {sync as realpath} from 'realpath-native';
import {addHook} from 'pirates';
import {
Options,
Transformer,
Expand Down Expand Up @@ -434,6 +435,49 @@ export default class ScriptTransformer {
return fileSource;
}

async requireAndTranspileModule<ModuleType = unknown>(
moduleName: string,
callback?: (module: ModuleType) => void | Promise<void>,
): Promise<ModuleType> {
// Load the transformer to avoid a cycle where we need to load a
// transformer in order to transform it in the require hooks
this.preloadTransformer(moduleName);

let transforming = false;
const revertHook = addHook(
(code, filename) => {
try {
transforming = true;
return this.transformSource(filename, code, false).code || code;
} finally {
transforming = false;
}
},
{
exts: [path.extname(moduleName)],
ignoreNodeModules: false,
matcher: filename => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return this.shouldTransform(filename);
},
},
);
const module: ModuleType = require(moduleName);

try {
if (callback) {
await callback(module);
}
} finally {
revertHook();
}

return module;
}

/**
* @deprecated use `this.shouldTransform` instead
*/
Expand Down

0 comments on commit dcc1918

Please sign in to comment.