Skip to content

Commit 1bda0c3

Browse files
committed
fix #4461, fix #4467: esm evaluation fixes
1 parent 90d7abb commit 1bda0c3

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* Fix ES module evaluation when an error is thrown ([#4461](https://github.com/evanw/esbuild/issues/4461), [#4467](https://github.com/evanw/esbuild/pull/4467))
6+
7+
If an error is thrown during ES module evaluation, esbuild previously didn't preserve the state of the module for subsequent module references. This was observable if `import()` is used to import the module multiple times. The thrown error is supposed to be thrown by every call to `import()`, not just the first. With this release, esbuild will now throw the same error every time you call `import()` on a module that throws during its evaluation.
8+
59
* Fix some edge cases around the `new` operator ([#4477](https://github.com/evanw/esbuild/issues/4477))
610

711
Previously esbuild incorrectly printed certain edge cases involving complex expressions inside the target of a `new` expression (specifically an optional chain and/or a tagged template literal). The generated code for the `new` target was not correctly wrapped with parentheses, and either contained a syntax error or had different semantics. These edge cases have been fixed so that they now correctly wrap the `new` target in parentheses. Here is an example of some affected code:

internal/runtime/runtime.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,22 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source {
170170
// This is for lazily-initialized ESM code. This has two implementations, a
171171
// compact one for minified code and a verbose one that generates friendly
172172
// names in V8's profiler and in stack traces.
173-
export var __esm = (fn, res) => function __init() {
174-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
173+
export var __esm = (fn, res, err) => function __init() {
174+
if (err) throw err[0]
175+
try {
176+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
177+
} catch (e) {
178+
throw (err = [e]), e
179+
}
180+
}
181+
export var __esmMin = (fn, res, err) => () => {
182+
if (err) throw err[0]
183+
try {
184+
return fn && (res = fn(fn = 0)), res
185+
} catch (e) {
186+
throw (err = [e]), e
187+
}
175188
}
176-
export var __esmMin = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
177189
178190
// Wraps a CommonJS closure and returns a require() function. This has two
179191
// implementations, a compact one for minified code and a verbose one that

scripts/end-to-end-tests.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,43 @@ for (const minify of [[], ['--minify']]) {
21042104
export let async = async () => { if (42 !== await B()) throw 'fail' }
21052105
`,
21062106
}, { async: true }),
2107+
2108+
// https://github.com/evanw/esbuild/issues/4461
2109+
test(['--bundle', 'in.js', '--outfile=node.js', '--format=esm'].concat(minify), {
2110+
'in.js': `export let async = async () => {
2111+
let error
2112+
for (let i = 0; i < 3; i++) {
2113+
try {
2114+
await import('./foo')
2115+
throw new Error('Expected an error')
2116+
} catch (e) {
2117+
if (!e || e.message !== 'stop') throw 'fail ' + i + ': ' + e
2118+
if (i > 0 && e !== error) throw 'fail ' + i + ': wrong object'
2119+
error = e
2120+
}
2121+
}
2122+
}`,
2123+
'foo.js': `
2124+
export let foo
2125+
throw new Error('stop')
2126+
`,
2127+
}, { async: true }),
2128+
test(['--bundle', 'in.js', '--outfile=node.js', '--format=esm'].concat(minify), {
2129+
'in.js': `export let async = async () => {
2130+
for (let i = 0; i < 3; i++) {
2131+
try {
2132+
await import('./foo')
2133+
throw new Error('Expected an error')
2134+
} catch (e) {
2135+
if (e !== null) throw 'fail ' + i + ': ' + e
2136+
}
2137+
}
2138+
}`,
2139+
'foo.js': `
2140+
export let foo
2141+
throw null
2142+
`,
2143+
}, { async: true }),
21072144
)
21082145
}
21092146

0 commit comments

Comments
 (0)