@opennextjs/cloudflare 0.3.10 → 0.4.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.
- package/README.md +1 -64
- package/dist/api/{get-cloudflare-context.d.ts → cloudflare-context.d.ts} +8 -1
- package/dist/api/cloudflare-context.js +114 -0
- package/dist/api/index.d.ts +1 -1
- package/dist/api/index.js +1 -1
- package/dist/api/kvCache.d.ts +0 -5
- package/dist/api/kvCache.js +15 -28
- package/dist/cli/build/build.d.ts +1 -1
- package/dist/cli/build/build.js +2 -12
- package/dist/cli/build/bundle-server.d.ts +4 -5
- package/dist/cli/build/bundle-server.js +44 -37
- package/dist/cli/build/open-next/compile-env-files.d.ts +1 -1
- package/dist/cli/build/open-next/compile-env-files.js +2 -2
- package/dist/cli/build/patches/ast/optional-deps.d.ts +6 -3
- package/dist/cli/build/patches/ast/optional-deps.js +4 -4
- package/dist/cli/build/patches/ast/util.d.ts +15 -4
- package/dist/cli/build/patches/ast/util.js +16 -5
- package/dist/cli/build/patches/ast/vercel-og.d.ts +23 -0
- package/dist/cli/build/patches/ast/vercel-og.js +58 -0
- package/dist/cli/build/patches/ast/vercel-og.spec.d.ts +1 -0
- package/dist/cli/build/patches/ast/vercel-og.spec.js +22 -0
- package/dist/cli/build/patches/investigated/copy-package-cli-files.d.ts +4 -3
- package/dist/cli/build/patches/investigated/copy-package-cli-files.js +8 -5
- package/dist/cli/build/patches/investigated/index.d.ts +1 -0
- package/dist/cli/build/patches/investigated/index.js +1 -0
- package/dist/cli/build/patches/investigated/patch-cache.d.ts +2 -2
- package/dist/cli/build/patches/investigated/patch-cache.js +7 -6
- package/dist/cli/build/patches/investigated/patch-require.d.ts +1 -4
- package/dist/cli/build/patches/investigated/patch-require.js +1 -4
- package/dist/cli/build/patches/investigated/patch-vercel-og-library.d.ts +7 -0
- package/dist/cli/build/patches/investigated/patch-vercel-og-library.js +39 -0
- package/dist/cli/build/patches/investigated/patch-vercel-og-library.spec.d.ts +1 -0
- package/dist/cli/build/patches/investigated/patch-vercel-og-library.spec.js +50 -0
- package/dist/cli/build/patches/investigated/update-webpack-chunks-file/index.d.ts +2 -5
- package/dist/cli/build/patches/investigated/update-webpack-chunks-file/index.js +6 -6
- package/dist/cli/build/patches/plugins/require-page.d.ts +6 -0
- package/dist/cli/build/patches/plugins/require-page.js +70 -0
- package/dist/cli/build/patches/plugins/wrangler-external.d.ts +20 -0
- package/dist/cli/build/patches/plugins/wrangler-external.js +36 -0
- package/dist/cli/build/patches/to-investigate/index.d.ts +0 -1
- package/dist/cli/build/patches/to-investigate/index.js +0 -1
- package/dist/cli/build/patches/to-investigate/inline-eval-manifest.d.ts +2 -2
- package/dist/cli/build/patches/to-investigate/inline-eval-manifest.js +21 -17
- package/dist/cli/build/patches/to-investigate/inline-middleware-manifest-require.d.ts +2 -2
- package/dist/cli/build/patches/to-investigate/inline-middleware-manifest-require.js +4 -2
- package/dist/cli/build/patches/to-investigate/patch-find-dir.d.ts +3 -6
- package/dist/cli/build/patches/to-investigate/patch-find-dir.js +11 -11
- package/dist/cli/build/patches/to-investigate/patch-read-file.d.ts +3 -3
- package/dist/cli/build/patches/to-investigate/patch-read-file.js +15 -16
- package/dist/cli/build/patches/to-investigate/wrangler-deps.d.ts +2 -2
- package/dist/cli/build/patches/to-investigate/wrangler-deps.js +36 -67
- package/dist/cli/build/utils/create-config-files.d.ts +1 -1
- package/dist/cli/project-options.d.ts +7 -0
- package/dist/cli/project-options.js +1 -0
- package/package.json +4 -4
- package/dist/api/get-cloudflare-context.js +0 -42
- package/dist/cli/build/patches/to-investigate/inline-next-require.d.ts +0 -6
- package/dist/cli/build/patches/to-investigate/inline-next-require.js +0 -36
- package/dist/cli/config.d.ts +0 -42
- package/dist/cli/config.js +0 -92
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { join,
|
|
1
|
+
import { join, relative } from "node:path";
|
|
2
|
+
import { getPackagePath } from "@opennextjs/aws/build/helper.js";
|
|
2
3
|
import { globSync } from "glob";
|
|
3
4
|
import { normalizePath } from "../../utils/index.js";
|
|
4
5
|
/**
|
|
@@ -8,24 +9,27 @@ import { normalizePath } from "../../utils/index.js";
|
|
|
8
9
|
* Note: we could/should probably just patch readFileSync here or something, but here the issue is that after the readFileSync call
|
|
9
10
|
* there is a vm `runInNewContext` call which we also don't support (source: https://github.com/vercel/next.js/blob/b1e32c5d1f/packages/next/src/server/load-manifest.ts#L88)
|
|
10
11
|
*/
|
|
11
|
-
export function inlineEvalManifest(code,
|
|
12
|
-
const
|
|
12
|
+
export function inlineEvalManifest(code, buildOpts) {
|
|
13
|
+
const { outputDir } = buildOpts;
|
|
14
|
+
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next");
|
|
15
|
+
const appDir = join(baseDir, "server/app");
|
|
16
|
+
const manifests = globSync(join(baseDir, "**/*_client-reference-manifest.js"));
|
|
13
17
|
return code.replace(/function evalManifest\((.+?), .+?\) {/, `$&
|
|
14
|
-
${
|
|
15
|
-
.map((
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
};
|
|
18
|
+
${manifests
|
|
19
|
+
.map((manifest) => {
|
|
20
|
+
const endsWith = normalizePath(relative(baseDir, manifest));
|
|
21
|
+
const key = normalizePath("/" + relative(appDir, manifest)).replace("_client-reference-manifest.js", "");
|
|
22
|
+
return `
|
|
23
|
+
if ($1.endsWith("${endsWith}")) {
|
|
24
|
+
require(${JSON.stringify(manifest)});
|
|
25
|
+
return {
|
|
26
|
+
__RSC_MANIFEST: {
|
|
27
|
+
"${key}": globalThis.__RSC_MANIFEST["${key}"],
|
|
28
|
+
},
|
|
29
|
+
};
|
|
27
30
|
}
|
|
28
|
-
|
|
31
|
+
`;
|
|
32
|
+
})
|
|
29
33
|
.join("\n")}
|
|
30
34
|
throw new Error("Unknown evalManifest: " + $1);
|
|
31
35
|
`);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
|
|
2
2
|
/**
|
|
3
3
|
* Inlines the middleware manifest from the build output to prevent a dynamic require statement
|
|
4
4
|
* as they result in runtime failures.
|
|
5
5
|
*/
|
|
6
|
-
export declare function inlineMiddlewareManifestRequire(code: string,
|
|
6
|
+
export declare function inlineMiddlewareManifestRequire(code: string, buildOpts: BuildOptions): string;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { getPackagePath } from "@opennextjs/aws/build/helper.js";
|
|
3
4
|
/**
|
|
4
5
|
* Inlines the middleware manifest from the build output to prevent a dynamic require statement
|
|
5
6
|
* as they result in runtime failures.
|
|
6
7
|
*/
|
|
7
|
-
export function inlineMiddlewareManifestRequire(code,
|
|
8
|
-
const
|
|
8
|
+
export function inlineMiddlewareManifestRequire(code, buildOpts) {
|
|
9
|
+
const { outputDir } = buildOpts;
|
|
10
|
+
const middlewareManifestPath = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server/middleware-manifest.json");
|
|
9
11
|
const middlewareManifest = existsSync(middlewareManifestPath)
|
|
10
12
|
? JSON.parse(readFileSync(middlewareManifestPath, "utf-8"))
|
|
11
13
|
: {};
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* (source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/lib/find-pages-dir.ts#L4-L13)
|
|
5
|
-
* (usage source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/server/next-server.ts#L450-L451)
|
|
6
|
-
* Note: `findDir` uses `fs.existsSync` under the hood, so patching that should be enough to make this work
|
|
3
|
+
* Patches `findDir` so that the next server can detect whether the `app` or `pages` directory exists
|
|
7
4
|
*/
|
|
8
|
-
export declare function patchFindDir(code: string,
|
|
5
|
+
export declare function patchFindDir(code: string, buildOpts: BuildOptions): string;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { getPackagePath } from "@opennextjs/aws/build/helper.js";
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
* (source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/lib/find-pages-dir.ts#L4-L13)
|
|
6
|
-
* (usage source: https://github.com/vercel/next.js/blob/ba995993/packages/next/src/server/next-server.ts#L450-L451)
|
|
7
|
-
* Note: `findDir` uses `fs.existsSync` under the hood, so patching that should be enough to make this work
|
|
5
|
+
* Patches `findDir` so that the next server can detect whether the `app` or `pages` directory exists
|
|
8
6
|
*/
|
|
9
|
-
export function patchFindDir(code,
|
|
7
|
+
export function patchFindDir(code, buildOpts) {
|
|
8
|
+
const { outputDir } = buildOpts;
|
|
9
|
+
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), ".next/server");
|
|
10
10
|
return code.replace(/function findDir\((?<dir>dir\d*), (?<name>name\d*)\) {/, `function findDir($dir, $name) {
|
|
11
11
|
if ($dir.endsWith(".next/server")) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
if ($name === "app") {
|
|
13
|
+
return ${existsSync(`${join(baseDir, "app")}`)};
|
|
14
|
+
}
|
|
15
|
+
if ($name === "pages") {
|
|
16
|
+
return ${existsSync(`${join(baseDir, "pages")}`)};
|
|
17
|
+
}
|
|
18
18
|
}
|
|
19
19
|
throw new Error("Unknown findDir call: " + $dir + " " + $name);
|
|
20
20
|
`);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function patchBuildId(code: string,
|
|
3
|
-
export declare function patchLoadManifest(code: string,
|
|
1
|
+
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
|
|
2
|
+
export declare function patchBuildId(code: string, buildOpts: BuildOptions): string;
|
|
3
|
+
export declare function patchLoadManifest(code: string, buildOpts: BuildOptions): string;
|
|
@@ -1,26 +1,25 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
-
import { join,
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
|
+
import { getBuildId, getPackagePath } from "@opennextjs/aws/build/helper.js";
|
|
3
4
|
import { globSync } from "glob";
|
|
4
5
|
import { normalizePath } from "../../utils/index.js";
|
|
5
|
-
export function patchBuildId(code,
|
|
6
|
-
// The
|
|
7
|
-
// so we add an early return to the `getBuildId` function so that the `readyFileSync` is never encountered
|
|
8
|
-
// (source: https://github.com/vercel/next.js/blob/15aeb92efb34c09a36/packages/next/src/server/next-server.ts#L438-L451)
|
|
9
|
-
// Note: we could/should probably just patch readFileSync here or something!
|
|
6
|
+
export function patchBuildId(code, buildOpts) {
|
|
7
|
+
// The Next code gets the buildId from the filesystem so we hardcode the value at build time.
|
|
10
8
|
return code.replace("getBuildId() {", `getBuildId() {
|
|
11
|
-
return ${JSON.stringify(
|
|
9
|
+
return ${JSON.stringify(getBuildId(buildOpts))};
|
|
12
10
|
`);
|
|
13
11
|
}
|
|
14
|
-
export function patchLoadManifest(code,
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
12
|
+
export function patchLoadManifest(code, buildOpts) {
|
|
13
|
+
// Inline manifest that Next would otherwise retrieve from the file system.
|
|
14
|
+
const { outputDir } = buildOpts;
|
|
15
|
+
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
|
|
16
|
+
const dotNextDir = join(baseDir, ".next");
|
|
17
|
+
const manifests = globSync(join(dotNextDir, "**/*-manifest.json"));
|
|
19
18
|
return code.replace(/function loadManifest\((.+?), .+?\) {/, `$&
|
|
20
|
-
${
|
|
21
|
-
.map((
|
|
22
|
-
if ($1.endsWith("${
|
|
23
|
-
return ${readFileSync(
|
|
19
|
+
${manifests
|
|
20
|
+
.map((manifest) => `
|
|
21
|
+
if ($1.endsWith("${normalizePath("/" + relative(dotNextDir, manifest))}")) {
|
|
22
|
+
return ${readFileSync(manifest, "utf-8")};
|
|
24
23
|
}
|
|
25
24
|
`)
|
|
26
25
|
.join("\n")}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function patchWranglerDeps(
|
|
1
|
+
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
|
|
2
|
+
export declare function patchWranglerDeps(buildOpts: BuildOptions): void;
|
|
@@ -1,60 +1,30 @@
|
|
|
1
|
-
import { readFileSync,
|
|
1
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { getPackagePath } from "@opennextjs/aws/build/helper.js";
|
|
3
4
|
import * as ts from "ts-morph";
|
|
4
5
|
import { tsParseFile } from "../../utils/index.js";
|
|
5
|
-
export function patchWranglerDeps(
|
|
6
|
+
export function patchWranglerDeps(buildOpts) {
|
|
6
7
|
console.log("# patchWranglerDeps");
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
const { outputDir } = buildOpts;
|
|
9
|
+
const nextDistDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts), "node_modules/next/dist");
|
|
10
|
+
const pagesRuntimeFile = join(nextDistDir, "compiled/next-server/pages.runtime.prod.js");
|
|
11
|
+
const patchedPagesRuntime = readFileSync(pagesRuntimeFile, "utf-8").replace(`e.exports=require("critters")`, `
|
|
12
|
+
try {
|
|
13
|
+
e.exports=require("critters");
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
console.error('critters is not installed');
|
|
17
|
+
}
|
|
18
|
+
`);
|
|
17
19
|
writeFileSync(pagesRuntimeFile, patchedPagesRuntime);
|
|
18
|
-
patchRequireReactDomServerEdge(
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
// [alias]
|
|
24
|
-
// # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
|
|
25
|
-
// # IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
|
|
26
|
-
// # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
|
|
27
|
-
// # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
|
|
28
|
-
// #"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
|
|
29
|
-
const tracerFile = join(distPath, "server/lib/trace/tracer.js");
|
|
20
|
+
patchRequireReactDomServerEdge(nextDistDir);
|
|
21
|
+
// we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
|
|
22
|
+
// try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
|
|
23
|
+
// causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
|
|
24
|
+
const tracerFile = join(nextDistDir, "server/lib/trace/tracer.js");
|
|
30
25
|
const patchedTracer = readFileSync(tracerFile, "utf-8").replaceAll(/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g, `throw new Error("@opentelemetry/api")`);
|
|
31
26
|
writeFileSync(tracerFile, patchedTracer);
|
|
32
27
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Next.js saves the node_modules/next/dist directory in either the standaloneApp path or in the
|
|
35
|
-
* standaloneRoot path, this depends on where the next dependency is actually saved (
|
|
36
|
-
* https://github.com/vercel/next.js/blob/39e06c75/packages/next/src/build/webpack-config.ts#L103-L104
|
|
37
|
-
* ) and can depend on the package manager used, if it is using workspaces, etc...
|
|
38
|
-
*
|
|
39
|
-
* This function checks the two potential paths for the dist directory and returns the first that it finds,
|
|
40
|
-
* it throws an error if it can't find either
|
|
41
|
-
*
|
|
42
|
-
* @param config
|
|
43
|
-
* @returns the node_modules/next/dist directory path
|
|
44
|
-
*/
|
|
45
|
-
function getDistPath(config) {
|
|
46
|
-
for (const root of [config.paths.output.standaloneApp, config.paths.output.standaloneRoot]) {
|
|
47
|
-
try {
|
|
48
|
-
const distPath = join(root, "node_modules/next/dist");
|
|
49
|
-
if (statSync(distPath).isDirectory())
|
|
50
|
-
return distPath;
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
/* empty */
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
throw new Error("Unexpected error: unable to detect the node_modules/next/dist directory");
|
|
57
|
-
}
|
|
58
28
|
/**
|
|
59
29
|
* `react-dom` v>=19 has a `server.edge` export: https://github.com/facebook/react/blob/a160102f3/packages/react-dom/package.json#L79
|
|
60
30
|
* but version of `react-dom` <= 18 do not have this export but have a `server.browser` export instead: https://github.com/facebook/react/blob/8a015b68/packages/react-dom/package.json#L49
|
|
@@ -70,10 +40,9 @@ function getDistPath(config) {
|
|
|
70
40
|
* it's not clear what code and how might be rely on this require call)
|
|
71
41
|
*
|
|
72
42
|
*/
|
|
73
|
-
function patchRequireReactDomServerEdge(
|
|
74
|
-
const distPath = getDistPath(config);
|
|
43
|
+
function patchRequireReactDomServerEdge(nextDistDir) {
|
|
75
44
|
// Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
|
|
76
|
-
const pagesRuntimeFile = join(
|
|
45
|
+
const pagesRuntimeFile = join(nextDistDir, "compiled/next-server/pages.runtime.prod.js");
|
|
77
46
|
const code = readFileSync(pagesRuntimeFile, "utf-8");
|
|
78
47
|
const file = tsParseFile(code);
|
|
79
48
|
// we need to update this function: `e=>{"use strict";e.exports=require("react-dom/server.edge")}`
|
|
@@ -125,21 +94,21 @@ function patchRequireReactDomServerEdge(config) {
|
|
|
125
94
|
return;
|
|
126
95
|
}
|
|
127
96
|
arrowFunction.setBodyText(`
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
97
|
+
// OpenNext patch
|
|
98
|
+
let ReactDOMServer;
|
|
99
|
+
try {
|
|
100
|
+
ReactDOMServer = require('react-dom/server.edge');
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (
|
|
103
|
+
error.code !== 'MODULE_NOT_FOUND' &&
|
|
104
|
+
error.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
|
|
105
|
+
) {
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
ReactDOMServer = require('react-dom/server.browser');
|
|
109
|
+
}
|
|
110
|
+
${parameterName}.exports = ReactDOMServer;
|
|
111
|
+
`);
|
|
143
112
|
});
|
|
144
113
|
const updatedCode = file.print();
|
|
145
114
|
writeFileSync(pagesRuntimeFile, updatedCode);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProjectOptions } from "../../
|
|
1
|
+
import type { ProjectOptions } from "../../project-options.js";
|
|
2
2
|
/**
|
|
3
3
|
* Creates a `wrangler.json` file for the user if a wrangler config file doesn't already exist,
|
|
4
4
|
* but only after asking for the user's confirmation.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
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.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"opennextjs-cloudflare": "dist/cli/index.js"
|
|
@@ -61,16 +61,16 @@
|
|
|
61
61
|
"vitest": "^2.1.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@ast-grep/napi": "^0.
|
|
64
|
+
"@ast-grep/napi": "^0.34.1",
|
|
65
65
|
"@dotenvx/dotenvx": "1.31.0",
|
|
66
|
-
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@
|
|
66
|
+
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@712",
|
|
67
67
|
"enquirer": "^2.4.1",
|
|
68
68
|
"glob": "^11.0.0",
|
|
69
69
|
"ts-morph": "^23.0.0",
|
|
70
70
|
"yaml": "^2.7.0"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
|
-
"wrangler": "^3.
|
|
73
|
+
"wrangler": "^3.105.0"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"clean": "rimraf dist",
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
// Note: this symbol needs to be kept in sync with the one used in `src/cli/templates/worker.ts`
|
|
2
|
-
const cloudflareContextSymbol = Symbol.for("__cloudflare-context__");
|
|
3
|
-
/**
|
|
4
|
-
* Utility to get the current Cloudflare context
|
|
5
|
-
*
|
|
6
|
-
* @returns the cloudflare context
|
|
7
|
-
*/
|
|
8
|
-
export async function getCloudflareContext() {
|
|
9
|
-
const global = globalThis;
|
|
10
|
-
const cloudflareContext = global[cloudflareContextSymbol];
|
|
11
|
-
if (!cloudflareContext) {
|
|
12
|
-
// the cloudflare context is initialized by the worker and is always present in production/preview,
|
|
13
|
-
// so, it not being present means that the application is running under `next dev`
|
|
14
|
-
return getCloudflareContextInNextDev();
|
|
15
|
-
}
|
|
16
|
-
return cloudflareContext;
|
|
17
|
-
}
|
|
18
|
-
const cloudflareContextInNextDevSymbol = Symbol.for("__next-dev/cloudflare-context__");
|
|
19
|
-
/**
|
|
20
|
-
* Gets a local proxy version of the cloudflare context (created using `getPlatformProxy`) when
|
|
21
|
-
* running in the standard next dev server (via `next dev`)
|
|
22
|
-
*
|
|
23
|
-
* @returns the local proxy version of the cloudflare context
|
|
24
|
-
*/
|
|
25
|
-
async function getCloudflareContextInNextDev() {
|
|
26
|
-
const global = globalThis;
|
|
27
|
-
if (!global[cloudflareContextInNextDevSymbol]) {
|
|
28
|
-
// Note: we never want wrangler to be bundled in the Next.js app, that's why the import below looks like it does
|
|
29
|
-
const { getPlatformProxy } = await import(
|
|
30
|
-
/* webpackIgnore: true */ `${"__wrangler".replaceAll("_", "")}`);
|
|
31
|
-
const { env, cf, ctx } = await getPlatformProxy({
|
|
32
|
-
// This allows the selection of a wrangler environment while running in next dev mode
|
|
33
|
-
environment: process.env.NEXT_DEV_WRANGLER_ENV,
|
|
34
|
-
});
|
|
35
|
-
global[cloudflareContextInNextDevSymbol] = {
|
|
36
|
-
env,
|
|
37
|
-
cf: cf,
|
|
38
|
-
ctx: ctx,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
return global[cloudflareContextInNextDevSymbol];
|
|
42
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Config } from "../../../config.js";
|
|
2
|
-
/**
|
|
3
|
-
* The following avoid various Next.js specific files `require`d at runtime since we can just read
|
|
4
|
-
* and inline their content during build time
|
|
5
|
-
*/
|
|
6
|
-
export declare function inlineNextRequire(code: string, config: Config): string;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
/**
|
|
4
|
-
* The following avoid various Next.js specific files `require`d at runtime since we can just read
|
|
5
|
-
* and inline their content during build time
|
|
6
|
-
*/
|
|
7
|
-
export function inlineNextRequire(code, config) {
|
|
8
|
-
const pagesManifestFile = join(config.paths.output.standaloneAppServer, "pages-manifest.json");
|
|
9
|
-
const appPathsManifestFile = join(config.paths.output.standaloneAppServer, "app-paths-manifest.json");
|
|
10
|
-
const pagesManifestFiles = existsSync(pagesManifestFile)
|
|
11
|
-
? Object.values(JSON.parse(readFileSync(pagesManifestFile, "utf-8"))).map((file) => ".next/server/" + file)
|
|
12
|
-
: [];
|
|
13
|
-
const appPathsManifestFiles = existsSync(appPathsManifestFile)
|
|
14
|
-
? Object.values(JSON.parse(readFileSync(appPathsManifestFile, "utf-8"))).map((file) => ".next/server/" + file)
|
|
15
|
-
: [];
|
|
16
|
-
const allManifestFiles = pagesManifestFiles.concat(appPathsManifestFiles);
|
|
17
|
-
const htmlPages = allManifestFiles.filter((file) => file.endsWith(".html"));
|
|
18
|
-
const pageModules = allManifestFiles.filter((file) => file.endsWith(".js"));
|
|
19
|
-
return code.replace(/const pagePath = getPagePath\(.+?\);/, `$&
|
|
20
|
-
${htmlPages
|
|
21
|
-
.map((htmlPage) => `
|
|
22
|
-
if (pagePath.endsWith("${htmlPage}")) {
|
|
23
|
-
return ${JSON.stringify(readFileSync(join(config.paths.output.standaloneApp, htmlPage), "utf-8"))};
|
|
24
|
-
}
|
|
25
|
-
`)
|
|
26
|
-
.join("\n")}
|
|
27
|
-
${pageModules
|
|
28
|
-
.map((module) => `
|
|
29
|
-
if (pagePath.endsWith("${module}")) {
|
|
30
|
-
return require(${JSON.stringify(join(config.paths.output.standaloneApp, module))});
|
|
31
|
-
}
|
|
32
|
-
`)
|
|
33
|
-
.join("\n")}
|
|
34
|
-
throw new Error("Unknown pagePath: " + pagePath);
|
|
35
|
-
`);
|
|
36
|
-
}
|
package/dist/cli/config.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
export type Config = {
|
|
2
|
-
build: {
|
|
3
|
-
skipNextBuild: boolean;
|
|
4
|
-
shouldMinify: boolean;
|
|
5
|
-
};
|
|
6
|
-
paths: {
|
|
7
|
-
source: {
|
|
8
|
-
root: string;
|
|
9
|
-
dotNext: string;
|
|
10
|
-
standaloneRoot: string;
|
|
11
|
-
};
|
|
12
|
-
output: {
|
|
13
|
-
root: string;
|
|
14
|
-
assets: string;
|
|
15
|
-
dotNext: string;
|
|
16
|
-
standaloneRoot: string;
|
|
17
|
-
standaloneApp: string;
|
|
18
|
-
standaloneAppDotNext: string;
|
|
19
|
-
standaloneAppServer: string;
|
|
20
|
-
};
|
|
21
|
-
internal: {
|
|
22
|
-
package: string;
|
|
23
|
-
templates: string;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
internalPackageName: string;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Computes the configuration.
|
|
30
|
-
*
|
|
31
|
-
* @param projectOpts The options for the project
|
|
32
|
-
* @returns The configuration, see `Config`
|
|
33
|
-
*/
|
|
34
|
-
export declare function getConfig(projectOpts: ProjectOptions): Config;
|
|
35
|
-
export declare function containsDotNextDir(folder: string): boolean;
|
|
36
|
-
export type ProjectOptions = {
|
|
37
|
-
sourceDir: string;
|
|
38
|
-
outputDir: string;
|
|
39
|
-
skipNextBuild: boolean;
|
|
40
|
-
skipWranglerConfigCheck: boolean;
|
|
41
|
-
minify: boolean;
|
|
42
|
-
};
|
package/dist/cli/config.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { readdirSync, statSync } from "node:fs";
|
|
2
|
-
import { join, relative } from "node:path";
|
|
3
|
-
const PACKAGE_NAME = "@opennextjs/cloudflare";
|
|
4
|
-
/**
|
|
5
|
-
* Computes the configuration.
|
|
6
|
-
*
|
|
7
|
-
* @param projectOpts The options for the project
|
|
8
|
-
* @returns The configuration, see `Config`
|
|
9
|
-
*/
|
|
10
|
-
export function getConfig(projectOpts) {
|
|
11
|
-
const sourceDirDotNext = join(projectOpts.sourceDir, ".next");
|
|
12
|
-
const dotNext = join(projectOpts.outputDir, ".next");
|
|
13
|
-
const appPath = getNextjsApplicationPath(dotNext).replace(/\/$/, "");
|
|
14
|
-
const standaloneRoot = join(dotNext, "standalone");
|
|
15
|
-
const standaloneApp = join(standaloneRoot, appPath);
|
|
16
|
-
const standaloneAppDotNext = join(standaloneApp, ".next");
|
|
17
|
-
const standaloneAppServer = join(standaloneAppDotNext, "server");
|
|
18
|
-
const nodeModules = join(standaloneApp, "node_modules");
|
|
19
|
-
const internalPackage = join(nodeModules, ...PACKAGE_NAME.split("/"));
|
|
20
|
-
const internalTemplates = join(internalPackage, "cli/templates");
|
|
21
|
-
return {
|
|
22
|
-
build: {
|
|
23
|
-
skipNextBuild: projectOpts.skipNextBuild,
|
|
24
|
-
shouldMinify: projectOpts.minify,
|
|
25
|
-
},
|
|
26
|
-
paths: {
|
|
27
|
-
source: {
|
|
28
|
-
root: projectOpts.sourceDir,
|
|
29
|
-
dotNext: sourceDirDotNext,
|
|
30
|
-
standaloneRoot: join(sourceDirDotNext, "standalone"),
|
|
31
|
-
},
|
|
32
|
-
output: {
|
|
33
|
-
root: projectOpts.outputDir,
|
|
34
|
-
assets: join(projectOpts.outputDir, "assets"),
|
|
35
|
-
dotNext,
|
|
36
|
-
standaloneRoot,
|
|
37
|
-
standaloneApp,
|
|
38
|
-
standaloneAppDotNext,
|
|
39
|
-
standaloneAppServer,
|
|
40
|
-
},
|
|
41
|
-
internal: {
|
|
42
|
-
package: internalPackage,
|
|
43
|
-
templates: internalTemplates,
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
internalPackageName: PACKAGE_NAME,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
export function containsDotNextDir(folder) {
|
|
50
|
-
try {
|
|
51
|
-
return statSync(join(folder, ".next")).isDirectory();
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* It basically tries to find the path that the application is under inside the `.next/standalone` directory, using the `.next/server` directory
|
|
59
|
-
* presence as the condition that needs to be met.
|
|
60
|
-
*
|
|
61
|
-
* For example:
|
|
62
|
-
* When I build the api application the `.next/server` directory is located in:
|
|
63
|
-
* `<dotNextDir>/standalone/next-apps/api/.next/server`
|
|
64
|
-
* and the function here given the `dotNextDir` returns `next-apps/api`
|
|
65
|
-
*/
|
|
66
|
-
function getNextjsApplicationPath(dotNextDir) {
|
|
67
|
-
const serverPath = findServerParentPath(dotNextDir);
|
|
68
|
-
if (!serverPath) {
|
|
69
|
-
throw new Error(`Unexpected Error: no \`.next/server\` folder could be found in \`${serverPath}\``);
|
|
70
|
-
}
|
|
71
|
-
return relative(join(dotNextDir, "standalone"), serverPath);
|
|
72
|
-
}
|
|
73
|
-
function findServerParentPath(parentPath) {
|
|
74
|
-
try {
|
|
75
|
-
if (statSync(join(parentPath, ".next/server")).isDirectory()) {
|
|
76
|
-
return parentPath;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
/* empty */
|
|
81
|
-
}
|
|
82
|
-
const folders = readdirSync(parentPath);
|
|
83
|
-
for (const folder of folders) {
|
|
84
|
-
const subFolder = join(parentPath, folder);
|
|
85
|
-
if (statSync(join(parentPath, folder)).isDirectory()) {
|
|
86
|
-
const dirServerPath = findServerParentPath(subFolder);
|
|
87
|
-
if (dirServerPath) {
|
|
88
|
-
return dirServerPath;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|