Skip to content

Commit

Permalink
fix: manifest inject in dynamic import entries (#128)
Browse files Browse the repository at this point in the history
* fix: manifest inject in dynamic import entries

* fix: avoid outputting server assets in assets folder
  • Loading branch information
DylanPiercey authored Apr 18, 2024
1 parent 58aec9f commit 8e10644
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-ducks-rule.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changeset/proud-pillows-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Change vites default `assetsDir` for server bundles to avoid making server assets available in client asset folders.
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}
}
}
Expand Down

0 comments on commit 8e10644

Please sign in to comment.