Skip to content

Commit

Permalink
handle collect tsconfig's extends fileds
Browse files Browse the repository at this point in the history
fix #323
  • Loading branch information
kamiazya committed Jan 28, 2023
1 parent 54340e8 commit 8968bf0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
7 changes: 0 additions & 7 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const path_ = require('path');
const tree = require('./tree');
const cyclic = require('./cyclic');
const graph = require('./graph');
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const madge = require('../lib/api');
const ts = require('typescript');
require('should');

describe('TypeScript', () => {
Expand Down Expand Up @@ -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({
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions test/typescript/with-config/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"target": "ES5",
"module": "CommonJS"
}
}
7 changes: 7 additions & 0 deletions test/typescript/with-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"target": "ESNext",
"allowJs": true
}
}

0 comments on commit 8968bf0

Please sign in to comment.