Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vite/src/module-runner/evaluatedModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,12 @@ export class EvaluatedModules {
if (!mod) return null
if (mod.map) return mod.map
if (!mod.meta || !('code' in mod.meta)) return null
const pattern = `//# ${SOURCEMAPPING_URL}=data:application/json;base64,`
const lastIndex = mod.meta.code.lastIndexOf(pattern)
if (lastIndex === -1) return null

const mapString = MODULE_RUNNER_SOURCEMAPPING_REGEXP.exec(
mod.meta.code,
mod.meta.code.slice(lastIndex),
)?.[1]
if (!mapString) return null
mod.map = new DecodedMap(JSON.parse(decodeBase64(mapString)), mod.file)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file contains sourceMappingURL pattern in string literals
// which should not crash the module runner

const text = '//# sourceMappingURL=data:application/json;base64,invalidbase64'

export function getMessage() {
return text
}

export function throwError() {
throw new Error('Test error for stacktrace')
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,20 @@
})
expect(serializeStackDeep(server, methodError).slice(0, 3)).toEqual([
'Error: crash',
' at crash (<root>/fixtures/has-error-deep.ts:2:9)',

Check failure on line 92 in packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts

View workflow job for this annotation

GitHub Actions / copilot

packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts > module runner initialization > should not crash when sourceMappingURL pattern appears in string literals

ReferenceError: server is not defined ❯ packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts:92:57
' at Module.main (<root>/fixtures/has-error-deep.ts:6:3)',

Check failure on line 93 in packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts

View workflow job for this annotation

GitHub Actions / copilot

packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts > module runner initialization > should not crash when sourceMappingURL pattern appears in string literals

AssertionError: expected [ …(6) ] to deeply equal [ …(3) ] - Expected + Received [ "Error: Test error for stacktrace", - " at throwError (<root>/fixtures/string-literal-sourcemap.ts:11:9)", - " at <root>/fixtures/string-literal-sourcemap.ts:11:9", + " at Module.throwError (<root>/fixtures/string-literal-sourcemap.ts:11:9)", + " at <root>/server-source-maps.spec.ts:91:44", + " at getError (<root>/server-source-maps.spec.ts:14:13)", + " at <root>/server-source-maps.spec.ts:91:25", + " at file:///home/runner/work/vite/vite/node_modules/.pnpm/@vitest[email protected]/node_modules/@vitest/runner/dist/chunk-hooks.js:752:20", ] ❯ packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts:93:73
])
})

it('should not crash when sourceMappingURL pattern appears in string literals', async ({
runner,
}) => {
const mod = await runner.import('/fixtures/string-literal-sourcemap.ts')
expect(mod.getMessage()).toBe(
'//# sourceMappingURL=data:application/json;base64,invalidbase64',
)

const error = await getError(() => mod.throwError())
expect(error.message).toBe('Test error for stacktrace')
})
})
Loading