Skip to content

Commit

Permalink
Add ability to resolve "extends" directive in tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Persson committed Jan 17, 2017
1 parent 78c353c commit f910ca8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/utils.ts
Expand Up @@ -73,7 +73,14 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
config = 'tsconfig.json';
}
if (typeof config === 'string') {
config = require(path.resolve(config)).compilerOptions;
const configPath = path.resolve(config);
const external = require(configPath);
config = external.compilerOptions || {};

if (typeof external.extends === 'string') {
const parentConfigPath = path.join(path.dirname(configPath), external.extends);
config = Object.assign({}, require(parentConfigPath).compilerOptions, config);
}
}
config.module = config.module || tsc.ModuleKind.CommonJS;
config.jsx = config.jsx || tsc.JsxEmit.React;
Expand All @@ -89,4 +96,4 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
}

return tsc.convertCompilerOptionsFromJson(config, undefined).options;
}
}
33 changes: 32 additions & 1 deletion tests/__tests__/tsconfig-string.spec.ts
Expand Up @@ -52,4 +52,35 @@ describe('get ts config from string', () => {
});
});

});
it('should correctly resolve the "extends" directive', () => {
const {getTSConfig} = require('../../src/utils');
const result = getTSConfig({
'__TS_CONFIG__': 'extends-tsconfig.json'
});

expect(result).toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'noEmitOnError': true,
'jsx': 2
});
});

it('should correctly override any config in the "extends" directive', () => {
const {getTSConfig} = require('../../src/utils');
const result = getTSConfig({
'__TS_CONFIG__': 'extends-with-overrides-tsconfig.json'
});

expect(result).toEqual ({
'target': 1,
'module': 1,
'moduleResolution': 2,
'noEmitOnError': true,
'jsx': 2,
'noImplicitAny': true
});
});

});
12 changes: 8 additions & 4 deletions tests/__ts-jest-mocks__/path.ts
Expand Up @@ -3,7 +3,9 @@ import { } from 'node';

interface MockedPath {
__setBaseDir(newBaseDir);
resolve(args);
resolve(...args);
dirname(...args);
join(...args);
}

const path = require.requireActual('path');
Expand All @@ -19,11 +21,13 @@ function __setBaseDir(newBaseDir) {

// A custom version of `readdirSync` that reads from the special mocked out
// file list set via __setMockFiles
function resolve(args) {
return path.resolve(baseDir, args);
function resolve(...args) {
return path.resolve(baseDir, ...args);
}

mockedPath.__setBaseDir = __setBaseDir;
mockedPath.resolve = resolve;
mockedPath.dirname = path.dirname;
mockedPath.join = path.join;

module.exports = mockedPath;
module.exports = mockedPath;
3 changes: 3 additions & 0 deletions tests/tsconfig-test/extends-tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "./my-tsconfig.json"
}
7 changes: 7 additions & 0 deletions tests/tsconfig-test/extends-with-overrides-tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "./my-tsconfig.json",
"compilerOptions": {
"target": "ES5",
"noImplicitAny": true
}
}

0 comments on commit f910ca8

Please sign in to comment.