Reputation: 16955
I'm trying to improve my tsc
build times and editor responsiveness by reducing the number of source files that TypeScript pulls in. I'm seeing lots of .d.ts
files in node_modules
that are being pulled in via transitive dependencies, and I'd like a way to exclude them from consideration during compilation.
To make this more concrete: I'm using PgTyped to generate TypeScript type declarations for my SQL queries. To use it, I need to install two dependencies:
npm install @pgtyped/query @pgtyped/cli
The @pgtyped/query
package depends on antlr4ts
, which ships with many bundled .d.ts
files of its own. These get pulled into my node_modules
directory:
$ ls node_modules/antlr4ts/**/*.d.ts | wc -l
179
Some of these types do get pulled in via node_modules/@pgtyped/query/lib/index.d.ts
. I can see this by using tsc --listFiles
:
$ tsc --noEmit --listFiles | grep antlr4ts
/myrepo/ts/node_modules/antlr4ts/atn/ATNStateType.d.ts
/myrepo/ts/node_modules/antlr4ts/misc/Stubs.d.ts
/myrepo/ts/node_modules/antlr4ts/misc/IntegerList.d.ts
/myrepo/ts/node_modules/antlr4ts/misc/Interval.d.ts
...
So far as I can tell (famous last words!) these are all irrelevant to the public API of PgTyped. If I just replaced them with any
, there would be no observable difference to my app and there would be ~100 fewer files that tsc
would need to load.
I've tried a few techniques suggested on the TypeScript Wiki's Performance Page, namely including **/node_modules
in my exclude
:
{
"compilerOptions": {
"lib": [ "dom", "DOM.Iterable", "es2019" ],
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true,
// ... many other settings ...
},
"exclude": [
"./cypress",
"**/node_modules",
"**/.*/"
]
}
I've set skipLibCheck
and also tried creating a declaration file to stub out antlr4ts
:
// declarations/antlr4ts.d.ts
declare module 'antlr4ts';
This file shows up in the output of tsc --listFiles
, but only after all 110 of the antlr4ts
declarations files.
Is it possible to prevent TypeScript from seeing any of these .d.ts
files? Ideally I'd like to have it give all of these antlr4ts
modules an any
type.
Upvotes: 3
Views: 498
Reputation: 1348
Try this in tsconfig:
"paths": {
"antlr4ts" : ["NOT_EXISTS"]
}
or perhaps point it to your declarations file
"paths": {
"antlr4ts" : ["declarations/antlr4ts.d.ts"]
}
Sidenote - I'm not sure if it will save a significant amount of build time, even if it works
Upvotes: 1
Reputation: 3723
To improve your editor responsiveness, you can try adding watchOptions
with excludeDirectories
to your tsconfig file (doc). This way, you can tell your editor should not to search inside node_modules.
From Typescript webpage:
TypeScript 3.8 introduces a new watchOptions field which allows users to tell the compiler/language service which watching strategies should be used to keep track of files and directories.
About the build time, typescript does not compile the files inside your node_modules, it only looks for type declaration files (d.ts). This should be quick (a few seconds) but it doesn't seem to be a solution to ignoring those files at the moment. One thing you can do to reduce build time is to use Typescript's incremental mode when compiling. This will save compiler info into your project and speed up the following compilations.
Upvotes: 3