Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ssr): exports dynamicDeps for ssrTransform, close #4898 #4909

Merged
merged 1 commit into from Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vite/src/node/server/transformRequest.ts
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
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
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