From 4c7ef4c663a53fd0406b64c093e36ff7a53731a1 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 27 Dec 2024 23:12:16 +0100 Subject: [PATCH 01/10] add new `normalizePathForInlineCode` utility --- .changeset/gold-mirrors-hide.md | 5 +++++ .../build/patches/investigated/patch-cache.ts | 6 ++++-- .../utils/normalize-path-for-inline-code.spec.ts | 10 ++++++++++ .../utils/normalize-path-for-inline-code.ts | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/gold-mirrors-hide.md create mode 100644 packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts create mode 100644 packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts diff --git a/.changeset/gold-mirrors-hide.md b/.changeset/gold-mirrors-hide.md new file mode 100644 index 00000000..db8830e7 --- /dev/null +++ b/.changeset/gold-mirrors-hide.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +TODO: add a proper changeset diff --git a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts index 4e82d503..2ac21791 100644 --- a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts +++ b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts @@ -2,6 +2,8 @@ import path from "node:path"; import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; +import { normalizePathForInlineCode } from "../../utils/normalize-path-for-inline-code.js"; + /** * Sets up the OpenNext cache handler in a Next.js build. * @@ -19,12 +21,12 @@ export async function patchCache(code: string, openNextOptions: BuildOptions): P // TODO: switch to cache.mjs const outputPath = path.join(outputDir, "server-functions", "default"); const packagePath = path.relative(monorepoRoot, appBuildOutputPath); - const cacheFile = path.join(outputPath, packagePath, "cache.cjs"); + const cacheFilePath = path.join(outputPath, packagePath, "cache.cjs"); return code.replace( "const { cacheHandler } = this.nextConfig;", `const cacheHandler = null; -CacheHandler = require('${cacheFile}').default; +CacheHandler = require('${normalizePathForInlineCode(cacheFilePath)}').default; ` ); } diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts new file mode 100644 index 00000000..78bc4b3f --- /dev/null +++ b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts @@ -0,0 +1,10 @@ +import { describe, expect, it } from "vitest"; + +import { normalizePathForInlineCode } from "./normalize-path-for-inline-code"; + +describe("normalizePathForInlineCode", () => { + it("should extract production env vars", () => { + const result = normalizePathForInlineCode("TODO"); + expect(result).toBeFalsy(); + }); +}); diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts new file mode 100644 index 00000000..624a946d --- /dev/null +++ b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts @@ -0,0 +1,16 @@ +import { normalizePath } from "./normalize-path.js"; + +/** + * TODO: add proper comment + * + * @param path + */ +export function normalizePathForInlineCode(path: string): string { + // let's normalize the path for good measure + const normalizedPath = normalizePath(path); + + // we need to escape + const doublyEscaped = normalizedPath.replaceAll("\\", "\\\\"); + + return doublyEscaped; +} From b38fa7126f2d32350d160910d401b2ae8c9fd68d Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 27 Dec 2024 23:39:33 +0100 Subject: [PATCH 02/10] implement simple unit tetsts --- .../normalize-path-for-inline-code.spec.ts | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts index 78bc4b3f..d9cdd263 100644 --- a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts +++ b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts @@ -3,8 +3,27 @@ import { describe, expect, it } from "vitest"; import { normalizePathForInlineCode } from "./normalize-path-for-inline-code"; describe("normalizePathForInlineCode", () => { - it("should extract production env vars", () => { - const result = normalizePathForInlineCode("TODO"); - expect(result).toBeFalsy(); + describe.runIf(process.platform === "win32")("windows", () => { + it("should produce an absolute path ready to be embedded in inlined code", () => { + const code = `const d = require('${normalizePathForInlineCode("/Users/me/projects/cloudflare/index.mjs")}').default;`; + expect(code).toEqual("const d = require('\\Users\\me\\projects\\cloudflare\\index.mjs').default;"); + }); + + it("should produce a relative path ready to be embedded in inlined code", () => { + const code = `const d = require('${normalizePathForInlineCode("../../tmp/index.mjs")}').default;`; + expect(code).toEqual("const d = require('..\\..\\tmp\\index.mjs').default;"); + }); + }); + + describe.runIf(process.platform === "linux" || process.platform === "darwin")("linux/mac", () => { + it("should produce an absolute path ready to be embedded in inlined code", () => { + const code = `const d = require('${normalizePathForInlineCode("/Users/me/projects/cloudflare/index.mjs")}').default;`; + expect(code).toEqual("const d = require('/Users/me/projects/cloudflare/index.mjs').default;"); + }); + + it("should produce a relative path ready to be embedded in inlined code", () => { + const code = `const d = require('${normalizePathForInlineCode("../../tmp/index.mjs")}').default;`; + expect(code).toEqual("const d = require('../../tmp/index.mjs').default;"); + }); }); }); From 1f2bbd07f78597a5685e23f1f1e3dd8a3252812e Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 27 Dec 2024 23:47:55 +0100 Subject: [PATCH 03/10] run tests on different OSes --- .github/workflows/checks.yml | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index dafd1144..734b25b7 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - script: ["prettier:check", "lint:check", "ts:check", "test"] + script: ["prettier:check", "lint:check", "ts:check"] steps: - name: Check out code uses: actions/checkout@v4 @@ -21,5 +21,21 @@ jobs: - name: Install Dependencies uses: ./.github/actions/install-dependencies - - name: ${{ matrix.script }} + - name: run "${{ matrix.script }}" script run: pnpm run ${{ matrix.script }} + tests: + name: tests (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-13, windows-2022, ubuntu-22.04] + steps: + - name: Check out code + uses: actions/checkout@v4 + + - name: Install Dependencies + uses: ./.github/actions/install-dependencies + + - name: run "test" script + run: pnpm run test From 9cab2d3a190706c07257c2b355f600d48df1d179 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 27 Dec 2024 23:50:12 +0100 Subject: [PATCH 04/10] run e2e tests on different OSes --- .github/workflows/playwright.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index 4fc920b3..87c1a4c8 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -9,7 +9,12 @@ on: jobs: test: timeout-minutes: 30 - runs-on: ubuntu-latest + name: e2e tests (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-13, windows-2022, ubuntu-22.04] steps: - name: Check out code uses: actions/checkout@v4 From 2ccd25bd0b5b7c14a069e5191e3f6fbf9b6bf1ee Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 27 Dec 2024 23:55:51 +0100 Subject: [PATCH 05/10] improve unit tests --- .../normalize-path-for-inline-code.spec.ts | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts index d9cdd263..5080743b 100644 --- a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts +++ b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts @@ -1,29 +1,29 @@ +import path from "node:path"; + import { describe, expect, it } from "vitest"; import { normalizePathForInlineCode } from "./normalize-path-for-inline-code"; +const isWindows = process.platform === "win32"; + describe("normalizePathForInlineCode", () => { - describe.runIf(process.platform === "win32")("windows", () => { - it("should produce an absolute path ready to be embedded in inlined code", () => { - const code = `const d = require('${normalizePathForInlineCode("/Users/me/projects/cloudflare/index.mjs")}').default;`; + it("should produce an absolute path ready to be embedded in inlined code", () => { + const joined = path.join("/", "Users", "me", "projects", "cloudflare", "index.mjs"); + const code = `const d = require('${normalizePathForInlineCode(joined)}').default;`; + if (!isWindows) { + expect(code).toEqual("const d = require('/Users/me/projects/cloudflare/index.mjs').default;"); + } else { expect(code).toEqual("const d = require('\\Users\\me\\projects\\cloudflare\\index.mjs').default;"); - }); - - it("should produce a relative path ready to be embedded in inlined code", () => { - const code = `const d = require('${normalizePathForInlineCode("../../tmp/index.mjs")}').default;`; - expect(code).toEqual("const d = require('..\\..\\tmp\\index.mjs').default;"); - }); + } }); - describe.runIf(process.platform === "linux" || process.platform === "darwin")("linux/mac", () => { - it("should produce an absolute path ready to be embedded in inlined code", () => { - const code = `const d = require('${normalizePathForInlineCode("/Users/me/projects/cloudflare/index.mjs")}').default;`; - expect(code).toEqual("const d = require('/Users/me/projects/cloudflare/index.mjs').default;"); - }); - - it("should produce a relative path ready to be embedded in inlined code", () => { - const code = `const d = require('${normalizePathForInlineCode("../../tmp/index.mjs")}').default;`; + it("should produce a relative path ready to be embedded in inlined code", () => { + const joined = path.join("..", "..", "tmp", "index.mjs"); + const code = `const d = require('${normalizePathForInlineCode(joined)}').default;`; + if (!isWindows) { expect(code).toEqual("const d = require('../../tmp/index.mjs').default;"); - }); + } else { + expect(code).toEqual("const d = require('..\\..\\tmp\\index.mjs').default;"); + } }); }); From 88d5818edb8004cef9feb61e3096cf13d164dc76 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sat, 28 Dec 2024 00:00:51 +0100 Subject: [PATCH 06/10] remove unnecessary new function --- .../build/patches/investigated/patch-cache.ts | 4 +-- .../normalize-path-for-inline-code.spec.ts | 29 ------------------- .../utils/normalize-path-for-inline-code.ts | 16 ---------- 3 files changed, 2 insertions(+), 47 deletions(-) delete mode 100644 packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts delete mode 100644 packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts diff --git a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts index 2ac21791..28bb4fc6 100644 --- a/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts +++ b/packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts @@ -2,7 +2,7 @@ import path from "node:path"; import type { BuildOptions } from "@opennextjs/aws/build/helper.js"; -import { normalizePathForInlineCode } from "../../utils/normalize-path-for-inline-code.js"; +import { normalizePath } from "../../utils/normalize-path.js"; /** * Sets up the OpenNext cache handler in a Next.js build. @@ -26,7 +26,7 @@ export async function patchCache(code: string, openNextOptions: BuildOptions): P return code.replace( "const { cacheHandler } = this.nextConfig;", `const cacheHandler = null; -CacheHandler = require('${normalizePathForInlineCode(cacheFilePath)}').default; +CacheHandler = require('${normalizePath(cacheFilePath)}').default; ` ); } diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts deleted file mode 100644 index 5080743b..00000000 --- a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import path from "node:path"; - -import { describe, expect, it } from "vitest"; - -import { normalizePathForInlineCode } from "./normalize-path-for-inline-code"; - -const isWindows = process.platform === "win32"; - -describe("normalizePathForInlineCode", () => { - it("should produce an absolute path ready to be embedded in inlined code", () => { - const joined = path.join("/", "Users", "me", "projects", "cloudflare", "index.mjs"); - const code = `const d = require('${normalizePathForInlineCode(joined)}').default;`; - if (!isWindows) { - expect(code).toEqual("const d = require('/Users/me/projects/cloudflare/index.mjs').default;"); - } else { - expect(code).toEqual("const d = require('\\Users\\me\\projects\\cloudflare\\index.mjs').default;"); - } - }); - - it("should produce a relative path ready to be embedded in inlined code", () => { - const joined = path.join("..", "..", "tmp", "index.mjs"); - const code = `const d = require('${normalizePathForInlineCode(joined)}').default;`; - if (!isWindows) { - expect(code).toEqual("const d = require('../../tmp/index.mjs').default;"); - } else { - expect(code).toEqual("const d = require('..\\..\\tmp\\index.mjs').default;"); - } - }); -}); diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts b/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts deleted file mode 100644 index 624a946d..00000000 --- a/packages/cloudflare/src/cli/build/utils/normalize-path-for-inline-code.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { normalizePath } from "./normalize-path.js"; - -/** - * TODO: add proper comment - * - * @param path - */ -export function normalizePathForInlineCode(path: string): string { - // let's normalize the path for good measure - const normalizedPath = normalizePath(path); - - // we need to escape - const doublyEscaped = normalizedPath.replaceAll("\\", "\\\\"); - - return doublyEscaped; -} From 5b9b47650b765686107ff2f04c044bfe365d66ed Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sat, 28 Dec 2024 00:03:58 +0100 Subject: [PATCH 07/10] add tests for normalize-path --- .../cli/build/utils/normalize-path.spec.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 packages/cloudflare/src/cli/build/utils/normalize-path.spec.ts diff --git a/packages/cloudflare/src/cli/build/utils/normalize-path.spec.ts b/packages/cloudflare/src/cli/build/utils/normalize-path.spec.ts new file mode 100644 index 00000000..7fa7a04f --- /dev/null +++ b/packages/cloudflare/src/cli/build/utils/normalize-path.spec.ts @@ -0,0 +1,22 @@ +import path from "node:path"; + +import { describe, expect, it } from "vitest"; + +import { normalizePath } from "./normalize-path"; + +describe("normalizePath", () => { + it("should produce an absolute path ready to be embedded in inlined code", () => { + const joined = path.join("/", "Users", "me", "projects", "cloudflare", "index.mjs"); + const result = normalizePath(joined); + // Note: the result is the same both on linux/mac and windows + expect(result).toEqual("/Users/me/projects/cloudflare/index.mjs"); + }); + + it("should produce a relative path ready to be embedded in inlined code", () => { + const joined = path.join("..", "..", "tmp", "index.mjs"); + const result = normalizePath(joined); + + // Note: the result is the same both on linux/mac and windows + expect(result).toEqual("../../tmp/index.mjs"); + }); +}); From ed89321b0dbcc95ccd9ba7e2ba2183809371e5f6 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sat, 28 Dec 2024 00:14:43 +0100 Subject: [PATCH 08/10] remove unnecessary test script --- examples/vercel-commerce/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/vercel-commerce/package.json b/examples/vercel-commerce/package.json index 2e0b5784..e838880a 100644 --- a/examples/vercel-commerce/package.json +++ b/examples/vercel-commerce/package.json @@ -11,7 +11,6 @@ "start": "next start", "prettier": "prettier --write --ignore-unknown .", "prettier:check": "prettier --check --ignore-unknown .", - "test": "pnpm prettier:check", "tofix-build:worker": "opennextjs-cloudflare", "tofix-dev:worker": "wrangler dev --port 8772", "tofix-preview:worker": "pnpm build:worker && pnpm dev:worker" From c94dbbfd4ac6d00776fa48a7b242e5fa1d7b97e7 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 30 Dec 2024 10:43:47 +0100 Subject: [PATCH 09/10] run playwright e2e in windows under WSL --- .github/workflows/playwright.yml | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index 87c1a4c8..04e759b1 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macos-13, windows-2022, ubuntu-22.04] + os: [macos-13, ubuntu-22.04] steps: - name: Check out code uses: actions/checkout@v4 @@ -36,3 +36,34 @@ jobs: - name: Run playwright dev tests run: pnpm e2e:dev + test-windows: + defaults: + run: + shell: wsl-bash {0} + timeout-minutes: 30 + name: e2e tests (windows-2022) + runs-on: windows-2022 + steps: + - name: Setup WSL + uses: Vampire/setup-wsl@v4 + + - name: Check out code + uses: actions/checkout@v4 + + - name: Install Dependencies + uses: ./.github/actions/install-dependencies + + - name: Install Playwright + run: pnpm run install-playwright + + - name: Build the tool + run: pnpm build + + - name: Build all workers + run: pnpm -r build:worker + + - name: Run playwright tests + run: pnpm e2e + + - name: Run playwright dev tests + run: pnpm e2e:dev From 0b0897b6806ce31225936940ff5d9f45f9473e80 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 30 Dec 2024 10:49:08 +0100 Subject: [PATCH 10/10] Revert "run playwright e2e in windows under WSL" This reverts commit c94dbbfd4ac6d00776fa48a7b242e5fa1d7b97e7. --- .github/workflows/playwright.yml | 33 +------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml index 04e759b1..87c1a4c8 100644 --- a/.github/workflows/playwright.yml +++ b/.github/workflows/playwright.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - os: [macos-13, ubuntu-22.04] + os: [macos-13, windows-2022, ubuntu-22.04] steps: - name: Check out code uses: actions/checkout@v4 @@ -36,34 +36,3 @@ jobs: - name: Run playwright dev tests run: pnpm e2e:dev - test-windows: - defaults: - run: - shell: wsl-bash {0} - timeout-minutes: 30 - name: e2e tests (windows-2022) - runs-on: windows-2022 - steps: - - name: Setup WSL - uses: Vampire/setup-wsl@v4 - - - name: Check out code - uses: actions/checkout@v4 - - - name: Install Dependencies - uses: ./.github/actions/install-dependencies - - - name: Install Playwright - run: pnpm run install-playwright - - - name: Build the tool - run: pnpm build - - - name: Build all workers - run: pnpm -r build:worker - - - name: Run playwright tests - run: pnpm e2e - - - name: Run playwright dev tests - run: pnpm e2e:dev