Skip to content

Commit 7153246

Browse files
authored
feat(compiler): expose internal ts Program via ConfigSet TsCompiler (#1433)
1 parent 7c97267 commit 7153246

17 files changed

+380
-163
lines changed

e2e/__monorepos__/simple/with-dependency/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
"ts-jest": {
3333
"diagnostics": true,
3434
"tsConfig": "<rootDir>/tsconfig.json",
35-
"compilerHost": true,
36-
"incremental": true
35+
"compilerHost": true
3736
}
3837
}
3938
},

package-lock.json

Lines changed: 22 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"@types/lodash.set": "4.x",
100100
"@types/mkdirp": "latest",
101101
"@types/node": "10.x",
102+
"@types/react": "^16.x",
102103
"@types/resolve": "latest",
103104
"@types/semver": "latest",
104105
"@types/yargs": "latest",

src/__mocks__/tsconfig.json

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

src/compiler/__snapshots__/language-service.spec.ts.snap

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ exports[`language service should compile js file for allowJs true 1`] = `
2020

2121
exports[`language service should compile tsx file for jsx preserve 1`] = `
2222
===[ FILE: test-jsx-preserve.tsx ]==============================================
23+
"use strict";
2324
var App = function () {
2425
return <>Test</>;
2526
};
26-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiJBQUNRLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1wcmVzZXJ2ZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICAgIH1cbiAgICAgICJdLCJ2ZXJzaW9uIjozfQ==
27+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiI7QUFDUSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1qc3gtcHJlc2VydmUudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgICB9XG4gICAgICAiXSwidmVyc2lvbiI6M30=
2728
===[ INLINE SOURCE MAPS ]=======================================================
2829
file: test-jsx-preserve.tsx
29-
mappings: 'AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
30+
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
3031
names: []
3132
sources:
3233
- test-jsx-preserve.tsx
@@ -41,6 +42,30 @@ exports[`language service should compile tsx file for jsx preserve 1`] = `
4142
================================================================================
4243
`;
4344

45+
exports[`language service should compile tsx file for other jsx options 1`] = `
46+
===[ FILE: test-jsx-options.tsx ]===============================================
47+
"use strict";
48+
var App = function () {
49+
return React.createElement(React.Fragment, null, "Test");
50+
};
51+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtb3B0aW9ucy50c3giLCJtYXBwaW5ncyI6IjtBQUNRLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1vcHRpb25zLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgICAgfVxuICAgICAgIl0sInZlcnNpb24iOjN9
52+
===[ INLINE SOURCE MAPS ]=======================================================
53+
file: test-jsx-options.tsx
54+
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
55+
names: []
56+
sources:
57+
- test-jsx-options.tsx
58+
sourcesContent:
59+
- |2-
60+
61+
const App = () => {
62+
return <>Test</>
63+
}
64+
65+
version: 3
66+
================================================================================
67+
`;
68+
4469
exports[`language service should report diagnostics related to typings with pathRegex config matches file name 1`] = `
4570
"TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
4671
test-match-regex-diagnostics.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."

src/compiler/__snapshots__/program.spec.ts.snap

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,25 @@ exports[`cannot compile should throw error with incremental program 1`] = `
4141
This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`."
4242
`;
4343

