diff --git a/.changeset/gold-fireants-talk.md b/.changeset/gold-fireants-talk.md new file mode 100644 index 000000000000..8a5f0b4269ed --- /dev/null +++ b/.changeset/gold-fireants-talk.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: always add `@sveltejs/kit` to `noExternal` for ssr build diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index ca0693a3a6a3..b7ef5324aa35 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -218,21 +218,6 @@ function kit({ svelte_config }) { const generated = path.posix.join(kit.outDir, 'generated'); - // This ensures that esm-env is inlined into the server output with the - // export conditions resolved correctly through Vite. This prevents adapters - // that bundle later on from resolving the export conditions incorrectly - // and for example include browser-only code in the server output - // because they for example use esbuild.build with `platform: 'browser'` - const noExternal = ['esm-env']; - - // Vitest bypasses Vite when loading external modules, so we bundle - // when it is detected to keep our virtual modules working. - // See https://github.com/sveltejs/kit/pull/9172 - // and https://vitest.dev/config/#deps-registernodeloader - if (process.env.TEST) { - noExternal.push('@sveltejs/kit'); - } - // dev and preview config can be shared /** @type {import('vite').UserConfig} */ const new_config = { @@ -269,7 +254,23 @@ function kit({ svelte_config }) { ] }, ssr: { - noExternal + noExternal: [ + // This ensures that esm-env is inlined into the server output with the + // export conditions resolved correctly through Vite. This prevents adapters + // that bundle later on from resolving the export conditions incorrectly + // and for example include browser-only code in the server output + // because they for example use esbuild.build with `platform: 'browser'` + 'esm-env', + // We need this for two reasons: + // 1. Without this, `@sveltejs/kit` imports are kept as-is in the server output, + // and that causes modules and therefore classes like `Redirect` to be imported twice + // under different IDs, which breaks a bunch of stuff because of failing instanceof checks. + // 2. Vitest bypasses Vite when loading external modules, so we bundle + // when it is detected to keep our virtual modules working. + // See https://github.com/sveltejs/kit/pull/9172 + // and https://vitest.dev/config/#deps-registernodeloader + '@sveltejs/kit' + ] } };