Skip to content

Commit 9f05ae8

Browse files
committed
review and e2e fix
1 parent 1ad1b7a commit 9f05ae8

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

examples/prisma/e2e/prisma.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import { test, expect } from "@playwright/test";
33
test.describe("playground/cloudflare", () => {
44
test("Prisma doesn't crash", async ({ page }) => {
55
await page.goto("/");
6-
const aliceName = await page.getByTestId("name-1").innerText();
6+
const aliceName = await page.getByTestId("name-Alice").innerText();
77
expect(aliceName).toBe("Alice");
8-
const aliceEmail = await page.getByTestId("email-1").innerText();
8+
const aliceEmail = await page.getByTestId("email-Alice").innerText();
99
expect(aliceEmail).toBe("[email protected]");
1010

11-
const bobName = await page.getByTestId("name-2").innerText();
11+
const bobName = await page.getByTestId("name-Bob").innerText();
1212
expect(bobName).toBe("Bob");
13-
const bobEmail = await page.getByTestId("email-2").innerText();
13+
const bobEmail = await page.getByTestId("email-Bob").innerText();
1414
expect(bobEmail).toBe("[email protected]");
1515

16-
const carolName = await page.getByTestId("name-3").innerText();
16+
const carolName = await page.getByTestId("name-Carol").innerText();
1717
expect(carolName).toBe("Carol");
18-
const carolEmail = await page.getByTestId("email-3").innerText();
18+
const carolEmail = await page.getByTestId("email-Carol").innerText();
1919
expect(carolEmail).toBe("[email protected]");
2020
});
2121
});

examples/prisma/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"build": "next build",
88
"start": "next start",
99
"lint": "next lint",
10-
"prepare:db": "npx prisma generate && wrangler d1 ",
11-
"build:worker": "npx prisma generate && pnpm opennextjs-cloudflare build",
10+
"prepare:db": "npx prisma generate && wrangler d1 execute db --file populate.sql",
11+
"build:worker": "pnpm prepare:db && pnpm opennextjs-cloudflare build",
1212
"preview:worker": "pnpm opennextjs-cloudflare preview",
1313
"preview": "pnpm build:worker && pnpm preview:worker",
1414
"e2e": "playwright test -c e2e/playwright.config.ts",

examples/prisma/src/app/page.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Image from "next/image";
21
import styles from "./page.module.css";
32
import { getDb } from "@/lib/db";
43

@@ -12,9 +11,9 @@ export default async function Home() {
1211
<ul>
1312
{allUsers.map((user) => (
1413
<li key={user.id}>
15-
<span data-testid={`name-${user.id}`}>{user.name}</span>
14+
<span data-testid={`name-${user.name}`}>{user.name}</span>
1615
<br />
17-
<span data-testid={`email-${user.id}`}>{user.email}</span>
16+
<span data-testid={`email-${user.name}`}>{user.email}</span>
1817
</li>
1918
))}
2019
</ul>

packages/cloudflare/src/cli/build/utils/workerd.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,30 @@ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
1111
* to only include the build condition if found (e.g. "workerd") and remove everything else.
1212
* If no build condition is found, it keeps everything as is.
1313
* It also returns a boolean indicating if the build condition was found.
14-
* @param exports The exports (or imports) object from the package.json
14+
* @param conditionMap The exports (or imports) object from the package.json
1515
* @param condition The build condition to look for
1616
* @returns An object with the transformed exports and a boolean indicating if the build condition was found
1717
*/
18-
export function transformBuildCondition(exports: { [key: string]: unknown }, condition: string) {
18+
export function transformBuildCondition(
19+
conditionMap: { [key: string]: unknown },
20+
condition: string
21+
): {
22+
transformedExports: { [key: string]: unknown };
23+
hasBuildCondition: boolean;
24+
} {
1925
const transformed: { [key: string]: unknown } = {};
20-
const hasTopLevelBuildCondition = Object.keys(exports).some(
21-
(key) => key === condition && typeof exports[key] === "string"
26+
const hasTopLevelBuildCondition = Object.keys(conditionMap).some(
27+
(key) => key === condition && typeof conditionMap[key] === "string"
2228
);
2329
let hasBuildCondition = hasTopLevelBuildCondition;
24-
for (const [key, value] of Object.entries(exports)) {
30+
for (const [key, value] of Object.entries(conditionMap)) {
2531
if (typeof value === "object" && value != null) {
2632
const { transformedExports, hasBuildCondition: innerBuildCondition } = transformBuildCondition(
2733
value as { [key: string]: unknown },
2834
condition
2935
);
3036
transformed[key] = transformedExports;
31-
hasBuildCondition = hasBuildCondition || innerBuildCondition;
37+
hasBuildCondition ||= innerBuildCondition;
3238
} else {
3339
// If it doesn't have the build condition, we need to keep everything as is
3440
// If it has the build condition, we need to keep only the build condition
@@ -55,17 +61,17 @@ interface PackageJson {
5561
* @returns An object with the transformed package.json and a boolean indicating if the build condition was found
5662
*/
5763
export function transformPackageJson(json: PackageJson) {
58-
const transformed: PackageJson = { ...json };
64+
const transformed: PackageJson = structuredClone(json);
5965
let hasBuildCondition = false;
6066
if (json.exports) {
6167
const exp = transformBuildCondition(json.exports, "workerd");
6268
transformed.exports = exp.transformedExports;
63-
hasBuildCondition = exp.hasBuildCondition;
69+
hasBuildCondition ||= exp.hasBuildCondition;
6470
}
6571
if (json.imports) {
6672
const imp = transformBuildCondition(json.imports, "workerd");
6773
transformed.imports = imp.transformedExports;
68-
hasBuildCondition = hasBuildCondition || imp.hasBuildCondition;
74+
hasBuildCondition ||= imp.hasBuildCondition;
6975
}
7076
return { transformed, hasBuildCondition };
7177
}
@@ -86,7 +92,7 @@ export async function copyWorkerdPackages(options: BuildOptions, nodePackages: M
8692
`Copying package using a workerd condition: ${path.relative(options.appPath, src)} -> ${path.relative(options.appPath, dst)}`
8793
);
8894
await fs.cp(src, dst, { recursive: true, force: true });
89-
// Write the transformed package.json
95+
// Overwrite with the transformed package.json
9096
await fs.writeFile(path.join(dst, "package.json"), JSON.stringify(transformed), "utf8");
9197
}
9298
} catch {

0 commit comments

Comments
 (0)