-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix server-only and client-only + fix(next): preserve exit code (#…
…480)
- Loading branch information
Showing
17 changed files
with
237 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { spawn } from "child_process"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
|
||
export function createEditor(filepath: string) { | ||
const init = readFileSync(filepath, "utf-8"); | ||
return { | ||
edit(editFn: (data: string) => string) { | ||
const next = editFn(init); | ||
writeFileSync(filepath, next); | ||
}, | ||
[Symbol.dispose]() { | ||
writeFileSync(filepath, init); | ||
}, | ||
}; | ||
} | ||
|
||
export async function runCommand(command: string, ...args: string[]) { | ||
const proc = spawn(command, args, { | ||
stdio: "pipe", | ||
env: { | ||
...process.env, | ||
NODE_ENV: undefined, | ||
}, | ||
}); | ||
|
||
let stdout = ""; | ||
let stderr = ""; | ||
proc.stdout.on("data", (data) => { | ||
stdout += data.toString(); | ||
}); | ||
|
||
proc.stderr.on("data", (data) => { | ||
stderr += data.toString(); | ||
}); | ||
|
||
const code = await new Promise<number>((resolve, reject) => { | ||
proc.once("close", (code) => { | ||
if (code === null) { | ||
reject(new Error("exit null")); | ||
} else { | ||
resolve(code); | ||
} | ||
}); | ||
|
||
proc.once("error", (error) => { | ||
reject(error); | ||
}); | ||
}); | ||
|
||
return { code, stdout, stderr }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { beforeAll, expect, it } from "vitest"; | ||
import { createEditor, runCommand } from "./helper"; | ||
|
||
beforeAll(() => { | ||
process.chdir("examples/minimal"); | ||
}); | ||
|
||
it("server only - success", async () => { | ||
using file = createEditor("app/_action.tsx"); | ||
file.edit((s) => s + `\n\n;import "server-only"`); | ||
|
||
const result = await runCommand("pnpm", "build"); | ||
expect(result.code).toBe(0); | ||
}); | ||
|
||
it("server only - error", async () => { | ||
using file = createEditor("app/_client.tsx"); | ||
file.edit((s) => s + `\n\n;import "server-only"`); | ||
|
||
const result = await runCommand("pnpm", "build"); | ||
expect(result.code).toBe(1); | ||
expect(result.stderr).toContain(`'server-only' is included in client build `); | ||
}); | ||
|
||
it("client only - success", async () => { | ||
using file = createEditor("app/_client.tsx"); | ||
file.edit((s) => s + `\n\n;import "client-only"`); | ||
|
||
const result = await runCommand("pnpm", "build"); | ||
expect(result.code).toBe(0); | ||
}); | ||
|
||
it("client only - error", async () => { | ||
using file = createEditor("app/_action.tsx"); | ||
file.edit((s) => s + `\n\n;import "client-only"`); | ||
|
||
const result = await runCommand("pnpm", "build"); | ||
expect(result.code).toBe(1); | ||
expect(result.stderr).toContain(`'client-only' is included in server build `); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
minimal template for testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use server"; | ||
|
||
let count = 0; | ||
|
||
export async function changeCount() { | ||
count++; | ||
} | ||
|
||
export function getCount() { | ||
return count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
|
||
export function TestClient() { | ||
const [count, setCount] = React.useState(0); | ||
|
||
return <button onClick={() => setCount(count + 1)}>Client: {count}</button>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type React from "react"; | ||
|
||
export default function Layout(props: React.PropsWithChildren) { | ||
return ( | ||
<html> | ||
<body> | ||
<div>[Layout]</div> | ||
{props.children} | ||
</body> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { changeCount, getCount } from "./_action"; | ||
import { TestClient } from "./_client"; | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<div>[Page]</div> | ||
<TestClient /> | ||
<form action={changeCount}> | ||
<button>Action: {getCount()}</button> | ||
</form> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@hiogawa/react-server-example-minimal", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"@hiogawa/react-server": "workspace:*", | ||
"next": "link:../../../react-server-next", | ||
"react": "19.0.0-rc-c21bcd627b-20240624", | ||
"react-dom": "19.0.0-rc-c21bcd627b-20240624", | ||
"react-server-dom-webpack": "19.0.0-rc-c21bcd627b-20240624" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "18.3.1", | ||
"@types/react-dom": "18.3.0", | ||
"vite": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"include": ["**/*.ts", "**/*.tsx"], | ||
"compilerOptions": { | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"skipLibCheck": true, | ||
"verbatimModuleSyntax": true, | ||
"noEmit": true, | ||
"moduleResolution": "Bundler", | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"lib": ["ESNext", "DOM"], | ||
"types": ["next"], | ||
"jsx": "react-jsx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import next from "next/vite"; | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({ | ||
plugins: [next()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
esbuild: { | ||
supported: { | ||
using: false, | ||
}, | ||
}, | ||
test: { | ||
dir: "e2e", | ||
pool: "forks", | ||
fileParallelism: false, | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.