Skip to content

Commit

Permalink
feat(compiler): expose internal ts Program via ConfigSet `TsCompile…
Browse files Browse the repository at this point in the history
…r` (#1433)
  • Loading branch information
ahnpnl authored Mar 19, 2020
1 parent 7c97267 commit 7153246
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 163 deletions.
3 changes: 1 addition & 2 deletions e2e/__monorepos__/simple/with-dependency/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"ts-jest": {
"diagnostics": true,
"tsConfig": "<rootDir>/tsconfig.json",
"compilerHost": true,
"incremental": true
"compilerHost": true
}
}
},
Expand Down
40 changes: 22 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"@types/lodash.set": "4.x",
"@types/mkdirp": "latest",
"@types/node": "10.x",
"@types/react": "^16.x",
"@types/resolve": "latest",
"@types/semver": "latest",
"@types/yargs": "latest",
Expand Down
6 changes: 0 additions & 6 deletions src/__mocks__/tsconfig.json

This file was deleted.

29 changes: 27 additions & 2 deletions src/compiler/__snapshots__/language-service.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ exports[`language service should compile js file for allowJs true 1`] = `

exports[`language service should compile tsx file for jsx preserve 1`] = `
===[ FILE: test-jsx-preserve.tsx ]==============================================
"use strict";
var App = function () {
return <>Test</>;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiJBQUNRLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1wcmVzZXJ2ZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICAgIH1cbiAgICAgICJdLCJ2ZXJzaW9uIjozfQ==
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiI7QUFDUSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1qc3gtcHJlc2VydmUudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgICB9XG4gICAgICAiXSwidmVyc2lvbiI6M30=
===[ INLINE SOURCE MAPS ]=======================================================
file: test-jsx-preserve.tsx
mappings: 'AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- test-jsx-preserve.tsx
Expand All @@ -41,6 +42,30 @@ exports[`language service should compile tsx file for jsx preserve 1`] = `
================================================================================
`;

exports[`language service should compile tsx file for other jsx options 1`] = `
===[ FILE: test-jsx-options.tsx ]===============================================
"use strict";
var App = function () {
return React.createElement(React.Fragment, null, "Test");
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtb3B0aW9ucy50c3giLCJtYXBwaW5ncyI6IjtBQUNRLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1vcHRpb25zLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgICAgfVxuICAgICAgIl0sInZlcnNpb24iOjN9
===[ INLINE SOURCE MAPS ]=======================================================
file: test-jsx-options.tsx
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- test-jsx-options.tsx
sourcesContent:
- |2-
const App = () => {
return <>Test</>
}
version: 3
================================================================================
`;

exports[`language service should report diagnostics related to typings with pathRegex config matches file name 1`] = `
"TypeScript diagnostics (customize using \`[jest-config].globals.ts-jest.diagnostics\` option):
test-match-regex-diagnostics.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."
Expand Down
66 changes: 60 additions & 6 deletions src/compiler/__snapshots__/program.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,25 @@ exports[`cannot compile should throw error with incremental program 1`] = `
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\`."
`;

exports[`cannot compile should throw error with incremental program 2`] = `"Unable to read file: test-cannot-compile.jsx"`;

exports[`cannot compile should throw error with normal program 1`] = `
"Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts.
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\`."
`;

exports[`jsx preserve should compile tsx file for jsx preserve with incremental program 1`] = `
exports[`cannot compile should throw error with normal program 2`] = `"Unable to read file: test-cannot-compile.jsx"`;

exports[`jsx preserve should compile tsx file for with incremental program 1`] = `
===[ FILE: test-jsx-preserve.tsx ]==============================================
"use strict";
var App = function () {
return <>Test</>;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiJBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1wcmVzZXJ2ZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgIH1cbiAgICAiXSwidmVyc2lvbiI6M30=
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiI7QUFDTSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1qc3gtcHJlc2VydmUudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICB9XG4gICAgIl0sInZlcnNpb24iOjN9
===[ INLINE SOURCE MAPS ]=======================================================
file: test-jsx-preserve.tsx
mappings: 'AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- test-jsx-preserve.tsx
Expand All @@ -69,15 +74,16 @@ exports[`jsx preserve should compile tsx file for jsx preserve with incremental
================================================================================
`;

exports[`jsx preserve should compile tsx file for jsx preserve with program 1`] = `
exports[`jsx preserve should compile tsx file with program 1`] = `
===[ FILE: test-jsx-preserve.tsx ]==============================================
"use strict";
var App = function () {
return <>Test</>;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiJBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxFQUFFLElBQUksR0FBRyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1wcmVzZXJ2ZS50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICBjb25zdCBBcHAgPSAoKSA9PiB7XG4gICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgIH1cbiAgICAiXSwidmVyc2lvbiI6M30=
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtcHJlc2VydmUudHN4IiwibWFwcGluZ3MiOiI7QUFDTSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsidGVzdC1qc3gtcHJlc2VydmUudHN4Il0sInNvdXJjZXNDb250ZW50IjpbIlxuICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICB9XG4gICAgIl0sInZlcnNpb24iOjN9
===[ INLINE SOURCE MAPS ]=======================================================
file: test-jsx-preserve.tsx
mappings: 'AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- test-jsx-preserve.tsx
Expand All @@ -92,6 +98,54 @@ exports[`jsx preserve should compile tsx file for jsx preserve with program 1`]
================================================================================
`;

exports[`other jsx options should compile tsx file for with incremental program 1`] = `
===[ FILE: test-jsx-options.tsx ]===============================================
"use strict";
var App = function () {
return React.createElement(React.Fragment, null, "Test");
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtb3B0aW9ucy50c3giLCJtYXBwaW5ncyI6IjtBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1vcHRpb25zLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgfVxuICAgICJdLCJ2ZXJzaW9uIjozfQ==
===[ INLINE SOURCE MAPS ]=======================================================
file: test-jsx-options.tsx
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- test-jsx-options.tsx
sourcesContent:
- |2-
const App = () => {
return <>Test</>
}
version: 3
================================================================================
`;

exports[`other jsx options should compile tsx file for with program 1`] = `
===[ FILE: test-jsx-options.tsx ]===============================================
"use strict";
var App = function () {
return React.createElement(React.Fragment, null, "Test");
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1qc3gtb3B0aW9ucy50c3giLCJtYXBwaW5ncyI6IjtBQUNNLElBQU0sR0FBRyxHQUFHO0lBQ1YsT0FBTyxpREFBUyxDQUFBO0FBQ2xCLENBQUMsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyJ0ZXN0LWpzeC1vcHRpb25zLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgcmV0dXJuIDw+VGVzdDwvPlxuICAgICAgfVxuICAgICJdLCJ2ZXJzaW9uIjozfQ==
===[ INLINE SOURCE MAPS ]=======================================================
file: test-jsx-options.tsx
mappings: ';AACM,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- test-jsx-options.tsx
sourcesContent:
- |2-
const App = () => {
return <>Test</>
}
version: 3
================================================================================
`;

exports[`typings incremental program should report diagnostics with pathRegex config matches file name 1`] = `"test-typings.ts: Emit skipped"`;

exports[`typings normal program should report diagnostics with pathRegex config matches file name 1`] = `
Expand Down
29 changes: 27 additions & 2 deletions src/compiler/__snapshots__/transpile-module.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,38 @@ exports[`transpile module with isolatedModule: true should compile js file for a
exports[`transpile module with isolatedModule: true should compile tsx file for jsx preserve 1`] = `
===[ FILE: foo.tsx ]============================================================
"use strict";
var App = function () {
return <>Test</>;
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiQUFDUSxJQUFNLEdBQUcsR0FBRztJQUNWLE9BQU8sRUFBRSxJQUFJLEdBQUcsQ0FBQTtBQUNsQixDQUFDLENBQUEiLCJuYW1lcyI6W10sInNvdXJjZXMiOlsiZm9vLnRzeCJdLCJzb3VyY2VzQ29udGVudCI6WyJcbiAgICAgICAgY29uc3QgQXBwID0gKCkgPT4ge1xuICAgICAgICAgIHJldHVybiA8PlRlc3Q8Lz5cbiAgICAgICAgfVxuICAgICAgIl0sInZlcnNpb24iOjN9
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiO0FBQ1EsSUFBTSxHQUFHLEdBQUc7SUFDVixPQUFPLEVBQUUsSUFBSSxHQUFHLENBQUE7QUFDbEIsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbImZvby50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICAgIH1cbiAgICAgICJdLCJ2ZXJzaW9uIjozfQ==
===[ INLINE SOURCE MAPS ]=======================================================
file: foo.tsx
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- foo.tsx
sourcesContent:
- |2-
const App = () => {
return <>Test</>
}
version: 3
================================================================================
`;
exports[`transpile module with isolatedModule: true should compile tsx file for other jsx options 1`] = `
===[ FILE: foo.tsx ]============================================================
"use strict";
var App = function () {
return React.createElement(React.Fragment, null, "Test");
};
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiZm9vLnRzeCIsIm1hcHBpbmdzIjoiO0FBQ1EsSUFBTSxHQUFHLEdBQUc7SUFDVixPQUFPLGlEQUFTLENBQUE7QUFDbEIsQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbImZvby50c3giXSwic291cmNlc0NvbnRlbnQiOlsiXG4gICAgICAgIGNvbnN0IEFwcCA9ICgpID0+IHtcbiAgICAgICAgICByZXR1cm4gPD5UZXN0PC8+XG4gICAgICAgIH1cbiAgICAgICJdLCJ2ZXJzaW9uIjozfQ==
===[ INLINE SOURCE MAPS ]=======================================================
file: foo.tsx
mappings: 'AACQ,IAAM,GAAG,GAAG;IACV,OAAO,EAAE,IAAI,GAAG,CAAA;AAClB,CAAC,CAAA'
mappings: ';AACQ,IAAM,GAAG,GAAG;IACV,OAAO,iDAAS,CAAA;AAClB,CAAC,CAAA'
names: []
sources:
- foo.tsx
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import mkdirp = require('mkdirp')
import { basename, extname, join, normalize } from 'path'

import { ConfigSet } from '../config/config-set'
import { CompileResult, MemoryCache, TsCompiler } from '../types'
import { CompileFn, CompileResult, MemoryCache, TsCompiler } from '../types'
import { sha1 } from '../util/sha1'

import { compileUsingLanguageService } from './language-service'
Expand Down Expand Up @@ -93,15 +93,15 @@ const isValidCacheContent = (contents: string): boolean => {
const readThrough = (
cachedir: string | undefined,
memoryCache: MemoryCache,
compile: CompileResult,
compileFn: CompileFn,
getExtension: (fileName: string) => string,
logger: Logger,
) => {
if (!cachedir) {
return (code: string, fileName: string, lineOffset?: number) => {
const normalizedFileName = normalize(fileName)
logger.debug({ normalizedFileName }, 'readThrough(): no cache')
const [value, sourceMap] = compile(code, normalizedFileName, lineOffset)
const [value, sourceMap] = compileFn(code, normalizedFileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)
memoryCache.outputs.set(normalizedFileName, output)

Expand Down Expand Up @@ -129,7 +129,7 @@ const readThrough = (
} catch (err) {}

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

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

return { cwd: configs.cwd, compile, extensions, cachedir, ts }
return { cwd: configs.cwd, compile, extensions, cachedir, ts, program: compileResult.program }
}
28 changes: 27 additions & 1 deletion src/compiler/language-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,33 @@ describe('language service', () => {
it('should compile tsx file for jsx preserve', () => {
const fileName = 'test-jsx-preserve.tsx',
compiler = makeCompiler({
tsJestConfig: { tsConfig: 'src/__mocks__/tsconfig.json' },
tsJestConfig: {
tsConfig: {
jsx: 'preserve' as any,
},
},
}),
source = `
const App = () => {
return <>Test</>
}
`
writeFileSync(fileName, source, 'utf8')
const compiled = compiler.compile(source, fileName)

expect(new ProcessedSource(compiled, fileName)).toMatchSnapshot()

removeSync(fileName)
})

it('should compile tsx file for other jsx options', () => {
const fileName = 'test-jsx-options.tsx',
compiler = makeCompiler({
tsJestConfig: {
tsConfig: {
jsx: 'react' as any,
},
},
}),
source = `
const App = () => {
Expand Down
Loading

0 comments on commit 7153246

Please sign in to comment.