GeezerVison is showing github.com — follow up: cjs evaluation fixes · evanw/esbuild@15300c3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: CHANGELOG.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | ## Unreleased | 4 | 4 |
| 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)) | 6 | 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. | | 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. | 8 | 8 |
| 9 | 9 | * Fix some edge cases around the `new` operator ([#4477](https://github.com/evanw/esbuild/issues/4477)) | 10 | 10 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/runtime/runtime.go +14-4Lines changed: 14 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -175,25 +175,35 @@ func Source(unsupportedJSFeatures compat.JSFeature) logger.Source { | 175 | 175 | try { | 176 | 176 | return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res | 177 | 177 | } catch (e) { | 178 | | - throw (err = [e]), e | | 178 | + throw err = [e], e | 179 | 179 | } | 180 | 180 | } | 181 | 181 | export var __esmMin = (fn, res, err) => () => { | 182 | 182 | if (err) throw err[0] | 183 | 183 | try { | 184 | 184 | return fn && (res = fn(fn = 0)), res | 185 | 185 | } catch (e) { | 186 | | - throw (err = [e]), e | | 186 | + throw err = [e], e | 187 | 187 | } | 188 | 188 | } | 189 | 189 |
| 190 | 190 | // Wraps a CommonJS closure and returns a require() function. This has two | 191 | 191 | // implementations, a compact one for minified code and a verbose one that | 192 | 192 | // generates friendly names in V8's profiler and in stack traces. | 193 | 193 | 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 | + } | 195 | 206 | } | 196 | | - export var __commonJSMin = (cb, mod) => () => (mod || cb((mod = {exports: {}}).exports, mod), mod.exports) | 197 | 207 |
| 198 | 208 | // Used to implement ESM exports both for "require()" and "import * as" | 199 | 209 | export var __export = (target, all) => { | |
Collapse file Copy file name to clipboardExpand all lines: scripts/end-to-end-tests.js +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2141,6 +2141,40 @@ for (const minify of [[], ['--minify']]) { | 2141 | 2141 | throw null | 2142 | 2142 | `, | 2143 | 2143 | }, { 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 | + }), | 2144 | 2178 | ) | 2145 | 2179 | } | 2146 | 2180 |
| |
|