Description
Context
I'm working on a tool for managing typescript monorepos. Kind of like Lerna, but it includes auto-generation of TypeScript project references + other quality of life improvements aimed specifically at typescript projects.
I've landed on making the tool a Yarn v2 (Berry) plugin, because Yarn's Plug n Play dependency system provides error-free hoisting unlike Lerna (packages' source within a PnP monorepo may only depend on what is in the package's package.json and cannot import hoisted deps depended on by other packages in the monorepo, and also packages are only installed once unlike node_modules which may install the same version of a packages nested multiple times which causes problems with libraries such as graphql-js which has instanceof checks which inadvertently breaks the symlink strategy used by Lerna).
Similar to ttypescript, Berry offers a package pnpify; it's a tool which creates an alternative tsc
command to add PnP module resolution support for vscode and direct compiling: documented here. The tool modifies typescript.tsdk
to point to a PnPify directory that contains its own implementations of tsc
& tsserver
.
In addition to the pnpify tool which is offered, @arcanis from Yarn offered some additional guidance on how to add PnP support directly to a TypeScript compiler host itself. See here.
The Request
I want to continue using ttypescript which adds transformer support to typescript along with PnP which replaces typescript's module resolution algorithm, so I was hoping you could
provide some documentation on how to install ttypescript directly into a custom TypeScript compiler via the Compiler API.
Looking at the ttypescript source itself, it seems you are using the Compiler API, I just don't know if the ttypescript packages are modular enough right now to be used in this way I'm looking for.
I was hoping I could do something like this:
import * as ts from 'typescript';
import * as path from 'path';
import { patchCreateProgram } from 'ttypescript/lib/patchCreateProgram';
import { resolveModuleName } from 'ts-pnp';
patchCreateProgram(ts);
const watchHost = ts.createWatchCompilerHost(
path.resolve('./tsconfig.json'),
{},
ts.sys,
ts.createProgram
)
// createCompilerHost is defined in the ts-pnp README example.
const pnpHost = createCompilerHost(options, moduleSearchLocations);
watchHost.resolveModuleNames = pnpHost.resolveModuleNames;
watchHost.resolveTypeReferenceDirectives = pnpHost.resolveTypeReferenceDirectives;
ts.createWatchProgram(watchHost);
I haven't been able to figure out though if using the patched createProgram method from ttypescript will be enough though.
Also I'm open to other suggestions for solving this problem.
An alternative route I'm evaluating is just using Webpack 5 which has built-in PnP module resolution support, then using the ttypescript compiler w/ ts-loader.