Skip to content

Commit

Permalink
async version of transform()
Browse files Browse the repository at this point in the history
  • Loading branch information
ychi committed May 10, 2020
1 parent 5c3d782 commit 25fa856
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,54 @@ export default class ScriptTransformer {

}

private async _transformAndBuildScriptAsync(
filename: Config.Path,
options: Options,
instrument: boolean,
fileSource?: string,
): Promise<TransformResult> {
const {
isCoreModule,
isInternalModule,
supportsDynamicImport,
supportsStaticESM,
} = options;
const content = stripShebang(
fileSource || fs.readFileSync(filename, 'utf8'),
);

let code = content;
let sourceMapPath: string | null = null;

const willTransform =
!isInternalModule &&
!isCoreModule &&
(this.shouldTransform(filename) || instrument);

try {
if (willTransform) {
const transformedSource = await this.transformSourceAsync(
filename,
content,
instrument,
supportsDynamicImport,
supportsStaticESM,
);

code = transformedSource.code;
sourceMapPath = transformedSource.sourceMapPath;
}

return {
code,
originalCode: content,
sourceMapPath,
};
} catch (e) {
throw handlePotentialSyntaxError(e);
}
}

private _transformAndBuildScript(
filename: Config.Path,
options: Options,
Expand Down Expand Up @@ -678,6 +726,40 @@ export default class ScriptTransformer {
}
}


async transformAsync(
filename: Config.Path,
options: Options,
fileSource?: string,
): Promise<TransformResult> {
let scriptCacheKey = undefined;
let instrument = false;

if (!options.isCoreModule) {
instrument =
options.coverageProvider === 'babel' &&
shouldInstrument(filename, options, this._config);
scriptCacheKey = getScriptCacheKey(filename, instrument);
const result = this._cache.transformedFiles.get(scriptCacheKey);
if (result) {
return result;
}
}

const result = await this._transformAndBuildScriptAsync(
filename,
options,
instrument,
fileSource,
);

if (scriptCacheKey) {
this._cache.transformedFiles.set(scriptCacheKey, result);
}

return result;
}

transform(
filename: Config.Path,
options: Options,
Expand Down

0 comments on commit 25fa856

Please sign in to comment.