Skip to content

Commit

Permalink
fix: move default assets dir check to be unconditional (#130)
Browse files Browse the repository at this point in the history
* fix: move default assets dir check to be unconditional

* fix: ensure we only trim asset url if it isn't empty
  • Loading branch information
DylanPiercey authored Apr 18, 2024
1 parent b787f13 commit 14bf61e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-dolphins-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/vite": patch
---

The previous patch of changing the default assetsDir for ssr was not applied in the correct branch. This is now fixed.
42 changes: 22 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ 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 @@ -310,31 +320,23 @@ 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;
const assetsDirEnd = assetsDirLen + 1;
const trimAssertsDir = (fileName: string) => {
if (fileName.startsWith(assetsDir)) {
switch (fileName[assetsDirLen]) {
case POSIX_SEP:
case WINDOWS_SEP:
return fileName.slice(assetsDirEnd);
}
}
const trimAssertsDir = assetsDir
? (fileName: string) => {
if (fileName.startsWith(assetsDir)) {
switch (fileName[assetsDirLen]) {
case POSIX_SEP:
case WINDOWS_SEP:
return fileName.slice(assetsDirEnd);
}
}

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

0 comments on commit 14bf61e

Please sign in to comment.