diff --git a/.changeset/happy-ducks-rule.md b/.changeset/happy-ducks-rule.md new file mode 100644 index 0000000..5fb2ab1 --- /dev/null +++ b/.changeset/happy-ducks-rule.md @@ -0,0 +1,5 @@ +--- +"@marko/vite": patch +--- + +Fix issue when a Marko server entry is loaded inside of another chunk which caused the assets injection runtime to be in a different chunk than the server entry (since it will codesplit the runtime). This change now ensures the asset manifest is only ever injected into chunks that reference the assets runtime injector. diff --git a/.changeset/proud-pillows-play.md b/.changeset/proud-pillows-play.md new file mode 100644 index 0000000..a14a059 --- /dev/null +++ b/.changeset/proud-pillows-play.md @@ -0,0 +1,5 @@ +--- +"@marko/vite": patch +--- + +Change vites default `assetsDir` for server bundles to avoid making server assets available in client asset folders. diff --git a/src/index.ts b/src/index.ts index 958bd72..452efb3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -310,6 +310,16 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] { ); } + if (isSSRBuild && !config.build?.ssrEmitAssets) { + config.build ??= {}; + + // For the server build vite will still output code split chunks to the `assets` directory by default. + // this is problematic since you might have server assets in your client assets folder. + // Here we change the default to be an empty string which makes the assetsDir essentially the same as + // as outDir for the server chunks. + config.build.assetsDir ??= ""; + } + const assetsDir = config.build?.assetsDir?.replace(/[/\\]$/, "") ?? "assets"; const assetsDirLen = assetsDir.length; @@ -748,13 +758,10 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] { const chunk = bundle[fileName]; if (chunk.type === "chunk") { - for (const id in chunk.modules) { - if (id.endsWith(serverEntryQuery)) { - serverManifest!.chunksNeedingAssets.push( - path.resolve(dir, fileName), - ); - break; - } + if (chunk.moduleIds.includes(renderAssetsRuntimeId)) { + serverManifest!.chunksNeedingAssets.push( + path.resolve(dir, fileName), + ); } } }