Skip to content

Commit 28d2767

Browse files
jjangga0214XhmikosRcraigspaeth
authored
fix(tsConfig): path mapping by filing-cabinet (#138)
* fix(tsConfig): path mapping by filing-cabinet A PR pending in filingc-cabinet should be merged and published before this to be merged. REF: dependents/node-filing-cabinet#100 * Add test to #138 --------- Co-authored-by: XhmikosR <[email protected]> Co-authored-by: Craig Spaeth <[email protected]>
1 parent f4f3844 commit 28d2767

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const list = dependencyTree.toList({
5050
* `requireConfig`: path to a requirejs config for AMD modules (allows for the result of aliased module paths)
5151
* `webpackConfig`: path to a webpack config for aliased modules
5252
* `tsConfig`: path to a typescript config (or a preloaded object representing the typescript config)
53+
* `tsConfigPath`: a (virtual) path to typescript config file when `tsConfig` option is given as an object, not a string. Needed to calculate [Path Mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping). If not given when `tsConfig` is an object, **Path Mapping** is ignored. This is not needed when `tsConfig` is given as a path string.
5354
* `nodeModulesConfig`: config for resolving entry file for node_modules
5455
* `visited`: object used for avoiding redundant subtree generations via memoization.
5556
* `nonExistent`: array used for storing the list of partial paths that do not exist

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module.exports._getDependencies = function(config = {}) {
106106
webpackConfig: config.webpackConfig,
107107
nodeModulesConfig: config.nodeModulesConfig,
108108
tsConfig: config.tsConfig,
109+
tsConfigPath: config.tsConfigPath,
109110
noTypeDefinitions: config.noTypeDefinitions
110111
});
111112

lib/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module.exports = class Config {
1818
this.nodeModulesConfig = options.nodeModulesConfig;
1919
this.detectiveConfig = options.detective || options.detectiveConfig || {};
2020
this.tsConfig = options.tsConfig;
21+
this.tsConfigPath = options.tsConfigPath;
2122
this.noTypeDefinitions = options.noTypeDefinitions;
22-
2323
this.filter = options.filter;
2424

2525
if (!this.filename) throw new Error('filename not given');
@@ -31,6 +31,7 @@ module.exports = class Config {
3131
const ts = require('typescript');
3232
const tsParsedConfig = ts.readJsonConfigFile(this.tsConfig, ts.sys.readFile);
3333
const obj = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path.dirname(this.tsConfig));
34+
this.tsConfigPath ||= this.tsConfig;
3435
this.tsConfig = obj.raw;
3536
}
3637

test/test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,18 @@ describe('dependencyTree', () => {
964964

965965
assert.equal(typeof config.tsConfig, 'object');
966966
});
967+
968+
it('includes the tsConfigPath so filing-cabinet can still resolve compilerOptions.paths correctly', () => {
969+
const directory = path.join(__dirname, 'fixtures/ts');
970+
const tsConfigPath = path.join(directory, '.tsconfig');
971+
const config = new Config({
972+
filename: 'foo',
973+
directory: 'bar',
974+
tsConfig: tsConfigPath
975+
});
976+
977+
assert.equal(config.tsConfigPath, tsConfigPath);
978+
});
967979
});
968980

969981
describe('when cloning', () => {

0 commit comments

Comments
 (0)