From f910ca8f1276631a7cae7dff726f65e50337222b Mon Sep 17 00:00:00 2001 From: Emil Persson Date: Tue, 17 Jan 2017 21:23:29 +0100 Subject: [PATCH] Add ability to resolve "extends" directive in tsconfig.json --- src/utils.ts | 11 +++++-- tests/__tests__/tsconfig-string.spec.ts | 33 ++++++++++++++++++- tests/__ts-jest-mocks__/path.ts | 12 ++++--- tests/tsconfig-test/extends-tsconfig.json | 3 ++ .../extends-with-overrides-tsconfig.json | 7 ++++ 5 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 tests/tsconfig-test/extends-tsconfig.json create mode 100644 tests/tsconfig-test/extends-with-overrides-tsconfig.json diff --git a/src/utils.ts b/src/utils.ts index 485865494d..3f6da23d6a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; @@ -89,4 +96,4 @@ export function getTSConfig(globals, collectCoverage: boolean = false) { } return tsc.convertCompilerOptionsFromJson(config, undefined).options; -} \ No newline at end of file +} diff --git a/tests/__tests__/tsconfig-string.spec.ts b/tests/__tests__/tsconfig-string.spec.ts index fccc4fb621..4cf6de6289 100644 --- a/tests/__tests__/tsconfig-string.spec.ts +++ b/tests/__tests__/tsconfig-string.spec.ts @@ -52,4 +52,35 @@ describe('get ts config from string', () => { }); }); -}); \ No newline at end of file + 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 + }); + }); + +}); diff --git a/tests/__ts-jest-mocks__/path.ts b/tests/__ts-jest-mocks__/path.ts index f3ad8e7735..9a3474b60a 100644 --- a/tests/__ts-jest-mocks__/path.ts +++ b/tests/__ts-jest-mocks__/path.ts @@ -3,7 +3,9 @@ import { } from 'node'; interface MockedPath { __setBaseDir(newBaseDir); - resolve(args); + resolve(...args); + dirname(...args); + join(...args); } const path = require.requireActual('path'); @@ -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; \ No newline at end of file +module.exports = mockedPath; diff --git a/tests/tsconfig-test/extends-tsconfig.json b/tests/tsconfig-test/extends-tsconfig.json new file mode 100644 index 0000000000..d0251e982d --- /dev/null +++ b/tests/tsconfig-test/extends-tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./my-tsconfig.json" +} diff --git a/tests/tsconfig-test/extends-with-overrides-tsconfig.json b/tests/tsconfig-test/extends-with-overrides-tsconfig.json new file mode 100644 index 0000000000..8b6bfd641e --- /dev/null +++ b/tests/tsconfig-test/extends-with-overrides-tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "./my-tsconfig.json", + "compilerOptions": { + "target": "ES5", + "noImplicitAny": true + } +}