Skip to content

Commit aff4544

Browse files
authoredJul 10, 2022
fix(ssr): sourcemap content (fixes #8657) (#8997)
1 parent ce790c4 commit aff4544

File tree

4 files changed

+162
-237
lines changed

4 files changed

+162
-237
lines changed
 

‎packages/vite/src/node/server/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ export async function createServer(
320320
ws,
321321
moduleGraph,
322322
ssrTransform(code: string, inMap: SourceMap | null, url: string) {
323-
return ssrTransform(code, inMap, url, {
323+
return ssrTransform(code, inMap, url, code, {
324324
json: { stringify: server.config.json?.stringify }
325325
})
326326
},

‎packages/vite/src/node/server/transformRequest.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ async function loadAndTransform(
235235
inMap: map,
236236
ssr
237237
})
238+
const originalCode = code
238239
if (
239240
transformResult == null ||
240241
(isObject(transformResult) && transformResult.code == null)
@@ -258,7 +259,7 @@ async function loadAndTransform(
258259
}
259260

260261
const result = ssr
261-
? await ssrTransform(code, map as SourceMap, url, {
262+
? await ssrTransform(code, map as SourceMap, url, originalCode, {
262263
json: { stringify: !!server.config.json?.stringify }
263264
})
264265
: ({

‎packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

+140-224
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import { transformWithEsbuild } from '../../plugins/esbuild'
33
import { traverseHtml } from '../../plugins/html'
44
import { ssrTransform } from '../ssrTransform'
55

6+
const ssrTransformSimple = async (code: string, url = '') =>
7+
ssrTransform(code, null, url, code)
8+
const ssrTransformSimpleCode = async (code: string, url?: string) =>
9+
(await ssrTransformSimple(code, url))?.code
10+
611
test('default import', async () => {
712
expect(
8-
(
9-
await ssrTransform(
10-
`import foo from 'vue';console.log(foo.bar)`,
11-
null,
12-
null
13-
)
14-
).code
13+
await ssrTransformSimpleCode(`import foo from 'vue';console.log(foo.bar)`)
1514
).toMatchInlineSnapshot(`
1615
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
1716
console.log(__vite_ssr_import_0__.default.bar)"
@@ -20,13 +19,9 @@ test('default import', async () => {
2019

2120
test('named import', async () => {
2221
expect(
23-
(
24-
await ssrTransform(
25-
`import { ref } from 'vue';function foo() { return ref(0) }`,
26-
null,
27-
null
28-
)
29-
).code
22+
await ssrTransformSimpleCode(
23+
`import { ref } from 'vue';function foo() { return ref(0) }`
24+
)
3025
).toMatchInlineSnapshot(`
3126
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
3227
function foo() { return __vite_ssr_import_0__.ref(0) }"
@@ -35,48 +30,43 @@ test('named import', async () => {
3530

3631
test('namespace import', async () => {
3732
expect(
38-
(
39-
await ssrTransform(
40-
`import * as vue from 'vue';function foo() { return vue.ref(0) }`,
41-
null,
42-
null
43-
)
44-
).code
33+
await ssrTransformSimpleCode(
34+
`import * as vue from 'vue';function foo() { return vue.ref(0) }`
35+
)
4536
).toMatchInlineSnapshot(`
4637
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
4738
function foo() { return __vite_ssr_import_0__.ref(0) }"
4839
`)
4940
})
5041

5142
test('export function declaration', async () => {
52-
expect((await ssrTransform(`export function foo() {}`, null, null)).code)
43+
expect(await ssrTransformSimpleCode(`export function foo() {}`))
5344
.toMatchInlineSnapshot(`
5445
"function foo() {}
5546
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return foo }});"
5647
`)
5748
})
5849

5950
test('export class declaration', async () => {
60-
expect((await ssrTransform(`export class foo {}`, null, null)).code)
51+
expect(await ssrTransformSimpleCode(`export class foo {}`))
6152
.toMatchInlineSnapshot(`
6253
"class foo {}
6354
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return foo }});"
6455
`)
6556
})
6657

6758
test('export var declaration', async () => {
68-
expect((await ssrTransform(`export const a = 1, b = 2`, null, null)).code)
59+
expect(await ssrTransformSimpleCode(`export const a = 1, b = 2`))
6960
.toMatchInlineSnapshot(`
70-
"const a = 1, b = 2
71-
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
72-
Object.defineProperty(__vite_ssr_exports__, \\"b\\", { enumerable: true, configurable: true, get(){ return b }});"
73-
`)
61+
"const a = 1, b = 2
62+
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
63+
Object.defineProperty(__vite_ssr_exports__, \\"b\\", { enumerable: true, configurable: true, get(){ return b }});"
64+
`)
7465
})
7566

7667
test('export named', async () => {
7768
expect(
78-
(await ssrTransform(`const a = 1, b = 2; export { a, b as c }`, null, null))
79-
.code
69+
await ssrTransformSimpleCode(`const a = 1, b = 2; export { a, b as c }`)
8070
).toMatchInlineSnapshot(`
8171
"const a = 1, b = 2;
8272
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
@@ -86,8 +76,7 @@ test('export named', async () => {
8676

8777
test('export named from', async () => {
8878
expect(
89-
(await ssrTransform(`export { ref, computed as c } from 'vue'`, null, null))
90-
.code
79+
await ssrTransformSimpleCode(`export { ref, computed as c } from 'vue'`)
9180
).toMatchInlineSnapshot(`
9281
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
9382
@@ -98,13 +87,9 @@ test('export named from', async () => {
9887

9988
test('named exports of imported binding', async () => {
10089
expect(
101-
(
102-
await ssrTransform(
103-
`import {createApp} from 'vue';export {createApp}`,
104-
null,
105-
null
106-
)
107-
).code
90+
await ssrTransformSimpleCode(
91+
`import {createApp} from 'vue';export {createApp}`
92+
)
10893
).toMatchInlineSnapshot(`
10994
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
11095
@@ -114,13 +99,9 @@ test('named exports of imported binding', async () => {
11499

115100
test('export * from', async () => {
116101
expect(
117-
(
118-
await ssrTransform(
119-
`export * from 'vue'\n` + `export * from 'react'`,
120-
null,
121-
null
122-
)
123-
).code
102+
await ssrTransformSimpleCode(
103+
`export * from 'vue'\n` + `export * from 'react'`
104+
)
124105
).toMatchInlineSnapshot(`
125106
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
126107
__vite_ssr_exportAll__(__vite_ssr_import_0__);
@@ -130,7 +111,7 @@ test('export * from', async () => {
130111
})
131112

132113
test('export * as from', async () => {
133-
expect((await ssrTransform(`export * as foo from 'vue'`, null, null)).code)
114+
expect(await ssrTransformSimpleCode(`export * as foo from 'vue'`))
134115
.toMatchInlineSnapshot(`
135116
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
136117
@@ -140,148 +121,126 @@ test('export * as from', async () => {
140121

141122
test('export default', async () => {
142123
expect(
143-
(await ssrTransform(`export default {}`, null, null)).code
124+
await ssrTransformSimpleCode(`export default {}`)
144125
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
145126
})
146127

147128
test('import.meta', async () => {
148129
expect(
149-
(await ssrTransform(`console.log(import.meta.url)`, null, null)).code
130+
await ssrTransformSimpleCode(`console.log(import.meta.url)`)
150131
).toMatchInlineSnapshot(`"console.log(__vite_ssr_import_meta__.url)"`)
151132
})
152133

153134
test('dynamic import', async () => {
154-
const result = await ssrTransform(
155-
`export const i = () => import('./foo')`,
156-
null,
157-
null
135+
const result = await ssrTransformSimple(
136+
`export const i = () => import('./foo')`
158137
)
159-
expect(result.code).toMatchInlineSnapshot(`
138+
expect(result?.code).toMatchInlineSnapshot(`
160139
"const i = () => __vite_ssr_dynamic_import__('./foo')
161140
Object.defineProperty(__vite_ssr_exports__, \\"i\\", { enumerable: true, configurable: true, get(){ return i }});"
162141
`)
163-
expect(result.deps).toEqual([])
164-
expect(result.dynamicDeps).toEqual(['./foo'])
142+
expect(result?.deps).toEqual([])
143+
expect(result?.dynamicDeps).toEqual(['./foo'])
165144
})
166145

167146
test('do not rewrite method definition', async () => {
168-
const result = await ssrTransform(
169-
`import { fn } from 'vue';class A { fn() { fn() } }`,
170-
null,
171-
null
147+
const result = await ssrTransformSimple(
148+
`import { fn } from 'vue';class A { fn() { fn() } }`
172149
)
173-
expect(result.code).toMatchInlineSnapshot(`
150+
expect(result?.code).toMatchInlineSnapshot(`
174151
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
175152
class A { fn() { __vite_ssr_import_0__.fn() } }"
176153
`)
177-
expect(result.deps).toEqual(['vue'])
154+
expect(result?.deps).toEqual(['vue'])
178155
})
179156

180157
test('do not rewrite when variable is in scope', async () => {
181-
const result = await ssrTransform(
182-
`import { fn } from 'vue';function A(){ const fn = () => {}; return { fn }; }`,
183-
null,
184-
null
158+
const result = await ssrTransformSimple(
159+
`import { fn } from 'vue';function A(){ const fn = () => {}; return { fn }; }`
185160
)
186-
expect(result.code).toMatchInlineSnapshot(`
161+
expect(result?.code).toMatchInlineSnapshot(`
187162
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
188163
function A(){ const fn = () => {}; return { fn }; }"
189164
`)
190-
expect(result.deps).toEqual(['vue'])
165+
expect(result?.deps).toEqual(['vue'])
191166
})
192167

193168
// #5472
194169
test('do not rewrite when variable is in scope with object destructuring', async () => {
195-
const result = await ssrTransform(
196-
`import { fn } from 'vue';function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }`,
197-
null,
198-
null
170+
const result = await ssrTransformSimple(
171+
`import { fn } from 'vue';function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }`
199172
)
200-
expect(result.code).toMatchInlineSnapshot(`
173+
expect(result?.code).toMatchInlineSnapshot(`
201174
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
202175
function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }"
203176
`)
204-
expect(result.deps).toEqual(['vue'])
177+
expect(result?.deps).toEqual(['vue'])
205178
})
206179

207180
// #5472
208181
test('do not rewrite when variable is in scope with array destructuring', async () => {
209-
const result = await ssrTransform(
210-
`import { fn } from 'vue';function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }`,
211-
null,
212-
null
182+
const result = await ssrTransformSimple(
183+
`import { fn } from 'vue';function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }`
213184
)
214-
expect(result.code).toMatchInlineSnapshot(`
185+
expect(result?.code).toMatchInlineSnapshot(`
215186
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
216187
function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }"
217188
`)
218-
expect(result.deps).toEqual(['vue'])
189+
expect(result?.deps).toEqual(['vue'])
219190
})
220191

221192
// #5727
222193
test('rewrite variable in string interpolation in function nested arguments', async () => {
223-
const result = await ssrTransform(
224-
`import { fn } from 'vue';function A({foo = \`test\${fn}\`} = {}){ return {}; }`,
225-
null,
226-
null
194+
const result = await ssrTransformSimple(
195+
`import { fn } from 'vue';function A({foo = \`test\${fn}\`} = {}){ return {}; }`
227196
)
228-
expect(result.code).toMatchInlineSnapshot(`
197+
expect(result?.code).toMatchInlineSnapshot(`
229198
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
230199
function A({foo = \`test\${__vite_ssr_import_0__.fn}\`} = {}){ return {}; }"
231200
`)
232-
expect(result.deps).toEqual(['vue'])
201+
expect(result?.deps).toEqual(['vue'])
233202
})
234203

235204
// #6520
236205
test('rewrite variables in default value of destructuring params', async () => {
237-
const result = await ssrTransform(
238-
`import { fn } from 'vue';function A({foo = fn}){ return {}; }`,
239-
null,
240-
null
206+
const result = await ssrTransformSimple(
207+
`import { fn } from 'vue';function A({foo = fn}){ return {}; }`
241208
)
242-
expect(result.code).toMatchInlineSnapshot(`
209+
expect(result?.code).toMatchInlineSnapshot(`
243210
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
244211
function A({foo = __vite_ssr_import_0__.fn}){ return {}; }"
245212
`)
246-
expect(result.deps).toEqual(['vue'])
213+
expect(result?.deps).toEqual(['vue'])
247214
})
248215

249216
test('do not rewrite when function declaration is in scope', async () => {
250-
const result = await ssrTransform(
251-
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`,
252-
null,
253-
null
217+
const result = await ssrTransformSimple(
218+
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`
254219
)
255-
expect(result.code).toMatchInlineSnapshot(`
220+
expect(result?.code).toMatchInlineSnapshot(`
256221
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
257222
function A(){ function fn() {}; return { fn }; }"
258223
`)
259-
expect(result.deps).toEqual(['vue'])
224+
expect(result?.deps).toEqual(['vue'])
260225
})
261226

262227
test('do not rewrite catch clause', async () => {
263-
const result = await ssrTransform(
264-
`import {error} from './dependency';try {} catch(error) {}`,
265-
null,
266-
null
228+
const result = await ssrTransformSimple(
229+
`import {error} from './dependency';try {} catch(error) {}`
267230
)
268-
expect(result.code).toMatchInlineSnapshot(`
231+
expect(result?.code).toMatchInlineSnapshot(`
269232
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
270233
try {} catch(error) {}"
271234
`)
272-
expect(result.deps).toEqual(['./dependency'])
235+
expect(result?.deps).toEqual(['./dependency'])
273236
})
274237

275238
// #2221
276239
test('should declare variable for imported super class', async () => {
277240
expect(
278-
(
279-
await ssrTransform(
280-
`import { Foo } from './dependency';` + `class A extends Foo {}`,
281-
null,
282-
null
283-
)
284-
).code
241+
await ssrTransformSimpleCode(
242+
`import { Foo } from './dependency';` + `class A extends Foo {}`
243+
)
285244
).toMatchInlineSnapshot(`
286245
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
287246
const Foo = __vite_ssr_import_0__.Foo;
@@ -291,15 +250,11 @@ test('should declare variable for imported super class', async () => {
291250
// exported classes: should prepend the declaration at root level, before the
292251
// first class that uses the binding
293252
expect(
294-
(
295-
await ssrTransform(
296-
`import { Foo } from './dependency';` +
297-
`export default class A extends Foo {}\n` +
298-
`export class B extends Foo {}`,
299-
null,
300-
null
301-
)
302-
).code
253+
await ssrTransformSimpleCode(
254+
`import { Foo } from './dependency';` +
255+
`export default class A extends Foo {}\n` +
256+
`export class B extends Foo {}`
257+
)
303258
).toMatchInlineSnapshot(`
304259
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
305260
const Foo = __vite_ssr_import_0__.Foo;
@@ -313,42 +268,32 @@ test('should declare variable for imported super class', async () => {
313268
// #4049
314269
test('should handle default export variants', async () => {
315270
// default anonymous functions
316-
expect(
317-
(await ssrTransform(`export default function() {}\n`, null, null)).code
318-
).toMatchInlineSnapshot(`
271+
expect(await ssrTransformSimpleCode(`export default function() {}\n`))
272+
.toMatchInlineSnapshot(`
319273
"__vite_ssr_exports__.default = function() {}
320274
"
321275
`)
322276
// default anonymous class
323-
expect((await ssrTransform(`export default class {}\n`, null, null)).code)
277+
expect(await ssrTransformSimpleCode(`export default class {}\n`))
324278
.toMatchInlineSnapshot(`
325279
"__vite_ssr_exports__.default = class {}
326280
"
327281
`)
328282
// default named functions
329283
expect(
330-
(
331-
await ssrTransform(
332-
`export default function foo() {}\n` +
333-
`foo.prototype = Object.prototype;`,
334-
null,
335-
null
336-
)
337-
).code
284+
await ssrTransformSimpleCode(
285+
`export default function foo() {}\n` + `foo.prototype = Object.prototype;`
286+
)
338287
).toMatchInlineSnapshot(`
339288
"function foo() {}
340289
foo.prototype = Object.prototype;
341290
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, configurable: true, value: foo });"
342291
`)
343292
// default named classes
344293
expect(
345-
(
346-
await ssrTransform(
347-
`export default class A {}\n` + `export class B extends A {}`,
348-
null,
349-
null
350-
)
351-
).code
294+
await ssrTransformSimpleCode(
295+
`export default class A {}\n` + `export class B extends A {}`
296+
)
352297
).toMatchInlineSnapshot(`
353298
"class A {}
354299
class B extends A {}
@@ -358,27 +303,30 @@ test('should handle default export variants', async () => {
358303
})
359304

360305
test('sourcemap source', async () => {
361-
expect(
362-
(await ssrTransform(`export const a = 1`, null, 'input.js')).map.sources
363-
).toStrictEqual(['input.js'])
306+
const map = (
307+
await ssrTransform(
308+
`export const a = 1`,
309+
null,
310+
'input.js',
311+
'export const a = 1 /* */'
312+
)
313+
)?.map
314+
expect(map?.sources).toStrictEqual(['input.js'])
315+
expect(map?.sourcesContent).toStrictEqual(['export const a = 1 /* */'])
364316
})
365317

366318
test('overwrite bindings', async () => {
367319
expect(
368-
(
369-
await ssrTransform(
370-
`import { inject } from 'vue';` +
371-
`const a = { inject }\n` +
372-
`const b = { test: inject }\n` +
373-
`function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
374-
`const d = inject\n` +
375-
`function f() { console.log(inject) }\n` +
376-
`function e() { const { inject } = { inject: true } }\n` +
377-
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
378-
null,
379-
null
380-
)
381-
).code
320+
await ssrTransformSimpleCode(
321+
`import { inject } from 'vue';` +
322+
`const a = { inject }\n` +
323+
`const b = { test: inject }\n` +
324+
`function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
325+
`const d = inject\n` +
326+
`function f() { console.log(inject) }\n` +
327+
`function e() { const { inject } = { inject: true } }\n` +
328+
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`
329+
)
382330
).toMatchInlineSnapshot(`
383331
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
384332
const a = { inject: __vite_ssr_import_0__.inject }
@@ -394,24 +342,20 @@ test('overwrite bindings', async () => {
394342

395343
test('Empty array pattern', async () => {
396344
expect(
397-
(await ssrTransform(`const [, LHS, RHS] = inMatch;`, null, null)).code
345+
await ssrTransformSimpleCode(`const [, LHS, RHS] = inMatch;`)
398346
).toMatchInlineSnapshot(`"const [, LHS, RHS] = inMatch;"`)
399347
})
400348

401349
test('function argument destructure', async () => {
402350
expect(
403-
(
404-
await ssrTransform(
405-
`
351+
await ssrTransformSimpleCode(
352+
`
406353
import { foo, bar } from 'foo'
407354
const a = ({ _ = foo() }) => {}
408355
function b({ _ = bar() }) {}
409356
function c({ _ = bar() + foo() }) {}
410-
`,
411-
null,
412-
null
413-
)
414-
).code
357+
`
358+
)
415359
).toMatchInlineSnapshot(`
416360
"
417361
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
@@ -425,19 +369,15 @@ function c({ _ = bar() + foo() }) {}
425369

426370
test('object destructure alias', async () => {
427371
expect(
428-
(
429-
await ssrTransform(
430-
`
372+
await ssrTransformSimpleCode(
373+
`
431374
import { n } from 'foo'
432375
const a = () => {
433376
const { type: n = 'bar' } = {}
434377
console.log(n)
435378
}
436-
`,
437-
null,
438-
null
439-
)
440-
).code
379+
`
380+
)
441381
).toMatchInlineSnapshot(`
442382
"
443383
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
@@ -452,9 +392,8 @@ const a = () => {
452392

453393
test('nested object destructure alias', async () => {
454394
expect(
455-
(
456-
await ssrTransform(
457-
`
395+
await ssrTransformSimpleCode(
396+
`
458397
import { remove, add, get, set, rest, objRest } from 'vue'
459398
460399
function a() {
@@ -479,11 +418,8 @@ get()
479418
set()
480419
rest()
481420
objRest()
482-
`,
483-
null,
484-
null
485-
)
486-
).code
421+
`
422+
)
487423
).toMatchInlineSnapshot(`
488424
"
489425
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
@@ -517,20 +453,16 @@ objRest()
517453

518454
test('class props', async () => {
519455
expect(
520-
(
521-
await ssrTransform(
522-
`
456+
await ssrTransformSimpleCode(
457+
`
523458
import { remove, add } from 'vue'
524459
525460
class A {
526461
remove = 1
527462
add = null
528463
}
529-
`,
530-
null,
531-
null
532-
)
533-
).code
464+
`
465+
)
534466
).toMatchInlineSnapshot(`
535467
"
536468
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
@@ -548,9 +480,8 @@ class A {
548480

549481
test('class methods', async () => {
550482
expect(
551-
(
552-
await ssrTransform(
553-
`
483+
await ssrTransformSimpleCode(
484+
`
554485
import foo from 'foo'
555486
556487
const bar = 'bar'
@@ -562,11 +493,8 @@ class A {
562493
#foo() {}
563494
bar(foo) {}
564495
}
565-
`,
566-
null,
567-
null
568-
)
569-
).code
496+
`
497+
)
570498
).toMatchInlineSnapshot(`
571499
"
572500
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"foo\\");
@@ -587,9 +515,8 @@ class A {
587515

588516
test('declare scope', async () => {
589517
expect(
590-
(
591-
await ssrTransform(
592-
`
518+
await ssrTransformSimpleCode(
519+
`
593520
import { aaa, bbb, ccc, ddd } from 'vue'
594521
595522
function foobar() {
@@ -612,11 +539,8 @@ function foobar() {
612539
613540
aaa()
614541
bbb()
615-
`,
616-
null,
617-
null
618-
)
619-
).code
542+
`
543+
)
620544
).toMatchInlineSnapshot(`
621545
"
622546
const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
@@ -661,7 +585,7 @@ test('jsx', async () => {
661585
`
662586
const id = '/foo.jsx'
663587
const result = await transformWithEsbuild(code, id)
664-
expect((await ssrTransform(result.code, null, '/foo.jsx')).code)
588+
expect(await ssrTransformSimpleCode(result.code, '/foo.jsx'))
665589
.toMatchInlineSnapshot(`
666590
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"react\\");
667591
@@ -676,17 +600,13 @@ test('jsx', async () => {
676600

677601
test('continuous exports', async () => {
678602
expect(
679-
(
680-
await ssrTransform(
681-
`
603+
await ssrTransformSimpleCode(
604+
`
682605
export function fn1() {
683606
}export function fn2() {
684607
}
685-
`,
686-
null,
687-
null
688-
)
689-
).code
608+
`
609+
)
690610
).toMatchInlineSnapshot(`
691611
"
692612
function fn1() {
@@ -710,28 +630,24 @@ export default (function getRandom() {
710630
});
711631
`.trim()
712632

713-
expect((await ssrTransform(code, null, null)).code).toMatchInlineSnapshot(`
633+
expect(await ssrTransformSimpleCode(code)).toMatchInlineSnapshot(`
714634
"__vite_ssr_exports__.default = (function getRandom() {
715635
return Math.random();
716636
});"
717637
`)
718638

719639
expect(
720-
(await ssrTransform(`export default (class A {});`, null, null)).code
640+
await ssrTransformSimpleCode(`export default (class A {});`)
721641
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = (class A {});"`)
722642
})
723643

724644
// #8002
725645
test('with hashbang', async () => {
726646
expect(
727-
(
728-
await ssrTransform(
729-
`#!/usr/bin/env node
730-
console.log("it can parse the hashbang")`,
731-
null,
732-
null
733-
)
734-
).code
647+
await ssrTransformSimpleCode(
648+
`#!/usr/bin/env node
649+
console.log("it can parse the hashbang")`
650+
)
735651
).toMatchInlineSnapshot(`
736652
"#!/usr/bin/env node
737653
console.log(\\"it can parse the hashbang\\")"

‎packages/vite/src/node/ssr/ssrTransform.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ export async function ssrTransform(
3838
code: string,
3939
inMap: SourceMap | null,
4040
url: string,
41+
originalCode: string,
4142
options?: TransformOptions
4243
): Promise<TransformResult | null> {
4344
if (options?.json?.stringify && isJSONRequest(url)) {
4445
return ssrTransformJSON(code, inMap)
4546
}
46-
return ssrTransformScript(code, inMap, url)
47+
return ssrTransformScript(code, inMap, url, originalCode)
4748
}
4849

4950
async function ssrTransformJSON(
@@ -61,7 +62,8 @@ async function ssrTransformJSON(
6162
async function ssrTransformScript(
6263
code: string,
6364
inMap: SourceMap | null,
64-
url: string
65+
url: string,
66+
originalCode: string
6567
): Promise<TransformResult | null> {
6668
const s = new MagicString(code)
6769

@@ -265,17 +267,23 @@ async function ssrTransformScript(
265267

266268
let map = s.generateMap({ hires: true })
267269
if (inMap && inMap.mappings && inMap.sources.length > 0) {
268-
map = combineSourcemaps(url, [
269-
{
270-
...map,
271-
sources: inMap.sources,
272-
sourcesContent: inMap.sourcesContent
273-
} as RawSourceMap,
274-
inMap as RawSourceMap
275-
]) as SourceMap
270+
map = combineSourcemaps(
271+
url,
272+
[
273+
{
274+
...map,
275+
sources: inMap.sources,
276+
sourcesContent: inMap.sourcesContent
277+
} as RawSourceMap,
278+
inMap as RawSourceMap
279+
],
280+
false
281+
) as SourceMap
276282
} else {
277283
map.sources = [url]
278-
map.sourcesContent = [code]
284+
// needs to use originalCode instead of code
285+
// because code might be already transformed even if map is null
286+
map.sourcesContent = [originalCode]
279287
}
280288

281289
return {

0 commit comments

Comments
 (0)
Please sign in to comment.