@opennextjs/cloudflare 0.5.10 → 0.5.12

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.
@@ -7,6 +7,7 @@
7
7
  *
8
8
  * This patch unrolls the dynamic require for all the existing chunks:
9
9
  *
10
+ * For multiple chunks:
10
11
  * switch (chunkId) {
11
12
  * case ID1: installChunk(require("./chunks/ID1"); break;
12
13
  * case ID2: installChunk(require("./chunks/ID2"); break;
@@ -14,10 +15,15 @@
14
15
  * case SELF_ID: installedChunks[chunkId] = 1; break;
15
16
  * default: throw new Error(`Unknown chunk ${chunkId}`);
16
17
  * }
18
+ *
19
+ * For a single chunk:
20
+ * require("./chunks/CHUNK_ID.js");
17
21
  */
18
22
  import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
19
- export declare function buildInlineChunksRule(chunks: number[]): string;
23
+ export declare function buildMultipleChunksRule(chunks: number[]): string;
24
+ export declare const singleChunkRule = "\nrule:\n pattern: ($CHUNK_ID, $_PROMISES) => { $$$ }\n inside: {pattern: $_.$_.require = $$$_, stopBy: end}\n all:\n - has: {pattern: $INSTALL(require(\"./chunks/\" + $$$)), stopBy: end}\n - has: {pattern: $SELF_ID == $CHUNK_ID, stopBy: end}\n - has: {pattern: \"$INSTALLED_CHUNK[$CHUNK_ID] = 1\", stopBy: end}\nfix: |\n ($CHUNK_ID, _) => {\n if (!$INSTALLED_CHUNK[$CHUNK_ID]) {\n try {\n $INSTALL(require(\"./chunks/$SELF_ID.js\"));\n } catch {}\n }\n }\n";
20
25
  /**
21
- * Fixes the webpack-runtime.js file by removing its webpack dynamic requires.
26
+ * Fixes the webpack-runtime.js and webpack-api-runtime.js files by inlining
27
+ * the webpack dynamic requires.
22
28
  */
23
29
  export declare function patchWebpackRuntime(buildOpts: BuildOptions): Promise<void>;
@@ -7,6 +7,7 @@
7
7
  *
8
8
  * This patch unrolls the dynamic require for all the existing chunks:
9
9
  *
10
+ * For multiple chunks:
10
11
  * switch (chunkId) {
11
12
  * case ID1: installChunk(require("./chunks/ID1"); break;
12
13
  * case ID2: installChunk(require("./chunks/ID2"); break;
@@ -14,12 +15,16 @@
14
15
  * case SELF_ID: installedChunks[chunkId] = 1; break;
15
16
  * default: throw new Error(`Unknown chunk ${chunkId}`);
16
17
  * }
18
+ *
19
+ * For a single chunk:
20
+ * require("./chunks/CHUNK_ID.js");
17
21
  */
18
- import { readdirSync, readFileSync, writeFileSync } from "node:fs";
22
+ import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
19
23
  import { join } from "node:path";
20
24
  import { getPackagePath } from "@opennextjs/aws/build/helper.js";
21
25
  import { patchCode } from "./util.js";
22
- export function buildInlineChunksRule(chunks) {
26
+ // Inline the code when there are multiple chunks
27
+ export function buildMultipleChunksRule(chunks) {
23
28
  return `
24
29
  rule:
25
30
  pattern: ($CHUNK_ID, $_PROMISES) => { $$$ }
@@ -39,19 +44,53 @@ ${chunks.map((chunk) => ` case ${chunk}: $INSTALL(require("./chunks/${ch
39
44
  }
40
45
  }`;
41
46
  }
47
+ // Inline the code when there is a single chunk.
48
+ // For example when there is a single Pages API route.
49
+ // Note: The chunk does not always exist which explain the need for the try...catch.
50
+ export const singleChunkRule = `
51
+ rule:
52
+ pattern: ($CHUNK_ID, $_PROMISES) => { $$$ }
53
+ inside: {pattern: $_.$_.require = $$$_, stopBy: end}
54
+ all:
55
+ - has: {pattern: $INSTALL(require("./chunks/" + $$$)), stopBy: end}
56
+ - has: {pattern: $SELF_ID == $CHUNK_ID, stopBy: end}
57
+ - has: {pattern: "$INSTALLED_CHUNK[$CHUNK_ID] = 1", stopBy: end}
58
+ fix: |
59
+ ($CHUNK_ID, _) => {
60
+ if (!$INSTALLED_CHUNK[$CHUNK_ID]) {
61
+ try {
62
+ $INSTALL(require("./chunks/$SELF_ID.js"));
63
+ } catch {}
64
+ }
65
+ }
66
+ `;
42
67
  /**
43
- * Fixes the webpack-runtime.js file by removing its webpack dynamic requires.
68
+ * Fixes the webpack-runtime.js and webpack-api-runtime.js files by inlining
69
+ * the webpack dynamic requires.
44
70
  */
45
71
  export async function patchWebpackRuntime(buildOpts) {
46
72
  const { outputDir } = buildOpts;
47
73
  const dotNextServerDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");
48
- const runtimeFile = join(dotNextServerDir, "webpack-runtime.js");
49
- const runtimeCode = readFileSync(runtimeFile, "utf-8");
50
74
  // Look for all the chunks.
51
75
  const chunks = readdirSync(join(dotNextServerDir, "chunks"))
52
76
  .filter((chunk) => /^\d+\.js$/.test(chunk))
53
77
  .map((chunk) => {
54
78
  return Number(chunk.replace(/\.js$/, ""));
55
79
  });
56
- writeFileSync(runtimeFile, patchCode(runtimeCode, buildInlineChunksRule(chunks)));
80
+ patchFile(join(dotNextServerDir, "webpack-runtime.js"), chunks);
81
+ patchFile(join(dotNextServerDir, "webpack-api-runtime.js"), chunks);
82
+ }
83
+ /**
84
+ * Inline chunks when the file exists.
85
+ *
86
+ * @param filename Path to the webpack runtime.
87
+ * @param chunks List of chunks in the chunks folder.
88
+ */
89
+ function patchFile(filename, chunks) {
90
+ if (existsSync(filename)) {
91
+ let code = readFileSync(filename, "utf-8");
92
+ code = patchCode(code, buildMultipleChunksRule(chunks));
93
+ code = patchCode(code, singleChunkRule);
94
+ writeFileSync(filename, code);
95
+ }
57
96
  }
@@ -1,57 +1,102 @@
1
1
  import { describe, expect, test } from "vitest";
2
2
  import { patchCode } from "./util.js";
3
- import { buildInlineChunksRule } from "./webpack-runtime.js";
3
+ import { buildMultipleChunksRule, singleChunkRule } from "./webpack-runtime.js";
4
4
  describe("webpack runtime", () => {
5
- test("patch runtime", () => {
6
- const code = `
7
- /******/ // require() chunk loading for javascript
8
- /******/ __webpack_require__.f.require = (chunkId, promises) => {
9
- /******/ // "1" is the signal for "already loaded"
10
- /******/ if (!installedChunks[chunkId]) {
11
- /******/ if (658 != chunkId) {
12
- /******/ installChunk(require("./chunks/" + __webpack_require__.u(chunkId)));
13
- /******/
14
- } else installedChunks[chunkId] = 1;
15
- /******/
5
+ describe("multiple chunks", () => {
6
+ test("patch runtime", () => {
7
+ const code = `
8
+ /******/ // require() chunk loading for javascript
9
+ /******/ __webpack_require__.f.require = (chunkId, promises) => {
10
+ /******/ // "1" is the signal for "already loaded"
11
+ /******/ if (!installedChunks[chunkId]) {
12
+ /******/ if (658 != chunkId) {
13
+ /******/ installChunk(require("./chunks/" + __webpack_require__.u(chunkId)));
14
+ /******/
15
+ } else installedChunks[chunkId] = 1;
16
+ /******/
17
+ }
18
+ /******/
19
+ };
20
+ `;
21
+ expect(patchCode(code, buildMultipleChunksRule([1, 2, 3]))).toMatchInlineSnapshot(`
22
+ "/******/ // require() chunk loading for javascript
23
+ /******/ __webpack_require__.f.require = (chunkId, _) => {
24
+ if (!installedChunks[chunkId]) {
25
+ switch (chunkId) {
26
+ case 1: installChunk(require("./chunks/1.js")); break;
27
+ case 2: installChunk(require("./chunks/2.js")); break;
28
+ case 3: installChunk(require("./chunks/3.js")); break;
29
+ case 658: installedChunks[chunkId] = 1; break;
30
+ default: throw new Error(\`Unknown chunk \${chunkId}\`);
16
31
  }
17
- /******/
18
- };
19
- `;
20
- expect(patchCode(code, buildInlineChunksRule([1, 2, 3]))).toMatchInlineSnapshot(`
21
- "/******/ // require() chunk loading for javascript
22
- /******/ __webpack_require__.f.require = (chunkId, _) => {
23
- if (!installedChunks[chunkId]) {
24
- switch (chunkId) {
25
- case 1: installChunk(require("./chunks/1.js")); break;
26
- case 2: installChunk(require("./chunks/2.js")); break;
27
- case 3: installChunk(require("./chunks/3.js")); break;
28
- case 658: installedChunks[chunkId] = 1; break;
29
- default: throw new Error(\`Unknown chunk \${chunkId}\`);
30
32
  }
31
33
  }
32
- }
33
- ;
34
- "
35
- `);
34
+ ;
35
+ "
36
+ `);
37
+ });
38
+ test("patch minified runtime", () => {
39
+ const code = `
40
+ t.f.require=(o,n)=>{e[o]||(658!=o?r(require("./chunks/"+t.u(o))):e[o]=1)}
41
+ `;
42
+ expect(patchCode(code, buildMultipleChunksRule([1, 2, 3]))).toMatchInlineSnapshot(`
43
+ "t.f.require=(o, _) => {
44
+ if (!e[o]) {
45
+ switch (o) {
46
+ case 1: r(require("./chunks/1.js")); break;
47
+ case 2: r(require("./chunks/2.js")); break;
48
+ case 3: r(require("./chunks/3.js")); break;
49
+ case 658: e[o] = 1; break;
50
+ default: throw new Error(\`Unknown chunk \${o}\`);
51
+ }
52
+ }
53
+ }
54
+
55
+ "
56
+ `);
57
+ });
36
58
  });
37
- test("patch minified runtime", () => {
38
- const code = `
39
- t.f.require=(o,n)=>{e[o]||(658!=o?r(require("./chunks/"+t.u(o))):e[o]=1)}
40
- `;
41
- expect(patchCode(code, buildInlineChunksRule([1, 2, 3]))).toMatchInlineSnapshot(`
42
- "t.f.require=(o, _) => {
43
- if (!e[o]) {
44
- switch (o) {
45
- case 1: r(require("./chunks/1.js")); break;
46
- case 2: r(require("./chunks/2.js")); break;
47
- case 3: r(require("./chunks/3.js")); break;
48
- case 658: e[o] = 1; break;
49
- default: throw new Error(\`Unknown chunk \${o}\`);
59
+ describe("single chunk", () => {
60
+ test("patch runtime", () => {
61
+ const code = `
62
+ /******/ // require() chunk loading for javascript
63
+ /******/ __webpack_require__.f.require = (chunkId, promises) => {
64
+ /******/ // "1" is the signal for "already loaded"
65
+ /******/ if(!installedChunks[chunkId]) {
66
+ /******/ if(710 == chunkId) {
67
+ /******/ installChunk(require("./chunks/" + __webpack_require__.u(chunkId)));
68
+ /******/ } else installedChunks[chunkId] = 1;
69
+ /******/ }
70
+ /******/ };
71
+ `;
72
+ expect(patchCode(code, singleChunkRule)).toMatchInlineSnapshot(`
73
+ "/******/ // require() chunk loading for javascript
74
+ /******/ __webpack_require__.f.require = (chunkId, _) => {
75
+ if (!installedChunks[chunkId]) {
76
+ try {
77
+ installChunk(require("./chunks/710.js"));
78
+ } catch {}
79
+ }
80
+ }
81
+ ;
82
+ "
83
+ `);
84
+ });
85
+ test("patch minified runtime", () => {
86
+ const code = `
87
+ o.f.require=(t,a)=>{e[t]||(710==t?r(require("./chunks/"+o.u(t))):e[t]=1)}
88
+ `;
89
+ expect(patchCode(code, singleChunkRule)).toMatchInlineSnapshot(`
90
+ "o.f.require=(t, _) => {
91
+ if (!e[t]) {
92
+ try {
93
+ r(require("./chunks/710.js"));
94
+ } catch {}
50
95
  }
51
96
  }
52
- }
53
97
 
54
- "
55
- `);
98
+ "
99
+ `);
100
+ });
56
101
  });
57
102
  });
@@ -36,7 +36,9 @@ function getRequires(idVariable, files, serverDir) {
36
36
  }
37
37
  export function inlineDynamicRequires(updater, buildOpts) {
38
38
  updater.updateContent("inline-node-module-loader", {
39
- filter: getCrossPlatformPathRegex(String.raw `/next/dist/server/lib/module-loader/node-module-loader\.js$`, { escape: false }),
39
+ filter: getCrossPlatformPathRegex(String.raw `/module-loader/node-module-loader\.js$`, {
40
+ escape: false,
41
+ }),
40
42
  contentFilter: /class NodeModuleLoader {/,
41
43
  }, async ({ contents }) => patchCode(contents, await getNodeModuleLoaderRule(buildOpts)));
42
44
  updater.updateContent("inline-require-page", {
@@ -1,15 +1,10 @@
1
1
  import type { ProjectOptions } from "../../project-options.js";
2
2
  /**
3
- * Creates a `wrangler.json` file for the user if a wrangler config file doesn't already exist,
3
+ * Creates a `wrangler.jsonc` file for the user if a wrangler config file doesn't already exist,
4
4
  * but only after asking for the user's confirmation.
5
5
  *
6
6
  * If the user refuses a warning is shown (which offers ways to opt out of this check to the user).
7
7
  *
8
- * Note: we generate a wrangler.json file with comments instead of using the jsonc extension,
9
- * we decided to do that since json is more common than jsonc, wrangler also parses
10
- * them in the same way and we also expect developers to associate `wrangler.json`
11
- * files to the jsonc language
12
- *
13
8
  * @param projectOpts The options for the project
14
9
  */
15
10
  export declare function createWranglerConfigIfNotExistent(projectOpts: ProjectOptions): Promise<void>;
@@ -3,16 +3,11 @@ import { join } from "node:path";
3
3
  import { getPackageTemplatesDirPath } from "../../../utils/get-package-templates-dir-path.js";
4
4
  import { askConfirmation } from "../../utils/ask-confirmation.js";
5
5
  /**
6
- * Creates a `wrangler.json` file for the user if a wrangler config file doesn't already exist,
6
+ * Creates a `wrangler.jsonc` file for the user if a wrangler config file doesn't already exist,
7
7
  * but only after asking for the user's confirmation.
8
8
  *
9
9
  * If the user refuses a warning is shown (which offers ways to opt out of this check to the user).
10
10
  *
11
- * Note: we generate a wrangler.json file with comments instead of using the jsonc extension,
12
- * we decided to do that since json is more common than jsonc, wrangler also parses
13
- * them in the same way and we also expect developers to associate `wrangler.json`
14
- * files to the jsonc language
15
- *
16
11
  * @param projectOpts The options for the project
17
12
  */
18
13
  export async function createWranglerConfigIfNotExistent(projectOpts) {
@@ -28,7 +23,7 @@ export async function createWranglerConfigIfNotExistent(projectOpts) {
28
23
  "(to avoid this check use the `--skipWranglerConfigCheck` flag or set a `SKIP_WRANGLER_CONFIG_CHECK` environment variable to `yes`)");
29
24
  return;
30
25
  }
31
- let wranglerConfig = readFileSync(join(getPackageTemplatesDirPath(), "wrangler.json"), "utf8");
26
+ let wranglerConfig = readFileSync(join(getPackageTemplatesDirPath(), "wrangler.jsonc"), "utf8");
32
27
  const appName = getAppNameFromPackageJson(projectOpts.sourceDir) ?? "app-name";
33
28
  if (appName) {
34
29
  wranglerConfig = wranglerConfig.replace('"app-name"', JSON.stringify(appName.replaceAll("_", "-")));
@@ -37,7 +32,7 @@ export async function createWranglerConfigIfNotExistent(projectOpts) {
37
32
  if (compatDate) {
38
33
  wranglerConfig = wranglerConfig.replace(/"compatibility_date": "\d{4}-\d{2}-\d{2}"/, `"compatibility_date": ${JSON.stringify(compatDate)}`);
39
34
  }
40
- writeFileSync(join(projectOpts.sourceDir, "wrangler.json"), wranglerConfig);
35
+ writeFileSync(join(projectOpts.sourceDir, "wrangler.jsonc"), wranglerConfig);
41
36
  }
42
37
  function getAppNameFromPackageJson(sourceDir) {
43
38
  try {
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.10",
4
+ "version": "0.5.12",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "opennextjs-cloudflare": "dist/cli/index.js"
@@ -54,22 +54,22 @@
54
54
  "eslint-plugin-unicorn": "^55.0.0",
55
55
  "globals": "^15.9.0",
56
56
  "mock-fs": "^5.4.1",
57
- "next": "14.2.11",
57
+ "next": "~14.2.24",
58
58
  "rimraf": "^6.0.1",
59
59
  "typescript": "^5.7.3",
60
60
  "typescript-eslint": "^8.7.0",
61
61
  "vitest": "^2.1.1"
62
62
  },
63
63
  "dependencies": {
64
- "@ast-grep/napi": "^0.34.1",
64
+ "@ast-grep/napi": "^0.36.1",
65
65
  "@dotenvx/dotenvx": "1.31.0",
66
- "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@756",
66
+ "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@7e23eee",
67
67
  "enquirer": "^2.4.1",
68
68
  "glob": "^11.0.0",
69
69
  "yaml": "^2.7.0"
70
70
  },
71
71
  "peerDependencies": {
72
- "wrangler": "^3.111.0"
72
+ "wrangler": "^3.114.1 || ^4.0.0"
73
73
  },
74
74
  "scripts": {
75
75
  "clean": "rimraf dist",
File without changes