Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not all imports in TypeScript are detected. #102

Closed
ThierryRietveld opened this issue Nov 5, 2021 · 4 comments
Closed

Not all imports in TypeScript are detected. #102

ThierryRietveld opened this issue Nov 5, 2021 · 4 comments

Comments

@ThierryRietveld
Copy link

ThierryRietveld commented Nov 5, 2021

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'
  },
});
@ThierryRietveld
Copy link
Author

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

@igrayson
Copy link
Contributor

igrayson commented Jan 5, 2022

@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

@XhmikosR
Copy link
Member

XhmikosR commented May 5, 2023

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

@XhmikosR
Copy link
Member

XhmikosR commented May 6, 2023

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

@XhmikosR XhmikosR closed this as completed May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants