Skip to content

Commit 450097f

Browse files
authored
fix: always add @sveltejs/kit to noExternal for ssr build (#9242)
fixes #9234
1 parent b720b66 commit 450097f

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

.changeset/gold-fireants-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: always add `@sveltejs/kit` to `noExternal` for ssr build

packages/kit/src/exports/vite/index.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,6 @@ function kit({ svelte_config }) {
218218

219219
const generated = path.posix.join(kit.outDir, 'generated');
220220

221-
// This ensures that esm-env is inlined into the server output with the
222-
// export conditions resolved correctly through Vite. This prevents adapters
223-
// that bundle later on from resolving the export conditions incorrectly
224-
// and for example include browser-only code in the server output
225-
// because they for example use esbuild.build with `platform: 'browser'`
226-
const noExternal = ['esm-env'];
227-
228-
// Vitest bypasses Vite when loading external modules, so we bundle
229-
// when it is detected to keep our virtual modules working.
230-
// See https://github.com/sveltejs/kit/pull/9172
231-
// and https://vitest.dev/config/#deps-registernodeloader
232-
if (process.env.TEST) {
233-
noExternal.push('@sveltejs/kit');
234-
}
235-
236221
// dev and preview config can be shared
237222
/** @type {import('vite').UserConfig} */
238223
const new_config = {
@@ -269,7 +254,23 @@ function kit({ svelte_config }) {
269254
]
270255
},
271256
ssr: {
272-
noExternal
257+
noExternal: [
258+
// This ensures that esm-env is inlined into the server output with the
259+
// export conditions resolved correctly through Vite. This prevents adapters
260+
// that bundle later on from resolving the export conditions incorrectly
261+
// and for example include browser-only code in the server output
262+
// because they for example use esbuild.build with `platform: 'browser'`
263+
'esm-env',
264+
// We need this for two reasons:
265+
// 1. Without this, `@sveltejs/kit` imports are kept as-is in the server output,
266+
// and that causes modules and therefore classes like `Redirect` to be imported twice
267+
// under different IDs, which breaks a bunch of stuff because of failing instanceof checks.
268+
// 2. Vitest bypasses Vite when loading external modules, so we bundle
269+
// when it is detected to keep our virtual modules working.
270+
// See https://github.com/sveltejs/kit/pull/9172
271+
// and https://vitest.dev/config/#deps-registernodeloader
272+
'@sveltejs/kit'
273+
]
273274
}
274275
};
275276

0 commit comments

Comments
 (0)