@opennextjs/cloudflare 0.5.6 → 0.5.8

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.
@@ -9,6 +9,7 @@ import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js";
9
9
  import * as patches from "./patches/index.js";
10
10
  import { inlineBuildId } from "./patches/plugins/build-id.js";
11
11
  import { ContentUpdater } from "./patches/plugins/content-updater.js";
12
+ import { inlineDynamicRequires } from "./patches/plugins/dynamic-requires.js";
12
13
  import { inlineEvalManifest } from "./patches/plugins/eval-manifest.js";
13
14
  import { patchFetchCacheSetMissingWaitUntil } from "./patches/plugins/fetch-cache-wait-until.js";
14
15
  import { inlineFindDir } from "./patches/plugins/find-dir.js";
@@ -18,7 +19,6 @@ import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
18
19
  import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
19
20
  import { fixRequire } from "./patches/plugins/require.js";
20
21
  import { shimRequireHook } from "./patches/plugins/require-hook.js";
21
- import { inlineRequirePage } from "./patches/plugins/require-page.js";
22
22
  import { setWranglerExternal } from "./patches/plugins/wrangler-external.js";
23
23
  import { normalizePath, patchCodeWithValidations } from "./utils/index.js";
24
24
  /** The dist directory of the Cloudflare adapter package */
