Skip to content

Commit fe0e45a

Browse files
authoredFeb 27, 2025··
refactor: simplify the code (#757)
1 parent 096c273 commit fe0e45a

File tree

5 files changed

+42
-74
lines changed

5 files changed

+42
-74
lines changed
 

‎packages/open-next/src/adapters/config/util.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function loadBuildId(nextDir: string) {
2222
}
2323

2424
export function loadHtmlPages(nextDir: string) {
25-
const filePath = path.join(nextDir, "server", "pages-manifest.json");
25+
const filePath = path.join(nextDir, "server/pages-manifest.json");
2626
const json = fs.readFileSync(filePath, "utf-8");
2727
return Object.entries(JSON.parse(json))
2828
.filter(([_, value]) => (value as string).endsWith(".html"))
@@ -81,8 +81,7 @@ export function loadPrerenderManifest(nextDir: string) {
8181
export function loadAppPathsManifest(nextDir: string) {
8282
const appPathsManifestPath = path.join(
8383
nextDir,
84-
"server",
85-
"app-paths-manifest.json",
84+
"server/app-paths-manifest.json",
8685
);
8786
const appPathsManifestJson = fs.existsSync(appPathsManifestPath)
8887
? fs.readFileSync(appPathsManifestPath, "utf-8")
@@ -120,7 +119,7 @@ export function loadAppPathsManifestKeys(nextDir: string) {
120119
}
121120

122121
export function loadMiddlewareManifest(nextDir: string) {
123-
const filePath = path.join(nextDir, "server", "middleware-manifest.json");
122+
const filePath = path.join(nextDir, "server/middleware-manifest.json");
124123
const json = fs.readFileSync(filePath, "utf-8");
125124
return JSON.parse(json) as MiddlewareManifest;
126125
}

‎packages/open-next/src/build/copyTracedFiles.ts

+10-21
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
writeFileSync,
1313
} from "node:fs";
1414
import path from "node:path";
15-
import type { NextConfig, PrerenderManifest } from "types/next-types";
1615

16+
import { loadConfig, loadPrerenderManifest } from "config/util.js";
1717
import logger from "../logger.js";
1818
import { MIDDLEWARE_TRACE_FILE } from "./constant.js";
1919

@@ -47,6 +47,7 @@ export async function copyTracedFiles({
4747
const dotNextDir = path.join(buildOutputPath, ".next");
4848
const standaloneDir = path.join(dotNextDir, "standalone");
4949
const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
50+
const standaloneServerDir = path.join(standaloneNextDir, "server");
5051
const outputNextDir = path.join(outputDir, packagePath, ".next");
5152

5253
const extractFiles = (files: string[], from = standaloneNextDir) =>
@@ -75,12 +76,12 @@ export async function copyTracedFiles({
7576
});
7677
}
7778
// create directory for pages
78-
if (existsSync(path.join(standaloneDir, ".next/server/pages"))) {
79+
if (existsSync(path.join(standaloneServerDir, "pages"))) {
7980
mkdirSync(path.join(outputNextDir, "server/pages"), {
8081
recursive: true,
8182
});
8283
}
83-
if (existsSync(path.join(standaloneDir, ".next/server/app"))) {
84+
if (existsSync(path.join(standaloneServerDir, "app"))) {
8485
mkdirSync(path.join(outputNextDir, "server/app"), {
8586
recursive: true,
8687
});
@@ -239,17 +240,15 @@ File ${fullFilePath} does not exist
239240

240241
mkdirSync(path.join(outputNextDir, "server"), { recursive: true });
241242

242-
readdirSync(path.join(standaloneNextDir, "server"))
243+
readdirSync(standaloneServerDir)
243244
.filter(
244245
(fileOrDir) =>
245-
!statSync(
246-
path.join(standaloneNextDir, "server", fileOrDir),
247-
).isDirectory(),
246+
!statSync(path.join(standaloneServerDir, fileOrDir)).isDirectory(),
248247
)
249248
.filter((file) => file !== "server.js")
250249
.forEach((file) =>
251250
copyFileSync(
252-
path.join(standaloneNextDir, "server", file),
251+
path.join(standaloneServerDir, file),
253252
path.join(path.join(outputNextDir, "server"), file),
254253
),
255254
);
@@ -285,24 +284,14 @@ File ${fullFilePath} does not exist
285284
const staticFiles: Array<string> = Object.values(
286285
JSON.parse(
287286
readFileSync(
288-
path.join(standaloneNextDir, "server/pages-manifest.json"),
287+
path.join(standaloneServerDir, "pages-manifest.json"),
289288
"utf8",
290289
),
291290
),
292291
);
293292
// Then we need to get all fallback: true dynamic routes html
294-
const prerenderManifest = JSON.parse(
295-
readFileSync(
296-
path.join(standaloneNextDir, "prerender-manifest.json"),
297-
"utf8",
298-
),
299-
) as PrerenderManifest;
300-
const config = JSON.parse(
301-
readFileSync(
302-
path.join(standaloneNextDir, "required-server-files.json"),
303-
"utf8",
304-
),
305-
).config as NextConfig;
293+
const prerenderManifest = loadPrerenderManifest(standaloneNextDir);
294+
const config = loadConfig(standaloneNextDir);
306295
const locales = config.i18n?.locales;
307296
Object.values(prerenderManifest.dynamicRoutes).forEach((route) => {
308297
if (typeof route.fallback === "string") {

‎packages/open-next/src/build/createMiddleware.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import fs from "node:fs";
22
import path from "node:path";
33

4-
import { loadFunctionsConfigManifest } from "config/util.js";
4+
import {
5+
loadFunctionsConfigManifest,
6+
loadMiddlewareManifest,
7+
} from "config/util.js";
58
import logger from "../logger.js";
6-
import type {
7-
MiddlewareInfo,
8-
MiddlewareManifest,
9-
} from "../types/next-types.js";
9+
import type { MiddlewareInfo } from "../types/next-types.js";
1010
import { buildEdgeBundle } from "./edge/createEdgeBundle.js";
1111
import * as buildHelper from "./helper.js";
1212
import { installDependencies } from "./installDeps.js";
@@ -27,15 +27,11 @@ export async function createMiddleware(
2727
) {
2828
logger.info("Bundling middleware function...");
2929

30-
const { appBuildOutputPath, config, outputDir } = options;
30+
const { config, outputDir } = options;
31+
const buildOutputDotNextDir = path.join(options.appBuildOutputPath, ".next");
3132

3233
// Get middleware manifest
33-
const middlewareManifest = JSON.parse(
34-
fs.readFileSync(
35-
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
36-
"utf8",
37-
),
38-
) as MiddlewareManifest;
34+
const middlewareManifest = loadMiddlewareManifest(buildOutputDotNextDir);
3935

4036
const edgeMiddlewareInfo = middlewareManifest.middleware["/"] as
4137
| MiddlewareInfo
@@ -44,7 +40,7 @@ export async function createMiddleware(
4440
if (!edgeMiddlewareInfo) {
4541
// If there is no middleware info, it might be a node middleware
4642
const functionsConfigManifest = loadFunctionsConfigManifest(
47-
path.join(appBuildOutputPath, ".next"),
43+
buildOutputDotNextDir,
4844
);
4945

5046
if (functionsConfigManifest?.functions["/_middleware"]) {

‎packages/open-next/src/build/edge/createEdgeBundle.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdirSync } from "node:fs";
33
import fs from "node:fs";
44
import path from "node:path";
55
import { build } from "esbuild";
6-
import type { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
6+
import type { MiddlewareInfo } from "types/next-types";
77
import type {
88
IncludedConverter,
99
IncludedOriginResolver,
@@ -13,6 +13,7 @@ import type {
1313
SplittedFunctionOptions,
1414
} from "types/open-next";
1515

16+
import { loadMiddlewareManifest } from "config/util.js";
1617
import type { OriginResolver } from "types/overrides.js";
1718
import logger from "../../logger.js";
1819
import { openNextEdgePlugins } from "../../plugins/edge.js";
@@ -173,23 +174,19 @@ export async function generateEdgeBundle(
173174
options: BuildOptions,
174175
fnOptions: SplittedFunctionOptions,
175176
) {
176-
const { appBuildOutputPath, outputDir } = options;
177177
logger.info(`Generating edge bundle for: ${name}`);
178178

179+
const buildOutputDotNextDir = path.join(options.appBuildOutputPath, ".next");
180+
179181
// Create output folder
180-
const outputPath = path.join(outputDir, "server-functions", name);
181-
fs.mkdirSync(outputPath, { recursive: true });
182+
const outputDir = path.join(options.outputDir, "server-functions", name);
183+
fs.mkdirSync(outputDir, { recursive: true });
182184

183185
// Copy open-next.config.mjs
184-
copyOpenNextConfig(options.buildDir, outputPath, true);
186+
copyOpenNextConfig(options.buildDir, outputDir, true);
185187

186188
// Load middleware manifest
187-
const middlewareManifest = JSON.parse(
188-
fs.readFileSync(
189-
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
190-
"utf8",
191-
),
192-
) as MiddlewareManifest;
189+
const middlewareManifest = loadMiddlewareManifest(buildOutputDotNextDir);
193190

194191
// Find functions
195192
const functions = Object.values(middlewareManifest.functions).filter((fn) =>
@@ -203,32 +200,28 @@ export async function generateEdgeBundle(
203200

204201
//Copy wasm files
205202
const wasmFiles = middlewareInfo.wasm;
206-
mkdirSync(path.join(outputPath, "wasm"), { recursive: true });
203+
mkdirSync(path.join(outputDir, "wasm"), { recursive: true });
207204
for (const wasmFile of wasmFiles) {
208205
fs.copyFileSync(
209-
path.join(appBuildOutputPath, ".next", wasmFile.filePath),
210-
path.join(outputPath, `wasm/${wasmFile.name}.wasm`),
206+
path.join(buildOutputDotNextDir, wasmFile.filePath),
207+
path.join(outputDir, `wasm/${wasmFile.name}.wasm`),
211208
);
212209
}
213210

214211
// Copy assets
215212
const assets = middlewareInfo.assets;
216-
mkdirSync(path.join(outputPath, "assets"), { recursive: true });
213+
mkdirSync(path.join(outputDir, "assets"), { recursive: true });
217214
for (const asset of assets) {
218215
fs.copyFileSync(
219-
path.join(appBuildOutputPath, ".next", asset.filePath),
220-
path.join(outputPath, `assets/${asset.name}`),
216+
path.join(buildOutputDotNextDir, asset.filePath),
217+
path.join(outputDir, `assets/${asset.name}`),
221218
);
222219
}
223220

224221
await buildEdgeBundle({
225222
middlewareInfo,
226-
entrypoint: path.join(
227-
options.openNextDistDir,
228-
"adapters",
229-
"edge-adapter.js",
230-
),
231-
outfile: path.join(outputPath, "index.mjs"),
223+
entrypoint: path.join(options.openNextDistDir, "adapters/edge-adapter.js"),
224+
outfile: path.join(outputDir, "index.mjs"),
232225
options,
233226
overrides: fnOptions.override,
234227
additionalExternals: options.config.edgeExternals,

‎packages/open-next/src/build/generateOutput.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as fs from "node:fs";
22
import path from "node:path";
3-
4-
import type { NextConfig } from "types/next-types.js";
53
import type {
64
BaseOverride,
75
DefaultOverrideOptions,
@@ -10,6 +8,7 @@ import type {
108
OverrideOptions,
119
} from "types/open-next";
1210

11+
import { loadConfig } from "config/util.js";
1312
import { type BuildOptions, getBuildId } from "./helper.js";
1413

1514
type BaseFunction = {
@@ -188,14 +187,8 @@ export async function generateOutput(options: BuildOptions) {
188187

189188
const defaultOriginCanstream = await canStream(config.default);
190189

191-
//Load required-server-files.json
192-
const requiredServerFiles = JSON.parse(
193-
fs.readFileSync(
194-
path.join(appBuildOutputPath, ".next", "required-server-files.json"),
195-
"utf-8",
196-
),
197-
).config as NextConfig;
198-
const prefixer = prefixPattern(requiredServerFiles.basePath ?? "");
190+
const nextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
191+
const prefixer = prefixPattern(nextConfig.basePath ?? "");
199192

200193
// First add s3 origins and image optimization
201194

@@ -206,9 +199,7 @@ export async function generateOutput(options: BuildOptions) {
206199
copy: [
207200
{
208201
from: ".open-next/assets",
209-
to: requiredServerFiles.basePath
210-
? `_assets${requiredServerFiles.basePath}`
211-
: "_assets",
202+
to: nextConfig.basePath ? `_assets${nextConfig.basePath}` : "_assets",
212203
cached: true,
213204
versionedSubDir: prefixer("_next"),
214205
},

0 commit comments

Comments
 (0)
Please sign in to comment.