Skip to content

Commit f9c5c16

Browse files
authored
fix: force remote module chunks to isolate themselves (#14571)
* fix: force remote module chunks to isolate themselves * remove hardcoded chunks
1 parent 56f679b commit f9c5c16

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

.changeset/puny-keys-poke.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: force remote module chunks to isolate themselves

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,27 @@ async function kit({ svelte_config }) {
618618
/** @type {Array<{ hash: string, file: string }>} */
619619
const remotes = [];
620620

621+
/**
622+
* A set of modules that imported by `.remote.ts` modules. By forcing these modules
623+
* into their own chunks, we ensure that each chunk created for a `.remote.ts`
624+
* module _only_ contains that module, hopefully avoiding any circular
625+
* dependency woes that arise from treating chunks as entries
626+
*/
627+
const imported_by_remotes = new Set();
628+
let uid = 1;
629+
621630
/** @type {import('vite').Plugin} */
622631
const plugin_remote = {
623632
name: 'vite-plugin-sveltekit-remote',
624633

634+
moduleParsed(info) {
635+
if (svelte_config.kit.moduleExtensions.some((ext) => info.id.endsWith(`.remote${ext}`))) {
636+
for (const id of info.importedIds) {
637+
imported_by_remotes.add(id);
638+
}
639+
}
640+
},
641+
625642
config(config) {
626643
if (!config.build?.ssr) {
627644
// only set manualChunks for the SSR build
@@ -644,21 +661,17 @@ async function kit({ svelte_config }) {
644661
config.build.rollupOptions.output = {
645662
...config.build.rollupOptions.output,
646663
manualChunks(id, meta) {
647-
// Prevent core runtime and env from ending up in a remote chunk, which could break because of initialization order
648-
if (id === `${runtime_directory}/app/server/index.js`) {
649-
return 'app-server';
650-
}
651-
if (id === `${runtime_directory}/shared-server.js`) {
652-
return 'app-shared-server';
653-
}
654-
655664
// Check if this is a *.remote.ts file
656665
if (svelte_config.kit.moduleExtensions.some((ext) => id.endsWith(`.remote${ext}`))) {
657666
const relative = posixify(path.relative(cwd, id));
658667

659668
return `remote-${hash(relative)}`;
660669
}
661670

671+
if (imported_by_remotes.has(id)) {
672+
return `chunk-${uid++}`;
673+
}
674+
662675
// If there was an existing manualChunks function, call it
663676
if (typeof manualChunks === 'function') {
664677
return manualChunks(id, meta);

0 commit comments

Comments
 (0)