Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable eslint for cloudflare package #44

Merged
merged 10 commits into from
Sep 27, 2024
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Prettier check
name: Code checks
on:
push:
branches: [main]
Expand All @@ -15,5 +15,5 @@ jobs:
node-version: lts/*
- name: Install dependencies
run: npm install -g pnpm && pnpm install
- name: Check formatting
run: pnpm prettier:check
- name: Run code checks
run: pnpm code:checks
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"scripts": {
"prettier:check": "prettier --check .",
"prettier:fix": "prettier --write .",
"lint:check": "pnpm -r lint:check",
"lint:fix": "pnpm -r lint:fix",
dario-piotrowicz marked this conversation as resolved.
Show resolved Hide resolved
"code:checks": "pnpm lint:check && pnpm lint:check",
"code:fixes": "pnpm prettier:fix && pnpm lint:fix",
"postinstall": "pnpm --filter cloudflare build",
"install-playwright": "playwright install --with-deps",
"e2e": "pnpm -r e2e",
Expand Down
30 changes: 30 additions & 0 deletions packages/cloudflare/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import eslintPluginUnicorn from "eslint-plugin-unicorn";
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

export default [
{
ignores: ["dist", "**/test-snapshots", "**/test-fixtures"],
},
{
files: ["**/*.{js,mjs,cjs,ts}"],
},
{
languageOptions: {
globals: globals.node,
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: {
"@typescript-eslint/ban-ts-comment": "off",
"unicorn/prefer-node-protocol": "error",
"sort-imports": "error",
},
},
];
7 changes: 7 additions & 0 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"build": "tsup",
"build:watch": "tsup --watch src",
"lint:check": "eslint",
"lint:fix": "eslint --fix",
"test": "vitest --run",
"test:watch": "vitest"
},
Expand Down Expand Up @@ -38,12 +40,17 @@
"homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
"devDependencies": {
"@cloudflare/workers-types": "catalog:",
"@eslint/js": "catalog:",
"@types/node": "catalog:",
"esbuild": "catalog:",
"eslint": "catalog:",
"eslint-plugin-unicorn": "catalog:",
"glob": "catalog:",
"globals": "catalog:",
"next": "catalog:",
"tsup": "catalog:",
"typescript": "catalog:",
"typescript-eslint": "catalog:",
"vitest": "catalog:"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/api/get-cloudflare-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "server-only";

declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface CloudflareEnv {}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, type Stats, statSync } from "node:fs";
import { type Stats, mkdirSync, statSync } from "node:fs";
import { parseArgs } from "node:util";
import { resolve } from "node:path";

