Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssrTransform): sourcemaps with multiple sources #17677

Merged
Merged
Show file tree
Hide file tree
Changes from all 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

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]) {
Copy link
Contributor Author

@AriPerkkio AriPerkkio Jul 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure what this comparison was here to check. In my repro it's failing with:

{
  sourcefile: './first.ts',
  escapedFilename: '/linux/fixtures/src/pre-bundle/bundle.js'
}

{
  sourcefile: './second.ts',
  escapedFilename: '/linux/fixtures/src/pre-bundle/bundle.js'
}

And removing this line doesn't break any test cases. Addition of this line goes as far as 2021, https://github.com/vitejs/vite/pull/2441/files#diff-071a32aedd2ea59472ebb69fb456e818b103d1a332da632e12c2d54395938ad1R421

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That commit was based on source map for Svelte. @benmccann I see you reviewed the PR that merged this line here https://github.com/sveltejs/svelte/pull/5584/files#diff-8742d6c5cbad8aec49f195bc6d25b152ac0cd8180cb876c019f3b74736f9d62aR227. Maybe you have more context on why this was needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not much of a sourcemap expert. @milahu could know more

From my personal standpoint, I'd just suggest to make sure any changes don't break SvelteKit on the ecosystem CI as we have a small set of sourcemap tests there.

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