|
| 1 | +import * as path from "node:path"; |
| 2 | +import { test, expect } from "@playwright/test"; |
| 3 | +import shell from "shelljs"; |
| 4 | +import glob from "glob"; |
| 5 | + |
| 6 | +import { createProject, viteBuild } from "./helpers/vite.js"; |
| 7 | + |
| 8 | +let files = { |
| 9 | + "app/utils.server.ts": String.raw` |
| 10 | + export const dotServerFile = "SERVER_ONLY_FILE"; |
| 11 | + `, |
| 12 | + "app/.server/utils.ts": String.raw` |
| 13 | + export const dotServerDir = "SERVER_ONLY_DIR"; |
| 14 | + `, |
| 15 | +}; |
| 16 | + |
| 17 | +test("Vite / build / .server file in client fails with expected error", async () => { |
| 18 | + let cwd = await createProject({ |
| 19 | + ...files, |
| 20 | + "app/routes/fail-server-file-in-client.tsx": String.raw` |
| 21 | + import { dotServerFile } from "~/utils.server"; |
| 22 | +
|
| 23 | + export default function() { |
| 24 | + console.log(dotServerFile); |
| 25 | + return <h1>Fail: Server file included in client</h1> |
| 26 | + } |
| 27 | + `, |
| 28 | + }); |
| 29 | + let client = viteBuild({ cwd })[0]; |
| 30 | + let stderr = client.stderr.toString("utf8"); |
| 31 | + expect(stderr).toMatch( |
| 32 | + `"dotServerFile" is not exported by "app/utils.server.ts"` |
| 33 | + ); |
| 34 | +}); |
| 35 | + |
| 36 | +test("Vite / build / .server dir in client fails with expected error", async () => { |
| 37 | + let cwd = await createProject({ |
| 38 | + ...files, |
| 39 | + "app/routes/fail-server-dir-in-client.tsx": String.raw` |
| 40 | + import { dotServerDir } from "~/.server/utils"; |
| 41 | +
|
| 42 | + export default function() { |
| 43 | + console.log(dotServerDir); |
| 44 | + return <h1>Fail: Server directory included in client</h1> |
| 45 | + } |
| 46 | + `, |
| 47 | + }); |
| 48 | + let client = viteBuild({ cwd })[0]; |
| 49 | + let stderr = client.stderr.toString("utf8"); |
| 50 | + expect(stderr).toMatch( |
| 51 | + `"dotServerDir" is not exported by "app/.server/utils.ts"` |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +test("Vite / build / dead-code elimination for server exports", async () => { |
| 56 | + let cwd = await createProject({ |
| 57 | + ...files, |
| 58 | + "app/routes/remove-server-exports-and-dce.tsx": String.raw` |
| 59 | + import fs from "node:fs"; |
| 60 | + import { json } from "@remix-run/node"; |
| 61 | + import { useLoaderData } from "@remix-run/react"; |
| 62 | +
|
| 63 | + import { dotServerFile } from "../utils.server"; |
| 64 | + import { dotServerDir } from "../.server/utils"; |
| 65 | +
|
| 66 | + export const loader = () => { |
| 67 | + let contents = fs.readFileSync("blah"); |
| 68 | + let data = dotServerFile + dotServerDir + serverOnly + contents; |
| 69 | + return json({ data }); |
| 70 | + } |
| 71 | +
|
| 72 | + export const action = () => { |
| 73 | + console.log(dotServerFile, dotServerDir, serverOnly); |
| 74 | + return null; |
| 75 | + } |
| 76 | +
|
| 77 | + export default function() { |
| 78 | + let { data } = useLoaderData<typeof loader>(); |
| 79 | + return ( |
| 80 | + <> |
| 81 | + <h2>Index</h2> |
| 82 | + <p>{data}</p> |
| 83 | + </> |
| 84 | + ); |
| 85 | + } |
| 86 | + `, |
| 87 | + }); |
| 88 | + let client = viteBuild({ cwd })[0]; |
| 89 | + expect(client.status).toBe(0); |
| 90 | + |
| 91 | + // detect client asset files |
| 92 | + let assetFiles = glob.sync("**/*.@(js|jsx|ts|tsx)", { |
| 93 | + cwd: path.join(cwd, "build/client"), |
| 94 | + absolute: true, |
| 95 | + }); |
| 96 | + |
| 97 | + // grep for server-only values in client assets |
| 98 | + let result = shell |
| 99 | + .grep("-l", /SERVER_ONLY_FILE|SERVER_ONLY_DIR|node:fs/, assetFiles) |
| 100 | + .stdout.trim() |
| 101 | + .split("\n") |
| 102 | + .filter((line) => line.length > 0); |
| 103 | + |
| 104 | + expect(result).toHaveLength(0); |
| 105 | +}); |
0 commit comments