Skip to content

Commit

Permalink
fix: use chunkFileNames default to avoid ssr assets in client folder
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Apr 18, 2024
1 parent 9d6ba52 commit cfa94d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-boats-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

Switch to overriding the default chunkFileNames instead of assets dir to avoid server assets in client assets folder.
43 changes: 21 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,6 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
runtimeId,
});

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 ??= "";
}

if (isTest) {
linked = false;
const { test } = config as any;
Expand Down Expand Up @@ -311,6 +301,17 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
}
}

if (isSSRBuild && !config.build?.rollupOptions?.output) {
// 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 chunkFileNames config to instead output to the outDir directly.
config.build ??= {};
config.build.rollupOptions ??= {};
config.build.rollupOptions.output = {
chunkFileNames: `[name]-[hash].js`,
};
}

if (basePathVar) {
config.experimental ??= {};

Expand All @@ -324,19 +325,17 @@ export default function markoPlugin(opts: Options = {}): vite.Plugin[] {
config.build?.assetsDir?.replace(/[/\\]$/, "") ?? "assets";
const assetsDirLen = assetsDir.length;
const assetsDirEnd = assetsDirLen + 1;
const trimAssertsDir = assetsDir
? (fileName: string) => {
if (fileName.startsWith(assetsDir)) {
switch (fileName[assetsDirLen]) {
case POSIX_SEP:
case WINDOWS_SEP:
return fileName.slice(assetsDirEnd);
}
}

return fileName;
const trimAssertsDir = (fileName: string) => {
if (fileName.startsWith(assetsDir)) {
switch (fileName[assetsDirLen]) {
case POSIX_SEP:
case WINDOWS_SEP:
return fileName.slice(assetsDirEnd);
}
: (fileName: string) => fileName;
}

return fileName;
};
config.experimental.renderBuiltUrl = (
fileName,
{ hostType, ssr },
Expand Down

0 comments on commit cfa94d0

Please sign in to comment.