From ea2770106efe759ad137246980cf4a591eb14780 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 22 Jul 2022 18:07:11 +0800 Subject: [PATCH] fix(ssr): no external symlink package (#9296) --- packages/vite/src/node/plugins/resolve.ts | 4 ++++ playground/ssr-deps/__tests__/ssr-deps.spec.ts | 5 +++++ playground/ssr-deps/linked-no-external/index.js | 5 +++++ playground/ssr-deps/linked-no-external/package.json | 6 ++++++ playground/ssr-deps/package.json | 3 ++- playground/ssr-deps/src/app.js | 4 ++++ pnpm-lock.yaml | 5 +++++ 7 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 playground/ssr-deps/linked-no-external/index.js create mode 100644 playground/ssr-deps/linked-no-external/package.json diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index af86ef76193c92..d82e1dc6bc63ac 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -656,6 +656,10 @@ export function tryNodeResolve( if (!externalize) { return resolved } + // dont external symlink packages + if (!resolved.id.includes('node_modules')) { + return + } const resolvedExt = path.extname(resolved.id) let resolvedId = id if (isDeepImport) { diff --git a/playground/ssr-deps/__tests__/ssr-deps.spec.ts b/playground/ssr-deps/__tests__/ssr-deps.spec.ts index 2d1066cade3374..8f4e097aec1c81 100644 --- a/playground/ssr-deps/__tests__/ssr-deps.spec.ts +++ b/playground/ssr-deps/__tests__/ssr-deps.spec.ts @@ -98,3 +98,8 @@ test('msg from external using external entry', async () => { 'Hello World!' ) }) + +test('msg from linked no external', async () => { + await page.goto(url) + expect(await page.textContent('.linked-no-external')).toMatch('Hello World!') +}) diff --git a/playground/ssr-deps/linked-no-external/index.js b/playground/ssr-deps/linked-no-external/index.js new file mode 100644 index 00000000000000..8322345e2aa5df --- /dev/null +++ b/playground/ssr-deps/linked-no-external/index.js @@ -0,0 +1,5 @@ +export const hello = function () { + // make sure linked package is not externalized so Vite features like + // import.meta.env works (or handling TS files) + return import.meta.env.DEV && 'Hello World!' +} diff --git a/playground/ssr-deps/linked-no-external/package.json b/playground/ssr-deps/linked-no-external/package.json new file mode 100644 index 00000000000000..40f16209737cb8 --- /dev/null +++ b/playground/ssr-deps/linked-no-external/package.json @@ -0,0 +1,6 @@ +{ + "name": "linked-no-external", + "private": true, + "type": "module", + "version": "0.0.0" +} diff --git a/playground/ssr-deps/package.json b/playground/ssr-deps/package.json index 9bf13cc84af69a..20f840d0f27133 100644 --- a/playground/ssr-deps/package.json +++ b/playground/ssr-deps/package.json @@ -26,7 +26,8 @@ "optimized-with-nested-external": "file:./optimized-with-nested-external", "optimized-cjs-with-nested-external": "file:./optimized-with-nested-external", "external-using-external-entry": "file:./external-using-external-entry", - "external-entry": "file:./external-entry" + "external-entry": "file:./external-entry", + "linked-no-external": "link:./linked-no-external" }, "devDependencies": { "cross-env": "^7.0.3", diff --git a/playground/ssr-deps/src/app.js b/playground/ssr-deps/src/app.js index 509c6de7ee4c36..987eaa3e1b43e9 100644 --- a/playground/ssr-deps/src/app.js +++ b/playground/ssr-deps/src/app.js @@ -11,6 +11,7 @@ import onlyObjectAssignedExports from 'only-object-assigned-exports' import requireAbsolute from 'require-absolute' import noExternalCjs from 'no-external-cjs' import importBuiltinCjs from 'import-builtin-cjs' +import { hello as linkedNoExternal } from 'linked-no-external' // This import will set a 'Hello World!" message in the nested-external non-entry dependency import 'non-optimized-with-nested-external' @@ -75,5 +76,8 @@ export async function render(url, rootDir) { const externalUsingExternalEntryMessage = externalUsingExternalEntry.hello() html += `\n

message from external-using-external-entry: ${externalUsingExternalEntryMessage}

` + const linkedNoExternalMessage = linkedNoExternal() + html += `\n

message from linked-no-external: ${linkedNoExternalMessage}

` + return html + '\n' } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5e568e34d60eb8..74c0cc0cb09e17 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -907,6 +907,7 @@ importers: external-using-external-entry: file:./external-using-external-entry forwarded-export: file:./forwarded-export import-builtin-cjs: file:./import-builtin-cjs + linked-no-external: link:./linked-no-external no-external-cjs: file:./no-external-cjs no-external-css: file:./no-external-css non-optimized-with-nested-external: file:./non-optimized-with-nested-external @@ -926,6 +927,7 @@ importers: external-using-external-entry: file:playground/ssr-deps/external-using-external-entry forwarded-export: file:playground/ssr-deps/forwarded-export import-builtin-cjs: file:playground/ssr-deps/import-builtin-cjs + linked-no-external: link:linked-no-external no-external-cjs: file:playground/ssr-deps/no-external-cjs no-external-css: file:playground/ssr-deps/no-external-css non-optimized-with-nested-external: file:playground/ssr-deps/non-optimized-with-nested-external @@ -965,6 +967,9 @@ importers: playground/ssr-deps/import-builtin-cjs: specifiers: {} + playground/ssr-deps/linked-no-external: + specifiers: {} + playground/ssr-deps/nested-external: specifiers: {}