Skip to content

Commit 1439cc2

Browse files
committed
fix path resolution issue
1 parent f8d6019 commit 1439cc2

3 files changed

Lines changed: 66 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# master
22

3+
- Fix minor path resolution issue in the new static assets setup.
4+
35
# 1.4.0
46

57
- Add Bun single-file executable support to the Vite plugin via `staticAssetRoutes.mode`, including an `embedded` mode for `bun build --compile` and normalized build-time asset URLs for generated browser assets like `resXClient_js`.

res-x-vite-plugin.mjs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,6 @@ export default function resXVitePlugin(options = {}) {
350350
delete bundle[fileName];
351351
});
352352

353-
const resolvedOutDir = resolveConfiguredPath(projectRoot, outDir);
354-
const resolvedPublicDir = resolveConfiguredPath(projectRoot, publicDir);
355353
const bundleFileNames = Object.values(bundle)
356354
.map(output => output.fileName)
357355
.filter(Boolean);
@@ -360,8 +358,9 @@ export default function resXVitePlugin(options = {}) {
360358
bundleFileNames,
361359
clientFileNameByFieldName,
362360
manifest,
363-
outDir: resolvedOutDir,
364-
publicDir: resolvedPublicDir,
361+
outDir,
362+
projectRoot,
363+
publicDir,
365364
staticAssetRoutes,
366365
});
367366

@@ -997,12 +996,12 @@ function getDummyStaticAssetRoutesFile(
997996
exactEntries: buildStaticAssetRouteEntries({
998997
publicEntries: getPublicRouteEntries({
999998
baseDir: resolvedPublicDir,
1000-
fileDir: resolvedPublicDir,
999+
fileDir: publicDir,
10011000
}),
10021001
assetEntries: getAssetDirContent(resolvedAssetDir).map(assetPath => ({
10031002
kind: "asset",
10041003
routePath: toRoutePath(path.join(assetRouteBase, assetPath)),
1005-
sourcePath: path.resolve(resolvedAssetDir, assetPath),
1004+
sourcePath: path.join(assetDir, assetPath),
10061005
})),
10071006
exactEntries:
10081007
resXClientLocation == null
@@ -1011,7 +1010,7 @@ function getDummyStaticAssetRoutesFile(
10111010
{
10121011
kind: "client",
10131012
routePath: toRoutePath(resXClientLocation),
1014-
sourcePath: resolveConfiguredPath(projectRoot, resXClientLocation),
1013+
sourcePath: resXClientLocation,
10151014
},
10161015
],
10171016
headers: staticAssetRoutes.headers,
@@ -1026,9 +1025,11 @@ function getGeneratedBuildManifest({
10261025
clientFileNameByFieldName,
10271026
manifest,
10281027
outDir,
1028+
projectRoot,
10291029
publicDir,
10301030
staticAssetRoutes,
10311031
}) {
1032+
const resolvedPublicDir = resolveConfiguredPath(projectRoot, publicDir);
10321033
const exposedAssetEntries = manifest.flatMap(entry => {
10331034
const generatedPath =
10341035
entry.kind === "asset"
@@ -1044,20 +1045,20 @@ function getGeneratedBuildManifest({
10441045
fieldName: entry.fieldName,
10451046
kind: entry.kind,
10461047
routePath: toRoutePath(generatedPath),
1047-
sourcePath: path.resolve(outDir, generatedPath),
1048+
sourcePath: path.join(outDir, generatedPath),
10481049
},
10491050
];
10501051
});
10511052

10521053
const serverAssetEntries = buildStaticAssetRouteEntries({
10531054
publicEntries: getPublicRouteEntries({
1054-
baseDir: publicDir,
1055+
baseDir: resolvedPublicDir,
10551056
fileDir: outDir,
10561057
}),
10571058
assetEntries: bundleFileNames.map(fileName => ({
10581059
kind: "bundle",
10591060
routePath: toRoutePath(fileName),
1060-
sourcePath: path.resolve(outDir, fileName),
1061+
sourcePath: path.join(outDir, fileName),
10611062
})),
10621063
exactEntries: exposedAssetEntries,
10631064
headers: staticAssetRoutes.headers,
@@ -1120,7 +1121,7 @@ function getPublicRouteEntries({ baseDir, fileDir }) {
11201121
return getPublicDirContent(baseDir).map(publicPath => ({
11211122
kind: "public",
11221123
routePath: toRoutePath(publicPath),
1123-
sourcePath: path.resolve(fileDir, publicPath),
1124+
sourcePath: path.join(fileDir, publicPath),
11241125
}));
11251126
}
11261127

test/StaticAssetRoutes.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ describe("static asset routes", () => {
506506
},
507507
],
508508
outDir,
509+
projectRoot: tempDir,
509510
publicDir,
510511
staticAssetRoutes: createStaticAssetRoutesConfig({
511512
headers: {
@@ -618,6 +619,7 @@ describe("static asset routes", () => {
618619
},
619620
],
620621
outDir,
622+
projectRoot: tempDir,
621623
publicDir,
622624
staticAssetRoutes,
623625
});
@@ -718,4 +720,54 @@ console.log(server.port);
718720
}
719721
});
720722
});
723+
724+
test("filesystem build routes keep outside-root outDir relative", async () => {
725+
const {
726+
createStaticAssetRoutesConfig,
727+
getBuildStaticAssetRoutesFile,
728+
getGeneratedBuildManifest,
729+
} = await getPluginTestHelpers();
730+
731+
await withTempDir(async tempDir => {
732+
const projectRoot = path.join(tempDir, "app");
733+
const outDir = "../dist";
734+
const publicDir = "public";
735+
const generatedDir = path.join(projectRoot, "src", "__generated__");
736+
const modulePath = path.join(generatedDir, "res-x-static-routes.js");
737+
738+
fs.mkdirSync(path.join(projectRoot, publicDir), { recursive: true });
739+
fs.mkdirSync(path.join(tempDir, "dist", "assets"), { recursive: true });
740+
fs.mkdirSync(generatedDir, { recursive: true });
741+
fs.writeFileSync(
742+
path.join(projectRoot, publicDir, "robots.txt"),
743+
"User-agent: *"
744+
);
745+
fs.writeFileSync(
746+
path.join(tempDir, "dist", "assets", "app.css"),
747+
"body { color: red; }"
748+
);
749+
750+
const staticAssetRoutes = createStaticAssetRoutesConfig();
751+
const generatedBuildManifest = getGeneratedBuildManifest({
752+
assetFileNameByBuildId: new Map(),
753+
bundleFileNames: ["assets/app.css"],
754+
clientFileNameByFieldName: new Map(),
755+
manifest: [],
756+
outDir,
757+
projectRoot,
758+
publicDir,
759+
staticAssetRoutes,
760+
});
761+
const content = getBuildStaticAssetRoutesFile({
762+
generatedFilePath: modulePath,
763+
projectRoot,
764+
serverAssetEntries: generatedBuildManifest.serverAssetEntries,
765+
staticAssetRoutes,
766+
});
767+
768+
expect(content.includes('Bun.file("../dist/assets/app.css")')).toBe(true);
769+
expect(content.includes('Bun.file("/')).toBe(false);
770+
expect(content.includes('Bun.file("../dist/robots.txt")')).toBe(true);
771+
});
772+
});
721773
});

0 commit comments

Comments
 (0)