Skip to content

Commit

Permalink
feat(ssr): exports dynamicDeps for ssrTransform, close vitejs#4898 (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored and aleclarson committed Nov 8, 2021
1 parent 27a98c4 commit 9e46327
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface TransformResult {
map: SourceMap | null
etag?: string
deps?: string[]
dynamicDeps?: string[]
}

export interface TransformOptions {
Expand Down
44 changes: 22 additions & 22 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,43 +149,43 @@ test('import.meta', async () => {
})

test('dynamic import', async () => {
expect(
(await ssrTransform(`export const i = () => import('./foo')`, null, null))
.code
).toMatchInlineSnapshot(`
const result = await ssrTransform(
`export const i = () => import('./foo')`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const i = () => __vite_ssr_dynamic_import__('./foo')
Object.defineProperty(__vite_ssr_exports__, \\"i\\", { enumerable: true, configurable: true, get(){ return i }});"
`)
expect(result.deps).toEqual([])
expect(result.dynamicDeps).toEqual(['./foo'])
})

test('do not rewrite method definition', async () => {
expect(
(
await ssrTransform(
`import { fn } from 'vue';class A { fn() { fn() } }`,
null,
null
)
).code
).toMatchInlineSnapshot(`
const result = await ssrTransform(
`import { fn } from 'vue';class A { fn() { fn() } }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
class A { fn() { __vite_ssr_import_0__.fn() } }"
`)
expect(result.deps).toEqual(['vue'])
})

test('do not rewrite catch clause', async () => {
expect(
(
await ssrTransform(
`import {error} from './dependency';try {} catch(error) {}`,
null,
null
)
).code
).toMatchInlineSnapshot(`
const result = await ssrTransform(
`import {error} from './dependency';try {} catch(error) {}`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
try {} catch(error) {}"
`)
expect(result.deps).toEqual(['./dependency'])
})

// #2221
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function ssrTransform(

let uid = 0
const deps = new Set<string>()
const dynamicDeps = new Set<string>()
const idToImportMap = new Map<string, string>()
const declaredConst = new Set<string>()

Expand Down Expand Up @@ -200,6 +201,9 @@ export async function ssrTransform(
},
onDynamicImport(node) {
s.overwrite(node.start, node.start + 6, ssrDynamicImportKey)
if (node.type === 'ImportExpression' && node.source.type === 'Literal') {
dynamicDeps.add(node.source.value as string)
}
}
})

Expand All @@ -221,7 +225,8 @@ export async function ssrTransform(
return {
code: s.toString(),
map,
deps: [...deps]
deps: [...deps],
dynamicDeps: [...dynamicDeps]
}
}

Expand Down

0 comments on commit 9e46327

Please sign in to comment.