Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fuzzy-windows-turbopack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/cloudflare": patch
---

fix: normalize Windows paths when patching the Turbopack runtime

Ensure traced Turbopack chunks are included in the generated runtime loaders when builds run on Windows.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@ import { describe, expect, test } from "vitest";

import {
loadWasmChunkFn,
patchTurbopackRuntime,
replaceLoadWebAssemblyModuleRule,
replaceLoadWebAssemblyRule,
} from "./turbopack.js";

describe("patchTurbopackRuntime", () => {
test("normalizes Windows paths before generating chunk loaders", async () => {
const patch = patchTurbopackRuntime.patches[0];
const code = "function loadRuntimeChunkPath() {}";

const patched = await patch.patchCode({
code,
filePath: String.raw`C:\project\.open-next\server-functions\default\.next\server\chunks\ssr\[turbopack]_runtime.js`,
tracedFiles: [
String.raw`C:\project\.open-next\server-functions\default\.next\server\chunks\ssr\route.js`,
String.raw`C:\project\.open-next\server-functions\default\.next\server\chunks\ssr\module.wasm`,
],
manifests: {} as never,
buildOptions: {} as never,
});

expect(patched).toContain(
'case "server/chunks/ssr/route.js": return require("C:/project/.open-next/server-functions/default/.next/server/chunks/ssr/route.js");'
);
expect(patched).toContain(
'case "server/chunks/ssr/module.wasm": return (await import("C:/project/.open-next/server-functions/default/.next/server/chunks/ssr/module.wasm")).default;'
);
});
});

describe("replaceLoadWebAssemblyModuleRule", () => {
test("rewrites Turbopack's loadWebAssemblyModule body", () => {
const code = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js";
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";

import { normalizePath } from "../../../utils/normalize-path.js";

const inlineChunksRule = `
rule:
kind: call_expression
Expand Down Expand Up @@ -261,6 +263,9 @@ export const patchTurbopackRuntime: CodePatcher = {
}),
contentFilter: /loadRuntimeChunkPath/,
patchCode: async ({ code, tracedFiles, filePath }) => {
tracedFiles = tracedFiles.map(normalizePath);
filePath = normalizePath(filePath);

const mappings = discoverExternalModuleMappings(filePath);
const externalImportRule = buildExternalImportRule(mappings, tracedFiles, code);
let patched = patchCode(code, externalImportRule);
Expand Down
15 changes: 15 additions & 0 deletions packages/cloudflare/src/cli/utils/normalize-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, test } from "vitest";

import { normalizePath } from "./normalize-path.js";

describe("normalizePath", () => {
test("normalizes Windows path separators on every platform", () => {
expect(normalizePath(String.raw`C:\project\.next\server\chunk.js`)).toBe(
"C:/project/.next/server/chunk.js"
);
});

test("leaves POSIX paths unchanged", () => {
expect(normalizePath("/project/.next/server/chunk.js")).toBe("/project/.next/server/chunk.js");
});
});
4 changes: 1 addition & 3 deletions packages/cloudflare/src/cli/utils/normalize-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { posix, sep } from "node:path";

export function normalizePath(path: string) {
return path.replaceAll(sep, posix.sep);
return path.replaceAll("\\", "/");
}
Loading