Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/cloudflare/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pluginJs from "@eslint/js";
import importPlugin from "eslint-plugin-import";
import simpleImportSort from "eslint-plugin-simple-import-sort";
import eslintPluginUnicorn from "eslint-plugin-unicorn";
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

export default [
Expand All @@ -18,13 +20,20 @@ export default [
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
name: "open-next",
plugins: {
unicorn: eslintPluginUnicorn,
"simple-import-sort": simpleImportSort,
import: importPlugin,
},
rules: {
"@typescript-eslint/ban-ts-comment": "off",
"unicorn/prefer-node-protocol": "error",
"sort-imports": "error",
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
"import/first": "error",
"import/newline-after-import": "error",
"import/no-duplicates": "error",
},
},
];
2 changes: 2 additions & 0 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"@types/node": "catalog:",
"esbuild": "catalog:",
"eslint": "catalog:",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-unicorn": "catalog:",
"glob": "catalog:",
"globals": "catalog:",
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare/src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Stats, mkdirSync, statSync } from "node:fs";
import { parseArgs } from "node:util";
import { mkdirSync, type Stats, statSync } from "node:fs";
import { resolve } from "node:path";
import { parseArgs } from "node:util";

export function getArgs(): {
skipNextBuild: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/cloudflare/src/cli/build/build-next-app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type AgentName as PackageManager, detect } from "package-manager-detector";
import { execSync } from "node:child_process";

import { type AgentName as PackageManager, detect } from "package-manager-detector";

/**
* Builds the Next.js app in the standard Next.js cli way (this outputs a `.next` directory)
*
Expand Down
16 changes: 9 additions & 7 deletions packages/cloudflare/src/cli/build/build-worker.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { Plugin, build } from "esbuild";
import { existsSync, readFileSync } from "node:fs";
import { cp, readFile, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";

import { build, Plugin } from "esbuild";

import { Config } from "../config";
import { copyPackageCliFiles } from "./patches/investigated/copy-package-cli-files";
import { copyPrerenderedRoutes } from "./utils";
import { fileURLToPath } from "node:url";
import { patchCache } from "./patches/investigated/patch-cache";
import { patchRequire } from "./patches/investigated/patch-require";
import { updateWebpackChunksFile } from "./patches/investigated/update-webpack-chunks-file";
import { inlineEvalManifest } from "./patches/to-investigate/inline-eval-manifest";
import { inlineMiddlewareManifestRequire } from "./patches/to-investigate/inline-middleware-manifest-require";
import { inlineNextRequire } from "./patches/to-investigate/inline-next-require";
import { patchCache } from "./patches/investigated/patch-cache";
import { patchExceptionBubbling } from "./patches/to-investigate/patch-exception-bubbling";
import { patchFindDir } from "./patches/to-investigate/patch-find-dir";
import { patchReadFile } from "./patches/to-investigate/patch-read-file";
import { patchRequire } from "./patches/investigated/patch-require";
import { patchWranglerDeps } from "./patches/to-investigate/wrangler-deps";
import { updateWebpackChunksFile } from "./patches/investigated/update-webpack-chunks-file";
import { copyPrerenderedRoutes } from "./utils";

/** The dist directory of the Cloudflare adapter package */
const packageDistDir = join(dirname(fileURLToPath(import.meta.url)), "..");
Expand Down
9 changes: 5 additions & 4 deletions packages/cloudflare/src/cli/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { containsDotNextDir, getConfig } from "../config";
import { cpSync } from "node:fs";
import { rm } from "node:fs/promises";
import { join } from "node:path";

import type { ProjectOptions } from "../config";
import { containsDotNextDir, getConfig } from "../config";
import { buildNextjsApp } from "./build-next-app";
import { buildWorker } from "./build-worker";
import { cpSync } from "node:fs";
import { join } from "node:path";
import { rm } from "node:fs/promises";

