Skip to content

Commit 8aa2521

Browse files
fix(vite-node): unable to handle errors where sourcemap mapping empty (#8071)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent dc469f2 commit 8aa2521

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

packages/vite-node/src/source-map-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ function mapSourcePosition(position: OriginalMapping) {
177177
if (!sourceMap) {
178178
// Call the (overridable) retrieveSourceMap function to get the source map.
179179
const urlAndMap = retrieveSourceMap(position.source)
180-
if (urlAndMap && urlAndMap.map) {
180+
const map = urlAndMap && urlAndMap.map
181+
if (map && !(typeof map === 'object' && 'mappings' in map && map.mappings === '')) {
181182
sourceMap = sourceMapCache[position.source] = {
182183
url: urlAndMap.url,
183-
map: new TraceMap(urlAndMap.map),
184+
map: new TraceMap(map),
184185
}
185186

186187
// Load all sources stored inline with the source map into the file cache
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-console
2+
console.log(new Error('[ok]').stack)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
plugins: [
5+
{
6+
name: 'repro',
7+
transform(code, id) {
8+
if (id.endsWith('/empty-mappings/main.ts')) {
9+
return { code, map: { mappings: '' } }
10+
}
11+
},
12+
},
13+
],
14+
})

test/vite-node/test/cli.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,9 @@ it('buildStart with all ssr', async () => {
7474
)
7575
await result.viteNode.waitForStdout('["buildStart:in","buildStart:out"]')
7676
})
77+
78+
it('empty mappings', async () => {
79+
const root = resolve(__dirname, '../src/empty-mappings')
80+
const result = await runViteNodeCli('--root', root, resolve(root, 'main.ts'))
81+
await result.viteNode.waitForStdout('[ok]')
82+
})

0 commit comments

Comments
 (0)