Skip to content

Commit 05ee8d4

Browse files
authored
fix: invalid paths in windows bundles (#247)
1 parent 7028c33 commit 05ee8d4

File tree

6 files changed

+497
-15
lines changed

6 files changed

+497
-15
lines changed

.changeset/slow-plants-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: invalid paths in windows bundles.

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
},
7373
"dependencies": {
7474
"@dotenvx/dotenvx": "catalog:",
75-
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@688",
75+
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@695",
7676
"glob": "catalog:",
7777
"ts-morph": "catalog:",
7878
"enquirer": "^2.4.1"

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { build, Plugin } from "esbuild";
88

99
import { Config } from "../config.js";
1010
import * as patches from "./patches/index.js";
11+
import { normalizePath } from "./utils/index.js";
1112

1213
/** The dist directory of the Cloudflare adapter package */
1314
const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../..");
@@ -117,10 +118,9 @@ globalThis.__BUILD_TIMESTAMP_MS__ = ${Date.now()};
117118

118119
const isMonorepo = monorepoRoot !== appPath;
119120
if (isMonorepo) {
120-
const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
121121
fs.writeFileSync(
122122
path.join(outputPath, "handler.mjs"),
123-
`export * from "./${packagePosixPath}/handler.mjs";`
123+
`export * from "./${normalizePath(packagePath)}/handler.mjs";`
124124
);
125125
}
126126

@@ -183,7 +183,7 @@ function createFixRequiresESBuildPlugin(config: Config): Plugin {
183183
name: "replaceRelative",
184184
setup(build) {
185185
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
186-
build.onResolve({ filter: /^\.\/require-hook$/ }, () => ({
186+
build.onResolve({ filter: /^\.(\/|\\)require-hook$/ }, () => ({
187187
path: path.join(config.paths.internal.templates, "shims", "empty.js"),
188188
}));
189189
},

packages/cloudflare/src/cli/build/open-next/createServerBundle.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { openNextReplacementPlugin } from "@opennextjs/aws/plugins/replacement.j
1717
import { openNextResolvePlugin } from "@opennextjs/aws/plugins/resolve.js";
1818
import type { FunctionOptions, SplittedFunctionOptions } from "@opennextjs/aws/types/open-next.js";
1919

20+
import { normalizePath } from "../utils/index.js";
21+
2022
export async function createServerBundle(options: buildHelper.BuildOptions) {
2123
const { config } = options;
2224
const foundRoutes = new Set<string>();
@@ -206,7 +208,7 @@ async function generateBundle(
206208
outfile: path.join(outputPath, packagePath, `index.${outfileExt}`),
207209
banner: {
208210
js: [
209-
`globalThis.monorepoPackagePath = "${packagePath}";`,
211+
`globalThis.monorepoPackagePath = "${normalizePath(packagePath)}";`,
210212
name === "default" ? "" : `globalThis.fnName = "${name}";`,
211213
].join(""),
212214
},
@@ -273,10 +275,10 @@ function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
273275
// the root of the bundle. We will create a dummy `index.mjs`
274276
// that re-exports the real handler.
275277

276-
// TOOD: use helper
277-
// Always use posix path for import path
278-
const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
279-
fs.writeFileSync(path.join(outputPath, "index.mjs"), `export * from "./${packagePosixPath}/index.mjs";`);
278+
fs.writeFileSync(
279+
path.join(outputPath, "index.mjs"),
280+
`export * from "./${normalizePath(packagePath)}/index.mjs";`
281+
);
280282
}
281283

282284
async function minifyServerBundle(outputDir: string) {

packages/cloudflare/src/cli/build/patches/investigated/patch-cache.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import path from "node:path";
22

33
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
44

5+
import { normalizePath } from "../../utils/index.js";
6+
57
/**
68
* Sets up the OpenNext cache handler in a Next.js build.
79
*
@@ -24,7 +26,7 @@ export async function patchCache(code: string, openNextOptions: BuildOptions): P
2426
return code.replace(
2527
"const { cacheHandler } = this.nextConfig;",
2628
`const cacheHandler = null;
27-
CacheHandler = require('${cacheFile}').default;
29+
CacheHandler = require('${normalizePath(cacheFile)}').default;
2830
`
2931
);
3032
}

0 commit comments

Comments
 (0)