/**
* Builds the application in a format that can be passed to workerd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Config } from "../../../config";
import { cpSync } from "node:fs";
import { join } from "node:path";

import { Config } from "../../../config";

/**
* Copies the template files present in the cloudflare adapter package into the standalone node_modules folder
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Config } from "../../../config";
import { build } from "esbuild";
import { join } from "node:path";

import { build } from "esbuild";

import { Config } from "../../../config";

/**
* Sets up the OpenNext cache handler in a Next.js build.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, expect, test } from "vitest";
import { getChunkInstallationIdentifiers } from "./get-chunk-installation-identifiers";
import { readFile } from "node:fs/promises";

import { describe, expect, test } from "vitest";

import { tsParseFile } from "../../../utils";
import { getChunkInstallationIdentifiers } from "./get-chunk-installation-identifiers";

describe("getChunkInstallationIdentifiers", () => {
test("gets chunk identifiers from unminified code", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, expect, test } from "vitest";
import { getFileContentWithUpdatedWebpackFRequireCode } from "./get-file-content-with-updated-webpack-f-require-code";
import { readFile } from "node:fs/promises";

import { describe, expect, test } from "vitest";

import { tsParseFile } from "../../../utils";
import { getFileContentWithUpdatedWebpackFRequireCode } from "./get-file-content-with-updated-webpack-f-require-code";

describe("getFileContentWithUpdatedWebpackFRequireCode", () => {
test("returns the updated content of the f.require function from unminified webpack runtime code", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFile } from "node:fs/promises";

import { describe, expect, test } from "vitest";

import { getUpdatedWebpackChunksFileContent } from "./get-updated-webpack-chunks-file-content";
import { readFile } from "node:fs/promises";

describe("getUpdatedWebpackChunksFileContent", () => {
test("returns the updated content of a webpack runtime chunks unminified file", async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tsParseFile } from "../../../utils";
import { getChunkInstallationIdentifiers } from "./get-chunk-installation-identifiers";
import { getFileContentWithUpdatedWebpackFRequireCode } from "./get-file-content-with-updated-webpack-f-require-code";
import { tsParseFile } from "../../../utils";

/**
* Updates the content of the webpack runtime file in a manner so that it doesn't perform runtime dynamic `require` calls which fail in our runtime.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";

import { Config } from "../../../../config";
import { getUpdatedWebpackChunksFileContent } from "./get-updated-webpack-chunks-file-content";
import { join } from "node:path";

/**
* Fixes the webpack-runtime.js file by removing its webpack dynamic requires.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join, posix } from "node:path";
import { Config } from "../../../config";

import { globSync } from "glob";

import { Config } from "../../../config";
import { normalizePath } from "../../utils";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { existsSync, readFileSync } from "node:fs";
import { Config } from "../../../config";
import { join } from "node:path";

import { Config } from "../../../config";

/**
* Inlines the middleware manifest from the build output to prevent a dynamic require statement
* as they result in runtime failures.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { existsSync, readFileSync } from "node:fs";
import { Config } from "../../../config";
import { join } from "node:path";

import { Config } from "../../../config";

/**
* The following avoid various Next.js specific files `require`d at runtime since we can just read
* and inline their content during build time
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Config } from "../../../config";
import { existsSync } from "node:fs";
import { join } from "node:path";

import { Config } from "../../../config";

/**
* Here we patch `findDir` so that the next server can detect whether the `app` or `pages` directory exists
* (source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/lib/find-pages-dir.ts#L4-L13)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { readFileSync } from "node:fs";
import { join, posix } from "node:path";
import { Config } from "../../../config";

import { globSync } from "glob";

import { Config } from "../../../config";
import { normalizePath } from "../../utils";
import { readFileSync } from "node:fs";

export function patchReadFile(code: string, config: Config): string {
console.log("# patchReadFile");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { readFileSync, statSync, writeFileSync } from "node:fs";
import { Config } from "../../../config";
import { join } from "node:path";

import { Config } from "../../../config";

export function patchWranglerDeps(config: Config) {
console.log("# patchWranglerDeps");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { NEXT_META_SUFFIX, SEED_DATA_DIR } from "../../constants/incremental-cache";
import { copyFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { Config } from "../../config";

import type { PrerenderManifest } from "next/dist/build";

import { Config } from "../../config";
import { NEXT_META_SUFFIX, SEED_DATA_DIR } from "../../constants/incremental-cache";
import { readPathsRecursively } from "./read-paths-recursively";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/build/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./ts-parse-file";
export * from "./copy-prerendered-routes";
export * from "./normalize-path";
export * from "./ts-parse-file";
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from "node:path";
import { readdirSync } from "node:fs";
import { join } from "node:path";

/**
* Recursively reads all file paths in a directory.
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join, relative } from "node:path";
import { readdirSync, statSync } from "node:fs";
import { join, relative } from "node:path";

const PACKAGE_NAME = "@opennextjs/cloudflare";

Expand Down
5 changes: 3 additions & 2 deletions packages/cloudflare/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
import { build } from "./build";
import { existsSync } from "node:fs";
import { getArgs } from "./args";
import { resolve } from "node:path";

import { getArgs } from "./args";
import { build } from "./build";

const nextAppDir = resolve(".");

console.log(`Building the Next.js app in the current folder (${nextAppDir})`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { KVNamespace } from "@cloudflare/workers-types";
import type {
CacheHandler,
CacheHandlerContext,
CacheHandlerValue,
} from "next/dist/server/lib/incremental-cache";
import type { IncrementalCacheValue } from "next/dist/server/response-cache";

import {
NEXT_BODY_SUFFIX,
NEXT_DATA_SUFFIX,
Expand All @@ -12,8 +15,6 @@ import {
SEED_DATA_DIR,
} from "../../constants/incremental-cache";
import { getSeedBodyFile, getSeedMetaFile, getSeedTextFile, parseCtx } from "./utils";
import type { IncrementalCacheValue } from "next/dist/server/response-cache";
import type { KVNamespace } from "@cloudflare/workers-types";

type CacheEntry = {
lastModified: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IncrementalCache } from "next/dist/server/lib/incremental-cache";

import { NEXT_META_SUFFIX } from "../../constants/incremental-cache";

type PrerenderedRouteMeta = {
Expand Down
12 changes: 7 additions & 5 deletions packages/cloudflare/src/cli/templates/worker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { ExportedHandler, Fetcher } from "@cloudflare/workers-types";
import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
import { AsyncLocalStorage } from "node:async_hooks";
import type { CloudflareContext } from "../../api";
import type { IncomingMessage } from "node:http";
import { MockedResponse } from "next/dist/server/lib/mock-request";
import Stream from "node:stream";

import type { ExportedHandler, Fetcher } from "@cloudflare/workers-types";
import type { NextConfig } from "next";
import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
import { MockedResponse } from "next/dist/server/lib/mock-request";
import type { NodeRequestHandler } from "next/dist/server/next-server";
import Stream from "node:stream";

import type { CloudflareContext } from "../../api";

const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);

Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cp } from "node:fs/promises";

import { defineConfig } from "tsup";

const cliConfig = defineConfig({
Expand Down
Loading