Skip to content

Commit

Permalink
chore: minor optimize getTransformer (#10050)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha committed May 16, 2020
1 parent 56079a5 commit 3bddaf8
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3bddaf8

Please sign in to comment.