Programmatically compile Typescript Project references solution and passing options per project

Viewed 107

To programmatically compile/build a project references, I use the ts.createSolutionBuilder API.

Problem is that in my use case I don't have the tsconfig.json file written in the filesystem per package. Instead, the tsconfig.json is calculated based on other criteria.

It's possible to pass the compiler options to this method, but it's only the tsconfig.json of the root that includes all the project references data. How do I pass the compiler-options per project/package?

1 Answers

I'm kinda surprised how lacking the docs on this are in typescript (well at 4.3.x at the moment of writing), but it seems it's quite simple - the argument that provides the paths can accept config links.

Here's my code:

// this selects directories to use
const packages = getTSDirectoriesFromGlobs(process.cwd(), opts._, configName);

// so I basically add the tsconfig names to my paths
const rootnames = packages.map(x => join(x, configName || "tsconfig.json"));

// and then pass that as the second argument
const solution = createSolutionBuilder(nodeSystem, rootnames, {});

// and then...
const exitCode = solution.build();

Works like a charm. :)

Actually I'll add a couple links to the code above in my PR to Tranform Hub where I introduced the link:

I was really surprised to see that it did improve the build times that much - the repo there is a big monorepo so it's quite interesting to see how well this works.

Anyway I hope this helps. :)

Related