From 13a7ebf9f692e294b74c9c47d478a98a70797044 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Sat, 16 Oct 2021 17:57:31 +0800 Subject: [PATCH] fix(isBuiltIn): consider the case of deep import --- packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts | 7 ++++++- packages/playground/ssr-vue/src/entry-server.js | 10 +++++++++- packages/playground/ssr-vue/src/message | 1 + packages/vite/src/node/utils.ts | 2 ++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 packages/playground/ssr-vue/src/message diff --git a/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts b/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts index f0ab340bc0ae4b..2cba97340b215c 100644 --- a/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts +++ b/packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts @@ -146,4 +146,9 @@ test('client navigation', async () => { test('import.meta.url', async () => { await page.goto(url) expect(await page.textContent('.protocol')).toEqual('file:') -}) \ No newline at end of file +}) + +test('deep import built-in module', async () => { + await page.goto(url) + expect(await page.textContent('.file-message')).toMatch('fs/promises') +}) diff --git a/packages/playground/ssr-vue/src/entry-server.js b/packages/playground/ssr-vue/src/entry-server.js index b1a5b6770d7fe2..fcf5dd6880b74b 100644 --- a/packages/playground/ssr-vue/src/entry-server.js +++ b/packages/playground/ssr-vue/src/entry-server.js @@ -13,7 +13,15 @@ export async function render(url, manifest) { // itself on ctx.modules. After the render, ctx.modules would contain all the // components that have been instantiated during this render call. const ctx = {} - const html = await renderToString(app, ctx) + let html = await renderToString(app, ctx) + + // for testing. Use deep import built-in module. PR #5248 + const fs = + process.versions.node.split('.')[0] >= '14' + ? await import('fs/promises') + : (await import('fs')).promises + const msg = await fs.readFile(new URL('./message', import.meta.url), 'utf-8') + html += `

msg read via deep import built-in module: ${msg}

` // the SSR manifest generated by Vite contains module -> chunk/asset mapping // which we can then use to determine what files need to be preloaded for this diff --git a/packages/playground/ssr-vue/src/message b/packages/playground/ssr-vue/src/message new file mode 100644 index 00000000000000..b0e64506dadd78 --- /dev/null +++ b/packages/playground/ssr-vue/src/message @@ -0,0 +1 @@ +"fs/promises" diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index b23c774058572b..e1379e01000201 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -38,6 +38,8 @@ export const normalizeId = (id: string): string => id.replace(/(\s*>\s*)/g, ' > ') export function isBuiltin(id: string): boolean { + const deepMatch = id.match(deepImportRE) + id = deepMatch ? deepMatch[1] || deepMatch[2] : id return builtins.includes(id) }