timocov / dts-bundle-generator

A tool to generate a single bundle of dts with types tree-shaking
MIT License
745 stars 38 forks source link

Don't error on missing re-exported declarations #332

Open jackw opened 1 week ago

jackw commented 1 week ago

Hey πŸ‘‹

Many thanks for creating this package. It's really useful! πŸ‘πŸ‘πŸ‘

I've come across a situation where some packages re-export types but due to having the re-exported types dependency set as a dev dependency it isn't installed via npm and dts-bundle-generator errors out.

// node_modules/my-package/dist/index.d.ts
export { TypeA, TypeB } from '@types/some-package'
Error while bundling types: TypeError: Cannot read properties of undefined (reading 'declarations')
    at getDeclarationsForSymbol (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/dts-bundle-generator/dist/helpers/typescript.js:114:16)
    at TypesUsageEvaluator.computeUsageForNode (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:117:97)
    at visitNodes (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/typescript/lib/typescript.js:31751:22)
    at forEachChildInSourceFile (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/typescript/lib/typescript.js:31964:12)
    at Object.forEachChild (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/typescript/lib/typescript.js:32264:35)
    at TypesUsageEvaluator.computeUsages (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:59:16)
    at new TypesUsageEvaluator (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/dts-bundle-generator/dist/types-usage-evaluator.js:12:14)
    at generateDtsBundle (/Users/jackwestbrook/dev/grafana/plugin-tools/node_modules/dts-bundle-generator/dist/bundle-generator.js:20:33)
    at generateTypes (file:///Users/jackwestbrook/dev/grafana/plugin-tools/packages/plugin-types-bundler/dist/bundleTypes.js:22:17)
    at file:///Users/jackwestbrook/dev/grafana/plugin-tools/packages/plugin-types-bundler/dist/bin/run.js:18:5

This error was quite confusing for me and solving it consisted of installing the missing package in my project. However I think it might make sense to offer the option to warn users that it's missing and allow execution to continue. Alternatively maybe it's enough to throw a more helpful error msg and force the user to install the missing package to make sure types are correctly bundled. I'm opening this PR as a starting point to discuss this further.

From my investigations without the re-exported package installed it seems typescript treats the missing re-exported packages as any and doesn't seem to surface any errors (at least none that I could find).

Additionally whilst doing this I noticed there is no way to change the log level when using this packages API. Is that something you'd consider adding as an option?

timocov commented 2 days ago

However I think it might make sense to offer the option to warn users that it's missing and allow execution to continue

I'm not sure I agree with this.

This error was quite confusing for me and solving it consisted of installing the missing package in my project

This feels like the only correct solution in a given situation, and the reason for this is because if a package is marked as "dev dependency" how come you're able to import it (even via transitive files)? It feels like some sort of misconfiguration. Can you share an example what packages generate such situation?

From my investigations without the re-exported package installed it seems typescript treats the missing re-exported packages as any and doesn't seem to surface any errors (at least none that I could find).

Do you have skipLibCheck option on by any chance?

Additionally whilst doing this I noticed there is no way to change the log level when using this packages API. Is that something you'd consider adding as an option?

Never thought about it tbh, I'm open for suggestions tho.