Skip to content

Commit 15300c3

Browse files
committed
follow up: cjs evaluation fixes
1 parent 1bda0c3 commit 15300c3

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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))
5+
* Fix module evaluation when an error is thrown ([#4461](https://github.com/evanw/esbuild/issues/4461), [#4467](https://github.com/evanw/esbuild/pull/4467))
66

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.
7+
If an error is thrown during module evaluation, esbuild previously didn't preserve the state of the module for subsequent module references. This was observable if `import()` or `require()` is used to import a module multiple times. The thrown error is supposed to be thrown by every call to `import()` or `require()`, not just the first. With this release, esbuild will now throw the same error every time you call `import()` or `require()` on a module that throws during its evaluation.
88

99
* Fix some edge cases around the `new` operator ([#4477](https://github.com/evanw/esbuild/issues/4477))
1010

internal/runtime/runtime.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,35 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source {
175175
try {
176176
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
177177
} catch (e) {
178-
throw (err = [e]), e
178+
throw err = [e], e
179179
}
180180
}
181181
export var __esmMin = (fn, res, err) => () => {
182182
if (err) throw err[0]
183183
try {
184184
return fn && (res = fn(fn = 0)), res
185185
} catch (e) {
186-
throw (err = [e]), e
186+
throw err = [e], e
187187
}
188188
}
189189
190190
// Wraps a CommonJS closure and returns a require() function. This has two
191191
// implementations, a compact one for minified code and a verbose one that
192192
// generates friendly names in V8's profiler and in stack traces.
193193
export var __commonJS = (cb, mod) => function __require() {
194-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {exports: {}}).exports, mod), mod.exports
194+
try {
195+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
196+
} catch (e) {
197+
throw mod = 0, e
198+
}
199+
}
200+
export var __commonJSMin = (cb, mod) => () => {
201+
try {
202+
return mod || cb((mod = { exports: {} }).exports, mod), mod.exports
203+
} catch (e) {
204+
throw mod = 0, e
205+
}
195206
}
196-
export var __commonJSMin = (cb, mod) => () => (mod || cb((mod = {exports: {}}).exports, mod), mod.exports)
197207
198208
// Used to implement ESM exports both for "require()" and "import * as"
199209
export var __export = (target, all) => {

scripts/end-to-end-tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,6 +2141,40 @@ for (const minify of [[], ['--minify']]) {
21412141
throw null
21422142
`,
21432143
}, { async: true }),
2144+
test(['--bundle', 'in.js', '--outfile=node.js'].concat(minify), {
2145+
'in.js': `
2146+
let errors = []
2147+
for (let i = 0; i < 3; i++) {
2148+
try {
2149+
require('./foo')
2150+
throw new Error('Expected an error')
2151+
} catch (e) {
2152+
if (e.message !== 'stop1') throw 'fail ' + i + ': ' + e
2153+
if (errors.includes(e)) throw 'fail ' + i + ': wrong object'
2154+
errors.push(e)
2155+
}
2156+
}
2157+
`,
2158+
'foo.js': `
2159+
exports.counter = (exports.counter | 0) + 1
2160+
throw new Error('stop' + exports.counter)
2161+
`,
2162+
}),
2163+
test(['--bundle', 'in.js', '--outfile=node.js'].concat(minify), {
2164+
'in.js': `
2165+
for (let i = 0; i < 3; i++) {
2166+
try {
2167+
require('./foo')
2168+
throw new Error('Expected an error')
2169+
} catch (e) {
2170+
if (e !== null) throw 'fail ' + i + ': ' + e
2171+
}
2172+
}
2173+
`,
2174+
'foo.js': `
2175+
throw null
2176+
`,
2177+
}),
21442178
)
21452179
}
21462180

0 commit comments

Comments
 (0)