diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js b/packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js new file mode 100644 index 00000000000000..1af646b6af7d1a --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js @@ -0,0 +1,30 @@ +function covered$1() { + return 'First' +} + +function uncovered$1() { + return 'Uncovered' +} + +var first = /*#__PURE__*/ Object.freeze({ + __proto__: null, + covered: covered$1, + uncovered: uncovered$1, +}) + +function covered() { + return 'Second' +} + +function uncovered() { + return 'Uncovered' +} + +var second = /*#__PURE__*/ Object.freeze({ + __proto__: null, + covered: covered, + uncovered: uncovered, +}) + +export { first, second } +//# sourceMappingURL=bundle.js.map diff --git a/packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js.map b/packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js.map new file mode 100644 index 00000000000000..13d34ae1da4fff --- /dev/null +++ b/packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js.map @@ -0,0 +1,17 @@ +{ + "version": 3, + "file": "bundle.js", + "sources": [ + "./first.ts", + "./second.ts" + ], + "sourcesContent": [ + "export function covered() {\n return \"First\";\n}\n\nexport function uncovered() {\n return \"Uncovered\";\n}\n", + "export function covered() {\n return \"Second\";\n}\n\nexport function uncovered() {\n return \"Uncovered\";\n}\n" + ], + "names": [ + "covered", + "uncovered" + ], + "mappings": "AAAO,SAASA,SAAO,GAAG;AAC1B,EAAE,OAAO,OAAO,CAAC;AACjB,CAAC;AACD;AACO,SAASC,WAAS,GAAG;AAC5B,EAAE,OAAO,WAAW,CAAC;AACrB;;;;;;;;ACNO,SAAS,OAAO,GAAG;AAC1B,EAAE,OAAO,QAAQ,CAAC;AAClB,CAAC;AACD;AACO,SAAS,SAAS,GAAG;AAC5B,EAAE,OAAO,WAAW,CAAC;AACrB;;;;;;;;;;" +} diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index 1c268835117deb..0bd938cdd46e0a 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -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' @@ -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( diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 1064c549bcf069..a3f000641bb6db 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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 @@ -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