@opennextjs/cloudflare 0.1.1 → 0.2.1

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 (2) hide show
  1. package/dist/cli/index.mjs +154 -146
  2. package/package.json +1 -1
@@ -235,25 +235,29 @@ var require_brace_expansion = __commonJS({
235
235
  });
236
236
 
237
237
  // src/cli/config.ts
238
- import path, { relative } from "node:path";
238
+ import { join, relative } from "node:path";
239
239
  import { readdirSync, statSync } from "node:fs";
240
240
  var PACKAGE_NAME = "@opennextjs/cloudflare";
241
- function getConfig(appDir, outputDir2) {
242
- const dotNext = path.join(outputDir2, ".next");
241
+ function getConfig(projectOpts) {
242
+ const dotNext = join(projectOpts.outputDir, ".next");
243
243
  const appPath = getNextjsApplicationPath(dotNext).replace(/\/$/, "");
244
- const standaloneRoot = path.join(dotNext, "standalone");
245
- const standaloneApp = path.join(standaloneRoot, appPath);
246
- const standaloneAppDotNext = path.join(standaloneApp, ".next");
247
- const standaloneAppServer = path.join(standaloneAppDotNext, "server");
248
- const nodeModules = path.join(standaloneApp, "node_modules");
249
- const internalPackage = path.join(nodeModules, ...PACKAGE_NAME.split("/"));
250
- const internalTemplates = path.join(internalPackage, "cli", "templates");
244
+ const standaloneRoot = join(dotNext, "standalone");
245
+ const standaloneApp = join(standaloneRoot, appPath);
246
+ const standaloneAppDotNext = join(standaloneApp, ".next");
247
+ const standaloneAppServer = join(standaloneAppDotNext, "server");
248
+ const nodeModules = join(standaloneApp, "node_modules");
249
+ const internalPackage = join(nodeModules, ...PACKAGE_NAME.split("/"));
250
+ const internalTemplates = join(internalPackage, "cli", "templates");
251
251
  process.env.__OPENNEXT_KV_BINDING_NAME ??= "NEXT_CACHE_WORKERS_KV";
252
252
  return {
253
- buildTimestamp: Date.now(),
253
+ build: {
254
+ timestamp: Date.now(),
255
+ skipNextBuild: projectOpts.skipNextBuild,
256
+ shouldMinify: projectOpts.minify
257
+ },
254
258
  paths: {
255
- nextApp: appDir,
256
- builderOutput: outputDir2,
259
+ sourceDir: projectOpts.sourceDir,
260
+ outputDir: projectOpts.outputDir,
257
261
  dotNext,
258
262
  standaloneRoot,
259
263
  standaloneApp,
@@ -270,7 +274,7 @@ function getConfig(appDir, outputDir2) {
270
274
  }
271
275
  function containsDotNextDir(folder) {
272
276
  try {
273
- return statSync(path.join(folder, ".next")).isDirectory();
277
+ return statSync(join(folder, ".next")).isDirectory();
274
278
  } catch {
275
279
  return false;
276
280
  }
@@ -280,19 +284,19 @@ function getNextjsApplicationPath(dotNextDir) {
280
284
  if (!serverPath) {
281
285
  throw new Error(`Unexpected Error: no \`.next/server\` folder could be found in \`${serverPath}\``);
282
286
  }
283
- return relative(path.join(dotNextDir, "standalone"), serverPath);
287
+ return relative(join(dotNextDir, "standalone"), serverPath);
284
288
  }
285
289
  function findServerParentPath(parentPath) {
286
290
  try {
287
- if (statSync(path.join(parentPath, ".next", "server")).isDirectory()) {
291
+ if (statSync(join(parentPath, ".next", "server")).isDirectory()) {
288
292
  return parentPath;
289
293
  }
290
294
  } catch {
291
295
  }
292
296
  const folders = readdirSync(parentPath);
293
297
  for (const folder of folders) {
294
- const subFolder = path.join(parentPath, folder);
295
- if (statSync(path.join(parentPath, folder)).isDirectory()) {
298
+ const subFolder = join(parentPath, folder);
299
+ if (statSync(join(parentPath, folder)).isDirectory()) {
296
300
  const dirServerPath = findServerParentPath(subFolder);
297
301
  if (dirServerPath) {
298
302
  return dirServerPath;
@@ -321,32 +325,32 @@ var LOCKS = {
321
325
  // ../../node_modules/.pnpm/package-manager-detector@0.2.0/node_modules/package-manager-detector/dist/detect.mjs
322
326
  import fs from "node:fs";
323
327
  import fsPromises from "node:fs/promises";
324
- import path2 from "node:path";
328
+ import path from "node:path";
325
329
  import process2 from "node:process";
326
330
  async function detect({ cwd, onUnknown } = {}) {
327
331
  for (const directory of lookup(cwd)) {
328
332
  for (const lock of Object.keys(LOCKS)) {
329
- if (await fileExists(path2.join(directory, lock))) {
333
+ if (await fileExists(path.join(directory, lock))) {
330
334
  const name = LOCKS[lock];
331
- const result2 = await parsePackageJson(path2.join(directory, "package.json"), onUnknown);
335
+ const result2 = await parsePackageJson(path.join(directory, "package.json"), onUnknown);
332
336
  if (result2)
333
337
  return result2;
334
338
  else
335
339
  return { name, agent: name };
336
340
  }
337
341
  }
338
- const result = await parsePackageJson(path2.join(directory, "package.json"), onUnknown);
342
+ const result = await parsePackageJson(path.join(directory, "package.json"), onUnknown);
339
343
  if (result)
340
344
  return result;
341
345
  }
342
346
  return null;
343
347
  }
344
348
  function* lookup(cwd = process2.cwd()) {
345
- let directory = path2.resolve(cwd);
346
- const { root } = path2.parse(directory);
349
+ let directory = path.resolve(cwd);
350
+ const { root } = path.parse(directory);
347
351
  while (directory && directory !== root) {
348
352
  yield directory;
349
- directory = path2.dirname(directory);
353
+ directory = path.dirname(directory);
350
354
  }
351
355
  }
352
356
  async function parsePackageJson(filepath, onUnknown) {
@@ -403,7 +407,7 @@ function runNextBuildCommand(packager, nextAppDir2) {
403
407
  cwd: nextAppDir2,
404
408
  env: {
405
409
  ...process.env,
406
- // equivalent to: https://github.com/sst/open-next/blob/f61b0e9/packages/open-next/src/build.ts#L168-L173
410
+ // equivalent to: https://github.com/opennextjs/opennextjs-aws/blob/f61b0e9/packages/open-next/src/build.ts#L168-L173
407
411
  // Equivalent to setting `output: "standalone"` in next.config.js
408
412
  NEXT_PRIVATE_STANDALONE: "true"
409
413
  }
@@ -413,15 +417,16 @@ function runNextBuildCommand(packager, nextAppDir2) {
413
417
  // src/cli/build/build-worker.ts
414
418
  import { build as build2 } from "esbuild";
415
419
  import { cp, readFile, writeFile } from "node:fs/promises";
420
+ import { dirname as dirname2, join as join13 } from "node:path";
416
421
  import { existsSync as existsSync5, readFileSync as readFileSync7 } from "node:fs";
417
422
 
418
423
  // src/cli/build/patches/investigated/copy-package-cli-files.ts
419
424
  import { cpSync } from "node:fs";
420
- import path3 from "node:path";
425
+ import { join as join2 } from "node:path";
421
426
  function copyPackageCliFiles(packageDistDir2, config) {
422
427
  console.log("# copyPackageTemplateFiles");
423
- const sourceDir = path3.join(packageDistDir2, "cli");
424
- const destinationDir = path3.join(config.paths.internalPackage, "cli");
428
+ const sourceDir = join2(packageDistDir2, "cli");
429
+ const destinationDir = join2(config.paths.internalPackage, "cli");
425
430
  cpSync(sourceDir, destinationDir, { recursive: true });
426
431
  }
427
432
 
@@ -439,16 +444,16 @@ var SEED_DATA_DIR = "cdn-cgi/_cf_seed_data";
439
444
 
440
445
  // src/cli/build/utils/copy-prerendered-routes.ts
441
446
  import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
442
- import { dirname, join as join2 } from "node:path";
447
+ import { dirname, join as join4 } from "node:path";
443
448
 
444
449
  // src/cli/build/utils/read-paths-recursively.ts
445
- import { join } from "node:path";
450
+ import { join as join3 } from "node:path";
446
451
  import { readdirSync as readdirSync2 } from "node:fs";
447
452
  function readPathsRecursively(dir) {
448
453
  try {
449
454
  const files = readdirSync2(dir, { withFileTypes: true });
450
455
  return files.flatMap((file) => {
451
- const filePath = join(dir, file.name);
456
+ const filePath = join3(dir, file.name);
452
457
  return file.isDirectory() ? readPathsRecursively(filePath) : filePath;
453
458
  });
454
459
  } catch {
@@ -459,29 +464,38 @@ function readPathsRecursively(dir) {
459
464
  // src/cli/build/utils/copy-prerendered-routes.ts
460
465
  function copyPrerenderedRoutes(config) {
461
466
  console.log("# copyPrerenderedRoutes");
462
- const serverAppDirPath = join2(config.paths.standaloneAppServer, "app");
463
- const prerenderManifestPath = join2(config.paths.standaloneAppDotNext, "prerender-manifest.json");
464
- const outputPath = join2(config.paths.builderOutput, "assets", SEED_DATA_DIR);
467
+ const serverAppDirPath = join4(config.paths.standaloneAppServer, "app");
468
+ const prerenderManifestPath = join4(config.paths.standaloneAppDotNext, "prerender-manifest.json");
469
+ const outputPath = join4(config.paths.outputDir, "assets", SEED_DATA_DIR);
465
470
  const prerenderManifest = existsSync(prerenderManifestPath) ? JSON.parse(readFileSync(prerenderManifestPath, "utf8")) : {};
466
471
  const prerenderedRoutes = Object.keys(prerenderManifest.routes);
467
472
  const prerenderedAssets = readPathsRecursively(serverAppDirPath).map((fullPath) => ({ fullPath, relativePath: fullPath.replace(serverAppDirPath, "") })).filter(
468
473
  ({ relativePath }) => prerenderedRoutes.includes(relativePath.replace(/\.\w+$/, "").replace(/^\/index$/, "/"))
469
474
  );
470
475
  prerenderedAssets.forEach(({ fullPath, relativePath }) => {
471
- const destPath = join2(outputPath, relativePath);
476
+ const destPath = join4(outputPath, relativePath);
472
477
  mkdirSync(dirname(destPath), { recursive: true });
473
478
  if (fullPath.endsWith(NEXT_META_SUFFIX)) {
474
479
  const data = JSON.parse(readFileSync(fullPath, "utf8"));
475
- writeFileSync(destPath, JSON.stringify({ ...data, lastModified: config.buildTimestamp }));
480
+ writeFileSync(destPath, JSON.stringify({ ...data, lastModified: config.build.timestamp }));
476
481
  } else {
477
482
  copyFileSync(fullPath, destPath);
478
483
  }
479
484
  });
480
485
  }
481
486
 
487
+ // src/cli/build/utils/normalize-path.ts
488
+ import { posix, sep } from "node:path";
489
+ function normalizePath(path3) {
490
+ return path3.replaceAll(sep, posix.sep);
491
+ }
492
+
482
493
  // src/cli/build/build-worker.ts
483
494
  import { fileURLToPath as fileURLToPath3 } from "node:url";
484
495
 
496
+ // src/cli/build/patches/to-investigate/inline-eval-manifest.ts
497
+ import { join as join5, posix as posix3 } from "node:path";
498
+
485
499
  // ../../node_modules/.pnpm/minimatch@10.0.1/node_modules/minimatch/dist/esm/index.js
486
500
  var import_brace_expansion = __toESM(require_brace_expansion(), 1);
487
501
 
@@ -1153,12 +1167,12 @@ var qmarksTestNoExtDot = ([$0]) => {
1153
1167
  return (f) => f.length === len && f !== "." && f !== "..";
1154
1168
  };
1155
1169
  var defaultPlatform = typeof process === "object" && process ? typeof process.env === "object" && process.env && process.env.__MINIMATCH_TESTING_PLATFORM__ || process.platform : "posix";
1156
- var path4 = {
1170
+ var path2 = {
1157
1171
  win32: { sep: "\\" },
1158
1172
  posix: { sep: "/" }
1159
1173
  };
1160
- var sep = defaultPlatform === "win32" ? path4.win32.sep : path4.posix.sep;
1161
- minimatch.sep = sep;
1174
+ var sep2 = defaultPlatform === "win32" ? path2.win32.sep : path2.posix.sep;
1175
+ minimatch.sep = sep2;
1162
1176
  var GLOBSTAR = Symbol("globstar **");
1163
1177
  minimatch.GLOBSTAR = GLOBSTAR;
1164
1178
  var qmark2 = "[^/]";
@@ -3185,7 +3199,7 @@ var LRUCache = class _LRUCache {
3185
3199
  };
3186
3200
 
3187
3201
  // ../../node_modules/.pnpm/path-scurry@2.0.0/node_modules/path-scurry/dist/esm/index.js
3188
- import { posix, win32 } from "node:path";
3202
+ import { posix as posix2, win32 } from "node:path";
3189
3203
  import { fileURLToPath } from "node:url";
3190
3204
  import { lstatSync, readdir as readdirCB, readdirSync as readdirSync3, readlinkSync, realpathSync as rps } from "fs";
3191
3205
  import * as actualFS from "node:fs";
@@ -4334,12 +4348,12 @@ var PathBase = class {
4334
4348
  /**
4335
4349
  * Get the Path object referenced by the string path, resolved from this Path
4336
4350
  */
4337
- resolve(path14) {
4338
- if (!path14) {
4351
+ resolve(path3) {
4352
+ if (!path3) {
4339
4353
  return this;
4340
4354
  }
4341
- const rootPath = this.getRootString(path14);
4342
- const dir = path14.substring(rootPath.length);
4355
+ const rootPath = this.getRootString(path3);
4356
+ const dir = path3.substring(rootPath.length);
4343
4357
  const dirParts = dir.split(this.splitSep);
4344
4358
  const result = rootPath ? this.getRoot(rootPath).#resolveParts(dirParts) : this.#resolveParts(dirParts);
4345
4359
  return result;
@@ -5091,8 +5105,8 @@ var PathWin32 = class _PathWin32 extends PathBase {
5091
5105
  /**
5092
5106
  * @internal
5093
5107
  */
5094
- getRootString(path14) {
5095
- return win32.parse(path14).root;
5108
+ getRootString(path3) {
5109
+ return win32.parse(path3).root;
5096
5110
  }
5097
5111
  /**
5098
5112
  * @internal
@@ -5138,8 +5152,8 @@ var PathPosix = class _PathPosix extends PathBase {
5138
5152
  /**
5139
5153
  * @internal
5140
5154
  */
5141
- getRootString(path14) {
5142
- return path14.startsWith("/") ? "/" : "";
5155
+ getRootString(path3) {
5156
+ return path3.startsWith("/") ? "/" : "";
5143
5157
  }
5144
5158
  /**
5145
5159
  * @internal
@@ -5188,7 +5202,7 @@ var PathScurryBase = class {
5188
5202
  *
5189
5203
  * @internal
5190
5204
  */
5191
- constructor(cwd = process.cwd(), pathImpl, sep2, { nocase, childrenCacheSize = 16 * 1024, fs: fs2 = defaultFS } = {}) {
5205
+ constructor(cwd = process.cwd(), pathImpl, sep3, { nocase, childrenCacheSize = 16 * 1024, fs: fs2 = defaultFS } = {}) {
5192
5206
  this.#fs = fsFromOption(fs2);
5193
5207
  if (cwd instanceof URL || cwd.startsWith("file://")) {
5194
5208
  cwd = fileURLToPath(cwd);
@@ -5199,7 +5213,7 @@ var PathScurryBase = class {
5199
5213
  this.#resolveCache = new ResolveCache();
5200
5214
  this.#resolvePosixCache = new ResolveCache();
5201
5215
  this.#children = new ChildrenCache(childrenCacheSize);
5202
- const split = cwdPath.substring(this.rootPath.length).split(sep2);
5216
+ const split = cwdPath.substring(this.rootPath.length).split(sep3);
5203
5217
  if (split.length === 1 && !split[0]) {
5204
5218
  split.pop();
5205
5219
  }
@@ -5228,11 +5242,11 @@ var PathScurryBase = class {
5228
5242
  /**
5229
5243
  * Get the depth of a provided path, string, or the cwd
5230
5244
  */
5231
- depth(path14 = this.cwd) {
5232
- if (typeof path14 === "string") {
5233
- path14 = this.cwd.resolve(path14);
5245
+ depth(path3 = this.cwd) {
5246
+ if (typeof path3 === "string") {
5247
+ path3 = this.cwd.resolve(path3);
5234
5248
  }
5235
- return path14.depth();
5249
+ return path3.depth();
5236
5250
  }
5237
5251
  /**
5238
5252
  * Return the cache of child entries. Exposed so subclasses can create
@@ -5719,9 +5733,9 @@ var PathScurryBase = class {
5719
5733
  process3();
5720
5734
  return results;
5721
5735
  }
5722
- chdir(path14 = this.cwd) {
5736
+ chdir(path3 = this.cwd) {
5723
5737
  const oldCwd = this.cwd;
5724
- this.cwd = typeof path14 === "string" ? this.cwd.resolve(path14) : path14;
5738
+ this.cwd = typeof path3 === "string" ? this.cwd.resolve(path3) : path3;
5725
5739
  this.cwd[setAsCwd](oldCwd);
5726
5740
  }
5727
5741
  };
@@ -5764,7 +5778,7 @@ var PathScurryPosix = class extends PathScurryBase {
5764
5778
  sep = "/";
5765
5779
  constructor(cwd = process.cwd(), opts = {}) {
5766
5780
  const { nocase = false } = opts;
5767
- super(cwd, posix, "/", { ...opts, nocase });
5781
+ super(cwd, posix2, "/", { ...opts, nocase });
5768
5782
  this.nocase = nocase;
5769
5783
  }
5770
5784
  /**
@@ -6077,8 +6091,8 @@ var MatchRecord = class {
6077
6091
  }
6078
6092
  // match, absolute, ifdir
6079
6093
  entries() {
6080
- return [...this.store.entries()].map(([path14, n]) => [
6081
- path14,
6094
+ return [...this.store.entries()].map(([path3, n]) => [
6095
+ path3,
6082
6096
  !!(n & 2),
6083
6097
  !!(n & 1)
6084
6098
  ]);
@@ -6283,9 +6297,9 @@ var GlobUtil = class {
6283
6297
  signal;
6284
6298
  maxDepth;
6285
6299
  includeChildMatches;
6286
- constructor(patterns, path14, opts) {
6300
+ constructor(patterns, path3, opts) {
6287
6301
  this.patterns = patterns;
6288
- this.path = path14;
6302
+ this.path = path3;
6289
6303
  this.opts = opts;
6290
6304
  this.#sep = !opts.posix && opts.platform === "win32" ? "\\" : "/";
6291
6305
  this.includeChildMatches = opts.includeChildMatches !== false;
@@ -6304,11 +6318,11 @@ var GlobUtil = class {
6304
6318
  });
6305
6319
  }
6306
6320
  }
6307
- #ignored(path14) {
6308
- return this.seen.has(path14) || !!this.#ignore?.ignored?.(path14);
6321
+ #ignored(path3) {
6322
+ return this.seen.has(path3) || !!this.#ignore?.ignored?.(path3);
6309
6323
  }
6310
- #childrenIgnored(path14) {
6311
- return !!this.#ignore?.childrenIgnored?.(path14);
6324
+ #childrenIgnored(path3) {
6325
+ return !!this.#ignore?.childrenIgnored?.(path3);
6312
6326
  }
6313
6327
  // backpressure mechanism
6314
6328
  pause() {
@@ -6523,8 +6537,8 @@ var GlobUtil = class {
6523
6537
  };
6524
6538
  var GlobWalker = class extends GlobUtil {
6525
6539
  matches = /* @__PURE__ */ new Set();
6526
- constructor(patterns, path14, opts) {
6527
- super(patterns, path14, opts);
6540
+ constructor(patterns, path3, opts) {
6541
+ super(patterns, path3, opts);
6528
6542
  }
6529
6543
  matchEmit(e) {
6530
6544
  this.matches.add(e);
@@ -6561,8 +6575,8 @@ var GlobWalker = class extends GlobUtil {
6561
6575
  };
6562
6576
  var GlobStream = class extends GlobUtil {
6563
6577
  results;
6564
- constructor(patterns, path14, opts) {
6565
- super(patterns, path14, opts);
6578
+ constructor(patterns, path3, opts) {
6579
+ super(patterns, path3, opts);
6566
6580
  this.results = new Minipass({
6567
6581
  signal: this.signal,
6568
6582
  objectMode: true
@@ -6856,21 +6870,18 @@ var glob = Object.assign(glob_, {
6856
6870
  glob.glob = glob;
6857
6871
 
6858
6872
  // src/cli/build/patches/to-investigate/inline-eval-manifest.ts
6859
- import path5 from "node:path";
6860
6873
  function inlineEvalManifest(code, config) {
6861
6874
  console.log("# inlineEvalManifest");
6862
6875
  const manifestJss = globSync(
6863
- path5.join(config.paths.standaloneAppDotNext, "**", "*_client-reference-manifest.js").replaceAll(path5.sep, path5.posix.sep)
6864
- ).map(
6865
- (file) => file.replaceAll(path5.sep, path5.posix.sep).replace(config.paths.standaloneApp.replaceAll(path5.sep, path5.posix.sep) + path5.posix.sep, "")
6866
- );
6876
+ normalizePath(join5(config.paths.standaloneAppDotNext, "**", "*_client-reference-manifest.js"))
6877
+ ).map((file) => normalizePath(file).replace(normalizePath(config.paths.standaloneApp) + posix3.sep, ""));
6867
6878
  return code.replace(
6868
6879
  /function evalManifest\((.+?), .+?\) {/,
6869
6880
  `$&
6870
6881
  ${manifestJss.map(
6871
6882
  (manifestJs) => `
6872
6883
  if ($1.endsWith("${manifestJs}")) {
6873
- require(${JSON.stringify(path5.join(config.paths.standaloneApp, manifestJs))});
6884
+ require(${JSON.stringify(join5(config.paths.standaloneApp, manifestJs))});
6874
6885
  return {
6875
6886
  __RSC_MANIFEST: {
6876
6887
  "${manifestJs.replace(".next/server/app", "").replace("_client-reference-manifest.js", "")}": globalThis.__RSC_MANIFEST["${manifestJs.replace(".next/server/app", "").replace("_client-reference-manifest.js", "")}"],
@@ -6886,10 +6897,10 @@ function inlineEvalManifest(code, config) {
6886
6897
 
6887
6898
  // src/cli/build/patches/to-investigate/inline-middleware-manifest-require.ts
6888
6899
  import { existsSync as existsSync2, readFileSync as readFileSync2 } from "node:fs";
6889
- import path6 from "node:path";
6900
+ import { join as join6 } from "node:path";
6890
6901
  function inlineMiddlewareManifestRequire(code, config) {
6891
6902
  console.log("# inlineMiddlewareManifestRequire");
6892
- const middlewareManifestPath = path6.join(config.paths.standaloneAppServer, "middleware-manifest.json");
6903
+ const middlewareManifestPath = join6(config.paths.standaloneAppServer, "middleware-manifest.json");
6893
6904
  const middlewareManifest = existsSync2(middlewareManifestPath) ? JSON.parse(readFileSync2(middlewareManifestPath, "utf-8")) : {};
6894
6905
  const patchedCode = code.replace(
6895
6906
  "require(this.middlewareManifestPath)",
@@ -6903,11 +6914,11 @@ function inlineMiddlewareManifestRequire(code, config) {
6903
6914
 
6904
6915
  // src/cli/build/patches/to-investigate/inline-next-require.ts
6905
6916
  import { existsSync as existsSync3, readFileSync as readFileSync3 } from "node:fs";
6906
- import path7 from "node:path";
6917
+ import { join as join7 } from "node:path";
6907
6918
  function inlineNextRequire(code, config) {
6908
6919
  console.log("# inlineNextRequire");
6909
- const pagesManifestFile = path7.join(config.paths.standaloneAppServer, "pages-manifest.json");
6910
- const appPathsManifestFile = path7.join(config.paths.standaloneAppServer, "app-paths-manifest.json");
6920
+ const pagesManifestFile = join7(config.paths.standaloneAppServer, "pages-manifest.json");
6921
+ const appPathsManifestFile = join7(config.paths.standaloneAppServer, "app-paths-manifest.json");
6911
6922
  const pagesManifestFiles = existsSync3(pagesManifestFile) ? Object.values(JSON.parse(readFileSync3(pagesManifestFile, "utf-8"))).map(
6912
6923
  (file) => ".next/server/" + file
6913
6924
  ) : [];
@@ -6923,14 +6934,14 @@ function inlineNextRequire(code, config) {
6923
6934
  ${htmlPages.map(
6924
6935
  (htmlPage) => `
6925
6936
  if (pagePath.endsWith("${htmlPage}")) {
6926
- return ${JSON.stringify(readFileSync3(path7.join(config.paths.standaloneApp, htmlPage), "utf-8"))};
6937
+ return ${JSON.stringify(readFileSync3(join7(config.paths.standaloneApp, htmlPage), "utf-8"))};
6927
6938
  }
6928
6939
  `
6929
6940
  ).join("\n")}
6930
6941
  ${pageModules.map(
6931
6942
  (module) => `
6932
6943
  if (pagePath.endsWith("${module}")) {
6933
- return require(${JSON.stringify(path7.join(config.paths.standaloneApp, module))});
6944
+ return require(${JSON.stringify(join7(config.paths.standaloneApp, module))});
6934
6945
  }
6935
6946
  `
6936
6947
  ).join("\n")}
@@ -6941,19 +6952,19 @@ function inlineNextRequire(code, config) {
6941
6952
 
6942
6953
  // src/cli/build/patches/investigated/patch-cache.ts
6943
6954
  import { build } from "esbuild";
6944
- import { join as join3 } from "node:path";
6955
+ import { join as join8 } from "node:path";
6945
6956
  async function patchCache(code, config) {
6946
6957
  console.log("# patchCache");
6947
6958
  const cacheHandlerFileName = "cache-handler.mjs";
6948
- const cacheHandlerEntrypoint = join3(config.paths.internalTemplates, "cache-handler", "index.ts");
6949
- const cacheHandlerOutputFile = join3(config.paths.builderOutput, cacheHandlerFileName);
6959
+ const cacheHandlerEntrypoint = join8(config.paths.internalTemplates, "cache-handler", "index.ts");
6960
+ const cacheHandlerOutputFile = join8(config.paths.outputDir, cacheHandlerFileName);
6950
6961
  await build({
6951
6962
  entryPoints: [cacheHandlerEntrypoint],
6952
6963
  bundle: true,
6953
6964
  outfile: cacheHandlerOutputFile,
6954
6965
  format: "esm",
6955
6966
  target: "esnext",
6956
- minify: true,
6967
+ minify: config.build.shouldMinify,
6957
6968
  define: {
6958
6969
  "process.env.__OPENNEXT_KV_BINDING_NAME": `"${config.cache.kvBindingName}"`
6959
6970
  }
@@ -6982,7 +6993,7 @@ function patchExceptionBubbling(code) {
6982
6993
 
6983
6994
  // src/cli/build/patches/to-investigate/patch-find-dir.ts
6984
6995
  import { existsSync as existsSync4 } from "node:fs";
6985
- import path8 from "node:path";
6996
+ import { join as join9 } from "node:path";
6986
6997
  function patchFindDir(code, config) {
6987
6998
  console.log("# patchFindDir");
6988
6999
  return code.replace(
@@ -6990,10 +7001,10 @@ function patchFindDir(code, config) {
6990
7001
  `function findDir(dir, name) {
6991
7002
  if (dir.endsWith(".next/server")) {
6992
7003
  if (name === "app") {
6993
- return ${existsSync4(`${path8.join(config.paths.standaloneAppServer, "app")}`)};
7004
+ return ${existsSync4(`${join9(config.paths.standaloneAppServer, "app")}`)};
6994
7005
  }
6995
7006
  if (name === "pages") {
6996
- return ${existsSync4(`${path8.join(config.paths.standaloneAppServer, "pages")}`)};
7007
+ return ${existsSync4(`${join9(config.paths.standaloneAppServer, "pages")}`)};
6997
7008
  }
6998
7009
  }
6999
7010
  throw new Error("Unknown findDir call: " + dir + " " + name);
@@ -7002,28 +7013,26 @@ function patchFindDir(code, config) {
7002
7013
  }
7003
7014
 
7004
7015
  // src/cli/build/patches/to-investigate/patch-read-file.ts
7005
- import path9 from "node:path";
7016
+ import { join as join10, posix as posix4 } from "node:path";
7006
7017
  import { readFileSync as readFileSync4 } from "node:fs";
7007
7018
  function patchReadFile(code, config) {
7008
7019
  console.log("# patchReadFile");
7009
7020
  code = code.replace(
7010
7021
  "getBuildId() {",
7011
7022
  `getBuildId() {
7012
- return ${JSON.stringify(readFileSync4(path9.join(config.paths.standaloneAppDotNext, "BUILD_ID"), "utf-8"))};
7023
+ return ${JSON.stringify(readFileSync4(join10(config.paths.standaloneAppDotNext, "BUILD_ID"), "utf-8"))};
7013
7024
  `
7014
7025
  );
7015
7026
  const manifestJsons = globSync(
7016
- path9.join(config.paths.standaloneAppDotNext, "**", "*-manifest.json").replaceAll(path9.sep, path9.posix.sep)
7017
- ).map(
7018
- (file) => file.replaceAll(path9.sep, path9.posix.sep).replace(config.paths.standaloneApp.replaceAll(path9.sep, path9.posix.sep) + path9.posix.sep, "")
7019
- );
7027
+ normalizePath(join10(config.paths.standaloneAppDotNext, "**", "*-manifest.json"))
7028
+ ).map((file) => normalizePath(file).replace(normalizePath(config.paths.standaloneApp) + posix4.sep, ""));
7020
7029
  code = code.replace(
7021
7030
  /function loadManifest\((.+?), .+?\) {/,
7022
7031
  `$&
7023
7032
  ${manifestJsons.map(
7024
7033
  (manifestJson) => `
7025
7034
  if ($1.endsWith("${manifestJson}")) {
7026
- return ${readFileSync4(path9.join(config.paths.standaloneApp, manifestJson), "utf-8")};
7035
+ return ${readFileSync4(join10(config.paths.standaloneApp, manifestJson), "utf-8")};
7027
7036
  }
7028
7037
  `
7029
7038
  ).join("\n")}
@@ -7041,17 +7050,17 @@ function patchRequire(code) {
7041
7050
 
7042
7051
  // src/cli/build/patches/to-investigate/wrangler-deps.ts
7043
7052
  import { readFileSync as readFileSync5, statSync as statSync2, writeFileSync as writeFileSync2 } from "node:fs";
7044
- import path10 from "node:path";
7053
+ import { join as join11 } from "node:path";
7045
7054
  function patchWranglerDeps(config) {
7046
7055
  console.log("# patchWranglerDeps");
7047
7056
  const distPath = getDistPath(config);
7048
- const pagesRuntimeFile = path10.join(distPath, "compiled", "next-server", "pages.runtime.prod.js");
7057
+ const pagesRuntimeFile = join11(distPath, "compiled", "next-server", "pages.runtime.prod.js");
7049
7058
  const patchedPagesRuntime = readFileSync5(pagesRuntimeFile, "utf-8").replace(
7050
7059
  `e.exports=require("critters")`,
7051
7060
  `e.exports={}`
7052
7061
  );
7053
7062
  writeFileSync2(pagesRuntimeFile, patchedPagesRuntime);
7054
- const tracerFile = path10.join(distPath, "server", "lib", "trace", "tracer.js");
7063
+ const tracerFile = join11(distPath, "server", "lib", "trace", "tracer.js");
7055
7064
  const patchedTracer = readFileSync5(tracerFile, "utf-8").replaceAll(
7056
7065
  /\w+\s*=\s*require\([^/]*opentelemetry.*\)/g,
7057
7066
  `throw new Error("@opentelemetry/api")`
@@ -7061,7 +7070,7 @@ function patchWranglerDeps(config) {
7061
7070
  function getDistPath(config) {
7062
7071
  for (const root of [config.paths.standaloneApp, config.paths.standaloneRoot]) {
7063
7072
  try {
7064
- const distPath = path10.join(root, "node_modules", "next", "dist");
7073
+ const distPath = join11(root, "node_modules", "next", "dist");
7065
7074
  if (statSync2(distPath).isDirectory()) return distPath;
7066
7075
  } catch {
7067
7076
  }
@@ -7069,9 +7078,6 @@ function getDistPath(config) {
7069
7078
  throw new Error("Unexpected error: unable to detect the node_modules/next/dist directory");
7070
7079
  }
7071
7080
 
7072
- // src/cli/build/build-worker.ts
7073
- import path12 from "node:path";
7074
-
7075
7081
  // src/cli/build/patches/investigated/update-webpack-chunks-file/index.ts
7076
7082
  import { readFileSync as readFileSync6, readdirSync as readdirSync4, writeFileSync as writeFileSync3 } from "node:fs";
7077
7083
 
@@ -7178,12 +7184,12 @@ async function getUpdatedWebpackChunksFileContent(fileContent, chunks) {
7178
7184
  }
7179
7185
 
7180
7186
  // src/cli/build/patches/investigated/update-webpack-chunks-file/index.ts
7181
- import path11 from "node:path";
7187
+ import { join as join12 } from "node:path";
7182
7188
  async function updateWebpackChunksFile(config) {
7183
7189
  console.log("# updateWebpackChunksFile");
7184
- const webpackRuntimeFile = path11.join(config.paths.standaloneAppServer, "webpack-runtime.js");
7190
+ const webpackRuntimeFile = join12(config.paths.standaloneAppServer, "webpack-runtime.js");
7185
7191
  const fileContent = readFileSync6(webpackRuntimeFile, "utf-8");
7186
- const chunks = readdirSync4(path11.join(config.paths.standaloneAppServer, "chunks")).filter((chunk) => /^\d+\.js$/.test(chunk)).map((chunk) => {
7192
+ const chunks = readdirSync4(join12(config.paths.standaloneAppServer, "chunks")).filter((chunk) => /^\d+\.js$/.test(chunk)).map((chunk) => {
7187
7193
  console.log(` - chunk ${chunk}`);
7188
7194
  return chunk.replace(/\.js$/, "");
7189
7195
  });
@@ -7192,28 +7198,24 @@ async function updateWebpackChunksFile(config) {
7192
7198
  }
7193
7199
 
7194
7200
  // src/cli/build/build-worker.ts
7195
- var packageDistDir = path12.join(path12.dirname(fileURLToPath3(import.meta.url)), "..");
7201
+ var packageDistDir = join13(dirname2(fileURLToPath3(import.meta.url)), "..");
7196
7202
  async function buildWorker(config) {
7197
7203
  console.log(`\x1B[35m\u2699\uFE0F Copying files...
7198
7204
  \x1B[0m`);
7199
- await cp(
7200
- path12.join(config.paths.dotNext, "static"),
7201
- path12.join(config.paths.builderOutput, "assets", "_next", "static"),
7202
- {
7203
- recursive: true
7204
- }
7205
- );
7206
- const publicDir = path12.join(config.paths.nextApp, "public");
7205
+ await cp(join13(config.paths.dotNext, "static"), join13(config.paths.outputDir, "assets", "_next", "static"), {
7206
+ recursive: true
7207
+ });
7208
+ const publicDir = join13(config.paths.sourceDir, "public");
7207
7209
  if (existsSync5(publicDir)) {
7208
- await cp(publicDir, path12.join(config.paths.builderOutput, "assets"), {
7210
+ await cp(publicDir, join13(config.paths.outputDir, "assets"), {
7209
7211
  recursive: true
7210
7212
  });
7211
7213
  }
7212
7214
  copyPrerenderedRoutes(config);
7213
7215
  copyPackageCliFiles(packageDistDir, config);
7214
- const workerEntrypoint = path12.join(config.paths.internalTemplates, "worker.ts");
7215
- const workerOutputFile = path12.join(config.paths.builderOutput, "index.mjs");
7216
- const nextConfigStr = readFileSync7(path12.join(config.paths.standaloneApp, "/server.js"), "utf8")?.match(
7216
+ const workerEntrypoint = join13(config.paths.internalTemplates, "worker.ts");
7217
+ const workerOutputFile = join13(config.paths.outputDir, "index.mjs");
7218
+ const nextConfigStr = readFileSync7(join13(config.paths.standaloneApp, "/server.js"), "utf8")?.match(
7217
7219
  /const nextConfig = ({.+?})\n/
7218
7220
  )?.[1] ?? {};
7219
7221
  console.log(`\x1B[35m\u2699\uFE0F Bundling the worker file...
@@ -7232,15 +7234,15 @@ async function buildWorker(config) {
7232
7234
  // Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:
7233
7235
  // eval("require")("bufferutil");
7234
7236
  // eval("require")("utf-8-validate");
7235
- "next/dist/compiled/ws": path12.join(config.paths.internalTemplates, "shims", "empty.ts"),
7237
+ "next/dist/compiled/ws": join13(config.paths.internalTemplates, "shims", "empty.ts"),
7236
7238
  // Note: we apply an empty shim to next/dist/compiled/edge-runtime since (amongst others) it generated the following `eval`:
7237
7239
  // eval(getModuleCode)(module, module.exports, throwingRequire, params.context, ...Object.values(params.scopedContext));
7238
7240
  // which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63
7239
7241
  // QUESTION: Why did I encountered this but mhart didn't?
7240
- "next/dist/compiled/edge-runtime": path12.join(config.paths.internalTemplates, "shims", "empty.ts"),
7242
+ "next/dist/compiled/edge-runtime": join13(config.paths.internalTemplates, "shims", "empty.ts"),
7241
7243
  // `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here
7242
7244
  // source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env
7243
- "@next/env": path12.join(config.paths.internalTemplates, "shims", "env.ts")
7245
+ "@next/env": join13(config.paths.internalTemplates, "shims", "env.ts")
7244
7246
  },
7245
7247
  define: {
7246
7248
  // config file used by Next.js, see: https://github.com/vercel/next.js/blob/68a7128/packages/next/src/build/utils.ts#L2137-L2139
@@ -7318,10 +7320,10 @@ function createFixRequiresESBuildPlugin(config) {
7318
7320
  name: "replaceRelative",
7319
7321
  setup(build4) {
7320
7322
  build4.onResolve({ filter: /^\.\/require-hook$/ }, () => ({
7321
- path: path12.join(config.paths.internalTemplates, "shims", "empty.ts")
7323
+ path: join13(config.paths.internalTemplates, "shims", "empty.ts")
7322
7324
  }));
7323
7325
  build4.onResolve({ filter: /\.\/lib\/node-fs-methods$/ }, () => ({
7324
- path: path12.join(config.paths.internalTemplates, "shims", "empty.ts")
7326
+ path: join13(config.paths.internalTemplates, "shims", "empty.ts")
7325
7327
  }));
7326
7328
  }
7327
7329
  };
@@ -7329,23 +7331,22 @@ function createFixRequiresESBuildPlugin(config) {
7329
7331
 
7330
7332
  // src/cli/build/index.ts
7331
7333
  import { cpSync as cpSync2 } from "node:fs";
7332
- import path13 from "node:path";
7334
+ import { join as join14 } from "node:path";
7333
7335
  import { rm } from "node:fs/promises";
7334
- async function build3(appDir, opts) {
7335
- if (!opts.skipBuild) {
7336
- await buildNextjsApp(appDir);
7336
+ async function build3(projectOpts) {
7337
+ if (!projectOpts.skipNextBuild) {
7338
+ await buildNextjsApp(projectOpts.sourceDir);
7337
7339
  }
7338
- if (!containsDotNextDir(appDir)) {
7339
- throw new Error(`.next folder not found in ${appDir}`);
7340
+ if (!containsDotNextDir(projectOpts.sourceDir)) {
7341
+ throw new Error(`.next folder not found in ${projectOpts.sourceDir}`);
7340
7342
  }
7341
- const outputDir2 = path13.resolve(opts.outputDir ?? appDir, ".worker-next");
7342
- await cleanDirectory(outputDir2);
7343
- cpSync2(path13.join(appDir, ".next"), path13.join(outputDir2, ".next"), { recursive: true });
7344
- const config = getConfig(appDir, outputDir2);
7343
+ await cleanDirectory(projectOpts.outputDir);
7344
+ cpSync2(join14(projectOpts.sourceDir, ".next"), join14(projectOpts.outputDir, ".next"), { recursive: true });
7345
+ const config = getConfig(projectOpts);
7345
7346
  await buildWorker(config);
7346
7347
  }
7347
- async function cleanDirectory(path14) {
7348
- return await rm(path14, { recursive: true, force: true });
7348
+ async function cleanDirectory(path3) {
7349
+ return await rm(path3, { recursive: true, force: true });
7349
7350
  }
7350
7351
 
7351
7352
  // src/cli/index.ts
@@ -7357,7 +7358,7 @@ import { parseArgs } from "node:util";
7357
7358
  import { resolve } from "node:path";
7358
7359
  function getArgs() {
7359
7360
  const {
7360
- values: { skipBuild: skipBuild2, output }
7361
+ values: { skipBuild, output, noMinify }
7361
7362
  } = parseArgs({
7362
7363
  options: {
7363
7364
  skipBuild: {
@@ -7368,6 +7369,10 @@ function getArgs() {
7368
7369
  output: {
7369
7370
  type: "string",
7370
7371
  short: "o"
7372
+ },
7373
+ noMinify: {
7374
+ type: "boolean",
7375
+ default: false
7371
7376
  }
7372
7377
  },
7373
7378
  allowPositionals: false
@@ -7378,18 +7383,19 @@ function getArgs() {
7378
7383
  }
7379
7384
  return {
7380
7385
  outputDir: outputDir2,
7381
- skipBuild: skipBuild2 || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD))
7386
+ skipNextBuild: skipBuild || ["1", "true", "yes"].includes(String(process.env.SKIP_NEXT_APP_BUILD)),
7387
+ minify: !noMinify
7382
7388
  };
7383
7389
  }
7384
- function assertDirArg(path14, argName, make) {
7390
+ function assertDirArg(path3, argName, make) {
7385
7391
  let dirStats;
7386
7392
  try {
7387
- dirStats = statSync3(path14);
7393
+ dirStats = statSync3(path3);
7388
7394
  } catch {
7389
7395
  if (!make) {
7390
7396
  throw new Error(`Error: the provided${argName ? ` "${argName}"` : ""} input is not a valid path`);
7391
7397
  }
7392
- mkdirSync2(path14);
7398
+ mkdirSync2(path3);
7393
7399
  return;
7394
7400
  }
7395
7401
  if (!dirStats.isDirectory()) {
@@ -7407,8 +7413,10 @@ if (!["js", "cjs", "mjs", "ts"].some((ext2) => existsSync6(`./next.config.${ext2
7407
7413
  );
7408
7414
  process.exit(1);
7409
7415
  }
7410
- var { skipBuild, outputDir } = getArgs();
7411
- await build3(nextAppDir, {
7412
- outputDir,
7413
- skipBuild: !!skipBuild
7416
+ var { skipNextBuild, outputDir, minify } = getArgs();
7417
+ await build3({
7418
+ sourceDir: nextAppDir,
7419
+ outputDir: resolve2(outputDir ?? nextAppDir, ".worker-next"),
7420
+ skipNextBuild,
7421
+ minify
7414
7422
  });
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.1.1",
4
+ "version": "0.2.1",
5
5
  "bin": "dist/cli/index.mjs",
6
6
  "main": "./dist/api/index.mjs",
7
7
  "types": "./dist/api/index.d.mts",