@@ -72,7 +72,7 @@ export async function bundleServer(buildOpts) {
72
72
  conditions: [],
73
73
  plugins: [
74
74
  shimRequireHook(buildOpts),
75
- inlineRequirePage(updater, buildOpts),
75
+ inlineDynamicRequires(updater, buildOpts),
76
76
  setWranglerExternal(),
77
77
  fixRequire(updater),
78
78
  handleOptionalDependencies(optionalDependencies),
@@ -0,0 +1,4 @@
1
+ import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
2
+ import type { Plugin } from "esbuild";
3
+ import type { ContentUpdater } from "./content-updater.js";
4
+ export declare function inlineDynamicRequires(updater: ContentUpdater, buildOpts: BuildOptions): Plugin;
@@ -0,0 +1,108 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { join, posix, sep } from "node:path";
3
+ import { getPackagePath } from "@opennextjs/aws/build/helper.js";
4
+ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
5
+ import { normalizePath } from "../../utils/normalize-path.js";
6
+ import { patchCode } from "../ast/util.js";
7
+ async function getPagesManifests(serverDir) {
8
+ try {
9
+ return Object.values(JSON.parse(await readFile(join(serverDir, "pages-manifest.json"), "utf-8")));
10
+ }
11
+ catch {
12
+ // The file does not exist
13
+ return [];
14
+ }
15
+ }
16
+ async function getAppPathsManifests(serverDir) {
17
+ try {
18
+ return Object.values(JSON.parse(await readFile(join(serverDir, "app-paths-manifest.json"), "utf-8")));
19
+ }
20
+ catch {
21
+ // The file does not exist
22
+ return [];
23
+ }
24
+ }
25
+ function getServerDir(buildOpts) {
26
+ return join(buildOpts.outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");
27
+ }
28
+ function getRequires(idVariable, files, serverDir) {
29
+ // Inline fs access and dynamic requires that are not supported by workerd.
30
+ return files
31
+ .map((file) => `
32
+ if (${idVariable}.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)}).endsWith(${JSON.stringify(normalizePath(file))})) {
33
+ return require(${JSON.stringify(join(serverDir, file))});
34
+ }`)
35
+ .join("\n");
36
+ }
37
+ export function inlineDynamicRequires(updater, buildOpts) {
38
+ updater.updateContent("inline-node-module-loader", {
39
+ filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/lib/module-loader/node-module-loader\.js$`, { escape: false }),
40
+ contentFilter: /class NodeModuleLoader {/,
41
+ }, async ({ contents }) => patchCode(contents, await getNodeModuleLoaderRule(buildOpts)));
42
+ updater.updateContent("inline-require-page", {
43
+ filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/require\.js$`, { escape: false }),
44
+ contentFilter: /function requirePage\(/,
45
+ }, async ({ contents }) => patchCode(contents, await getRequirePageRule(buildOpts)));
46
+ return { name: "inline-dynamic-requires", setup() { } };
47
+ }
48
+ async function getNodeModuleLoaderRule(buildOpts) {
49
+ const serverDir = getServerDir(buildOpts);
50
+ const manifests = await getPagesManifests(serverDir);
51
+ const files = manifests.filter((file) => file.endsWith(".js"));
52
+ return `
53
+ rule:
54
+ kind: method_definition
55
+ all:
56
+ - has:
57
+ field: name
58
+ regex: ^load$
59
+ - has:
60
+ field: parameters
61
+ has:
62
+ kind: required_parameter
63
+ pattern: $ID
64
+ inside:
65
+ stopBy:
66
+ kind: class_declaration
67
+ kind: class_declaration
68
+ has:
69
+ field: name
70
+ regex: ^NodeModuleLoader$
71
+ fix: |
72
+ async load($ID) {
73
+ ${getRequires("$ID", files, serverDir)}
74
+ }`;
75
+ }
76
+ async function getRequirePageRule(buildOpts) {
77
+ const serverDir = getServerDir(buildOpts);
78
+ const pagesManifests = await getPagesManifests(serverDir);
79
+ const appPathsManifests = await getAppPathsManifests(serverDir);
80
+ const manifests = pagesManifests.concat(appPathsManifests);
81
+ const htmlFiles = manifests.filter((file) => file.endsWith(".html"));
82
+ const jsFiles = manifests.filter((file) => file.endsWith(".js"));
83
+ return {
84
+ rule: {
85
+ pattern: `
86
+ function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) {
87
+ const $_ = getPagePath($$$ARGS);
88
+ $$$_BODY
89
+ }`,
90
+ }, // Inline fs access and dynamic require that are not supported by workerd.
91
+ fix: `
92
+ function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) {
93
+ const pagePath = getPagePath($$$ARGS).replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
94
+
95
+ // html
96
+ ${(await Promise.all(htmlFiles.map(async (file) => `if (pagePath.endsWith(${JSON.stringify(normalizePath(file))})) {
97
+ return ${JSON.stringify(await readFile(join(serverDir, file), "utf-8"))};
98
+ }`))).join("\n")}
99
+ // js
100
+ process.env.__NEXT_PRIVATE_RUNTIME_TYPE = $IS_APP_PATH ? 'app' : 'pages';
101
+ try {
102
+ ${getRequires("pagePath", jsFiles, serverDir)}
103
+ } finally {
104
+ process.env.__NEXT_PRIVATE_RUNTIME_TYPE = '';
105
+ }
106
+ }`,
107
+ };
108
+ }
@@ -2,7 +2,7 @@
2
2
  * Inline `evalManifest` as it relies on `readFileSync` and `runInNewContext`
3
3
  * that are not supported by workerd.
4
4
  */
5
- import { join, relative } from "node:path";
5
+ import { join, posix, relative, sep } from "node:path";
6
6
  import { getPackagePath } from "@opennextjs/aws/build/helper.js";
7
7
  import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
8
8
  import { glob } from "glob";
@@ -46,8 +46,7 @@ function evalManifest($PATH, $$$ARGS) {
46
46
  },
47
47
  fix: `
48
48
  function evalManifest($PATH, $$$ARGS) {
49
- const { platform } = require('process');
50
- $PATH = platform === 'win32' ? $PATH.replaceAll('\\\\', '/') : $PATH;
49
+ $PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
51
50
  ${returnManifests}
52
51
  throw new Error(\`Unexpected evalManifest(\${$PATH}) call!\`);
53
52
  }`,
@@ -2,7 +2,7 @@
2
2
  * Inline `findDir` as it relies on `existsSync` which is not supported by workerd.
3
3
  */
4
4
  import { existsSync } from "node:fs";
5
- import { join } from "node:path";
5
+ import { join, posix, sep } from "node:path";
6
6
  import { getPackagePath } from "@opennextjs/aws/build/helper.js";
7
7
  import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
8
8
  import { patchCode } from "../ast/util.js";
@@ -22,8 +22,7 @@ rule:
22
22
  pattern: function findDir($DIR, $NAME) { $$$_ }
23
23
  fix: |-
24
24
  function findDir($DIR, $NAME) {
25
- const { platform } = require('process');
26
- $DIR = platform === 'win32' ? $DIR.replaceAll('\\\\', '/') : $DIR;
25
+ $DIR = $DIR.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
27
26
  if ($DIR.endsWith(".next/server")) {
28
27
  if ($NAME === "app") {
29
28
  return ${appExists};
@@ -2,7 +2,7 @@
2
2
  * Inline `loadManifest` as it relies on `readFileSync` that is not supported by workerd.
3
3
  */
4
4
  import { readFile } from "node:fs/promises";
5
- import { join, relative } from "node:path";
5
+ import { join, posix, relative, sep } from "node:path";
6
6
  import { getPackagePath } from "@opennextjs/aws/build/helper.js";
7
7
  import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
8
8
  import { glob } from "glob";
@@ -33,8 +33,7 @@ function loadManifest($PATH, $$$ARGS) {
33
33
  },
34
34
  fix: `
35
35
  function loadManifest($PATH, $$$ARGS) {
36
- const { platform } = require('process');
37
- $PATH = platform === 'win32' ? $PATH.replaceAll('\\\\', '/') : $PATH;
36
+ $PATH = $PATH.replaceAll(${JSON.stringify(sep)}, ${JSON.stringify(posix.sep)});
38
37
  ${returnManifests}
39
38
  throw new Error(\`Unexpected loadManifest(\${$PATH}) call!\`);
40
39
  }`,
@@ -28,7 +28,7 @@ export async function createWranglerConfigIfNotExistent(projectOpts) {
28
28
  "(to avoid this check use the `--skipWranglerConfigCheck` flag or set a `SKIP_WRANGLER_CONFIG_CHECK` environment variable to `yes`)");
29
29
  return;
30
30
  }
31
- let wranglerConfig = readFileSync(join(getPackageTemplatesDirPath(), "defaults/wrangler.json"), "utf8");
31
+ let wranglerConfig = readFileSync(join(getPackageTemplatesDirPath(), "wrangler.json"), "utf8");
32
32
  const appName = getAppNameFromPackageJson(projectOpts.sourceDir) ?? "app-name";
33
33
  if (appName) {
34
34
  wranglerConfig = wranglerConfig.replace('"app-name"', JSON.stringify(appName.replaceAll("_", "-")));
@@ -80,6 +80,6 @@ export async function createOpenNextConfigIfNotExistent(projectOpts) {
80
80
  if (!answer) {
81
81
  throw new Error("The `open-next.config.ts` file is required, aborting!");
82
82
  }
83
- cpSync(join(getPackageTemplatesDirPath(), "defaults/open-next.config.ts"), openNextConfigPath);
83
+ cpSync(join(getPackageTemplatesDirPath(), "open-next.config.ts"), openNextConfigPath);
84
84
  }
85
85
  }
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.5.6",
4
+ "version": "0.5.8",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "opennextjs-cloudflare": "dist/cli/index.js"
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
44
44
  "devDependencies": {
45
- "@cloudflare/workers-types": "^4.20241230.0",
45
+ "@cloudflare/workers-types": "^4.20250224.0",
46
46
  "@eslint/js": "^9.11.1",
47
47
  "@tsconfig/strictest": "^2.0.5",
48
48
  "@types/mock-fs": "^4.13.4",
@@ -69,7 +69,7 @@
69
69
  "yaml": "^2.7.0"
70
70
  },
71
71
  "peerDependencies": {
72
- "wrangler": "^3.107.3"
72
+ "wrangler": "^3.111.0"
73
73
  },
74
74
  "scripts": {
75
75
  "clean": "rimraf dist",
@@ -0,0 +1,7 @@
1
+ // default open-next.config.ts file created by @opennextjs/cloudflare
2
+ import { defineCloudflareConfig } from "@opennextjs/cloudflare/config";
3
+ import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache";
4
+
5
+ export default defineCloudflareConfig({
6
+ incrementalCache: kvIncrementalCache,
7
+ });
@@ -1,3 +0,0 @@
1
- import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
2
- import type { ContentUpdater } from "./content-updater.js";
3
- export declare function inlineRequirePage(updater: ContentUpdater, buildOpts: BuildOptions): import("esbuild").Plugin;
@@ -1,70 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import { join } from "node:path";
3
- import { getPackagePath } from "@opennextjs/aws/build/helper.js";
4
- import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
5
- import { normalizePath } from "../../utils/normalize-path.js";
6
- import { patchCode } from "../ast/util.js";
7
- export function inlineRequirePage(updater, buildOpts) {
8
- return updater.updateContent("inline-require-page", {
9
- filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/require\.js$`, { escape: false }),
10
- contentFilter: /function requirePage\(/,
11
- }, async ({ contents }) => patchCode(contents, await getRule(buildOpts)));
12
- }
13
- async function getRule(buildOpts) {
14
- const { outputDir } = buildOpts;
15
- const serverDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");
16
- const pagesManifestFile = join(serverDir, "pages-manifest.json");
17
- const appPathsManifestFile = join(serverDir, "app-paths-manifest.json");
18
- let pagesManifests = [];
19
- try {
20
- pagesManifests = Object.values(JSON.parse(await readFile(pagesManifestFile, "utf-8")));
21
- }
22
- catch {
23
- // The file does not exists
24
- pagesManifests = [];
25
- }
26
- let appPathsManifests;
27
- try {
28
- appPathsManifests = Object.values(JSON.parse(await readFile(appPathsManifestFile, "utf-8")));
29
- }
30
- catch {
31
- // The file does not exists
32
- appPathsManifests = [];
33
- }
34
- const manifests = pagesManifests.concat(appPathsManifests).map((path) => normalizePath(path));
35
- const htmlFiles = manifests.filter((file) => file.endsWith(".html"));
36
- const jsFiles = manifests.filter((file) => file.endsWith(".js"));
37
- // Inline fs access and dynamic require that are not supported by workerd.
38
- const fnBody = `
39
- // html
40
- ${(await Promise.all(htmlFiles.map(async (file) => `if (pagePath.endsWith("${file}")) {
41
- return ${JSON.stringify(await readFile(join(serverDir, file), "utf-8"))};
42
- }`))).join("\n")}
43
- // js
44
- process.env.__NEXT_PRIVATE_RUNTIME_TYPE = isAppPath ? 'app' : 'pages';
45
- try {
46
- ${jsFiles
47
- .map((file) => `if (pagePath.endsWith("${file}")) {
48
- return require(${JSON.stringify(join(serverDir, file))});
49
- }`)
50
- .join("\n")}
51
- } finally {
52
- process.env.__NEXT_PRIVATE_RUNTIME_TYPE = '';
53
- }
54
- `;
55
- return {
56
- rule: {
57
- pattern: `
58
- function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) {
59
- const $_ = getPagePath($$$ARGS);
60
- $$$_BODY
61
- }`,
62
- },
63
- fix: `
64
- function requirePage($PAGE, $DIST_DIR, $IS_APP_PATH) {
65
- const { platform } = require('process');
66
- const pagePath = platform === 'win32' ? getPagePath($$$ARGS).replaceAll('\\\\', '/') : getPagePath($$$ARGS);
67
- ${fnBody}
68
- }`,
69
- };
70
- }
@@ -1,7 +0,0 @@
1
- // default open-next.config.ts file created by @opennextjs/cloudflare
2
- import { defineCloudflareConfig } from "@opennextjs/cloudflare/dist/api/config";
3
- import kvIncrementalCache from "@opennextjs/cloudflare/dist/api/kv-cache";
4
-
5
- export default defineCloudflareConfig({
6
- incrementalCache: kvIncrementalCache,
7
- });