-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
CommonJS exports of objects with prototypes are handed differently from webpack #326
Comments
Thank you for reporting this inconsistency. I did a quick survey of how different tools handle this. Here's the code I ran: import Paper from "./paper.js";
import { Segment } from "./paper.js";
import * as star from "./paper.js";
console.log(Paper, Paper.Segment, Segment, star); And the results:
Everything does something different with the exception that esbuild matches Parcel. It looks like none of them consider inherited properties as exports for star imports. Right now regular imports and star imports behave consistently in esbuild, so I think addressing this issue while maintaining compatibility with star imports means making regular imports and star imports no longer consistent with each other. Edit: Actually, I've thought about this more and I changed my mind. I think it is appropriate for esbuild to break compatibility with star exports. Ideally either something is exported or it isn't. If we're going to consider prototype properties as exports, then that should be treated consistently in all cases. So I now believe the correct output should be this:
|
This was just released in version 0.6.22. |
@evanw I just came across this inconsistency in our project, where we recently (very happily) switched form We use
Because of that, the An interesting thing to note here is that For now I've rewritten our import to work around this, but it's concerning that this only manifests at runtime, as it makes me worry that we have other such imports from other libraries. Curious if you have any advice on how we should prevent this. |
Overview
Consider the following (simplified) code from a real codebase:
The
paper
module is a CommonJS module that exports an object. The unusual thing about that object is that it doesn’t have its own properties – instead, all properties are inherited.When webpack and ESBuild encounter such module, they seem to behave differently. Both webpack and ESBuild pass this CommonJS module through an interop – which converts
to (roughly)
However, unlike webpack, ESBuild ignores inherited properties. This means the following code:
imports
class Path
with webpack butundefined
with ESBuild. And so, with ESBuild,new Path()
results inTypeError: Path is not a constructor
.Repro
Clone https://github.com/iamakulov/esbuild-webpack-prototype-repro
Run
yarn
Run the webpack build and verify that it resolves the named export properly:
Run the webpack build and verify that it doesn’t resolve the named export:
The text was updated successfully, but these errors were encountered: