From dcc1918792545277b04e649a68460877bf202181 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 28 Jul 2019 00:35:28 +0530 Subject: [PATCH] refactor: Extract transform code to @jest-transform (#8756) --- CHANGELOG.md | 1 + packages/jest-core/package.json | 1 - packages/jest-core/src/runGlobalHook.ts | 49 ++++--------------- packages/jest-transform/package.json | 1 + .../jest-transform/src/ScriptTransformer.ts | 44 +++++++++++++++++ 5 files changed, 55 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbe84a49aa01..e92a2f5d7cc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-core/package.json b/packages/jest-core/package.json index 7986a583c215..89a8a31d4177 100644 --- a/packages/jest-core/package.json +++ b/packages/jest-core/package.json @@ -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", diff --git a/packages/jest-core/src/runGlobalHook.ts b/packages/jest-core/src/runGlobalHook.ts index 6b20792ba019..f0490863c924 100644 --- a/packages/jest-core/src/runGlobalHook.ts +++ b/packages/jest-core/src/runGlobalHook.ts @@ -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'; @@ -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); + }); }); } diff --git a/packages/jest-transform/package.json b/packages/jest-transform/package.json index 36db7ac359b2..bf3b6f14792f 100644 --- a/packages/jest-transform/package.json +++ b/packages/jest-transform/package.json @@ -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", diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index aa594db3ed00..aafff4c113fe 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -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, @@ -434,6 +435,49 @@ export default class ScriptTransformer { return fileSource; } + async requireAndTranspileModule( + moduleName: string, + callback?: (module: ModuleType) => void | Promise, + ): Promise { + // 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 */