dependents/node-filing-cabinet

Not all imports in TypeScript are detected.

ThierryRietveld opened this issue · 4 comments

I'm using dependency-tree to get a tree of my TypeScript project, but it does not show all imported files. When an import statement directs to a folder with an index file, it will not detect it. When I step through the call stack the problem is probably in the tsLookup() function in filing-cabinet.

The line: const namedModule = ts.resolveModuleName(dependency, filename, compilerOptions, host); does not resolve in a module. am I doing something wrong, or is this a bug?

When I change the files to .js everything works as expected.

Example

  • File Structure

    ├── main.ts
    ├── lib
        ├── index.ts
    
  • main.ts

    import { func } from './lib';
  • index.ts

    export function func() {
      
    }
  • Output

    { 'main.ts': {} }
  • Desired output

    { 'main.ts': { 'main.ts/lib/index.ts': {} } }

Config dependency-tree

const tree = dependencyTree({
  filename: 'src/index.ts',
  directory: 'src',
  nodeModulesConfig: {
    entry: 'module'
  },
});

I just see the pull request #100, which is proberly the solution for this bug.

@ThierryRietveld I don't think #100 is related. I believe your problem is this default value: https://github.com/dependents/node-filing-cabinet/blob/master/index.js#L219-L222:

compilerOptions.module = ts.ModuleKind.AMD;

AMD module resolution will not look for ./lib/index.ts. It's also probably not what most modern projects want.

Try setting this in your tsconfig.json:

{ 
  "compilerOptions": {
    "module": "CommonJS"
  }
}

CommonJS is normally the default value provided by typescript itself. It's unfortunate that filing-cabinet provides a different default.

https://www.typescriptlang.org/tsconfig#module

Any PRs to improve things are welcome, along with accompanied tests.

Actually, in v4.0.0 I removed that code so the TypeScript default will be used.