@opennextjs/cloudflare 0.4.4 → 0.4.6
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/dist/cli/build/bundle-server.js +10 -4
- package/dist/cli/build/open-next/compile-env-files.js +3 -1
- package/dist/cli/build/patches/investigated/update-webpack-chunks-file/get-file-content-with-updated-webpack-f-require-code.js +12 -4
- package/dist/cli/build/patches/investigated/update-webpack-chunks-file/get-file-content-with-updated-webpack-f-require-code.test.js +6 -4
- package/dist/cli/build/utils/version.js +1 -1
- package/dist/cli/templates/worker.js +10 -7
- package/package.json +2 -2
|
@@ -50,6 +50,16 @@ export async function bundleServer(buildOpts) {
|
|
|
50
50
|
target: "esnext",
|
|
51
51
|
minify: false,
|
|
52
52
|
metafile: true,
|
|
53
|
+
// Next traces files using the default conditions from `nft` (`node`, `require`, `import` and `default`)
|
|
54
|
+
//
|
|
55
|
+
// Because we use the `node` platform for this build, the "module" condition is used when no conditions are defined.
|
|
56
|
+
// We explicitly set the conditions to an empty array to disable the "module" condition in order to match Next tracing.
|
|
57
|
+
//
|
|
58
|
+
// See:
|
|
59
|
+
// - default nft conditions: https://github.com/vercel/nft/blob/2b55b01/readme.md#exports--imports
|
|
60
|
+
// - Next no explicit override: https://github.com/vercel/next.js/blob/2efcf11/packages/next/src/build/collect-build-traces.ts#L287
|
|
61
|
+
// - ESBuild `node` platform: https://esbuild.github.io/api/#platform
|
|
62
|
+
conditions: [],
|
|
53
63
|
plugins: [
|
|
54
64
|
createFixRequiresESBuildPlugin(buildOpts),
|
|
55
65
|
inlineRequirePagePlugin(buildOpts),
|
|
@@ -126,10 +136,6 @@ globalThis.__BUILD_TIMESTAMP_MS__ = ${Date.now()};
|
|
|
126
136
|
`,
|
|
127
137
|
},
|
|
128
138
|
});
|
|
129
|
-
if (result.errors.length > 0) {
|
|
130
|
-
result.errors.forEach((error) => console.error(error));
|
|
131
|
-
throw new Error(`There was a problem bundling the server.`);
|
|
132
|
-
}
|
|
133
139
|
fs.writeFileSync(openNextServerBundle + ".meta.json", JSON.stringify(result.metafile, null, 2));
|
|
134
140
|
await updateWorkerBundledCode(openNextServerBundle, buildOpts);
|
|
135
141
|
const isMonorepo = monorepoRoot !== appPath;
|
|
@@ -5,5 +5,7 @@ import { extractProjectEnvVars } from "../utils/index.js";
|
|
|
5
5
|
* Compiles the values extracted from the project's env files to the output directory for use in the worker.
|
|
6
6
|
*/
|
|
7
7
|
export function compileEnvFiles(buildOpts) {
|
|
8
|
-
|
|
8
|
+
const envDir = path.join(buildOpts.outputDir, "env");
|
|
9
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
10
|
+
["production", "development", "test"].forEach((mode) => fs.appendFileSync(path.join(envDir, `next-env.mjs`), `export const ${mode} = ${JSON.stringify(extractProjectEnvVars(mode, buildOpts))};\n`));
|
|
9
11
|
}
|
|
@@ -68,9 +68,17 @@ export async function getFileContentWithUpdatedWebpackFRequireCode(sourceFile, {
|
|
|
68
68
|
.map((parameter) => parameter.getName());
|
|
69
69
|
const chunkId = functionParameterNames[0];
|
|
70
70
|
const functionBody = webpackFRequireFunction.getBody();
|
|
71
|
-
functionBody.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
functionBody.replaceWithText(`
|
|
72
|
+
{
|
|
73
|
+
if (${installedChunks}[${chunkId}]) {
|
|
74
|
+
return;
|
|
75
|
+
}${chunks
|
|
76
|
+
.map((chunk) => `
|
|
77
|
+
if(${chunkId} === ${chunk}) {
|
|
78
|
+
return ${installChunk}(require("./chunks/${chunk}.js"));
|
|
79
|
+
}`)
|
|
80
|
+
.join("")}
|
|
81
|
+
throw new Error(\`Unknown chunk \${${chunkId}}\`);
|
|
82
|
+
}`);
|
|
75
83
|
return sourceFile.print();
|
|
76
84
|
}
|
|
@@ -7,15 +7,17 @@ describe("getFileContentWithUpdatedWebpackFRequireCode", () => {
|
|
|
7
7
|
const fileContent = await readFile(`${import.meta.dirname}/test-fixtures/unminified-webpacks-file.js`, "utf8");
|
|
8
8
|
const tsSourceFile = tsParseFile(fileContent);
|
|
9
9
|
const updatedFCode = await getFileContentWithUpdatedWebpackFRequireCode(tsSourceFile, { installChunk: "installChunk", installedChunks: "installedChunks" }, ["658"]);
|
|
10
|
-
expect(unstyleCode(updatedFCode)).toContain(`if (installedChunks[chunkId]) return
|
|
11
|
-
expect(unstyleCode(updatedFCode)).toContain(`if (chunkId === 658) return installChunk(require("./chunks/658.js"))
|
|
10
|
+
expect(unstyleCode(updatedFCode)).toContain(`if (installedChunks[chunkId]) { return; }`);
|
|
11
|
+
expect(unstyleCode(updatedFCode)).toContain(`if (chunkId === 658) { return installChunk(require("./chunks/658.js")); }`);
|
|
12
|
+
expect(unstyleCode(updatedFCode)).not.toContain(`require("./chunks/" +`);
|
|
12
13
|
});
|
|
13
14
|
test("returns the updated content of the f.require function from minified webpack runtime code", async () => {
|
|
14
15
|
const fileContent = await readFile(`${import.meta.dirname}/test-fixtures/minified-webpacks-file.js`, "utf8");
|
|
15
16
|
const tsSourceFile = tsParseFile(fileContent);
|
|
16
17
|
const updatedFCode = await getFileContentWithUpdatedWebpackFRequireCode(tsSourceFile, { installChunk: "r", installedChunks: "e" }, ["658"]);
|
|
17
|
-
expect(unstyleCode(updatedFCode)).toContain("if (e[o]) return;");
|
|
18
|
-
expect(unstyleCode(updatedFCode)).toContain(`if (o === 658) return r(require("./chunks/658.js"))
|
|
18
|
+
expect(unstyleCode(updatedFCode)).toContain("if (e[o]) { return; }");
|
|
19
|
+
expect(unstyleCode(updatedFCode)).toContain(`if (o === 658) { return r(require("./chunks/658.js")); }`);
|
|
20
|
+
expect(unstyleCode(updatedFCode)).not.toContain(`require("./chunks/" +`);
|
|
19
21
|
});
|
|
20
22
|
});
|
|
21
23
|
function unstyleCode(text) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { fileURLToPath, URL } from "node:url";
|
|
4
4
|
export function getVersion() {
|
|
5
5
|
const require = createRequire(import.meta.url);
|
|
6
6
|
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
// @ts-expect-error: resolved by wrangler build
|
|
3
|
+
import * as nextEnvVars from "./env/next-env.mjs";
|
|
4
|
+
// @ts-expect-error: resolved by wrangler build
|
|
3
5
|
import { handler as middlewareHandler } from "./middleware/handler.mjs";
|
|
4
6
|
// @ts-expect-error: resolved by wrangler build
|
|
5
7
|
import { handler as serverHandler } from "./server-functions/default/handler.mjs";
|
|
@@ -12,14 +14,13 @@ globalThis[Symbol.for("__cloudflare-context__")] = new Proxy({}, {
|
|
|
12
14
|
get: (_, property) => Reflect.get(cloudflareContextALS.getStore(), property),
|
|
13
15
|
set: (_, property, value) => Reflect.set(cloudflareContextALS.getStore(), property, value),
|
|
14
16
|
});
|
|
17
|
+
// Populate process.env on the first request
|
|
18
|
+
let processEnvPopulated = false;
|
|
15
19
|
export default {
|
|
16
20
|
async fetch(request, env, ctx) {
|
|
17
21
|
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
|
|
18
22
|
const url = new URL(request.url);
|
|
19
|
-
|
|
20
|
-
await populateProcessEnv(url, env.NEXTJS_ENV);
|
|
21
|
-
process.env.__PROCESS_ENV_POPULATED = "1";
|
|
22
|
-
}
|
|
23
|
+
populateProcessEnv(url, env.NEXTJS_ENV);
|
|
23
24
|
if (url.pathname === "/_next/image") {
|
|
24
25
|
const imageUrl = url.searchParams.get("url") ?? "";
|
|
25
26
|
return imageUrl.startsWith("/")
|
|
@@ -44,9 +45,11 @@ export default {
|
|
|
44
45
|
*
|
|
45
46
|
* Note that cloudflare env string values are copied by the middleware handler.
|
|
46
47
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
function populateProcessEnv(url, nextJsEnv) {
|
|
49
|
+
if (processEnvPopulated) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
processEnvPopulated = true;
|
|
50
53
|
const mode = nextJsEnv ?? "production";
|
|
51
54
|
if (nextEnvVars[mode]) {
|
|
52
55
|
for (const key in nextEnvVars[mode]) {
|
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.
|
|
4
|
+
"version": "0.4.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"opennextjs-cloudflare": "dist/cli/index.js"
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"dependencies": {
|
|
64
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@724",
|
|
67
67
|
"enquirer": "^2.4.1",
|
|
68
68
|
"glob": "^11.0.0",
|
|
69
69
|
"ts-morph": "^23.0.0",
|