Skip to content

Commit

Permalink
fix(ssrTransform): sourcemaps with multiple sources
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jul 14, 2024
1 parent 07bc489 commit 6fa6370
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 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
3 changes: 1 addition & 2 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,7 +862,7 @@ export function combineSourcemaps(
map = remapping(sourcemapList, () => null)
} else {
map = remapping(sourcemapList[0], function loader(sourcefile) {
if (sourcefile === escapedFilename && sourcemapList[mapIndex]) {
if (sourcemapList[mapIndex]) {
return sourcemapList[mapIndex++]
} else {
return null
Expand Down

0 comments on commit 6fa6370

Please sign in to comment.