Skip to content

Commit

Permalink
Avoid transpile the TypeScript code twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunle committed Jan 4, 2018
1 parent c610c13 commit bbc00cb
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 212 deletions.
20 changes: 11 additions & 9 deletions src/install.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import * as sourceMapSupport from 'source-map-support';
import { defaultRetrieveFileHandler } from './default-retrieve-file-handler';

export function install() {
const options: sourceMapSupport.Options = {};
export function install(filePath: string, fileContent: string) {
const options: sourceMapSupport.Options = {};

options.retrieveFile = defaultRetrieveFileHandler;
// options.retrieveFile = defaultRetrieveFileHandler;
options.retrieveFile = path =>
path === filePath ? fileContent : undefined;

/* tslint:disable */
// disabling tslint because the types for the source-map-support version
// in use here don't have the 'environment' property on options
options['environment'] = 'node';
/* tslint:disable */
/* tslint:disable */
// disabling tslint because the types for the source-map-support version
// in use here don't have the 'environment' property on options
options['environment'] = 'node';
/* tslint:disable */

return sourceMapSupport.install(options);
return sourceMapSupport.install(options);
}
119 changes: 63 additions & 56 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,87 @@ import * as tsc from 'typescript';
import { JestConfig, Path, TransformOptions } from './jest-types';
import { getPostProcessHook } from './postprocess';
import {
cacheFile,
getTSConfig,
getTSJestConfig,
injectSourcemapHook,
cacheFile,
getTSConfig,
getTSJestConfig,
injectSourcemapHook,
} from './utils';

export function process(
src: string,
filePath: Path,
jestConfig: JestConfig,
transformOptions: TransformOptions = { instrument: false },
src: string,
filePath: Path,
jestConfig: JestConfig,
transformOptions: TransformOptions = { instrument: false },
) {
// transformOptions.instrument is a proxy for collectCoverage
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(
jestConfig.globals,
transformOptions.instrument,
);
// transformOptions.instrument is a proxy for collectCoverage
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(
jestConfig.globals,
transformOptions.instrument,
);

const isTsFile = /\.tsx?$/.test(filePath);
const isJsFile = /\.jsx?$/.test(filePath);
const isHtmlFile = /\.html$/.test(filePath);
const isTsFile = /\.tsx?$/.test(filePath);
const isJsFile = /\.jsx?$/.test(filePath);
const isHtmlFile = /\.html$/.test(filePath);

// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
src = 'module.exports=`' + src + '`;';
}
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
src = 'module.exports=`' + src + '`;';
}

const processFile =
compilerOptions.allowJs === true ? isTsFile || isJsFile : isTsFile;
const processFile =
compilerOptions.allowJs === true ? isTsFile || isJsFile : isTsFile;

if (!processFile) {
return src;
}
if (!processFile) {
return src;
}

const tsTranspiled = tsc.transpileModule(src, {
compilerOptions,
fileName: filePath,
});
const tsTranspiled = tsc.transpileModule(src, {
compilerOptions,
fileName: filePath,
});

const postHook = getPostProcessHook(
compilerOptions,
jestConfig,
getTSJestConfig(jestConfig.globals),
);
const postHook = getPostProcessHook(
compilerOptions,
jestConfig,
getTSJestConfig(jestConfig.globals),
);

const outputText = postHook(
tsTranspiled.outputText,
filePath,
jestConfig,
transformOptions,
);
const outputText = postHook(
tsTranspiled.outputText,
filePath,
jestConfig,
transformOptions,
);

const modified = injectSourcemapHook(outputText);
const modified = injectSourcemapHook(
filePath,
tsTranspiled.outputText,
outputText,
);

cacheFile(jestConfig, filePath, modified);
cacheFile(jestConfig, filePath, modified);

return modified;
return modified;
}

export function getCacheKey(
fileData: string,
filePath: Path,
jestConfigStr: string,
transformOptions: TransformOptions = { instrument: false },
fileData: string,
filePath: Path,
jestConfigStr: string,
transformOptions: TransformOptions = { instrument: false },
): string {
const jestConfig: JestConfig = JSON.parse(jestConfigStr);
const jestConfig: JestConfig = JSON.parse(jestConfigStr);

const tsConfig = getTSConfig(jestConfig.globals, transformOptions.instrument);
const tsConfig = getTSConfig(
jestConfig.globals,
transformOptions.instrument,
);

return crypto
.createHash('md5')
.update(JSON.stringify(tsConfig), 'utf8')
.update(JSON.stringify(transformOptions), 'utf8')
.update(fileData + filePath + jestConfigStr, 'utf8')
.digest('hex');
return crypto
.createHash('md5')
.update(JSON.stringify(tsConfig), 'utf8')
.update(JSON.stringify(transformOptions), 'utf8')
.update(fileData + filePath + jestConfigStr, 'utf8')
.digest('hex');
}

0 comments on commit bbc00cb

Please sign in to comment.