44+
exports[`cannot compile should throw error with incremental program 2`] = `"Unable to read file: test-cannot-compile.jsx"`;
45+
4446
exports[`cannot compile should throw error with normal program 1`] = `
4547
"Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts.
4648
This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`."
4749
`;
4850

49-
exports[`jsx preserve should compile tsx file for jsx preserve with incremental program 1`] = `
51+
exports[`cannot compile should throw error with normal program 2`] = `"Unable to read file: test-cannot-compile.jsx"`;
52+
53+
exports[`jsx preserve should compile tsx file for with incremental program 1`] = `
5054
===[ FILE: test-jsx-preserve.tsx ]==============================================
55+
"use strict";
5156
var App = function () {
5257
return <>Test</>;
5358
};
54-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiJBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1wcmVzZXJ2ZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgIH1cbiAgICAiXSwidmVyc2lvbiI6M30=
59+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiI7QUFDTSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1qc3gtcHJlc2VydmUudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICB9XG4gICAgIl0sInZlcnNpb24iOjN9
5560
===[ INLINE SOURCE MAPS ]=======================================================
5661
file: test-jsx-preserve.tsx
57-
mappings: 'AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
62+
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
5863
names: []
5964
sources:
6065
- test-jsx-preserve.tsx
@@ -69,15 +74,16 @@ exports[`jsx preserve should compile tsx file for jsx preserve with incremental
6974
================================================================================
7075
`;
7176

72-
exports[`jsx preserve should compile tsx file for jsx preserve with program 1`] = `
77+
exports[`jsx preserve should compile tsx file with program 1`] = `
7378
===[ FILE: test-jsx-preserve.tsx ]==============================================
79+
"use strict";
7480
var App = function () {
7581
return <>Test</>;
7682
};
77-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiJBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1wcmVzZXJ2ZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgIH1cbiAgICAiXSwidmVyc2lvbiI6M30=
83+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiI7QUFDTSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1qc3gtcHJlc2VydmUudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICB9XG4gICAgIl0sInZlcnNpb24iOjN9
7884
===[ INLINE SOURCE MAPS ]=======================================================
7985
file: test-jsx-preserve.tsx
80-
mappings: 'AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
86+
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
8187
names: []
8288
sources:
8389
- test-jsx-preserve.tsx
@@ -92,6 +98,54 @@ exports[`jsx preserve should compile tsx file for jsx preserve with program 1`]
9298
================================================================================
9399
`;
94100

101+
exports[`other jsx options should compile tsx file for with incremental program 1`] = `
102+
===[ FILE: test-jsx-options.tsx ]===============================================
103+
"use strict";
104+
var App = function () {
105+
return React.createElement(React.Fragment, null, "Test");
106+
};
107+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtb3B0aW9ucy50c3giLCJtYXBwaW5ncyI6IjtBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1vcHRpb25zLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgfVxuICAgICJdLCJ2ZXJzaW9uIjozfQ==
108+
===[ INLINE SOURCE MAPS ]=======================================================
109+
file: test-jsx-options.tsx
110+
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
111+
names: []
112+
sources:
113+
- test-jsx-options.tsx
114+
sourcesContent:
115+
- |2-
116+
117+
const App = () => {
118+
return <>Test</>
119+
}
120+
121+
version: 3
122+
================================================================================
123+
`;
124+
125+
exports[`other jsx options should compile tsx file for with program 1`] = `
126+
===[ FILE: test-jsx-options.tsx ]===============================================
127+
"use strict";
128+
var App = function () {
129+
return React.createElement(React.Fragment, null, "Test");
130+
};
131+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtb3B0aW9ucy50c3giLCJtYXBwaW5ncyI6IjtBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1vcHRpb25zLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgfVxuICAgICJdLCJ2ZXJzaW9uIjozfQ==
132+
===[ INLINE SOURCE MAPS ]=======================================================
133+
file: test-jsx-options.tsx
134+
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
135+
names: []
136+
sources:
137+
- test-jsx-options.tsx
138+
sourcesContent:
139+
- |2-
140+
141+
const App = () => {
142+
return <>Test</>
143+
}
144+
145+
version: 3
146+
================================================================================
147+
`;
148+
95149
exports[`typings incremental program should report diagnostics with pathRegex config matches file name 1`] = `"test-typings.ts: Emit skipped"`;
96150

