diff --git a/.changeset/puny-keys-poke.md b/.changeset/puny-keys-poke.md new file mode 100644 index 000000000000..d97a424a3b0e --- /dev/null +++ b/.changeset/puny-keys-poke.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: force remote module chunks to isolate themselves diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index f7b2a07c0a02..2801e06fde3f 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -618,10 +618,27 @@ async function kit({ svelte_config }) { /** @type {Array<{ hash: string, file: string }>} */ const remotes = []; + /** + * A set of modules that imported by `.remote.ts` modules. By forcing these modules + * into their own chunks, we ensure that each chunk created for a `.remote.ts` + * module _only_ contains that module, hopefully avoiding any circular + * dependency woes that arise from treating chunks as entries + */ + const imported_by_remotes = new Set(); + let uid = 1; + /** @type {import('vite').Plugin} */ const plugin_remote = { name: 'vite-plugin-sveltekit-remote', + moduleParsed(info) { + if (svelte_config.kit.moduleExtensions.some((ext) => info.id.endsWith(`.remote${ext}`))) { + for (const id of info.importedIds) { + imported_by_remotes.add(id); + } + } + }, + config(config) { if (!config.build?.ssr) { // only set manualChunks for the SSR build @@ -644,14 +661,6 @@ async function kit({ svelte_config }) { config.build.rollupOptions.output = { ...config.build.rollupOptions.output, manualChunks(id, meta) { - // Prevent core runtime and env from ending up in a remote chunk, which could break because of initialization order - if (id === `${runtime_directory}/app/server/index.js`) { - return 'app-server'; - } - if (id === `${runtime_directory}/shared-server.js`) { - return 'app-shared-server'; - } - // Check if this is a *.remote.ts file if (svelte_config.kit.moduleExtensions.some((ext) => id.endsWith(`.remote${ext}`))) { const relative = posixify(path.relative(cwd, id)); @@ -659,6 +668,10 @@ async function kit({ svelte_config }) { return `remote-${hash(relative)}`; } + if (imported_by_remotes.has(id)) { + return `chunk-${uid++}`; + } + // If there was an existing manualChunks function, call it if (typeof manualChunks === 'function') { return manualChunks(id, meta);