Skip to content

Commit 3bddaf8

Browse files
authored
chore: minor optimize getTransformer (#10050)
1 parent 56079a5 commit 3bddaf8

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,34 +176,37 @@ export default class ScriptTransformer {
176176
}
177177

178178
private _getTransformer(filename: Config.Path) {
179-
let transform: Transformer | null = null;
180179
if (!this._config.transform || !this._config.transform.length) {
181180
return null;
182181
}
183182

184183
const transformPath = this._getTransformPath(filename);
185-
if (transformPath) {
186-
const transformer = this._transformCache.get(transformPath);
187-
if (transformer != null) {
188-
return transformer;
189-
}
190184

191-
transform = require(transformPath);
185+
if (!transformPath) {
186+
return null;
187+
}
192188

193-
if (!transform) {
194-
throw new TypeError('Jest: a transform must export something.');
195-
}
196-
const transformerConfig = this._transformConfigCache.get(transformPath);
197-
if (typeof transform.createTransformer === 'function') {
198-
transform = transform.createTransformer(transformerConfig);
199-
}
200-
if (typeof transform.process !== 'function') {
201-
throw new TypeError(
202-
'Jest: a transform must export a `process` function.',
203-
);
204-
}
205-
this._transformCache.set(transformPath, transform);
189+
const transformer = this._transformCache.get(transformPath);
190+
if (transformer) {
191+
return transformer;
206192
}
193+
194+
let transform: Transformer = require(transformPath);
195+
196+
if (!transform) {
197+
throw new TypeError('Jest: a transform must export something.');
198+
}
199+
const transformerConfig = this._transformConfigCache.get(transformPath);
200+
if (typeof transform.createTransformer === 'function') {
201+
transform = transform.createTransformer(transformerConfig);
202+
}
203+
if (typeof transform.process !== 'function') {
204+
throw new TypeError(
205+
'Jest: a transform must export a `process` function.',
206+
);
207+
}
208+
this._transformCache.set(transformPath, transform);
209+
207210
return transform;
208211
}
209212

0 commit comments

Comments
 (0)