Skip to content

Commit b84cd5f

Browse files
Prepare for the npm package (#8)
Co-authored-by: Peter Bacon Darwin <[email protected]>
1 parent 0ec7c7c commit b84cd5f

24 files changed

+114
-26
lines changed

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright (c) 2020 Cloudflare, Inc.
2+
3+
Permission is hereby granted, free of charge, to any
4+
person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the
6+
Software without restriction, including without
7+
limitation the rights to use, copy, modify, merge,
8+
publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software
10+
is furnished to do so, subject to the following
11+
conditions:
12+
13+
The above copyright notice and this permission notice
14+
shall be included in all copies or substantial portions
15+
of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25+
DEALINGS IN THE SOFTWARE.

TODO.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ DONE:
1414
## Install
1515

1616
- `npx create-next-app@latest <app-name> --use-npm` (use npm to avoid symlinks)
17+
1718
- update next.config.mjs as follows
1819

1920
```typescript

builder/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Next.js builder for Cloudflare
2+
3+
## Build your app
4+
5+
- update the `next.config.mjs` as follows
6+
7+
```typescript
8+
/** @type {import('next').NextConfig} */
9+
const nextConfig = {
10+
output: "standalone",
11+
experimental: {
12+
serverMinification: false,
13+
},
14+
};
15+
16+
export default nextConfig;
17+
```
18+
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+
```
25+
26+
- Execute `npx @flarelabs-net/builder@latest` in your app folder
27+
28+
## Serve your app
29+
30+
- add a `wrangler.toml` at the root of your project
31+
32+
```toml
33+
#:schema node_modules/wrangler/config-schema.json
34+
name = "<app-name>"
35+
main = ".worker-next/index.mjs"
36+
37+
compatibility_date = "2024-08-29"
38+
compatibility_flags = ["nodejs_compat_v2"]
39+
workers_dev = true
40+
minify = false
41+
42+
# Use the new Workers + Assets to host the static frontend files
43+
experimental_assets = { directory = ".worker-next/assets", binding = "ASSETS" }
44+
```
45+
46+
- Use `wrangler dev`

builder/package.json

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
{
2-
"name": "builder",
2+
"name": "@flarelabs-net/builder",
3+
"description": "Cloudflare builder for next apps",
4+
"version": "0.0.1",
35
"scripts": {
46
"build": "tsup",
57
"build:watch": "tsup --watch src"
68
},
79
"bin": "dist/index.mjs",
810
"files": [
11+
"README.md",
912
"dist"
1013
],
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/flarelabs-net/poc-next.git",
17+
"directory": "builder"
18+
},
19+
"keywords": [
20+
"cloudflare",
21+
"workers",
22+
"next.js"
23+
],
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/flarelabs-net/poc-next/issues"
27+
},
28+
"homepage": "https://github.com/flarelabs-net/poc-next",
1129
"devDependencies": {
1230
"@types/node": "^22.2.0",
1331
"esbuild": "^0.23.0",

builder/src/build/build-worker/index.ts builder/src/build/build-worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextjsAppPaths } from "../../nextjs-paths";
1+
import { NextjsAppPaths } from "../nextjs-paths";
22
import { build, Plugin } from "esbuild";
33
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
44
import { cp, readFile, writeFile } from "node:fs/promises";

builder/src/build/index.ts builder/src/build/build.ts

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ export async function build(inputNextAppDir: string, opts: BuildOptions): Promis
4343

4444
const templateDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "templates");
4545

46-
console.log({ outputDir, nextjsAppPaths, templateDir });
47-
4846
await buildWorker(outputDir, nextjsAppPaths, templateDir);
4947
}
5048

builder/src/build/build-worker/patches/investigated/copy-templates.ts builder/src/build/patches/investigated/copy-templates.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "node:path";
2-
import { NextjsAppPaths } from "../../../../nextjs-paths";
2+
import { NextjsAppPaths } from "../../../nextjs-paths";
33
import { cpSync } from "node:fs";
44

