Skip to content

Commit 04ab3de

Browse files
committed
refactor: cleans up huge amount of stuff + removes all deps
1 parent dfa9c0f commit 04ab3de

21 files changed

Lines changed: 361 additions & 783 deletions

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ jspm_packages
4646
.npm
4747

4848
# Optional REPL history
49-
.node_repl_history
49+
.node_repl_history
50+
51+
*.tgz

package.json

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,15 @@
6565
],
6666
"globals": {
6767
"ts-jest": {
68-
"tsConfigFile": "tsconfig.base.json"
68+
"tsConfig": "tsconfig.base.json"
6969
}
7070
}
7171
},
72-
"dependencies": {
73-
"closest-file-data": "^0.1.4",
74-
"fs-extra": "6.0.1",
75-
"lodash": "^4.17.10"
76-
},
77-
"peerDependencies": {
78-
"babel-core": "^6.0.0 || ^7.0.0-0",
79-
"babel-jest": "^23.0.0 || ^24.0.0",
80-
"jest": "^23.0.0 || ^24.0.0",
81-
"typescript": "^2.0.0 || ^3.0.0"
72+
"dependencies": {},
73+
"optionalDependencies": {
74+
"babel-jest": ">=22 <24",
75+
"jest": ">=22 <24",
76+
"typescript": ">=2.7 <3"
8277
},
8378
"devDependencies": {
8479
"@babel/core": "^7.0.0-beta.54",
@@ -93,12 +88,13 @@
9388
"@types/react": "16.4.7",
9489
"@types/yargs": "^11.0.0",
9590
"babel-core": "^7.0.0-0",
96-
"babel-jest": "^23.4.0",
91+
"babel-jest": "^23.4.2",
9792
"babel-preset-jest": "^23.2.0",
9893
"cpx": "^1.5.0",
9994
"cross-spawn": "latest",
10095
"cross-spawn-with-kill": "latest",
10196
"doctoc": "latest",
97+
"fs-extra": "6.0.1",
10298
"husky": "^0.14.3",
10399
"jest": "^23.4.1",
104100
"jest-config": "^23.4.1",

src/index.ts

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,89 @@
1-
import getCacheKey from './utils/get-cache-key';
2-
import preprocess from './preprocess';
3-
4-
const createTransformer = (options?: any): jest.Transformer => {
5-
// options are always empty, must be the older jest API giving options here
6-
return {
7-
canInstrument: true,
8-
getCacheKey,
9-
process: preprocess,
10-
createTransformer: undefined as any,
11-
};
12-
};
13-
14-
const mod = createTransformer();
15-
mod.createTransformer = createTransformer;
16-
export = mod;
1+
// tslint:disable:member-ordering
2+
import { TsJestInstance, TsJestGlobalOptions } from './types';
3+
import TsProgram from './ts-program';
4+
5+
class TsJestTransformer implements jest.Transformer {
6+
private _instances = new Map<jest.Path, TsJestInstance>();
7+
instanceFor(
8+
jestConfig: jest.ProjectConfig,
9+
rootDir: jest.Path = jestConfig.rootDir || process.cwd(),
10+
): TsJestInstance {
11+
if (this._instances.has(rootDir)) {
12+
return this._instances.get(rootDir) as TsJestInstance;
13+
}
14+
const { globals = {} } = jestConfig as any;
15+
const options: TsJestGlobalOptions = { ...globals['ts-jest'] };
16+
17+
const shouldWrapHtml = !!globals.__TRANSFORM_HTML__;
18+
19+
let tsProgram: TsProgram;
20+
const instance: TsJestInstance = {
21+
shouldWrapHtml,
22+
get tsProgram(): TsProgram {
23+
return tsProgram || (tsProgram = new TsProgram(rootDir, options));
24+
},
25+
get tsConfig() {
26+
return this.tsProgram.compilerOptions;
27+
},
28+
// TODO: get using babel-jest
29+
useBabelJest: options.useBabelJest !== false,
30+
};
31+
this._instances.set(rootDir, instance);
32+
return instance;
33+
}
34+
35+
process(
36+
source: string,
37+
path: jest.Path,
38+
jestConfig: jest.ProjectConfig,
39+
transformOptions?: jest.TransformOptions,
40+
): jest.TransformedSource | string {
41+
let result: string | jest.TransformedSource;
42+
43+
// get the tranformer instance
44+
const instance = this.instanceFor(jestConfig);
45+
46+
// transpile TS code (source maps are included)
47+
result = instance.tsProgram.transpileModule(
48+
path,
49+
source,
50+
transformOptions && transformOptions.instrument,
51+
);
52+
53+
// calling babel-jest transformer
54+
if (instance.useBabelJest) {
55+
result = this.babelJest.process(
56+
result,
57+
path,
58+
jestConfig,
59+
transformOptions,
60+
);
61+
}
62+
63+
return result;
64+
}
65+
66+
private _babelJest!: jest.Transformer;
67+
get babelJest(): jest.Transformer {
68+
if (this._babelJest) return this._babelJest; // tslint:disable-line
69+
let babelJest = require('babel-jest');
70+
if (typeof babelJest.createTransformer === 'function') {
71+
babelJest = babelJest.createTransformer();
72+
}
73+
return (this._babelJest = babelJest);
74+
}
75+
76+
// TODO: use jest-config package to try to get current config and see if we are going to use babel jest or not
77+
// in which case we'd return `true` there:
78+
// get canInstrument() {}
79+
}
80+
81+
let transformer: jest.Transformer;
82+
function createTransformer() {
83+
return (transformer = new TsJestTransformer());
84+
}
85+
function tsProcess(...args: any[]): any {
86+
return (createTransformer() as any).process(...args);
87+
}
88+
89+
export { createTransformer, tsProcess as process };

src/postprocess.ts

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/preprocess.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)