From a87b26c3da94e52e32ee73529c614fb86e080e80 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 17 Nov 2019 10:00:55 +1300 Subject: [PATCH 1/2] Create `createTranspilingRequire` function --- .../jest-transform/src/ScriptTransformer.ts | 19 ++++++++++++++++++- packages/jest-transform/src/index.ts | 5 ++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 20a73eca63a2..c50d97494438 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -9,7 +9,7 @@ import {createHash} from 'crypto'; import * as path from 'path'; import {Script} from 'vm'; import {Config} from '@jest/types'; -import {createDirectory, isPromise} from 'jest-util'; +import {createDirectory, interopRequireDefault, isPromise} from 'jest-util'; import * as fs from 'graceful-fs'; import {transformSync as babelTransform} from '@babel/core'; // @ts-ignore: should just be `require.resolve`, but the tests mess that up @@ -534,6 +534,23 @@ export default class ScriptTransformer { } } +export function createTranspilingRequire(config: Config.ProjectConfig) { + const transformer = new ScriptTransformer(config); + + return function requireAndTranspileModule( + resolverPath: string, + applyInteropRequireDefault: boolean = false, + ): TModuleType { + const transpiledModule = transformer.requireAndTranspileModule( + resolverPath, + ); + + return applyInteropRequireDefault + ? interopRequireDefault(transpiledModule).default + : transpiledModule; + }; +} + const removeFile = (path: Config.Path) => { try { fs.unlinkSync(path); diff --git a/packages/jest-transform/src/index.ts b/packages/jest-transform/src/index.ts index c5be0650c891..5ef88c45ce60 100644 --- a/packages/jest-transform/src/index.ts +++ b/packages/jest-transform/src/index.ts @@ -5,7 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -export {default as ScriptTransformer} from './ScriptTransformer'; +export { + default as ScriptTransformer, + createTranspilingRequire, +} from './ScriptTransformer'; export {default as shouldInstrument} from './shouldInstrument'; export { Transformer, From 57bd1e45ed8697114764fd6e3bf06a8b5ba63a65 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 24 Nov 2019 12:00:35 +1300 Subject: [PATCH 2/2] Add entry to CHANGELOG about `createTranspilingRequire` --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00582f8d818b..2ec72c721e1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ - `[jest-snapshot]` [**BREAKING**] Improve report when the matcher has properties ([#9104](https://github.com/facebook/jest/pull/9104)) - `[jest-snapshot]` Improve colors when snapshots are updatable ([#9132](https://github.com/facebook/jest/pull/9132)) - `[jest-snapshot]` Ignore indentation for most serialized objects ([#9203](https://github.com/facebook/jest/pull/9203)) +- `[jest-transform]` Create `createTranspilingRequire` function for easy transpiling modules ([#9194](https://github.com/facebook/jest/pull/9194)) - `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867)) - `[jest-worker]` [**BREAKING**] Return a promise from `end()`, resolving with the information whether workers exited gracefully ([#8206](https://github.com/facebook/jest/pull/8206)) - `[jest-reporters]` Transform file paths into hyperlinks ([#8980](https://github.com/facebook/jest/pull/8980))