55
/**

builder/src/build/build-worker/patches/to-investigate/inline-eval-manifest.ts builder/src/build/patches/to-investigate/inline-eval-manifest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { globSync } from "glob";
2-
import { NextjsAppPaths } from "../../../../nextjs-paths";
2+
import { NextjsAppPaths } from "../../../nextjs-paths";
33

44
/**
55
* `evalManifest` relies on readFileSync so we need to patch the function so that it instead returns the content of the manifest files

builder/src/build/build-worker/patches/to-investigate/inline-next-require.ts builder/src/build/patches/to-investigate/inline-next-require.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readFileSync, existsSync } from "node:fs";
2-
import { NextjsAppPaths } from "../../../../nextjs-paths";
2+
import { NextjsAppPaths } from "../../../nextjs-paths";
33

44
/**
55
* The following avoid various Next.js specific files `require`d at runtime since we can just read

builder/src/build/build-worker/patches/to-investigate/patch-find-dir.ts builder/src/build/patches/to-investigate/patch-find-dir.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NextjsAppPaths } from "../../../../nextjs-paths";
1+
import { NextjsAppPaths } from "../../../nextjs-paths";
22
import { existsSync } from "node:fs";
33

44
/**

builder/src/build/build-worker/patches/to-investigate/patch-read-file.ts builder/src/build/patches/to-investigate/patch-read-file.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from "node:fs";
22
import { globSync } from "glob";
3-
import { NextjsAppPaths } from "../../../../nextjs-paths";
3+
import { NextjsAppPaths } from "../../../nextjs-paths";
44

55
export function patchReadFile(code: string, nextjsAppPaths: NextjsAppPaths): string {
66
console.log("# patchReadFile");

builder/src/build/build-worker/patches/to-investigate/wrangler-deps.ts builder/src/build/patches/to-investigate/wrangler-deps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "node:path";
22
import fs, { writeFileSync } from "node:fs";
3-
import { NextjsAppPaths } from "../../../../nextjs-paths";
3+
import { NextjsAppPaths } from "../../../nextjs-paths";
44

55
export function patchWranglerDeps(paths: NextjsAppPaths) {
66
console.log("# patchWranglerDeps");

builder/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { resolve } from "node:path";
22
import { getArgs } from "./args";
33
import { existsSync } from "node:fs";
4-
import { build } from "./build";
4+
import { build } from "./build/build";
55

6-
const inputNextAppDir = resolve(".");
6+
const nextAppDir = resolve(".");
77

8-
console.log({ inputNextAppDir });
8+
console.log(`Building the Next.js app in the current folder (${nextAppDir})`);
99

1010
if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`))) {
1111
// TODO: we can add more validation later
@@ -14,7 +14,7 @@ if (!["js", "cjs", "mjs", "ts"].some((ext) => existsSync(`./next.config.${ext}`)
1414

1515
const { skipBuild, outputDir } = getArgs();
1616

17-
await build(inputNextAppDir, {
17+
await build(nextAppDir, {
1818
outputDir,
1919
skipBuild: !!skipBuild,
2020
});

builder/tsup.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { defineConfig } from "tsup";
44
export default defineConfig({
55
entry: ["src/index.ts"],
66
outDir: "dist",
7-
dts: true,
7+
dts: false,
88
format: ["esm"],
99
platform: "node",
1010
external: ["esbuild"],
1111
onSuccess: async () => {
12-
await cp(`${__dirname}/src/build/build-worker/templates`, `${__dirname}/dist/templates`, {
12+
await cp(`${__dirname}/src/templates`, `${__dirname}/dist/templates`, {
1313
recursive: true,
1414
});
1515
},

examples/api/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "next build",
88
"start": "next start",
99
"lint": "next lint",
10-
"build:worker": "builder",
10+
"build:worker": "pnpm builder",
1111
"dev:worker": "wrangler dev --port 8770",
1212
"preview:worker": "pnpm build:worker && pnpm dev:worker",
1313
"e2e": "playwright test"
@@ -18,7 +18,7 @@
1818
"react-dom": "^18"
1919
},
2020
"devDependencies": {
21-
"builder": "workspace:*",
21+
"@flarelabs-net/builder": "workspace:*",
2222
"@playwright/test": "1.47.0",
2323
"@types/node": "^22.2.0",
2424
"node-url": "npm:url@^0.11.4",

examples/create-next-app/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "next build",
88
"start": "next start",
99
"lint": "next lint",
10-
"build:worker": "builder",
10+
"build:worker": "pnpm builder",
1111
"dev:worker": "wrangler dev --port 8771",
1212
"preview:worker": "pnpm build:worker && pnpm dev:worker",
1313
"e2e": "playwright test -c e2e/playwright.config.ts"
@@ -18,7 +18,7 @@
1818
"next": "14.2.11"
1919
},
2020
"devDependencies": {
21-
"builder": "workspace:*",
21+
"@flarelabs-net/builder": "workspace:*",
2222
"@playwright/test": "1.47.0",
2323
"@types/node": "^20",
2424
"@types/react": "^18",

pnpm-lock.yaml

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)