What's the most efficient way to emit type declarations for a Typescript project (library) along with code transpiling and bundling?
I'm using webpack, ts-loader and fork-ts-checker-webpack-plugin.
In one build cycle (build command) I'm trying to get:
- dev version of the library (unminified, inline source maps, etc)
- prod version of the library (minified + separate source map file)
- type declaration
The first 2 options are working well with fork-ts-checker-webpack-plugin. With thread-loader and cache-loader thrown into the mix things get even better. However, the way they work doesn't allow for emitting of .d.ts files.
So what I'm trying to figure out what is the best way to emit such files.
Right now the options that I see:
- Use
tsc
at the end of code bundling. Run it withspawnSync
, for example. - Create another webpack config object to run webpack exclusively in the
emitDeclarationOnly
mode - Run ts-loader with
{transpileOnly: false, happyPackMode: false}
, but that defeats the goal of using threads
What might be faster in general? Is there another approach?
My own answer for the time being that seems to work:
In webpack.config.js somewhere above the part that exports your webpack config, add the following (tweak arguments to suit your needs):
spawn("tsc", ["-p", "src", "--emitDeclarationOnly", "--declaration", "--declarationDir", "dist/@types", "--skipLibCheck"]);
This code will spawn a child process and run in parallel with whatever webpack is going to do. So now webpack doesn't have to worry about Typescript types at all.