From 3bddaf84da445b1f4bab91d16c9fd2f4abba233c Mon Sep 17 00:00:00 2001 From: Mikhail Bodrov Date: Sat, 16 May 2020 10:56:38 +0300 Subject: [PATCH] chore: minor optimize getTransformer (#10050) --- .../jest-transform/src/ScriptTransformer.ts | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 7e12631195ab..fc4373c5fc48 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -176,34 +176,37 @@ export default class ScriptTransformer { } private _getTransformer(filename: Config.Path) { - let transform: Transformer | null = null; if (!this._config.transform || !this._config.transform.length) { return null; } const transformPath = this._getTransformPath(filename); - if (transformPath) { - const transformer = this._transformCache.get(transformPath); - if (transformer != null) { - return transformer; - } - transform = require(transformPath); + if (!transformPath) { + return null; + } - if (!transform) { - throw new TypeError('Jest: a transform must export something.'); - } - const transformerConfig = this._transformConfigCache.get(transformPath); - if (typeof transform.createTransformer === 'function') { - transform = transform.createTransformer(transformerConfig); - } - if (typeof transform.process !== 'function') { - throw new TypeError( - 'Jest: a transform must export a `process` function.', - ); - } - this._transformCache.set(transformPath, transform); + const transformer = this._transformCache.get(transformPath); + if (transformer) { + return transformer; } + + let transform: Transformer = require(transformPath); + + if (!transform) { + throw new TypeError('Jest: a transform must export something.'); + } + const transformerConfig = this._transformConfigCache.get(transformPath); + if (typeof transform.createTransformer === 'function') { + transform = transform.createTransformer(transformerConfig); + } + if (typeof transform.process !== 'function') { + throw new TypeError( + 'Jest: a transform must export a `process` function.', + ); + } + this._transformCache.set(transformPath, transform); + return transform; }