97151
exports[`typings normal program should report diagnostics with pathRegex config matches file name 1`] = `

src/compiler/__snapshots__/transpile-module.spec.ts.snap

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,38 @@ exports[`transpile module with isolatedModule: true should compile js file for a
2020
2121
exports[`transpile module with isolatedModule: true should compile tsx file for jsx preserve 1`] = `
2222
===[ FILE: foo.tsx ]============================================================
23+
"use strict";
2324
var App = function () {
2425
return <>Test</>;
2526
};
26-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiQUFDUSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsiZm9vLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgICAgfVxuICAgICAgIl0sInZlcnNpb24iOjN9
27+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiO0FBQ1EsSUFBTSxHQUFHLEdBQUc7SUFDVixPQUFPLEVBQUUsSUFBSSxHQUFHLENBQUE7QUFDbEIsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbImZvby50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICAgIH1cbiAgICAgICJdLCJ2ZXJzaW9uIjozfQ==
28+
===[ INLINE SOURCE MAPS ]=======================================================
29+
file: foo.tsx
30+
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
31+
names: []
32+
sources:
33+
- foo.tsx
34+
sourcesContent:
35+
- |2-
36+
37+
const App = () => {
38+
return <>Test</>
39+
}
40+
41+
version: 3
42+
================================================================================
43+
`;
44+
45+
exports[`transpile module with isolatedModule: true should compile tsx file for other jsx options 1`] = `
46+
===[ FILE: foo.tsx ]============================================================
47+
"use strict";
48+
var App = function () {
49+
return React.createElement(React.Fragment, null, "Test");
50+
};
51+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiO0FBQ1EsSUFBTSxHQUFHLEdBQUc7SUFDVixPQUFPLGlEQUFTLENBQUE7QUFDbEIsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbImZvby50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICAgIH1cbiAgICAgICJdLCJ2ZXJzaW9uIjozfQ==
2752
===[ INLINE SOURCE MAPS ]=======================================================
2853
file: foo.tsx
29-
mappings: 'AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
54+
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
3055
names: []
3156
sources:
3257
- foo.tsx

src/compiler/instance.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import mkdirp = require('mkdirp')
3535
import { basename, extname, join, normalize } from 'path'
3636

3737
import { ConfigSet } from '../config/config-set'
38-
import { CompileResult, MemoryCache, TsCompiler } from '../types'
38+
import { CompileFn, CompileResult, MemoryCache, TsCompiler } from '../types'
3939
import { sha1 } from '../util/sha1'
4040

4141
import { compileUsingLanguageService } from './language-service'
@@ -93,15 +93,15 @@ const isValidCacheContent = (contents: string): boolean => {
9393
const readThrough = (
9494
cachedir: string | undefined,
9595
memoryCache: MemoryCache,
96-
compile: CompileResult,
96+
compileFn: CompileFn,
9797
getExtension: (fileName: string) => string,
9898
logger: Logger,
9999
) => {
100100
if (!cachedir) {
101101
return (code: string, fileName: string, lineOffset?: number) => {
102102
const normalizedFileName = normalize(fileName)
103103
logger.debug({ normalizedFileName }, 'readThrough(): no cache')
104-
const [value, sourceMap] = compile(code, normalizedFileName, lineOffset)
104+
const [value, sourceMap] = compileFn(code, normalizedFileName, lineOffset)
105105
const output = updateOutput(value, fileName, sourceMap, getExtension)
106106
memoryCache.outputs.set(normalizedFileName, output)
107107

@@ -129,7 +129,7 @@ const readThrough = (
129129
} catch (err) {}
130130

131131
logger.debug({ fileName }, 'readThrough(): cache miss')
132-
const [value, sourceMap] = compile(code, normalizedFileName, lineOffset)
132+
const [value, sourceMap] = compileFn(code, normalizedFileName, lineOffset)
133133
const output = updateOutput(value, normalizedFileName, sourceMap, getExtension)
134134

135135
logger.debug({ normalizedFileName, outputPath }, 'readThrough(): writing caches')
@@ -181,7 +181,7 @@ export const createCompiler = (configs: ConfigSet): TsCompiler => {
181181
} else {
182182
compileResult = compileUsingTranspileModule(configs, logger)
183183
}
184-
const compile = readThrough(cachedir, memoryCache, compileResult, getExtension, logger)
184+
const compile = readThrough(cachedir, memoryCache, compileResult.compileFn, getExtension, logger)
185185

186-
return { cwd: configs.cwd, compile, extensions, cachedir, ts }
186+
return { cwd: configs.cwd, compile, extensions, cachedir, ts, program: compileResult.program }
187187
}

src/compiler/language-service.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,33 @@ describe('language service', () => {
8080
it('should compile tsx file for jsx preserve', () => {
8181
const fileName = 'test-jsx-preserve.tsx',
8282
compiler = makeCompiler({
83-
tsJestConfig: { tsConfig: 'src/__mocks__/tsconfig.json' },
83+
tsJestConfig: {
84+
tsConfig: {
85+
jsx: 'preserve' as any,
86+
},
87+
},
88+
}),
89+
source = `
90+
const App = () => {
91+
return <>Test</>
92+
}
93+
`
94+
writeFileSync(fileName, source, 'utf8')
95+
const compiled = compiler.compile(source, fileName)
96+
97+
expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()
98+
99+
removeSync(fileName)
100+
})
101+
102+
it('should compile tsx file for other jsx options', () => {
103+
const fileName = 'test-jsx-options.tsx',
104+
compiler = makeCompiler({
105+
tsJestConfig: {
106+
tsConfig: {
107+
jsx: 'react' as any,
108+
},
109+
},
84110
}),
85111
source = `
86112
const App = () => {

0 commit comments

Comments
 (0)