-
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
Weird behavior when bundling to esnext
#1360
Comments
Part 1I haven't looked into exactly why this happens yet, but this is likely a known limitation of esbuild's class transform. See #1328 for a prior discussion about this. Part 2I'm unable to reproduce this using the sample code from part 1. I tried |
Part 2 Reproductionimport { Nexwidget } from 'nexwidget';
class TestElement extends Nexwidget {
get template() {
return this.testAttribute;
}
}
TestElement.createAttributes({ testAttribute: String });
TestElement.register(); Looks like it has to do with private static methods. |
Running into this now, it appears that bundling always transforms private fields. Test case: // input.js
class Foo {
static #hello(){
console.log("!");
}
}
Using esnext/es2020, the output is unchanged from the input.
Bundling, though, always adds transforms, and same applies for most instances of private methods, private static methods, private getters, etc. (() => {
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
// esbuildtest.js
var _hello, hello_fn;
var Foo = class {
};
_hello = new WeakSet();
hello_fn = function() {
console.log("!");
};
__privateAdd(Foo, _hello);
})();
FWIW, on my 0.12.12 build, when |
Same thing if using static properties export class Foo {
static b = 1;
} When running (() => {
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
// esbuildtest.js
var Foo = class {
};
__publicField(Foo, "b", 1);
})(); |
class Foo {
static one = 'one';
#two = 'two';
} If you have a static public field and a private field (either static or not), the private fields will always be downgraded. So you cannot mix static fields with private fields in a class. |
We're seeing this for static fields:
Bundled with
It would be fine, except it breaks tree-shaking 😭 i.e., if we now use this bundled library somewhere,
but it get's a little clunky when TypeScript comes into play. |
I'm having the same result. Updated @matthewp's url to the working domain |
Any news? |
This issue has two parts
Part 1
esbuild version: 0.12.6
I have 2 dependencies using private fields and accessors syntax (ES2022). One of them (
nexwidget
) haslit-html
as its dependency that is written in ES2017.Consider this:
If I run esbuild with this config:
Why is there still a polyfill for private fields and accessors in the output file?
Part 2
Browser: Chrome 91
If there is a more complex code using the same dependencies (more modules, etc.) bundled with this config:
In Chrome 91 (I haven't tested other browsers) I get:
While with this config (with the exact same input), there are no errors:
The text was updated successfully, but these errors were encountered: