Skip to content

Commit 8624ae9

Browse files
authored
refactor: code cleanup (#602)
1 parent 5bf072f commit 8624ae9

File tree

6 files changed

+15
-27
lines changed

6 files changed

+15
-27
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,13 @@ export async function createMiddleware(options: buildHelper.BuildOptions) {
3131
return { useMiddleware: false };
3232
}
3333

34-
// Create output folder
35-
let outputPath = path.join(outputDir, "server-function");
36-
3734
const commonMiddlewareOptions = {
3835
middlewareInfo: entry,
3936
options,
40-
appBuildOutputPath,
4137
};
4238

4339
if (config.middleware?.external) {
44-
outputPath = path.join(outputDir, "middleware");
40+
const outputPath = path.join(outputDir, "middleware");
4541
fs.mkdirSync(outputPath, { recursive: true });
4642

4743
// Copy open-next.config.mjs

packages/open-next/src/build/createServerBundle.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import fs from "node:fs";
22
import { createRequire } from "node:module";
33
import path from "node:path";
44

5-
import type {
6-
FunctionOptions,
7-
OpenNextConfig,
8-
SplittedFunctionOptions,
9-
} from "types/open-next";
5+
import type { FunctionOptions, SplittedFunctionOptions } from "types/open-next";
106

117
import logger from "../logger.js";
128
import { minifyAll } from "../minimize-js.js";
@@ -40,9 +36,9 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
4036
const routes = fnOptions.routes;
4137
routes.forEach((route) => foundRoutes.add(route));
4238
if (fnOptions.runtime === "edge") {
43-
await generateEdgeBundle(name, config, options, fnOptions);
39+
await generateEdgeBundle(name, options, fnOptions);
4440
} else {
45-
await generateBundle(name, config, options, fnOptions);
41+
await generateBundle(name, options, fnOptions);
4642
}
4743
});
4844

@@ -53,7 +49,7 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
5349

5450
const remainingRoutes = new Set<string>();
5551

56-
const { monorepoRoot, appBuildOutputPath } = options;
52+
const { appBuildOutputPath, monorepoRoot } = options;
5753

5854
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
5955

@@ -104,7 +100,7 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
104100
}
105101

106102
// Generate default function
107-
await generateBundle("default", config, options, {
103+
await generateBundle("default", options, {
108104
...defaultFn,
109105
// @ts-expect-error - Those string are RouteTemplate
110106
routes: Array.from(remainingRoutes),
@@ -114,11 +110,11 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
114110

115111
async function generateBundle(
116112
name: string,
117-
config: OpenNextConfig,
118113
options: buildHelper.BuildOptions,
119114
fnOptions: SplittedFunctionOptions,
120115
) {
121-
const { appPath, appBuildOutputPath, outputDir, monorepoRoot } = options;
116+
const { appPath, appBuildOutputPath, config, outputDir, monorepoRoot } =
117+
options;
122118
logger.info(`Building server function: ${name}...`);
123119

124120
// Create output folder

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import path from "path";
66
import type { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
77
import type {
88
IncludedConverter,
9-
OpenNextConfig,
109
OverrideOptions,
1110
RouteTemplate,
1211
SplittedFunctionOptions,
@@ -20,7 +19,6 @@ import type { BuildOptions } from "../helper.js";
2019
import { copyOpenNextConfig, esbuildAsync } from "../helper.js";
2120

2221
interface BuildEdgeBundleOptions {
23-
appBuildOutputPath: string;
2422
middlewareInfo: MiddlewareInfo;
2523
entrypoint: string;
2624
outfile: string;
@@ -34,7 +32,6 @@ interface BuildEdgeBundleOptions {
3432
}
3533

3634
export async function buildEdgeBundle({
37-
appBuildOutputPath,
3835
middlewareInfo,
3936
entrypoint,
4037
outfile,
@@ -95,7 +92,7 @@ export async function buildEdgeBundle({
9592
}),
9693
openNextEdgePlugins({
9794
middlewareInfo,
98-
nextDir: path.join(appBuildOutputPath, ".next"),
95+
nextDir: path.join(options.appBuildOutputPath, ".next"),
9996
edgeFunctionHandlerPath: path.join(
10097
options.openNextDistDir,
10198
"core",
@@ -165,7 +162,6 @@ export function copyMiddlewareAssetsAndWasm({}) {}
165162

166163
export async function generateEdgeBundle(
167164
name: string,
168-
config: OpenNextConfig,
169165
options: BuildOptions,
170166
fnOptions: SplittedFunctionOptions,
171167
) {
@@ -218,7 +214,6 @@ export async function generateEdgeBundle(
218214
}
219215

220216
await buildEdgeBundle({
221-
appBuildOutputPath,
222217
middlewareInfo: fn,
223218
entrypoint: path.join(
224219
options.openNextDistDir,
@@ -228,6 +223,6 @@ export async function generateEdgeBundle(
228223
outfile: path.join(outputPath, "index.mjs"),
229224
options,
230225
overrides: fnOptions.override,
231-
additionalExternals: config.edgeExternals,
226+
additionalExternals: options.config.edgeExternals,
232227
});
233228
}

packages/open-next/src/core/routing/matcher.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ export function fixDataPage(
346346
}
347347

348348
if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) {
349-
let newPath = rawPath.replace(dataPattern, "").replace(/\.json$/, "");
350-
newPath = newPath === "/index" ? "/" : newPath;
349+
const newPath = rawPath
350+
.slice(dataPattern.length, -".json".length)
351+
.replace(/^\/index$/, "/");
351352
query.__nextDataReq = "1";
352353

353354
return {

packages/open-next/src/core/routingHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function applyMiddlewareHeaders(
7373
export default async function routingHandler(
7474
event: InternalEvent,
7575
): Promise<InternalResult | MiddlewareOutputEvent> {
76-
const nextHeaders = addNextConfigHeaders(event, ConfigHeaders) ?? {};
76+
const nextHeaders = addNextConfigHeaders(event, ConfigHeaders);
7777

7878
let internalEvent = fixDataPage(event, BuildId);
7979
if ("statusCode" in internalEvent) {

packages/open-next/src/plugins/replacement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const importPattern = /\/\/#import([\s\S]*?)\n\/\/#endImport/gm;
2323
* deletes: ["id1"],
2424
* })
2525
*
26-
* To inject arbritary code by using (import at top of file):
26+
* To inject arbitrary code by using (import at top of file):
2727
*
2828
* //#import
2929
*

0 commit comments

Comments
 (0)