Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ideas on how to combine ttypescript with Yarn's PnPify? #87

Closed
ozyman42 opened this issue Apr 4, 2020 · 5 comments
Closed

Ideas on how to combine ttypescript with Yarn's PnPify? #87

ozyman42 opened this issue Apr 4, 2020 · 5 comments

Comments

@ozyman42
Copy link

ozyman42 commented Apr 4, 2020

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.

@cevek
Copy link
Owner

cevek commented Apr 5, 2020

Ideally, ttypescript should be pnp compatible. So one reason it is not compatible now is it using of resolve module, which try to find typescript lib in process.cwd() folder for lib/tsc.ts
so, if I remove this part I think it would be completely pnp compatible and it can use pnpfied typescript without any problem

@arcanis
Copy link

arcanis commented Apr 5, 2020

The resolve module is compatible with PnP (since 1.9), so this should be ok already.

provide some documentation on how to install ttypescript directly into a custom TypeScript compiler via the Compiler API.

Note that Yarn doesn't have a user-facing "custom TypeScript compiler". We patch the typescript package in-place at install time in order to automatically merge microsoft/TypeScript#35206, so as far as ttypescript is concerned, they can just keep using typescript and it'll work just fine.

@cevek
Copy link
Owner

cevek commented Sep 7, 2020

I close this issue because it works with pnp properly

@cevek cevek closed this as completed Sep 7, 2020
@ozyman42
Copy link
Author

ozyman42 commented Sep 9, 2020

The one area where it doesn't totally work is with the custom VSCode support through custom TSDK, since Yarn's PnPify and ttypescript provide different tsdk files. I'm hoping VS Code will just adopt zip reading soon though so this custom tsdk from yarn is no longer necessary.

@ozyman42
Copy link
Author

ozyman42 commented Sep 9, 2020

Seems like it would be a big effort to get both tsdk files to work together and not sure it's really worth the effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants