Skip to content
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

Annotate module wrapper functions in debug builds #1244

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,54 @@

This fixes a bug with TypeScript code that uses `declare` on a class field and your `tsconfig.json` file has `"useDefineForClassFields": true`. Fields marked as `declare` should not be defined in the generated code, but they were incorrectly being declared as `undefined`. These fields are now correctly omitted from the generated code.

* Annotate module wrapper functions in debug builds ([#1236](https://github.com/evanw/esbuild/pull/1236))

Sometimes esbuild needs to wrap certain modules in a function when bundling. This is done both for lazy evaluation and for CommonJS modules that use a top-level `return` statement. Previously these functions were all anonymous, so stack traces for errors thrown during initialization looked like this:

```
Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
at getElectronPath (out.js:16:13)
at out.js:19:21
at out.js:1:45
at out.js:24:3
at out.js:1:45
at out.js:29:3
at out.js:1:45
at Object.<anonymous> (out.js:33:1)
```

This release adds names to these anonymous functions when minification is disabled. The above stack trace now looks like this:

```
Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
at getElectronPath (out.js:19:15)
at node_modules/electron/index.js (out.js:22:23)
at __require (out.js:2:44)
at src/base/window.js (out.js:29:5)
at __require (out.js:2:44)
at src/base/kiosk.js (out.js:36:5)
at __require (out.js:2:44)
at Object.<anonymous> (out.js:41:1)
```

This is similar to Webpack's development-mode behavior:

```
Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
at getElectronPath (out.js:23:11)
at Object../node_modules/electron/index.js (out.js:27:18)
at __webpack_require__ (out.js:96:41)
at Object../src/base/window.js (out.js:49:1)
at __webpack_require__ (out.js:96:41)
at Object../src/base/kiosk.js (out.js:38:1)
at __webpack_require__ (out.js:96:41)
at out.js:109:1
at out.js:111:3
at Object.<anonymous> (out.js:113:12)
```

These descriptive function names will additionally be available when using a profiler such as the one included in the "Performance" tab in Chrome Developer Tools. Previously all functions were named `(anonymous)` which made it difficult to investigate performance issues during bundle initialization.

## 0.11.18

* Add support for OpenBSD on x86-64 ([#1235](https://github.com/evanw/esbuild/issues/1235))
Expand Down
13 changes: 11 additions & 2 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,8 @@ func applyOptionDefaults(options *config.Options) {
{Data: "-", Placeholder: config.HashPlaceholder},
}
}

options.ProfilerNames = !options.MinifyIdentifiers
}

func (b *Bundle) Compile(log logger.Log, options config.Options, timer *helpers.Timer) ([]graph.OutputFile, string) {
Expand Down Expand Up @@ -2142,6 +2144,7 @@ func (b *Bundle) generateMetadataJSON(results []graph.OutputFile, allReachableFi
type runtimeCacheKey struct {
MangleSyntax bool
MinifyIdentifiers bool
ProfilerNames bool
ES6 bool
Platform config.Platform
}
Expand All @@ -2161,6 +2164,7 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.
// All configuration options that the runtime code depends on must go here
MangleSyntax: options.MangleSyntax,
MinifyIdentifiers: options.MinifyIdentifiers,
ProfilerNames: options.ProfilerNames,
Platform: options.Platform,
ES6: runtime.CanUseES6(options.UnsupportedJSFeatures),
}
Expand Down Expand Up @@ -2197,7 +2201,7 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.
MangleSyntax: key.MangleSyntax,
MinifyIdentifiers: key.MinifyIdentifiers,
Platform: key.Platform,
Defines: cache.processedDefines(key.Platform),
Defines: cache.processedDefines(key.Platform, key.ProfilerNames),
UnsupportedJSFeatures: compat.UnsupportedJSFeatures(
map[compat.Engine][]int{compat.ES: {constraint}}),

Expand Down Expand Up @@ -2225,7 +2229,7 @@ func (cache *runtimeCache) parseRuntime(options *config.Options) (source logger.
return
}

func (cache *runtimeCache) processedDefines(key config.Platform) (defines *config.ProcessedDefines) {
func (cache *runtimeCache) processedDefines(key config.Platform, profilerNames bool) (defines *config.ProcessedDefines) {
ok := false

// Cache hit?
Expand Down Expand Up @@ -2256,6 +2260,11 @@ func (cache *runtimeCache) processedDefines(key config.Platform) (defines *confi
return &js_ast.EString{Value: js_lexer.StringToUTF16(platform)}
},
},
"__profiler": {
DefineFunc: func(da config.DefineArgs) js_ast.E {
return &js_ast.EBoolean{Value: profilerNames}
},
},
})
defines = &result

Expand Down
60 changes: 36 additions & 24 deletions internal/bundler/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3371,21 +3371,27 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
}
}

// "__commonJS((exports, module) => { ... })"
var value js_ast.Expr
if c.options.UnsupportedJSFeatures.Has(compat.Arrow) {
value = js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: commonJSRef}},
Args: []js_ast.Expr{{Data: &js_ast.EFunction{Fn: js_ast.Fn{Args: args, Body: js_ast.FnBody{Stmts: stmts}}}}},
}}
var cjsArgs []js_ast.Expr
if c.options.ProfilerNames {
// "__commonJS({ 'file.js'(exports, module) { ... } })"
cjsArgs = []js_ast.Expr{{Data: &js_ast.EObject{Properties: []js_ast.Property{{
IsMethod: !c.options.UnsupportedJSFeatures.Has(compat.ObjectExtensions),
Key: js_ast.Expr{Data: &js_ast.EString{Value: js_lexer.StringToUTF16(file.InputFile.Source.PrettyPath)}},
Value: &js_ast.Expr{Data: &js_ast.EFunction{Fn: js_ast.Fn{Args: args, Body: js_ast.FnBody{Stmts: stmts}}}},
}}}}}
} else if c.options.UnsupportedJSFeatures.Has(compat.Arrow) {
// "__commonJS(function (exports, module) { ... })"
cjsArgs = []js_ast.Expr{{Data: &js_ast.EFunction{Fn: js_ast.Fn{Args: args, Body: js_ast.FnBody{Stmts: stmts}}}}}
} else {
value = js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: commonJSRef}},
Args: []js_ast.Expr{{Data: &js_ast.EArrow{Args: args, Body: js_ast.FnBody{Stmts: stmts}}}},
}}
// "__commonJS((exports, module) => { ... })"
cjsArgs = []js_ast.Expr{{Data: &js_ast.EArrow{Args: args, Body: js_ast.FnBody{Stmts: stmts}}}}
}
value := js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: commonJSRef}},
Args: cjsArgs,
}}

// "var require_foo = __commonJS((exports, module) => { ... });"
// "var require_foo = __commonJS(...);"
stmts = append(stmtList.outsideWrapperPrefix, js_ast.Stmt{Data: &js_ast.SLocal{
Decls: []js_ast.Decl{{
Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: repr.AST.WrapperRef}},
Expand Down Expand Up @@ -3433,19 +3439,25 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
}
stmts = stmts[:end]

// "__esm(() => { ... })"
var value js_ast.Expr
if c.options.UnsupportedJSFeatures.Has(compat.Arrow) {
value = js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: esmRef}},
Args: []js_ast.Expr{{Data: &js_ast.EFunction{Fn: js_ast.Fn{Body: js_ast.FnBody{Stmts: stmts}, IsAsync: isAsync}}}},
}}
var esmArgs []js_ast.Expr
if c.options.ProfilerNames {
// "__esm({ 'file.js'() { ... } })"
esmArgs = []js_ast.Expr{{Data: &js_ast.EObject{Properties: []js_ast.Property{{
IsMethod: !c.options.UnsupportedJSFeatures.Has(compat.ObjectExtensions),
Key: js_ast.Expr{Data: &js_ast.EString{Value: js_lexer.StringToUTF16(file.InputFile.Source.PrettyPath)}},
Value: &js_ast.Expr{Data: &js_ast.EFunction{Fn: js_ast.Fn{Body: js_ast.FnBody{Stmts: stmts}, IsAsync: isAsync}}},
}}}}}
} else if c.options.UnsupportedJSFeatures.Has(compat.Arrow) {
// "__esm(function () { ... })"
esmArgs = []js_ast.Expr{{Data: &js_ast.EFunction{Fn: js_ast.Fn{Body: js_ast.FnBody{Stmts: stmts}, IsAsync: isAsync}}}}
} else {
value = js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: esmRef}},
Args: []js_ast.Expr{{Data: &js_ast.EArrow{Body: js_ast.FnBody{Stmts: stmts}, IsAsync: isAsync}}},
}}
// "__esm(() => { ... })"
esmArgs = []js_ast.Expr{{Data: &js_ast.EArrow{Body: js_ast.FnBody{Stmts: stmts}, IsAsync: isAsync}}}
}
value := js_ast.Expr{Data: &js_ast.ECall{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: esmRef}},
Args: esmArgs,
}}

// "var foo, bar;"
if !c.options.MangleSyntax && len(decls) > 0 {
Expand All @@ -3455,7 +3467,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
decls = nil
}

// "var init_foo = __esm(() => { ... });"
// "var init_foo = __esm(...);"
stmts = append(stmtList.outsideWrapperPrefix, js_ast.Stmt{Data: &js_ast.SLocal{
Decls: append(decls, js_ast.Decl{
Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: repr.AST.WrapperRef}},
Expand Down
Loading