Skip to content

Commit

Permalink
fix(tsConfig): path mapping by filing-cabinet
Browse files Browse the repository at this point in the history
A PR pending in filingc-cabinet should be merged and published before
this to be merged. REF: dependents/node-filing-cabinet#100
  • Loading branch information
jjangga0214 committed Aug 16, 2021
1 parent b04dc3b commit c9759f9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var list = dependencyTree.toList({
* `requireConfig`: path to a requirejs config for AMD modules (allows for the result of aliased module paths)
* `webpackConfig`: path to a webpack config for aliased modules
* `tsConfig`: path to a typescript config (or a preloaded object representing the typescript config)
* `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.
* `nodeModulesConfig`: config for resolving entry file for node_modules
* `visited`: object used for avoiding redundant subtree generations via memoization.
* `nonExistent`: array used for storing the list of partial paths that do not exist
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Config = require('./lib/Config');
* @param {Boolean} [options.noTypeDefinitions] For TypeScript imports, whether to resolve to `*.js` instead of `*.d.ts`.
* @return {Object}
*/
module.exports = function(options) {
module.exports = function (options) {
const config = new Config(options);

if (!fs.existsSync(config.filename)) {
Expand Down Expand Up @@ -67,7 +67,7 @@ module.exports = function(options) {
*
* Params are those of module.exports
*/
module.exports.toList = function(options) {
module.exports.toList = function (options) {
options.isListForm = true;

return module.exports(options);
Expand All @@ -81,7 +81,7 @@ module.exports.toList = function(options) {
* @param {Config} config
* @return {Array}
*/
module.exports._getDependencies = function(config) {
module.exports._getDependencies = function (config) {
let dependencies;
const precinctOptions = config.detectiveConfig;
precinctOptions.includeCore = false;
Expand Down Expand Up @@ -111,6 +111,7 @@ module.exports._getDependencies = function(config) {
webpackConfig: config.webpackConfig,
nodeModulesConfig: config.nodeModulesConfig,
tsConfig: config.tsConfig,
tsConfigPath: config.tsConfigPath,
noTypeDefinitions: config.noTypeDefinitions
});

Expand Down Expand Up @@ -158,7 +159,7 @@ function traverse(config) {
if (config.filter) {
debug('using filter function to filter out dependencies');
debug('unfiltered number of dependencies: ' + dependencies.length);
dependencies = dependencies.filter(function(filePath) {
dependencies = dependencies.filter(function (filePath) {
return config.filter(filePath, config.filename);
});
debug('filtered number of dependencies: ' + dependencies.length);
Expand Down
4 changes: 3 additions & 1 deletion lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ class Config {
if (!this.directory) { throw new Error('directory not given'); }
if (this.filter && typeof this.filter !== 'function') { throw new Error('filter must be a function'); }

this.tsConfigPath = options.tsConfigPath
if ('string' === typeof this.tsConfig) {
debug('preparsing the ts config into an object for performance');
const ts = require('typescript');
const tsParsedConfig = ts.readJsonConfigFile(this.tsConfig, ts.sys.readFile);
const obj = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path.dirname(this.tsConfig));
this.tsConfigPath = this.tsConfigPath || this.tsConfig
this.tsConfig = obj.raw;
}

Expand All @@ -39,7 +41,7 @@ class Config {
debug('visited: ', this.visited);
}

clone () {
clone() {
return new Config(this);
}
}
Expand Down

0 comments on commit c9759f9

Please sign in to comment.