Skip to content

Commit d3c67f9

Browse files
committed
fix #4310: add Iterator and other known globals
1 parent 4a51f0b commit d3c67f9

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66

77
The previous release introduced support for parsing media queries which unintentionally introduced a regression with the removal of duplicate media rules during minification. Specifically the grammar for `@media <media-type> and <media-condition-without-or> { ... }` was missing an equality check for the `<media-condition-without-or>` part, so rules with different suffix clauses in this position would incorrectly compare equal and be deduplicated. This release fixes the regression.
88

9+
* Update the list of known JavaScript globals ([#4310](https://github.com/evanw/esbuild/issues/4310))
10+
11+
This release updates esbuild's internal list of known JavaScript globals. These are globals that are known to not have side-effects when the property is accessed. For example, accessing the global `Array` property is considered to be side-effect free but accessing the global `scrollY` property can trigger a layout, which is a side-effect. This is used by esbuild's tree-shaking to safely remove unused code that is known to be side-effect free. This update adds the following global properties:
12+
13+
From [ES2017](https://tc39.es/ecma262/2017/):
14+
- `Atomics`
15+
- `SharedArrayBuffer`
16+
17+
From [ES2020](https://tc39.es/ecma262/2020/):
18+
- `BigInt64Array`
19+
- `BigUint64Array`
20+
21+
From [ES2021](https://tc39.es/ecma262/2021/):
22+
- `FinalizationRegistry`
23+
- `WeakRef`
24+
25+
From [ES2025](https://tc39.es/ecma262/2025/):
26+
- `Float16Array`
27+
- `Iterator`
28+
29+
Note that this does not indicate that constructing any of these objects is side-effect free, just that accessing the identifier is side-effect free. For example, this now allows esbuild to tree-shake classes that extend from `Iterator`:
30+
31+
```js
32+
// This can now be tree-shaken by esbuild:
33+
class ExampleIterator extends Iterator {}
34+
```
35+
936
* Add support for the new `@view-transition` CSS rule ([#4313](https://github.com/evanw/esbuild/pull/4313))
1037

1138
With this release, esbuild now has improved support for pretty-printing and minifying the new `@view-transition` rule (which esbuild was previously unaware of):

internal/bundler_tests/bundler_dce_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4930,3 +4930,20 @@ func TestDCEOfNegatedBigints(t *testing.T) {
49304930
},
49314931
})
49324932
}
4933+
4934+
// https://github.com/evanw/esbuild/issues/4310
4935+
func TestDCEOfIteratorSuperclassIssue4310(t *testing.T) {
4936+
dce_suite.expectBundled(t, bundled{
4937+
files: map[string]string{
4938+
"/entry.js": `
4939+
class Keep extends NotIterator {}
4940+
class Remove extends Iterator {}
4941+
`,
4942+
},
4943+
entryPaths: []string{"/entry.js"},
4944+
options: config.Options{
4945+
Mode: config.ModeBundle,
4946+
AbsOutputDir: "/out",
4947+
},
4948+
})
4949+
}

internal/bundler_tests/snapshots/snapshots_dce.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,13 @@ use(isNotPure);
652652
} };
653653
})();
654654

655+
================================================================================
656+
TestDCEOfIteratorSuperclassIssue4310
657+
---------- /out/entry.js ----------
658+
// entry.js
659+
var Keep = class extends NotIterator {
660+
};
661+
655662
================================================================================
656663
TestDCEOfNegatedBigints
657664
---------- /out/entry.js ----------

internal/config/globals.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ var processedGlobals *ProcessedDefines
2727
// these functions has any side effects. It only says something about
2828
// referencing these function without calling them.
2929
var knownGlobals = [][]string{
30-
// These global identifiers should exist in all JavaScript environments. This
31-
// deliberately omits "NaN", "Infinity", and "undefined" because these are
32-
// treated as automatically-inlined constants instead of identifiers.
33-
{"Array"},
34-
{"Boolean"},
35-
{"Function"},
36-
{"Math"},
37-
{"Number"},
38-
{"Object"},
39-
{"RegExp"},
40-
{"String"},
41-
4230
// Object: Static methods
4331
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods
4432
{"Object", "assign"},
@@ -166,36 +154,57 @@ var knownGlobals = [][]string{
166154
{"JSON", "parse"},
167155
{"JSON", "stringify"},
168156

169-
// Other globals present in both the browser and node (except "eval" because
170-
// it has special behavior)
157+
// Other globals present in both the browser and node. This should include at
158+
// least the following properties:
159+
// https://tc39.es/ecma262/multipage/global-object.html#sec-constructor-properties-of-the-global-object
160+
//
161+
// Exceptions:
162+
// - Don't include "eval" because it has special behavior
163+
// - Don't include "NaN", "Infinity", and "undefined" because esbuild treats
164+
// these as automatically-inlined constants instead of identifiers
171165
{"AbortController"},
172166
{"AbortSignal"},
173167
{"AggregateError"},
168+
{"Array"},
174169
{"ArrayBuffer"},
170+
{"Atomics"},
175171
{"BigInt"},
172+
{"BigInt64Array"},
173+
{"BigUint64Array"},
174+
{"Boolean"},
176175
{"DataView"},
177176
{"Date"},
178177
{"Error"},
179178
{"EvalError"},
180179
{"Event"},
181180
{"EventTarget"},
181+
{"FinalizationRegistry"},
182+
{"Float16Array"},
182183
{"Float32Array"},
183184
{"Float64Array"},
185+
{"Function"},
184186
{"Int16Array"},
185187
{"Int32Array"},
186188
{"Int8Array"},
187189
{"Intl"},
190+
{"Iterator"},
188191
{"JSON"},
189192
{"Map"},
193+
{"Math"},
190194
{"MessageChannel"},
191195
{"MessageEvent"},
192196
{"MessagePort"},
197+
{"Number"},
198+
{"Object"},
193199
{"Promise"},
194200
{"Proxy"},
195201
{"RangeError"},
196202
{"ReferenceError"},
197203
{"Reflect"},
204+
{"RegExp"},
198205
{"Set"},
206+
{"SharedArrayBuffer"},
207+
{"String"},
199208
{"Symbol"},
200209
{"SyntaxError"},
201210
{"TextDecoder"},
@@ -209,6 +218,7 @@ var knownGlobals = [][]string{
209218
{"Uint8Array"},
210219
{"Uint8ClampedArray"},
211220
{"WeakMap"},
221+
{"WeakRef"},
212222
{"WeakSet"},
213223
{"WebAssembly"},
214224
{"clearInterval"},

0 commit comments

Comments
 (0)