@opennextjs/cloudflare 0.4.4 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,6 +50,16 @@ export async function bundleServer(buildOpts) {
50
50
  target: "esnext",
51
51
  minify: false,
52
52
  metafile: true,
53
+ // Next traces files using the default conditions from `nft` (`node`, `require`, `import` and `default`)
54
+ //
55
+ // Because we use the `node` platform for this build, the "module" condition is used when no conditions are defined.
56
+ // We explicitly set the conditions to an empty array to disable the "module" condition in order to match Next tracing.
57
+ //
58
+ // See:
59
+ // - default nft conditions: https://github.com/vercel/nft/blob/2b55b01/readme.md#exports--imports
60
+ // - Next no explicit override: https://github.com/vercel/next.js/blob/2efcf11/packages/next/src/build/collect-build-traces.ts#L287
61
+ // - ESBuild `node` platform: https://esbuild.github.io/api/#platform
62
+ conditions: [],
53
63
  plugins: [
54
64
  createFixRequiresESBuildPlugin(buildOpts),
55
65
  inlineRequirePagePlugin(buildOpts),
@@ -126,10 +136,6 @@ globalThis.__BUILD_TIMESTAMP_MS__ = ${Date.now()};
126
136
  `,
127
137
  },
128
138
  });
129
- if (result.errors.length > 0) {
130
- result.errors.forEach((error) => console.error(error));
131
- throw new Error(`There was a problem bundling the server.`);
132
- }
133
139
  fs.writeFileSync(openNextServerBundle + ".meta.json", JSON.stringify(result.metafile, null, 2));
134
140
  await updateWorkerBundledCode(openNextServerBundle, buildOpts);
135
141
  const isMonorepo = monorepoRoot !== appPath;
@@ -5,5 +5,7 @@ import { extractProjectEnvVars } from "../utils/index.js";
5
5
  * Compiles the values extracted from the project's env files to the output directory for use in the worker.
6
6
  */
7
7
  export function compileEnvFiles(buildOpts) {
8
- ["production", "development", "test"].forEach((mode) => fs.appendFileSync(path.join(buildOpts.outputDir, `.env.mjs`), `export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, buildOpts))};\n`));
8
+ const envDir = path.join(buildOpts.outputDir, "env");
9
+ fs.mkdirSync(envDir, { recursive: true });
10
+ ["production", "development", "test"].forEach((mode) => fs.appendFileSync(path.join(envDir, `next-env.mjs`), `export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, buildOpts))};\n`));
9
11
  }
@@ -1,6 +1,6 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { join } from "node:path";
3
- import { fileURLToPath } from "node:url";
3
+ import { fileURLToPath, URL } from "node:url";
4
4
  export function getVersion() {
5
5
  const require = createRequire(import.meta.url);
6
6
  const __dirname = fileURLToPath(new URL(".", import.meta.url));
@@ -1,5 +1,7 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  // @ts-expect-error: resolved by wrangler build
3
+ import * as nextEnvVars from "./env/next-env.mjs";
4
+ // @ts-expect-error: resolved by wrangler build
3
5
  import { handler as middlewareHandler } from "./middleware/handler.mjs";
4
6
  // @ts-expect-error: resolved by wrangler build
5
7
  import { handler as serverHandler } from "./server-functions/default/handler.mjs";
@@ -12,14 +14,13 @@ globalThis[Symbol.for("__cloudflare-context__")] = new Proxy({}, {
12
14
  get: (_, property) => Reflect.get(cloudflareContextALS.getStore(), property),
13
15
  set: (_, property, value) => Reflect.set(cloudflareContextALS.getStore(), property, value),
14
16
  });
17
+ // Populate process.env on the first request
18
+ let processEnvPopulated = false;
15
19
  export default {
16
20
  async fetch(request, env, ctx) {
17
21
  return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
18
22
  const url = new URL(request.url);
19
- if (process.env.__PROCESS_ENV_POPULATED !== "1") {
20
- await populateProcessEnv(url, env.NEXTJS_ENV);
21
- process.env.__PROCESS_ENV_POPULATED = "1";
22
- }
23
+ populateProcessEnv(url, env.NEXTJS_ENV);
23
24
  if (url.pathname === "/_next/image") {
24
25
  const imageUrl = url.searchParams.get("url") ?? "";
25
26
  return imageUrl.startsWith("/")
@@ -44,9 +45,11 @@ export default {
44
45
  *
45
46
  * Note that cloudflare env string values are copied by the middleware handler.
46
47
  */
47
- async function populateProcessEnv(url, nextJsEnv) {
48
- // @ts-expect-error: resolved by wrangler build
49
- const nextEnvVars = await import("./.env.mjs");
48
+ function populateProcessEnv(url, nextJsEnv) {
49
+ if (processEnvPopulated) {
50
+ return;
51
+ }
52
+ processEnvPopulated = true;
50
53
  const mode = nextJsEnv ?? "production";
51
54
  if (nextEnvVars[mode]) {
52
55
  for (const key in nextEnvVars[mode]) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@opennextjs/cloudflare",
3
3
  "description": "Cloudflare builder for next apps",
4
- "version": "0.4.4",
4
+ "version": "0.4.5",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "opennextjs-cloudflare": "dist/cli/index.js"
@@ -63,7 +63,7 @@
63
63
  "dependencies": {
64
64
  "@ast-grep/napi": "^0.34.1",
65
65
  "@dotenvx/dotenvx": "1.31.0",
66
- "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@722",
66
+ "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@724",
67
67
  "enquirer": "^2.4.1",
68
68
  "glob": "^11.0.0",
69
69
  "ts-morph": "^23.0.0",