Skip to content

Weird behavior when bundling to esnext #1360

Closed
@Hawmex

Description

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) has lit-html as its dependency that is written in ES2017.
Consider this:

import { Nexbounce } from 'nexbounce';
import { render } from 'nexwidget';

let counter = 0;

const nexbounce = new Nexbounce();

nexbounce.enqueue(() => (counter += 3));
nexbounce.enqueue(() => (counter += 1));
nexbounce.enqueue(() => (counter += 2));

setTimeout(() => render(counter, document.body));

If I run esbuild with this config:

.\node_modules\.bin\esbuild src/index.js --bundle --target=esnext --splitting --outdir=build --format=esm

Why is there still a polyfill for private fields and accessors in the output file?

var __accessCheck = (obj, member, msg) => {
  if (!member.has(obj))
    throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
  __accessCheck(obj, member, "read from private field");
  return getter ? getter.call(obj) : member.get(obj);
};
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);
};
var __privateSet = (obj, member, value, setter) => {
  __accessCheck(obj, member, "write to private field");
  setter ? setter.call(obj, value) : member.set(obj, value);
  return value;
};
var __privateMethod = (obj, member, method) => {
  __accessCheck(obj, member, "access private method");
  return method;
};

//...

Part 2

Browser: Chrome 91

If there is a more complex code using the same dependencies (more modules, etc.) bundled with this config:

.\node_modules\.bin\esbuild src/index.js --bundle --target=esnext --splitting --outdir=build --format=esm

In Chrome 91 (I haven't tested other browsers) I get:

Uncaught TypeError: Cannot read from private field
    at __accessCheck (chunk-PSDPPOBH.js:3)
    at __privateGet (chunk-PSDPPOBH.js:6)
    at Function.__create (chunk-PSDPPOBH.js:1293)
    at Function.createAttributes (chunk-PSDPPOBH.js:1093)
    at chunk-PSDPPOBH.js:1409

While with this config (with the exact same input), there are no errors:

.\node_modules\.bin\esbuild src/index.js --bundle --target=es2020--splitting --outdir=build --format=esm

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions