Skip to content

Commit

Permalink
fix: always bundle SvelteKit, always externalize CommonJS dependencie…
Browse files Browse the repository at this point in the history
…s during development (#9203)

* simplify

* add comments etc

* no need to noExternal kit itself

* update comment

* reinstate old comment
  • Loading branch information
Rich-Harris authored Feb 26, 2023
1 parent 447fa1c commit 942ad18
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,17 @@ function kit({ svelte_config }) {
'$app',
'$env'
]
},
ssr: {
// 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'`
noExternal: ['esm-env']
}
};

// Vitest will only call resolveId for packages that are being bundled
// Without this it will not be able to load our virtual modules
// See https://vitest.dev/config/#deps-registernodeloader
const noExternal = process.env.TEST ? ['@sveltejs/kit'] : [];

if (is_build) {
if (!new_config.build) new_config.build = {};
new_config.build.ssr = !secondary_build_started;
Expand All @@ -272,13 +275,6 @@ function kit({ svelte_config }) {
__SVELTEKIT_EMBEDDED__: kit.embedded ? 'true' : 'false'
};

// 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'`
noExternal.push('esm-env');

if (!secondary_build_started) {
manifest_data = (await sync.all(svelte_config, config_env.mode)).manifest_data;
}
Expand All @@ -288,11 +284,14 @@ function kit({ svelte_config }) {
__SVELTEKIT_DEV__: 'true',
__SVELTEKIT_EMBEDDED__: kit.embedded ? 'true' : 'false'
};
}

new_config.ssr = {
noExternal
};
// 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

0 comments on commit 942ad18

Please sign in to comment.