Skip to content

Commit

Permalink
always apply "--avoid-tdz"
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Nov 23, 2020
1 parent 519c68d commit 869e811
Show file tree
Hide file tree
Showing 17 changed files with 407 additions and 362 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -53,6 +53,10 @@
console.log(old.test() === old);
```

* The `--allow-tdz` option is now always applied during bundling

This option turns top-level `let`, `const`, and `class` statements into `var` statements to work around some severe performance issues in the JavaScript run-time environment in Safari. Previously you had to explicitly enable this option. Now this behavior will always happen, and there is no way to turn it off. This means the `--allow-tdz` option is now meaningless and no longer does anything. It will be removed in a future release.

## 0.8.12

* Added an API for incremental builds ([#21](https://github.com/evanw/esbuild/issues/21))
Expand Down
24 changes: 21 additions & 3 deletions internal/bundler/bundler_default_test.go
Expand Up @@ -3409,7 +3409,7 @@ func TestOutbase(t *testing.T) {
})
}

func TestAvoidTDZNoBundle(t *testing.T) {
func TestAvoidTDZ(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
Expand All @@ -3421,14 +3421,32 @@ func TestAvoidTDZNoBundle(t *testing.T) {
export class Bar {}
export let bar = 123
`,
"/foo.js": `
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
})
}

func TestAvoidTDZNoBundle(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
class Foo {
static foo = new Foo
}
let foo = Foo.foo
console.log(foo)
export class Bar {}
export let bar = 123
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModePassThrough,
AbsOutputFile: "/out.js",
AvoidTDZ: true,
},
})
}
Expand Down
21 changes: 3 additions & 18 deletions internal/bundler/linker.go
Expand Up @@ -2833,33 +2833,18 @@ func (c *linkerContext) convertStmtsForChunk(sourceIndex uint32, stmtList *stmtL
}

case *js_ast.SClass:
if c.options.AvoidTDZ {
stmt.Data = &js_ast.SLocal{
IsExport: s.IsExport && !shouldStripExports,
Decls: []js_ast.Decl{{
Binding: js_ast.Binding{Loc: s.Class.Name.Loc, Data: &js_ast.BIdentifier{Ref: s.Class.Name.Ref}},
Value: &js_ast.Expr{Loc: stmt.Loc, Data: &js_ast.EClass{Class: s.Class}},
}},
}
} else if shouldStripExports && s.IsExport {
if shouldStripExports && s.IsExport {
// Be careful to not modify the original statement
clone := *s
clone.IsExport = false
stmt.Data = &clone
}

case *js_ast.SLocal:
stripExport := shouldStripExports && s.IsExport
avoidTDZ := c.options.AvoidTDZ && s.Kind != js_ast.LocalVar
if stripExport || avoidTDZ {
if shouldStripExports && s.IsExport {
// Be careful to not modify the original statement
clone := *s
if stripExport {
clone.IsExport = false
}
if avoidTDZ {
clone.Kind = js_ast.LocalVar
}
clone.IsExport = false
stmt.Data = &clone
}

Expand Down
62 changes: 31 additions & 31 deletions internal/bundler/snapshots/snapshots_dce.txt
Expand Up @@ -18,13 +18,13 @@ console.log("side effects");
// /entry.jsx
function KeepMe1() {
}
let keepMe2 = React.createElement(KeepMe1, null);
var keepMe2 = React.createElement(KeepMe1, null);
function keepMe3() {
console.log("side effects");
}
let keepMe4 = keepMe3();
let keepMe5 = pure();
let keepMe6 = some.fn();
var keepMe4 = keepMe3();
var keepMe5 = pure();
var keepMe6 = some.fn();

================================================================================
TestFileLoaderRemoveUnused
Expand Down Expand Up @@ -61,7 +61,7 @@ var require_index_main = __commonJS((exports) => {
__export(exports, {
foo: () => foo2
});
const foo2 = 123;
var foo2 = 123;
console.log("this should be kept");
});

Expand Down Expand Up @@ -103,7 +103,7 @@ var require_index_main = __commonJS((exports) => {
__export(exports, {
foo: () => foo
});
const foo = 123;
var foo = 123;
console.log("this should be kept");
});

Expand Down Expand Up @@ -164,7 +164,7 @@ var require_demo_pkg = __commonJS((exports) => {
__export(exports, {
foo: () => foo
});
const foo = 123;
var foo = 123;
console.log("hello");
});

Expand All @@ -189,7 +189,7 @@ console.log(demo_pkg.foo);
TestPackageJsonSideEffectsFalseKeepNamedImportES6
---------- /out.js ----------
// /Users/user/project/node_modules/demo-pkg/index.js
const foo = 123;
var foo = 123;
console.log("hello");

// /Users/user/project/src/entry.js
Expand All @@ -216,7 +216,7 @@ var demo_pkg_exports = {};
__export(demo_pkg_exports, {
foo: () => foo
});
const foo = 123;
var foo = 123;
console.log("hello");

// /Users/user/project/src/entry.js
Expand Down Expand Up @@ -307,25 +307,25 @@ TestRemoveUnusedPureCommentCalls
// /entry.js
function bar() {
}
let bare = foo(bar);
let at_no = /* @__PURE__ */ foo(bar());
let new_at_no = /* @__PURE__ */ new foo(bar());
let num_no = /* @__PURE__ */ foo(bar());
let new_num_no = /* @__PURE__ */ new foo(bar());
let dot_no = /* @__PURE__ */ foo(sideEffect()).dot(bar());
let new_dot_no = /* @__PURE__ */ new foo(sideEffect()).dot(bar());
let nested_no = [1, /* @__PURE__ */ foo(bar()), 2];
let new_nested_no = [1, /* @__PURE__ */ new foo(bar()), 2];
let single_at_no = /* @__PURE__ */ foo(bar());
let new_single_at_no = /* @__PURE__ */ new foo(bar());
let single_num_no = /* @__PURE__ */ foo(bar());
let new_single_num_no = /* @__PURE__ */ new foo(bar());
let bad_no = foo(bar);
let new_bad_no = new foo(bar);
let parens_no = foo(bar);
let new_parens_no = new foo(bar);
let exp_no = /* @__PURE__ */ foo() ** foo();
let new_exp_no = /* @__PURE__ */ new foo() ** foo();
var bare = foo(bar);
var at_no = /* @__PURE__ */ foo(bar());
var new_at_no = /* @__PURE__ */ new foo(bar());
var num_no = /* @__PURE__ */ foo(bar());
var new_num_no = /* @__PURE__ */ new foo(bar());
var dot_no = /* @__PURE__ */ foo(sideEffect()).dot(bar());
var new_dot_no = /* @__PURE__ */ new foo(sideEffect()).dot(bar());
var nested_no = [1, /* @__PURE__ */ foo(bar()), 2];
var new_nested_no = [1, /* @__PURE__ */ new foo(bar()), 2];
var single_at_no = /* @__PURE__ */ foo(bar());
var new_single_at_no = /* @__PURE__ */ new foo(bar());
var single_num_no = /* @__PURE__ */ foo(bar());
var new_single_num_no = /* @__PURE__ */ new foo(bar());
var bad_no = foo(bar);
var new_bad_no = new foo(bar);
var parens_no = foo(bar);
var new_parens_no = new foo(bar);
var exp_no = /* @__PURE__ */ foo() ** foo();
var new_exp_no = /* @__PURE__ */ new foo() ** foo();

================================================================================
TestTextLoaderRemoveUnused
Expand All @@ -339,7 +339,7 @@ TestTreeShakingReactElements
// /entry.jsx
function Foo() {
}
let d = /* @__PURE__ */ React.createElement("div", null);
let e = /* @__PURE__ */ React.createElement(Foo, null, d);
let f = /* @__PURE__ */ React.createElement(React.Fragment, null, e);
var d = /* @__PURE__ */ React.createElement("div", null);
var e = /* @__PURE__ */ React.createElement(Foo, null, d);
var f = /* @__PURE__ */ React.createElement(React.Fragment, null, e);
console.log(f);

0 comments on commit 869e811

Please sign in to comment.