Skip to content

Commit

Permalink
fix(vite): remove server exports from resource routes
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Oct 30, 2023
1 parent 05bc45b commit 68ef921
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-wasps-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

fix server code interop for vite build
54 changes: 53 additions & 1 deletion integration/vite-build-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as path from "node:path";
import { test, expect } from "@playwright/test";
import shell from "shelljs";
import glob from "glob";

import {
createAppFixture,
Expand Down Expand Up @@ -51,13 +54,25 @@ test.describe("Vite build", () => {
`,
"app/routes/_index.tsx": js`
import { useState, useEffect } from "react";
import { json} from "@remix-run/node";
import { serverOnly1, serverOnly2 } from "../utils.server";
export const loader = () => {
return json({ serverOnly1 })
}
export const action = () => {
console.log(serverOnly2)
return null
}
export default function() {
const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
}, []);
return (
<>
<h2>Index</h2>
Expand All @@ -66,6 +81,24 @@ test.describe("Vite build", () => {
);
}
`,
"app/routes/resource.ts": js`
import { json} from "@remix-run/node";
import { serverOnly1, serverOnly2 } from "../utils.server";
export const loader = () => {
return json({ serverOnly1 })
}
export const action = () => {
console.log(serverOnly2)
return null
}
`,
"app/utils.server.ts": js`
export const serverOnly1 = "SERVER_ONLY_1"
export const serverOnly2 = "SERVER_ONLY_2"
`,
},
});

Expand All @@ -76,6 +109,25 @@ test.describe("Vite build", () => {
appFixture.close();
});

test("server code is removed from client assets", async () => {
let publicBuildDir = path.join(fixture.projectDir, "public/build");

// detect client asset files
let assetFiles = glob.sync("**/*.@(js|jsx|ts|tsx)", {
cwd: publicBuildDir,
absolute: true,
});

// grep for server-only values in client assets
let result = shell
.grep("-l", /SERVER_ONLY_1|SERVER_ONLY_2/, assetFiles)
.stdout.trim()
.split("\n")
.filter((line) => line.length > 0);

expect(result).toHaveLength(0);
});

test("server renders matching routes", async () => {
let res = await fixture.requestDocument("/");
expect(res.status).toBe(200);
Expand Down
14 changes: 0 additions & 14 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,20 +632,6 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {

let serverExports = ["loader", "action", "headers"];

let routeExports = await getRouteModuleExports(
viteChildCompiler,
pluginConfig,
route.file
);

// ignore resource routes that only have server exports
// note: resource routes for fullstack components don't need a `default` export
// but still need their server exports removed
let browserExports = routeExports.filter(
(x) => !serverExports.includes(x)
);
if (browserExports.length === 0) return;

return {
code: removeExports(code, serverExports),
map: null,
Expand Down

0 comments on commit 68ef921

Please sign in to comment.