-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
moduleResolution: bundler
diverges from bundlers following node semantics in regards to default
import
#54102
Comments
Isnβt this controlled by |
Yeah, Iβm aware of this, but of course itβs not a |
If you ever need a hug... I'm here for you. |
I forked https://sokra.github.io/interop-test to https://andrewbranch.github.io/interop-test, removed a bunch of import and export variations that werenβt super interesting to me (there are still a ton), and added .ts/.mts cases to esbuild and Webpack. I need to add similar TS cases to Rollup, and Iβd like to add Parcel, Bun, and perhaps others. But it shows basically what weβve already established in this issue:
At first, it struck me as odd that these bundlers voluntarily adopted interop restrictions that Node tried very hard to avoid, but couldnβt for technical reasons. But doing so is the best way for them to handle code written for Nodeβitβs a cross-compatibility strategy. I think it would be best to push all bundlers toward doing this, if theyβre not already. But since TypeScript isnβt yet set up to model this behavior (without also adopting However, itβs not possible for us to model bundlers like Webpack that apply the Node-compat behavior to .mjs files, but not to .mts files. Thatβs something weβll need to push for across the bundler/TS-runtime ecosystem (Iβm assuming Webpack isnβt the only one that will need changes) before any TS option can work. |
I recall that you mentioned knowing about this issue for quite some time. Is there some tracking issue about this in webpack? Did their team comment on this anyhow in the past? |
I donβt know; Iβm in the process of figuring out exactly what to propose, and plan to talk to Sean soon. I havenβt mentioned it earlier because it was something I noticed in passing while focusing on module resolution, and needed to double check that I wasnβt just holding it wrong π₯΄ |
I opened webpack/webpack#17288, and also am showing relevant results at https://andrewbranch.github.io/interop-test/#synthesizing-default-exports-for-cjs-modules |
@Andarist do you remember how you originally encountered this issue, i.e. what libraries were involved? |
@andrewbranch likely it comes from me and @emmatown trying to implement our library building scheme that is meant to avoid dual package hazard and to provide consistent module shape across formats. We ended up emitting an extra proxy files to work around it. Notice the |
@andrewbranch what ever came of this change as well as this webpack issue? |
Our current opinion is that the prevalence of people having problems with this doesnβt merit additional confusing configuration. |
Bug Report
π Search Terms
moduleResolution bundler node webpack default namespace
π Version & Regression Information
moduleResolution: bundler
β― Playground Link
Repro case can be found here
π Actual behavior
When resolving a
.d.mts
file that proxies to.d.ts
in a CJS package withmoduleResolution: bundler
, TypeScript complains with an error like:The reason that it complains about it is that TypeScript is resolving
default
import as if the.d.ts
in this CJS package could export the "real" default export. webpack actually follows semantics coined by node and it loads the namespace object as thedefault
export of that "cjs file".So the type-level reality diverges there from runtime. At runtime, with webpack - it works exactly like it would work in node. TypeScript thinks that the intention was to load
module.exports.default
there though.π Expected behavior
I'm not exactly sure what's the expected behavior here because "resolving" like a bundler is quite under-specified and bundlers are not quite consistent when it comes to this. Even when we check this very same repro, we can see that I had to use
defaultIsModuleExports: true
in Rollup for it to behave the same. By default, Rollup behaves in the same way as TypeScript does.cc @andrewbranch
The text was updated successfully, but these errors were encountered: