Skip to content

Commit

Permalink
fix(ssrTransform): sourcemaps with multiple sources (#17677)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 17, 2024
1 parent 63fabfc commit f321fa8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { expect, test } from 'vitest'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { assert, expect, test } from 'vitest'
import type { SourceMap } from 'rollup'
import { transformWithEsbuild } from '../../plugins/esbuild'
import { ssrTransform } from '../ssrTransform'

Expand Down Expand Up @@ -411,11 +414,33 @@ test('sourcemap source', async () => {
'input.js',
'export const a = 1 /* */',
)
)?.map
)?.map as SourceMap

expect(map?.sources).toStrictEqual(['input.js'])
expect(map?.sourcesContent).toStrictEqual(['export const a = 1 /* */'])
})

test('sourcemap with multiple sources', async () => {
const code = readFixture('bundle.js')
const map = readFixture('bundle.js.map')

const result = await ssrTransform(code, JSON.parse(map), '', code)
assert(result?.map)

const { sources } = result.map as SourceMap
expect(sources).toContain('./first.ts')
expect(sources).toContain('./second.ts')

function readFixture(filename: string) {
const url = new URL(
`./fixtures/bundled-with-sourcemaps/${filename}`,
import.meta.url,
)

return readFileSync(fileURLToPath(url), 'utf8')
}
})

test('overwrite bindings', async () => {
expect(
await ssrTransformSimpleCode(
Expand Down
13 changes: 8 additions & 5 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,6 @@ export function combineSourcemaps(
}
return newSourcemaps
})
const escapedFilename = escapeToLinuxLikePath(filename)

// We don't declare type here so we can convert/fake/map as RawSourceMap
let map //: SourceMap
Expand All @@ -863,11 +862,15 @@ export function combineSourcemaps(
map = remapping(sourcemapList, () => null)
} else {
map = remapping(sourcemapList[0], function loader(sourcefile) {
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
return sourcemapList[mapIndex++]
} else {
return null
const mapForSources = sourcemapList
.slice(mapIndex)
.find((s) => s.sources.includes(sourcefile))

if (mapForSources) {
mapIndex++
return mapForSources
}
return null
})
}
if (!map.file) {
Expand Down

0 comments on commit f321fa8

Please sign in to comment.