diff --git a/bin/cli.js b/bin/cli.js index d8f14cd7..f9f8b149 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -111,13 +111,6 @@ if (program.tsConfig) { config.tsConfig = program.tsConfig; } -if (config.tsConfig) { - const ts = require('typescript'); - const tsParsedConfig = ts.readJsonConfigFile(config.tsConfig, ts.sys.readFile); - const obj = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path.dirname(config.tsConfig)); - config.tsConfig = obj.raw; -} - if (program.includeNpm) { config.includeNpm = program.includeNpm; } diff --git a/lib/api.js b/lib/api.js index 76a989ee..26773624 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,5 +1,6 @@ 'use strict'; +const path_ = require('path'); const tree = require('./tree'); const cyclic = require('./cyclic'); const graph = require('./graph'); @@ -46,6 +47,16 @@ class Madge { } this.config = Object.assign({}, defaultConfig, config); + if (typeof this.config.tsConfig === 'string') { + const ts = require('typescript'); + const tsParsedConfig = ts.readJsonConfigFile(this.config.tsConfig, ts.sys.readFile); + const obj = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path_.dirname(config.tsConfig)); + this.config.tsConfig = { + ...obj.raw, + compilerOptions: obj.options + }; + log('using tsconfig %o', this.config.tsConfig); + } if (typeof path === 'object' && !Array.isArray(path)) { this.tree = path; diff --git a/test/typescript.js b/test/typescript.js index 0267472d..bce562fa 100644 --- a/test/typescript.js +++ b/test/typescript.js @@ -2,6 +2,7 @@ 'use strict'; const madge = require('../lib/api'); +const ts = require('typescript'); require('should'); describe('TypeScript', () => { @@ -42,6 +43,22 @@ describe('TypeScript', () => { }).catch(done); }); + it('got tsConfig as a string, "extends" field is interpreted', (done) => { + madge(dir + '/custom-paths/import.ts', {tsConfig: dir + '/with-config/tsconfig.json'}).then((res) => { + res.config.tsConfig.should.eql({ + extends: './tsconfig.base.json', + compilerOptions: { + target: ts.ScriptTarget.ESNext, + module: ts.ModuleKind.CommonJS, + allowJs: true, + configFilePath: undefined + }, + compileOnSave: undefined + }); + done(); + }).catch(done); + }); + it('supports CJS modules when using mixedImports option', (done) => { madge(dir + '/mixed.ts', {detectiveOptions: {ts: {mixedImports: true}}}).then((res) => { res.obj().should.eql({ diff --git a/test/typescript/with-config/index.ts b/test/typescript/with-config/index.ts new file mode 100644 index 00000000..e69de29b diff --git a/test/typescript/with-config/tsconfig.base.json b/test/typescript/with-config/tsconfig.base.json new file mode 100644 index 00000000..b41e8801 --- /dev/null +++ b/test/typescript/with-config/tsconfig.base.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "CommonJS" + } +} \ No newline at end of file diff --git a/test/typescript/with-config/tsconfig.json b/test/typescript/with-config/tsconfig.json new file mode 100644 index 00000000..273803b7 --- /dev/null +++ b/test/typescript/with-config/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "target": "ESNext", + "allowJs": true + } +}