follow up: cjs evaluation fixes

This commit is contained in:
Evan Wallace
2026-06-07 23:50:39 -04:00
parent adec3d2d28
commit c3013c9b4e
3 changed files with 50 additions and 6 deletions
+2 -2
View File
@@ -2,9 +2,9 @@
## Unreleased
* 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))
* Fix module evaluation when an error is thrown ([#4461](https://github.com/evanw/esbuild/issues/4461), [#4467](https://github.com/evanw/esbuild/pull/4467))
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.
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.
* Fix some edge cases around the `new` operator ([#4477](https://github.com/evanw/esbuild/issues/4477))
+14 -4
View File
@@ -175,7 +175,7 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source {
try {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
} catch (e) {
throw (err = [e]), e
throw err = [e], e
}
}
export var __esmMin = (fn, res, err) => () => {
@@ -183,7 +183,7 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source {
try {
return fn && (res = fn(fn = 0)), res
} catch (e) {
throw (err = [e]), e
throw err = [e], e
}
}
@@ -191,9 +191,19 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source {
// implementations, a compact one for minified code and a verbose one that
// generates friendly names in V8's profiler and in stack traces.
export var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = {exports: {}}).exports, mod), mod.exports
try {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports
} catch (e) {
throw mod = 0, e
}
}
export var __commonJSMin = (cb, mod) => () => {
try {
return mod || cb((mod = { exports: {} }).exports, mod), mod.exports
} catch (e) {
throw mod = 0, e
}
}
export var __commonJSMin = (cb, mod) => () => (mod || cb((mod = {exports: {}}).exports, mod), mod.exports)
// Used to implement ESM exports both for "require()" and "import * as"
export var __export = (target, all) => {
+34
View File
@@ -2141,6 +2141,40 @@ for (const minify of [[], ['--minify']]) {
throw null
`,
}, { async: true }),
test(['--bundle', 'in.js', '--outfile=node.js'].concat(minify), {
'in.js': `
let errors = []
for (let i = 0; i < 3; i++) {
try {
require('./foo')
throw new Error('Expected an error')
} catch (e) {
if (e.message !== 'stop1') throw 'fail ' + i + ': ' + e
if (errors.includes(e)) throw 'fail ' + i + ': wrong object'
errors.push(e)
}
}
`,
'foo.js': `
exports.counter = (exports.counter | 0) + 1
throw new Error('stop' + exports.counter)
`,
}),
test(['--bundle', 'in.js', '--outfile=node.js'].concat(minify), {
'in.js': `
for (let i = 0; i < 3; i++) {
try {
require('./foo')
throw new Error('Expected an error')
} catch (e) {
if (e !== null) throw 'fail ' + i + ': ' + e
}
}
`,
'foo.js': `
throw null
`,
}),
)
}