From 6fa637003927ecfdf7fadccc29dadf6dbdd3db03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sun, 14 Jul 2024 15:30:53 +0300 Subject: [PATCH 1/2] fix(ssrTransform): sourcemaps with multiple sources --- .../bundled-with-sourcemaps/bundle.js | 30 +++++++++++++++++++ .../bundled-with-sourcemaps/bundle.js.map | 17 +++++++++++ .../node/ssr/__tests__/ssrTransform.spec.ts | 29 ++++++++++++++++-- packages/vite/src/node/utils.ts | 3 +- 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js create mode 100644 packages/vite/src/node/ssr/__tests__/fixtures/bundled-with-sourcemaps/bundle.js.map 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 From 7a806b8f9f2d7d203d39c1744c2de092631b29a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 16 Jul 2024 15:16:35 +0300 Subject: [PATCH 2/2] fix: vite-plugin-vue source maps pass --- packages/vite/src/node/utils.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index a3f000641bb6db..79648f09105db2 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -862,11 +862,15 @@ export function combineSourcemaps( map = remapping(sourcemapList, () => null) } else { map = remapping(sourcemapList[0], function loader(sourcefile) { - if (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) {