@opennextjs/cloudflare 0.0.0-5454280 → 0.0.0-698638

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.
Files changed (3) hide show
  1. package/README.md +15 -22
  2. package/dist/index.mjs +161 -54
  3. package/package.json +10 -6
package/README.md CHANGED
@@ -1,31 +1,22 @@
1
1
  # Next.js builder for Cloudflare
2
2
 
3
- ## Build your app
3
+ How to update a Next.js application to run on Cloudflare.
4
4
 
5
- - update the `next.config.mjs` as follows
5
+ ## Configure your app
6
6
 
7
- ```typescript
8
- /** @type {import('next').NextConfig} */
9
- const nextConfig = {
10
- output: "standalone",
11
- experimental: {
12
- serverMinification: false,
13
- },
14
- };
7
+ - add the following `devDependencies` to the `package.json`:
15
8
 
16
- export default nextConfig;
9
+ ```bash
10
+ pnpm add -D wrangler@latest @opennextjs/cloudflare
17
11
  ```
18
12
 
19
- - add the following `devDependency` to the `package.json`:
20
-
21
- ```json
22
- "node-url": "npm:url@^0.11.4",
23
- "wrangler": "^3.77.0"
24
- ```
13
+ ## Serve your app
25
14
 
26
- - Execute `npx @flarelabs-net/builder@latest` in your app folder
15
+ - build the app and adapt it for Cloudflare
27
16
 
28
- ## Serve your app
17
+ ```bash
18
+ pnpx cloudflare
19
+ ```
29
20
 
30
21
  - add a `wrangler.toml` at the root of your project
31
22
 
@@ -36,11 +27,13 @@
36
27
 
37
28
  compatibility_date = "2024-08-29"
38
29
  compatibility_flags = ["nodejs_compat_v2"]
39
- workers_dev = true
40
- minify = false
41
30
 
42
31
  # Use the new Workers + Assets to host the static frontend files
43
32
  experimental_assets = { directory = ".worker-next/assets", binding = "ASSETS" }
44
33
  ```
45
34
 
46
- - Use `wrangler dev`
35
+ - Preview the app in Wrangler
36
+
37
+ ```bash
38
+ pnpm wrangler dev
39
+ ```
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env node
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -24,9 +25,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
25
  mod
25
26
  ));
26
27
 
