Skip to content
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/puny-keys-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: force remote module chunks to isolate themselves
29 changes: 21 additions & 8 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -644,21 +661,17 @@ 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));

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);
Expand Down
Loading