Expand Down
26 changes: 12 additions & 14 deletions packages/cloudflare/src/cli/build/build-worker.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { Config } from "../config";
import { build, Plugin } from "esbuild";
import { existsSync, readFileSync } from "node:fs";
import { Plugin, build } from "esbuild";
import { cp, readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { patchRequire } from "./patches/investigated/patch-require";
import { existsSync, readFileSync } from "node:fs";
import { Config } from "../config";
import { copyPackageCliFiles } from "./patches/investigated/copy-package-cli-files";

import { patchReadFile } from "./patches/to-investigate/patch-read-file";
import { patchFindDir } from "./patches/to-investigate/patch-find-dir";
import { inlineNextRequire } from "./patches/to-investigate/inline-next-require";
import { fileURLToPath } from "node:url";
import { inlineEvalManifest } from "./patches/to-investigate/inline-eval-manifest";
import { inlineNextRequire } from "./patches/to-investigate/inline-next-require";
import { patchCache } from "./patches/investigated/patch-cache";
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 path from "node:path";
import { updateWebpackChunksFile } from "./patches/investigated/update-webpack-chunks-file";
import { patchCache } from "./patches/investigated/patch-cache";

/** The dist directory of the Cloudflare adapter package */
const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
Expand Down Expand Up @@ -177,10 +175,10 @@ function createFixRequiresESBuildPlugin(templateDir: string): Plugin {
name: "replaceRelative",
setup(build) {
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
build.onResolve({ filter: /^\.\/require-hook$/ }, (args) => ({
build.onResolve({ filter: /^\.\/require-hook$/ }, () => ({
path: path.join(templateDir, "shims", "empty.ts"),
}));
build.onResolve({ filter: /\.\/lib\/node-fs-methods$/ }, (args) => ({
build.onResolve({ filter: /\.\/lib\/node-fs-methods$/ }, () => ({
path: path.join(templateDir, "shims", "empty.ts"),
}));
},
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare/src/cli/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { rm } from "node:fs/promises";
import { containsDotNextDir, getConfig } from "../config";
import { buildNextjsApp } from "./build-next-app";
import { buildWorker } from "./build-worker";
import { containsDotNextDir, getConfig } from "../config";
import { cpSync } from "node:fs";
import path 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,5 +1,5 @@
import path from "node:path";
import { Config } from "../../../config";
import path from "node:path";

/**
* Install the cloudflare KV cache handler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { readFile } from "node:fs/promises";

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

import { describe, expect, test } from "vitest";
import { getChunkInstallationIdentifiers } from "./get-chunk-installation-identifiers";
import { readFile } from "node:fs/promises";
import { tsParseFile } from "../../../utils";

describe("getChunkInstallationIdentifiers", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { readFile } from "node:fs/promises";

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

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 { tsParseFile } from "../../../utils";

describe("getFileContentWithUpdatedWebpackFRequireCode", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { readFile } from "node:fs/promises";

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

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,8 +1,7 @@
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";

import { Config } from "../../../../cli/config";
import { readFileSync, readdirSync, writeFileSync } from "node:fs";
import { Config } from "../../../../config";
import { getUpdatedWebpackChunksFileContent } from "./get-updated-webpack-chunks-file-content";
import path 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,6 @@
import { Config } from "../../../config";
import { globSync } from "glob";
import path from "node:path";
import { Config } from "../../../cli/config";

/**
* `evalManifest` relies on readFileSync so we need to patch the function so that it instead returns the content of the manifest files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, existsSync } from "node:fs";
import { existsSync, readFileSync } from "node:fs";
import { Config } from "../../../cli/config";
import path from "node:path";

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

/**
* Here we patch `findDir` so that the next server can detect whether the `app` or `pages` directory exists
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from "node:fs";
import { Config } from "../../../config";
import { globSync } from "glob";
import { Config } from "../../../cli/config";
import path from "node:path";
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,6 +1,6 @@
import path from "node:path";
import fs, { writeFileSync } from "node:fs";
import { Config } from "../../../cli/config";
import { Config } from "../../../config";
import path from "node:path";

export function patchWranglerDeps(config: Config) {
console.log("# patchWranglerDeps");
Expand Down Expand Up @@ -33,7 +33,7 @@ export function patchWranglerDeps(config: Config) {
// Remove the need for an alias in wrangler.toml:
//
// [alias]
// # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
// # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
// # IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
// # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
// # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
Expand Down
3 changes: 2 additions & 1 deletion packages/cloudflare/src/cli/cache-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default class CfWorkersKvCacheHandler implements CacheHandler {
async set(
key: string,
entry: IncrementalCacheValue | null,
ctx: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_ctx: {
revalidate?: number | false;
fetchCache?: boolean;
fetchUrl?: string;
Expand Down
8 changes: 5 additions & 3 deletions packages/cloudflare/src/cli/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readdirSync, statSync } from "node:fs";
import path, { relative } from "node:path";
import { readdirSync, statSync } from "node:fs";

const PACKAGE_NAME = "@opennextjs/cloudflare";

Expand Down Expand Up @@ -76,7 +76,7 @@ export function getConfig(appDir: string, outputDir: string): Config {
export function containsDotNextDir(folder: string): boolean {
try {
return statSync(path.join(folder, ".next")).isDirectory();
} catch (e) {
} catch {
return false;
}
}
Expand Down Expand Up @@ -105,7 +105,9 @@ function findServerParentPath(parentPath: string): string | undefined {
if (statSync(path.join(parentPath, ".next", "server")).isDirectory()) {
return parentPath;
}
} catch {}
} catch {
/* empty */
}

const folders = readdirSync(parentPath);

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

const nextAppDir = resolve(".");

Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/templates/shims/node-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function existsSync(path: string) {
return FILES.has(path);
}

async function readFile(path: string, options: unknown): Promise<any> {
async function readFile(path: string, options: unknown): Promise<unknown> {
console.log(
"readFile",
{ path, options }
Expand Down
Loading