27
- // ../node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js
28
+ // ../../node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js
28
29
  var require_balanced_match = __commonJS({
29
- "../node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js"(exports, module) {
30
+ "../../node_modules/.pnpm/balanced-match@1.0.2/node_modules/balanced-match/index.js"(exports, module) {
30
31
  "use strict";
31
32
  module.exports = balanced;
32
33
  function balanced(a, b, str) {
@@ -82,9 +83,9 @@ var require_balanced_match = __commonJS({
82
83
  }
83
84
  });
84
85
 
85
- // ../node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js
86
+ // ../../node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js
86
87
  var require_brace_expansion = __commonJS({
87
- "../node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js"(exports, module) {
88
+ "../../node_modules/.pnpm/brace-expansion@2.0.1/node_modules/brace-expansion/index.js"(exports, module) {
88
89
  "use strict";
89
90
  var balanced = require_balanced_match();
90
91
  module.exports = expandTop;
@@ -283,7 +284,7 @@ function assertDirArg(path6, argName, make) {
283
284
  }
284
285
 
285
286
  // src/index.ts
286
- import { existsSync as existsSync3 } from "node:fs";
287
+ import { existsSync as existsSync4 } from "node:fs";
287
288
 
288
289
  // src/build/build.ts
289
290
  import { rm } from "node:fs/promises";
@@ -309,7 +310,7 @@ function runNextBuildCommand(packager, nextAppDir2) {
309
310
 
310
311
  // src/build/build-worker.ts
311
312
  import { build } from "esbuild";
312
- import { readdirSync as readdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "node:fs";
313
+ import { existsSync as existsSync3, readFileSync as readFileSync4 } from "node:fs";
313
314
  import { cp, readFile, writeFile } from "node:fs/promises";
314
315
 
315
316
  // src/build/patches/investigated/patch-require.ts
@@ -331,10 +332,10 @@ function copyTemplates(srcDir, nextjsAppPaths) {
331
332
  // src/build/patches/to-investigate/patch-read-file.ts
332
333
  import { readFileSync } from "node:fs";
333
334
 
334
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/index.js
335
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/index.js
335
336
  var import_brace_expansion = __toESM(require_brace_expansion(), 1);
336
337
 
337
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/assert-valid-pattern.js
338
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/assert-valid-pattern.js
338
339
  var MAX_PATTERN_LENGTH = 1024 * 64;
339
340
  var assertValidPattern = (pattern) => {
340
341
  if (typeof pattern !== "string") {
@@ -345,7 +346,7 @@ var assertValidPattern = (pattern) => {
345
346
  }
346
347
  };
347
348
 
348
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/brace-expressions.js
349
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/brace-expressions.js
349
350
  var posixClasses = {
350
351
  "[:alnum:]": ["\\p{L}\\p{Nl}\\p{Nd}", true],
351
352
  "[:alpha:]": ["\\p{L}\\p{Nl}", true],
@@ -454,12 +455,12 @@ var parseClass = (glob2, position) => {
454
455
  return [comb, uflag, endPos - pos, true];
455
456
  };
456
457
 
457
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/unescape.js
458
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/unescape.js
458
459
  var unescape = (s, { windowsPathsNoEscape = false } = {}) => {
459
460
  return windowsPathsNoEscape ? s.replace(/\[([^\/\\])\]/g, "$1") : s.replace(/((?!\\).|^)\[([^\/\\])\]/g, "$1$2").replace(/\\([^\/])/g, "$1");
460
461
  };
461
462
 
462
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/ast.js
463
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/ast.js
463
464
  var types = /* @__PURE__ */ new Set(["!", "?", "+", "*", "@"]);
464
465
  var isExtglobType = (c) => types.has(c);
465
466
  var startNoTraversal = "(?!(?:^|/)\\.\\.?(?:$|/))";
@@ -938,12 +939,12 @@ var AST = class _AST {
938
939
  }
939
940
  };
940
941
 
941
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/escape.js
942
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/escape.js
942
943
  var escape = (s, { windowsPathsNoEscape = false } = {}) => {
943
944
  return windowsPathsNoEscape ? s.replace(/[?*()[\]]/g, "[$&]") : s.replace(/[?*()[\]\\]/g, "\\$&");
944
945
  };
945
946
 
946
- // ../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/index.js
947
+ // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/index.js
947
948
  var minimatch = (p, pattern, options = {}) => {
948
949
  assertValidPattern(pattern);
949
950
  if (!options.nocomment && pattern.charAt(0) === "#") {
@@ -1661,10 +1662,10 @@ minimatch.Minimatch = Minimatch;
1661
1662
  minimatch.escape = escape;
1662
1663
  minimatch.unescape = unescape;
1663
1664
 
1664
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/glob.js
1665
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/glob.js
1665
1666
  import { fileURLToPath as fileURLToPath2 } from "node:url";
1666
1667
 
1667
- // ../node_modules/.pnpm/lru-cache@11.0.0/node_modules/lru-cache/dist/esm/index.js
1668
+ // ../../node_modules/.pnpm/lru-cache@11.0.0/node_modules/lru-cache/dist/esm/index.js
1668
1669
  var perf = typeof performance === "object" && performance && typeof performance.now === "function" ? performance : Date;
1669
1670
  var warned = /* @__PURE__ */ new Set();
1670
1671
  var PROCESS = typeof process === "object" && !!process ? process : {};
@@ -3033,14 +3034,14 @@ var LRUCache = class _LRUCache {
3033
3034
  }
3034
3035
  };
3035
3036
 
3036
- // ../node_modules/.pnpm/path-scurry@2.0.0/node_modules/path-scurry/dist/esm/index.js
3037
+ // ../../node_modules/.pnpm/path-scurry@2.0.0/node_modules/path-scurry/dist/esm/index.js
3037
3038
  import { posix, win32 } from "node:path";
3038
3039
  import { fileURLToPath } from "node:url";
3039
3040
  import { lstatSync, readdir as readdirCB, readdirSync, readlinkSync, realpathSync as rps } from "fs";
3040
3041
  import * as actualFS from "node:fs";
3041
3042
  import { lstat, readdir, readlink, realpath } from "node:fs/promises";
3042
3043
 
3043
- // ../node_modules/.pnpm/minipass@7.1.2/node_modules/minipass/dist/esm/index.js
3044
+ // ../../node_modules/.pnpm/minipass@7.1.2/node_modules/minipass/dist/esm/index.js
3044
3045
  import { EventEmitter } from "node:events";
3045
3046
  import Stream from "node:stream";
3046
3047
  import { StringDecoder } from "node:string_decoder";
@@ -3918,7 +3919,7 @@ var Minipass = class extends EventEmitter {
3918
3919
  }
3919
3920
  };
3920
3921
 
3921
- // ../node_modules/.pnpm/path-scurry@2.0.0/node_modules/path-scurry/dist/esm/index.js
3922
+ // ../../node_modules/.pnpm/path-scurry@2.0.0/node_modules/path-scurry/dist/esm/index.js
3922
3923
  var realpathSync = rps.native;
3923
3924
  var defaultFS = {
3924
3925
  lstatSync,
@@ -5644,7 +5645,7 @@ var PathScurryDarwin = class extends PathScurryPosix {
5644
5645
  var Path = process.platform === "win32" ? PathWin32 : PathPosix;
5645
5646
  var PathScurry = process.platform === "win32" ? PathScurryWin32 : process.platform === "darwin" ? PathScurryDarwin : PathScurryPosix;
5646
5647
 
5647
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/pattern.js
5648
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/pattern.js
5648
5649
  var isPatternList = (pl) => pl.length >= 1;
5649
5650
  var isGlobList = (gl) => gl.length >= 1;
5650
5651
  var Pattern = class _Pattern {
@@ -5809,7 +5810,7 @@ var Pattern = class _Pattern {
5809
5810
  }
5810
5811
  };
5811
5812
 
5812
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/ignore.js
5813
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/ignore.js
5813
5814
  var defaultPlatform2 = typeof process === "object" && process && typeof process.platform === "string" ? process.platform : "linux";
5814
5815
  var Ignore = class {
5815
5816
  relative;
@@ -5896,7 +5897,7 @@ var Ignore = class {
5896
5897
  }
5897
5898
  };
5898
5899
 
5899
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/processor.js
5900
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/processor.js
5900
5901
  var HasWalkedCache = class _HasWalkedCache {
5901
5902
  store;
5902
5903
  constructor(store = /* @__PURE__ */ new Map()) {
@@ -6117,7 +6118,7 @@ var Processor = class _Processor {
6117
6118
  }
6118
6119
  };
6119
6120
 
6120
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/walker.js
6121
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/walker.js
6121
6122
  var makeIgnore = (ignore, opts) => typeof ignore === "string" ? new Ignore([ignore], opts) : Array.isArray(ignore) ? new Ignore(ignore, opts) : ignore;
6122
6123
  var GlobUtil = class {
6123
6124
  path;
@@ -6444,7 +6445,7 @@ var GlobStream = class extends GlobUtil {
6444
6445
  }
6445
6446
  };
6446
6447
 
6447
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/glob.js
6448
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/glob.js
6448
6449
  var defaultPlatform3 = typeof process === "object" && process && typeof process.platform === "string" ? process.platform : "linux";
6449
6450
  var Glob = class {
6450
6451
  absolute;
@@ -6644,7 +6645,7 @@ var Glob = class {
6644
6645
  }
6645
6646
  };
6646
6647
 
6647
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/has-magic.js
6648
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/has-magic.js
6648
6649
  var hasMagic = (pattern, options = {}) => {
6649
6650
  if (!Array.isArray(pattern)) {
6650
6651
  pattern = [pattern];
@@ -6656,7 +6657,7 @@ var hasMagic = (pattern, options = {}) => {
6656
6657
  return false;
6657
6658
  };
6658
6659
 
6659
- // ../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/index.js
6660
+ // ../../node_modules/.pnpm/glob@11.0.0/node_modules/glob/dist/esm/index.js
6660
6661
  function globStreamSync(pattern, options = {}) {
6661
6662
  return new Glob(pattern, options).streamSync();
6662
6663
  }
@@ -6842,12 +6843,138 @@ function patchWranglerDeps(paths) {
6842
6843
  writeFileSync(tracerFile, pacthedTracer);
6843
6844
  }
6844
6845
 
6846
+ // src/build/patches/investigated/update-webpack-chunks-file/index.ts
6847
+ import { readdirSync as readdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "node:fs";
6848
+
6849
+ // src/build/patches/investigated/update-webpack-chunks-file/get-chunk-installation-identifiers.ts
6850
+ import * as ts from "ts-morph";
6851
+ async function getChunkInstallationIdentifiers(sourceFile) {
6852
+ const installChunkDeclaration = getInstallChunkDeclaration(sourceFile);
6853
+ const installedChunksDeclaration = getInstalledChunksDeclaration(sourceFile, installChunkDeclaration);
6854
+ return {
6855
+ installChunk: installChunkDeclaration.getName(),
6856
+ installedChunks: installedChunksDeclaration.getName()
6857
+ };
6858
+ }
6859
+ function getInstallChunkDeclaration(sourceFile) {
6860
+ const installChunkDeclaration = sourceFile.getDescendantsOfKind(ts.SyntaxKind.VariableDeclaration).find((declaration) => {
6861
+ const arrowFunction = declaration.getInitializerIfKind(ts.SyntaxKind.ArrowFunction);
6862
+ if (!arrowFunction) return false;
6863
+ const functionParameters = arrowFunction.getParameters();
6864
+ if (functionParameters.length !== 1) return false;
6865
+ const arrowFunctionBodyBlock = arrowFunction.getFirstChildByKind(ts.SyntaxKind.Block);
6866
+ if (!arrowFunctionBodyBlock) return false;
6867
+ const statementKinds = arrowFunctionBodyBlock.getStatements().map((statement) => statement.getKind());
6868
+ const forInStatements = statementKinds.filter((s) => s === ts.SyntaxKind.ForInStatement);
6869
+ const forStatements = statementKinds.filter((s) => s === ts.SyntaxKind.ForStatement);
6870
+ if (forInStatements.length !== 1 || forStatements.length !== 1) return false;
6871
+ const parameterName = functionParameters[0].getText();
6872
+ const functionParameterAccessedProperties = arrowFunctionBodyBlock.getDescendantsOfKind(ts.SyntaxKind.PropertyAccessExpression).filter(
6873
+ (propertyAccessExpression) => propertyAccessExpression.getExpression().getText() === parameterName
6874
+ ).map((propertyAccessExpression) => propertyAccessExpression.getName());
6875
+ if (functionParameterAccessedProperties.join(", ") !== "modules, ids, runtime") return false;
6876
+ return true;
6877
+ });
6878
+ if (!installChunkDeclaration) {
6879
+ throw new Error("ERROR: unable to find the installChunk function declaration");
6880
+ }
6881
+ return installChunkDeclaration;
6882
+ }
6883
+ function getInstalledChunksDeclaration(sourceFile, installChunkDeclaration) {
6884
+ const allVariableDeclarations = sourceFile.getDescendantsOfKind(ts.SyntaxKind.VariableDeclaration);
6885
+ const installChunkDeclarationIdx = allVariableDeclarations.findIndex(
6886
+ (declaration) => declaration === installChunkDeclaration
6887
+ );
6888
+ const installedChunksDeclaration = allVariableDeclarations[installChunkDeclarationIdx - 1];
6889
+ if (!installedChunksDeclaration?.getInitializer()?.isKind(ts.SyntaxKind.ObjectLiteralExpression)) {
6890
+ throw new Error("ERROR: unable to find the installedChunks declaration");
6891
+ }
6892
+ return installedChunksDeclaration;
6893
+ }
6894
+
6895
+ // src/build/patches/investigated/update-webpack-chunks-file/get-file-content-with-updated-webpack-f-require-code.ts
6896
+ import * as ts2 from "ts-morph";
6897
+ async function getFileContentWithUpdatedWebpackFRequireCode(sourceFile, { installedChunks, installChunk }, chunks) {
6898
+ const webpackFRequireFunction = sourceFile.getDescendantsOfKind(ts2.SyntaxKind.ArrowFunction).find((arrowFunction) => {
6899
+ const binaryExpression = arrowFunction.getFirstAncestorByKind(ts2.SyntaxKind.BinaryExpression);
6900
+ if (!binaryExpression) return false;
6901
+ const binaryExpressionLeft = binaryExpression.getLeft();
6902
+ if (!binaryExpressionLeft.getText().endsWith(".f.require")) return false;
6903
+ const binaryExpressionOperator = binaryExpression.getOperatorToken();
6904
+ if (binaryExpressionOperator.getText() !== "=") return false;
6905
+ const binaryExpressionRight = binaryExpression.getRight();
6906
+ if (binaryExpressionRight !== arrowFunction) return false;
6907
+ const arrowFunctionBody = arrowFunction.getBody();
6908
+ if (!arrowFunctionBody.isKind(ts2.SyntaxKind.Block)) return false;
6909
+ const arrowFunctionBodyText = arrowFunctionBody.getText();
6910
+ const functionUsesChunkInstallationVariables = arrowFunctionBodyText.includes(installChunk) && arrowFunctionBodyText.includes(installedChunks);
6911
+ if (!functionUsesChunkInstallationVariables) return false;
6912
+ const functionParameters = arrowFunction.getParameters();
6913
+ if (functionParameters.length !== 2) return false;
6914
+ const callsInstallChunk = arrowFunctionBody.getDescendantsOfKind(ts2.SyntaxKind.CallExpression).some((callExpression) => callExpression.getExpression().getText() === installChunk);
6915
+ if (!callsInstallChunk) return false;
6916
+ const functionFirstParameterName = functionParameters[0]?.getName();
6917
+ const accessesInstalledChunksUsingItsFirstParameter = arrowFunctionBody.getDescendantsOfKind(ts2.SyntaxKind.ElementAccessExpression).some((elementAccess) => {
6918
+ return elementAccess.getExpression().getText() === installedChunks && elementAccess.getArgumentExpression()?.getText() === functionFirstParameterName;
6919
+ });
6920
+ if (!accessesInstalledChunksUsingItsFirstParameter) return false;
6921
+ return true;
6922
+ });
6923
+ if (!webpackFRequireFunction) {
6924
+ throw new Error("ERROR: unable to find the webpack f require function declaration");
6925
+ }
6926
+ const functionParameterNames = webpackFRequireFunction.getParameters().map((parameter) => parameter.getName());
6927
+ const chunkId = functionParameterNames[0];
6928
+ const functionBody = webpackFRequireFunction.getBody();
6929
+ functionBody.insertStatements(0, [
6930
+ `if (${installedChunks}[${chunkId}]) return;`,
6931
+ ...chunks.map(
6932
+ (chunk) => `
6933
+ if(${chunkId} === ${chunk}) return ${installChunk}(require("./chunks/${chunk}.js"));`
6934
+ )
6935
+ ]);
6936
+ return sourceFile.print();
6937
+ }
6938
+
6939
+ // src/build/utils/ts-parse-file.ts
6940
+ import * as ts3 from "ts-morph";
6941
+ function tsParseFile(fileContent) {
6942
+ const project = new ts3.Project();
6943
+ const sourceFile = project.createSourceFile("file.js", fileContent);
6944
+ return sourceFile;
6945
+ }
6946
+
6947
+ // src/build/patches/investigated/update-webpack-chunks-file/get-updated-webpack-chunks-file-content.ts
6948
+ async function getUpdatedWebpackChunksFileContent(fileContent, chunks) {
6949
+ const tsSourceFile = tsParseFile(fileContent);
6950
+ const chunkInstallationIdentifiers = await getChunkInstallationIdentifiers(tsSourceFile);
6951
+ const updatedFileContent = getFileContentWithUpdatedWebpackFRequireCode(
6952
+ tsSourceFile,
6953
+ chunkInstallationIdentifiers,
6954
+ chunks
6955
+ );
6956
+ return updatedFileContent;
6957
+ }
6958
+
6959
+ // src/build/patches/investigated/update-webpack-chunks-file/index.ts
6960
+ async function updateWebpackChunksFile(nextjsAppPaths) {
6961
+ console.log("# updateWebpackChunksFile");
6962
+ const webpackRuntimeFile = `${nextjsAppPaths.standaloneAppServerDir}/webpack-runtime.js`;
6963
+ const fileContent = readFileSync3(webpackRuntimeFile, "utf-8");
6964
+ const chunks = readdirSync2(`${nextjsAppPaths.standaloneAppServerDir}/chunks`).filter((chunk) => /^\d+\.js$/.test(chunk)).map((chunk) => {
6965
+ console.log(` - chunk ${chunk}`);
6966
+ return chunk.replace(/\.js$/, "");
6967
+ });
6968
+ const updatedFileContent = await getUpdatedWebpackChunksFileContent(fileContent, chunks);
6969
+ writeFileSync2(webpackRuntimeFile, updatedFileContent);
6970
+ }
6971
+
6845
6972
  // src/build/build-worker.ts
6846
- async function buildWorker(outputDir2, nextjsAppPaths, templateSrcDir) {
6973
+ async function buildWorker(inputNextAppDir, outputDir2, nextjsAppPaths, templateSrcDir) {
6847
6974
  const templateDir = copyTemplates(templateSrcDir, nextjsAppPaths);
6848
6975
  const workerEntrypoint = `${templateDir}/worker.ts`;
6849
6976
  const workerOutputFile = `${outputDir2}/index.mjs`;
6850
- const nextConfigStr = readFileSync3(nextjsAppPaths.standaloneAppDir + "/server.js", "utf8")?.match(
6977
+ const nextConfigStr = readFileSync4(nextjsAppPaths.standaloneAppDir + "/server.js", "utf8")?.match(
6851
6978
  /const nextConfig = ({.+?})\n/
6852
6979
  )?.[1] ?? {};
6853
6980
  console.log(`\x1B[35m\u2699\uFE0F Bundling the worker file...
@@ -6941,6 +7068,11 @@ Request = globalThis.Request;
6941
7068
  await cp(`${nextjsAppPaths.dotNextDir}/static`, `${outputDir2}/assets/_next/static`, {
6942
7069
  recursive: true
6943
7070
  });
7071
+ if (existsSync3(`${inputNextAppDir}/public`)) {
7072
+ await cp(`${inputNextAppDir}/public`, `${outputDir2}/assets`, {
7073
+ recursive: true
7074
+ });
7075
+ }
6944
7076
  console.log(`\x1B[35mWorker saved in \`${workerOutputFile}\` \u{1F680}
6945
7077
  \x1B[0m`);
6946
7078
  }
@@ -6954,31 +7086,6 @@ async function updateWorkerBundledCode(workerOutputFile, nextjsAppPaths) {
6954
7086
  patchedCode = inlineEvalManifest(patchedCode, nextjsAppPaths);
6955
7087
  await writeFile(workerOutputFile, patchedCode);
6956
7088
  }
6957
- async function updateWebpackChunksFile(nextjsAppPaths) {
6958
- console.log("# updateWebpackChunksFile");
6959
- const webpackRuntimeFile = `${nextjsAppPaths.standaloneAppServerDir}/webpack-runtime.js`;
6960
- console.log({ webpackRuntimeFile });
6961
- const fileContent = readFileSync3(webpackRuntimeFile, "utf-8");
6962
- const chunks = readdirSync2(`${nextjsAppPaths.standaloneAppServerDir}/chunks`).filter((chunk) => /^\d+\.js$/.test(chunk)).map((chunk) => {
6963
- console.log(` - chunk ${chunk}`);
6964
- return chunk.replace(/\.js$/, "");
6965
- });
6966
- const updatedFileContent = fileContent.replace(
6967
- "__webpack_require__.f.require = (chunkId, promises) => {",
6968
- `__webpack_require__.f.require = (chunkId, promises) => {
6969
- if (installedChunks[chunkId]) return;
6970
- ${chunks.map(
6971
- (chunk) => `
6972
- if (chunkId === ${chunk}) {
6973
- installChunk(require("./chunks/${chunk}.js"));
6974
- return;
6975
- }
6976
- `
6977
- ).join("\n")}
6978
- `
6979
- );
6980
- writeFileSync2(webpackRuntimeFile, updatedFileContent);
6981
- }
6982
7089
  function createFixRequiresESBuildPlugin(templateDir) {
6983
7090
  return {
6984
7091
  name: "replaceRelative",
@@ -7068,7 +7175,7 @@ async function build2(inputNextAppDir, opts) {
7068
7175
  await cleanDirectory(outputDir2);
7069
7176
  const nextjsAppPaths = getNextjsAppPaths(inputNextAppDir);
7070
7177
  const templateDir = path5.join(path5.dirname(fileURLToPath3(import.meta.url)), "templates");
7071
- await buildWorker(outputDir2, nextjsAppPaths, templateDir);
7178
+ await buildWorker(inputNextAppDir, outputDir2, nextjsAppPaths, templateDir);
7072
7179
  }
7073
7180
  async function cleanDirectory(path6) {
7074
7181
  return await rm(path6, { recursive: true, force: true });
@@ -7077,7 +7184,7 @@ async function cleanDirectory(path6) {
7077
7184
  // src/index.ts
7078
7185
  var nextAppDir = resolve2(".");
7079
7186
  console.log(`Building the Next.js app in the current folder (${nextAppDir})`);
7080
- if (!["js", "cjs", "mjs", "ts"].some((ext2) => existsSync3(`./next.config.${ext2}`))) {
7187
+ if (!["js", "cjs", "mjs", "ts"].some((ext2) => existsSync4(`./next.config.${ext2}`))) {
7081
7188
  throw new Error("Error: Not in a Next.js app project");
7082
7189
  }
7083
7190
  var { skipBuild, outputDir } = getArgs();
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.0.0-5454280",
4
+ "version": "0.0.0-0698638",
5
5
  "bin": "dist/index.mjs",
6
6
  "files": [
7
7
  "README.md",
@@ -10,7 +10,7 @@
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "https://github.com/flarelabs-net/poc-next.git",
13
- "directory": "builder"
13
+ "directory": "packages/cloudflare"
14
14
  },
15
15
  "keywords": [
16
16
  "cloudflare",
@@ -23,16 +23,20 @@
23
23
  },
24
24
  "homepage": "https://github.com/flarelabs-net/poc-next",
25
25
  "devDependencies": {
26
- "@cloudflare/workers-types": "^4.20240909.0",
27
26
  "@types/node": "^22.2.0",
28
27
  "esbuild": "^0.23.0",
29
28
  "glob": "^11.0.0",
30
- "next": "14.2.5",
31
29
  "tsup": "^8.2.4",
32
- "typescript": "^5.5.4"
30
+ "typescript": "^5.5.4",
31
+ "vitest": "^2.1.1"
32
+ },
33
+ "dependencies": {
34
+ "ts-morph": "^23.0.0"
33
35
  },
34
36
  "scripts": {
35
37
  "build": "tsup",
36
- "build:watch": "tsup --watch src"
38
+ "build:watch": "tsup --watch src",
39
+ "test": "vitest --run",
40
+ "test:watch": "vitest"
37
41
  }
38
42
  }