-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 5.2 broke the ability to build hybrid node packages #55705
Comments
See #54567. |
@fatcerberus That faq doesn't cover the use case described here. The way I've been doing hybrid modules (as described in this issue) doesn't exhibit the problems described in that faq. For example: https://arethetypeswrong.github.io/?p=is-actual-promise%401.0.0 (I have many others. See also rimraf, mkdirp, glob, lru-cache, minimatch, minipass, isexe, and almost everything in the @tapjs namespace.) My recourse here seems to be that I just need to edit the package.json file between builds, which is pretty unfortunate. Packages can contain both commonjs and esm. That's what the package.json exports field is for. If tsc is building to a location that is the target of a |
That would be #54546, which was abandoned due to conflicting design constraints IIRC. (I also just noticed that you did indeed comment on that issue) You might be interested in following this issue, however: #54593 |
The way this was being done prior to 5.1 was a bug, not a feature. It was super unsafe and was internally inconsistent between module resolution, module checking, and emit. There are already many third-party solutions for unsafely doing dual-emit. tsc is currently the only good type checker for TypeScript and a pretty poor emitter, so it doesnβt make a lot of sense to keep a mode around that does incorrect type checking while emitting slower than any popular third-party emitter.
Unfortunate for DX, but youβre actually getting correct type checking when you do that, assuming you put a package.json with the appropriate
I mean, yes and no. The code being emitted is not broken, but the I think something like #54546 is probably still on the table. Also somewhat related is #55221, where Iβm considering allowing for finer grained control of various pieces of module infrastructure, including how module format is detected, possibly including values that override any package.json detection, which would suit your use case. |
Yes, something can be both
Awesome. Yes, I would love a That's sort of what It seems like the solution for ts 5.2 at least is "edit package.json between builds", and I'm most of the way through writing a tool that'll just do all this for me automatically anyway. Feel free to close this, but consider it a vote in favor of "please give me back my footgun, I was using it, and my feet are perfectly safe, I promise." |
No, it really was materially different from what you get by modifying your package.json
I strongly considered doing this myself. Are you considering packaging that tool as a standalone dependency? I would love to have more good workarounds I can recommend to people before whatever comes out of #55221.
The new footgun will be much more accurate π |
I think Iβd prefer my footguns to be inaccurate if Iβm actually firing them. π |
Of course! Not quite ready for prime time, still working the kinks out of it by porting tapjs to it, but should be good to go in the next few days or so: https://github.com/isaacs/tshy |
π Search Terms
hybrid
commonjs
esm
module
moduleResolution
package.json
π Version & Regression Information
β― Playground Link
No response
π» Code
src/index.ts
tsconfig.json
tsconfig/cjs.json
tsconfig/esm.json
package.json
scripts/fixup.sh
π Actual behavior
π Expected behavior
Should create a CommonJS module at
./dist/cjs/index.js
.Should create an ESM module at
./dist/mjs/index.js
Should not emit errors.
I also tried setting the
module
tonode16
in thecjs.json
, but then it emits ESM for the CJS build.If I remove
"type": "module"
from the root package.json (first of all, that's a pain, because my tests are ESM, but w/e), then it emits CommonJS for both.If it's going to force
module
to matchmoduleResolution
, and infer the type from thepackage.json
file, then it should infer the type from the package.json file that is closest to the build location, not the source location, because that is where Node will get the type from.Better yet, it should just allow CommonJS even if the package.json is
"type": "module"
and has a"require"
export point.Or just trust that the author of the config file knows what they're doing, and obey the explicitly stated config. If someone writes
"module": "commonjs"
, then TSC should emit a commonjs module, regardless of what the package.json'stype
is.Additional information about the issue
The only workaround I can see right now is to have my build script edit the root
package.json
(or write apackage.json
temporarily in./src
) in between each build, and that just feels terribly brittle.The text was updated successfully, but these errors were encountered: