Skip to content

Commit

Permalink
fix: add source and sourcesContent to transformed SSR modules (#2285)
Browse files Browse the repository at this point in the history
close #2284
  • Loading branch information
Rich-Harris authored Feb 26, 2021
1 parent e9f4c29 commit 72be67b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ export async function transformRequest(
}

if (ssr) {
return (mod.ssrTransformResult = await ssrTransform(code, map as SourceMap))
return (mod.ssrTransformResult = await ssrTransform(
code,
map as SourceMap,
url
))
} else {
return (mod.transformResult = {
code,
Expand Down
42 changes: 31 additions & 11 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { ssrTransform } from '../ssrTransform'

test('default import', async () => {
expect(
(await ssrTransform(`import foo from 'vue';console.log(foo.bar)`, null))
.code
(
await ssrTransform(
`import foo from 'vue';console.log(foo.bar)`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = __vite_ssr_import__(\\"vue\\")
console.log(__vite_ssr_import_0__.default.bar)"
Expand All @@ -16,6 +21,7 @@ test('named import', async () => {
(
await ssrTransform(
`import { ref } from 'vue';function foo() { return ref(0) }`,
null,
null
)
).code
Expand All @@ -30,6 +36,7 @@ test('namespace import', async () => {
(
await ssrTransform(
`import * as vue from 'vue';function foo() { return vue.ref(0) }`,
null,
null
)
).code
Expand All @@ -40,23 +47,23 @@ test('namespace import', async () => {
})

test('export function decl', async () => {
expect((await ssrTransform(`export function foo() {}`, null)).code)
expect((await ssrTransform(`export function foo() {}`, null, null)).code)
.toMatchInlineSnapshot(`
"function foo() {}
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, get(){ return foo }})"
`)
})

test('export class decl', async () => {
expect((await ssrTransform(`export class foo {}`, null)).code)
expect((await ssrTransform(`export class foo {}`, null, null)).code)
.toMatchInlineSnapshot(`
"class foo {}
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, get(){ return foo }})"
`)
})

test('export var decl', async () => {
expect((await ssrTransform(`export const a = 1, b = 2`, null)).code)
expect((await ssrTransform(`export const a = 1, b = 2`, null, null)).code)
.toMatchInlineSnapshot(`
"const a = 1, b = 2
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, get(){ return a }})
Expand All @@ -66,7 +73,8 @@ test('export var decl', async () => {

test('export named', async () => {
expect(
(await ssrTransform(`const a = 1, b = 2; export { a, b as c }`, null)).code
(await ssrTransform(`const a = 1, b = 2; export { a, b as c }`, null, null))
.code
).toMatchInlineSnapshot(`
"const a = 1, b = 2;
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, get(){ return a }})
Expand All @@ -76,7 +84,8 @@ test('export named', async () => {

test('export named from', async () => {
expect(
(await ssrTransform(`export { ref, computed as c } from 'vue'`, null)).code
(await ssrTransform(`export { ref, computed as c } from 'vue'`, null, null))
.code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = __vite_ssr_import__(\\"vue\\")
Expand All @@ -90,6 +99,7 @@ test('named exports of imported binding', async () => {
(
await ssrTransform(
`import {createApp} from 'vue';export {createApp}`,
null,
null
)
).code
Expand All @@ -101,7 +111,7 @@ test('named exports of imported binding', async () => {
})

test('export * from', async () => {
expect((await ssrTransform(`export * from 'vue'`, null)).code)
expect((await ssrTransform(`export * from 'vue'`, null, null)).code)
.toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = __vite_ssr_import__(\\"vue\\")
Expand All @@ -111,19 +121,20 @@ test('export * from', async () => {

test('export default', async () => {
expect(
(await ssrTransform(`export default {}`, null)).code
(await ssrTransform(`export default {}`, null, null)).code
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
})

test('import.meta', async () => {
expect(
(await ssrTransform(`console.log(import.meta.url)`, null)).code
(await ssrTransform(`console.log(import.meta.url)`, null, null)).code
).toMatchInlineSnapshot(`"console.log(__vite_ssr_import_meta__.url)"`)
})

test('dynamic import', async () => {
expect(
(await ssrTransform(`export const i = () => import('./foo')`, null)).code
(await ssrTransform(`export const i = () => import('./foo')`, null, null))
.code
).toMatchInlineSnapshot(`
"const i = () => __vite_ssr_dynamic_import__('./foo')
Object.defineProperty(__vite_ssr_exports__, \\"i\\", { enumerable: true, get(){ return i }})"
Expand All @@ -135,6 +146,7 @@ test('do not rewrite method definition', async () => {
(
await ssrTransform(
`import { fn } from 'vue';class A { fn() { fn() } }`,
null,
null
)
).code
Expand All @@ -150,6 +162,7 @@ test('should declare variable for imported super class', async () => {
(
await ssrTransform(
`import { Foo } from './dep';` + `class A extends Foo {}`,
null,
null
)
).code
Expand All @@ -167,6 +180,7 @@ test('should declare variable for imported super class', async () => {
`import { Foo } from './dep';` +
`export default class A extends Foo {}\n` +
`export class B extends Foo {}`,
null,
null
)
).code
Expand All @@ -178,3 +192,9 @@ test('should declare variable for imported super class', async () => {
Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, get(){ return B }})"
`)
})

test('sourcemap source', async () => {
expect(
(await ssrTransform(`export const a = 1`, null, 'input.js')).map.sources
).toStrictEqual(['input.js'])
})
6 changes: 5 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const ssrImportMetaKey = `__vite_ssr_import_meta__`

export async function ssrTransform(
code: string,
inMap: SourceMap | null
inMap: SourceMap | null,
url: string
): Promise<TransformResult | null> {
const s = new MagicString(code)

Expand Down Expand Up @@ -183,6 +184,9 @@ export async function ssrTransform(
sources: inMap.sources,
sourcesContent: inMap.sourcesContent
}) as SourceMap
} else {
map.sources = [url]
map.sourcesContent = [code]
}

return {
Expand Down

0 comments on commit 72be67b

Please sign in to comment.