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

Typescript module refactor (and minification) will break vue-tsc #2095

Closed
cefn opened this issue Nov 4, 2022 · 9 comments
Closed

Typescript module refactor (and minification) will break vue-tsc #2095

cefn opened this issue Nov 4, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@cefn
Copy link

cefn commented Nov 4, 2022

The current strategy in vue-tsc for dynamically patching tsc by intercepting readFileSync before require will not survive the PR which converts Typescript to use modules as per microsoft/TypeScript#51387 (comment)

That PR has several ticks and is green to main.

The Typescript module refactor may be followed up by minification of the distributed code which would make the strategy unmaintainable.

In the short term this will mean pinning the highest compatible peerDependency version to be the last release before modules (possibly 4.8.x). The wildcard currently in place would implicitly include upcoming versions which will break at runtime.

In the medium term @weswigham has hinted at an api-compliant approach to extend the behaviours of tsc in the ways implied by the patch and the proxy ... ( see microsoft/TypeScript#51387 (comment) )

@cefn
Copy link
Author

cefn commented Nov 4, 2022

A strategy employed in Yarn is based on https://github.com/cevek/ttypescript via https://github.com/nonara/ts-patch

@weswigham
Copy link

Yarn's approach isn't great, and I don't recommend it (and comes out from them wanting to patch the real tsc vs y'all, who already have your own variant). Your needs are simple enough that our compiler APIs should be sufficient. I think you can just copy the tsc entrypoint logic from ts and call ts.setSys (an internal API rn, but needed to polyfill direct assignment to ts.sys post-modules) before the executeCommandLine call with a custom System implementation that provides the file lookup behavior you're looking for.

@weswigham
Copy link

Actually, apparently I've misunderstood what's being done, sorry (why override readfile just to patch a js file you're about to execute?). I'm just going to go out on a limb and say these kinds of modifications aren't officially supported and definitely not guaranteed to be stable across TS versions, so... Good luck? This is definitely one of the weirder ways to float a patch on something, considering you could actually maintain a fork & test your modified tsc.

@jakebailey
Copy link

You may still be able to modify the output tsc file, it just won't be the same patch; but, I would agree that we should try and figure out how to actually support you in our API rather than needing to patch.

@johnsoncodehk
Copy link
Member

I'll see what should we do when TS releases a beta version for this, I think no snag, vue-tsc should just need a patch version to support TS v4 and v5 both, so don't worry. :)

@johnsoncodehk johnsoncodehk added the enhancement New feature or request label Nov 8, 2022
@cefn
Copy link
Author

cefn commented Nov 8, 2022

If minification of tsc is a followup (making patching infeasible) isn't this a time to restructure how vue-tsc wires into tsc though?

One concern here is if people have prior versions of vue-tsc for any reason they will break because of the wildcard matching (e.g. any Typescript fulfils the peer relationship with no warning, including those versions which can't work at all). Currently the patch routine has no detection whether the replacements match, meaning a silent failure.

So that suggests to me getting a semver-range into the peerDependencies as a short-term resolution, followed up by some future strategy to make vue-tsc Typescript-tooling-API aware.

@johnsoncodehk
Copy link
Member

If minification of tsc is a followup

Thanks for telling me, I haven't checked the PR yet 😅. With it, we can look forward to some better way instead of proxy readFileSync in vue-tsc.

For example, I hope the original tsc.js decoupling base ts library and allows input it with a parameter:

// bin/tsc
const ts = require("../lib/typescript.js")
require("../lib/tsc.js")(ts)

So we can very easy to do any hacking with it in vue-tsc:

const ts = require("typescript/lib/typescript.js")
ts. supportedTSExtensions = ...
ts. supportedJSExtensions = ...
ts. allSupportedExtensions = ...
ts. createProgram = ...
require("typescript/lib/tsc.js")(ts)

But we're at a passive position, depending on how typescript actually refactors it.

@cefn
Copy link
Author

cefn commented Nov 8, 2022

Well the original PR I linked to has already merged to main, so you can see the API surface you can work with already, noting comments from core Typescript contributors;

@weswigham ...

just copy the tsc entrypoint logic from ts and call ts.setSys (an internal API rn, but needed to polyfill direct assignment to ts.sys post-modules)

@jakebailey ...

we should try and figure out how to actually support you in our API rather than needing to patch.

So if you are able to bottom out what's already possible with the APIs, and that's not enough, there's an appetite for understanding how the Typescript API needs to be opened up to permit downstream tooling to do its work. But if you need any tweaks in place in advance of a breaking release the discussion needs to start immediately I think.

johnsoncodehk added a commit that referenced this issue Nov 8, 2022
johnsoncodehk added a commit that referenced this issue Nov 8, 2022
@johnsoncodehk
Copy link
Member

johnsoncodehk commented Nov 8, 2022

@cefn I have checked the output tsc.js after the PR, proxying readFileSync is still the only way, and I just fix code replacement for TS v5 in a59ee4e.

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

No branches or pull requests

4 participants