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

fix: bundle SvelteKit when using Vitest #9172

Merged
merged 8 commits into from
Feb 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-olives-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: bundle SvelteKit when using Vitest
44 changes: 24 additions & 20 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ 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 = {
Expand Down Expand Up @@ -252,6 +267,9 @@ function kit({ svelte_config }) {
'$app',
'$env'
]
},
ssr: {
noExternal
}
};

Expand All @@ -267,19 +285,6 @@ function kit({ svelte_config }) {
__SVELTEKIT_EMBEDDED__: kit.embedded ? 'true' : 'false'
};

new_config.ssr = {
noExternal: [
// TODO document why this is necessary
'@sveltejs/kit',
// 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 to resolve 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'
]
};

if (!secondary_build_started) {
manifest_data = (await sync.all(svelte_config, config_env.mode)).manifest_data;
}
Expand All @@ -290,13 +295,12 @@ function kit({ svelte_config }) {
__SVELTEKIT_EMBEDDED__: kit.embedded ? 'true' : 'false'
};

new_config.ssr = {
// Without this, Vite will treat `@sveltejs/kit` as noExternal if it's
// a linked dependency, and that causes modules to be imported twice
// under different IDs, which breaks a bunch of stuff
// https://github.com/vitejs/vite/pull/9296
external: ['@sveltejs/kit', 'cookie', 'set-cookie-parser']
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
};
// These Kit dependencies are packaged as CommonJS, which means they must always be externalized.
// Without this, the tests will still pass but `pnpm dev` will fail in projects that link `@sveltejs/kit`.
/** @type {NonNullable<import('vite').UserConfig['ssr']>} */ (new_config.ssr).external = [
'cookie',
'set-cookie-parser'
];
}

warn_overridden_config(config, new_config);
Expand Down