Skip to content

Commit

Permalink
fix: invalid paths in windows bundles (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Jan 10, 2025
1 parent 7028c33 commit 05ee8d4
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-plants-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

fix: invalid paths in windows bundles.
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"dependencies": {
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@688",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@695",
"glob": "catalog:",
"ts-morph": "catalog:",
"enquirer": "^2.4.1"
Expand Down
6 changes: 3 additions & 3 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { build, Plugin } from "esbuild";

import { Config } from "../config.js";
import * as patches from "./patches/index.js";
import { normalizePath } from "./utils/index.js";

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

const isMonorepo = monorepoRoot !== appPath;
if (isMonorepo) {
const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
fs.writeFileSync(
path.join(outputPath, "handler.mjs"),
`export * from "./${packagePosixPath}/handler.mjs";`
`export * from "./${normalizePath(packagePath)}/handler.mjs";`
);
}

Expand Down Expand Up @@ -183,7 +183,7 @@ function createFixRequiresESBuildPlugin(config: Config): Plugin {
name: "replaceRelative",
setup(build) {
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
build.onResolve({ filter: /^\.\/require-hook$/ }, () => ({
build.onResolve({ filter: /^\.(\/|\\)require-hook$/ }, () => ({
path: path.join(config.paths.internal.templates, "shims", "empty.js"),
}));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { openNextReplacementPlugin } from "@opennextjs/aws/plugins/replacement.j
import { openNextResolvePlugin } from "@opennextjs/aws/plugins/resolve.js";
import type { FunctionOptions, SplittedFunctionOptions } from "@opennextjs/aws/types/open-next.js";

import { normalizePath } from "../utils/index.js";

export async function createServerBundle(options: buildHelper.BuildOptions) {
const { config } = options;
const foundRoutes = new Set<string>();
Expand Down Expand Up @@ -206,7 +208,7 @@ async function generateBundle(
outfile: path.join(outputPath, packagePath, `index.${outfileExt}`),
banner: {
js: [
`globalThis.monorepoPackagePath = "${packagePath}";`,
`globalThis.monorepoPackagePath = "${normalizePath(packagePath)}";`,
name === "default" ? "" : `globalThis.fnName = "${name}";`,
].join(""),
},
Expand Down Expand Up @@ -273,10 +275,10 @@ function addMonorepoEntrypoint(outputPath: string, packagePath: string) {
// the root of the bundle. We will create a dummy `index.mjs`
// that re-exports the real handler.

// TOOD: use helper
// Always use posix path for import path
const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
fs.writeFileSync(path.join(outputPath, "index.mjs"), `export * from "./${packagePosixPath}/index.mjs";`);
fs.writeFileSync(
path.join(outputPath, "index.mjs"),
`export * from "./${normalizePath(packagePath)}/index.mjs";`
);
}

async function minifyServerBundle(outputDir: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import path from "node:path";

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

import { normalizePath } from "../../utils/index.js";

/**
* Sets up the OpenNext cache handler in a Next.js build.
*
Expand All @@ -24,7 +26,7 @@ export async function patchCache(code: string, openNextOptions: BuildOptions): P
return code.replace(
"const { cacheHandler } = this.nextConfig;",
`const cacheHandler = null;
CacheHandler = require('${cacheFile}').default;
CacheHandler = require('${normalizePath(cacheFile)}').default;
`
);
}
Loading

0 comments on commit 05ee8d4

Please sign in to comment.