Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/open-next/src/adapters/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function loadBuildId(nextDir: string) {
}

export function loadHtmlPages(nextDir: string) {
const filePath = path.join(nextDir, "server", "pages-manifest.json");
const filePath = path.join(nextDir, "server/pages-manifest.json");
const json = fs.readFileSync(filePath, "utf-8");
return Object.entries(JSON.parse(json))
.filter(([_, value]) => (value as string).endsWith(".html"))
Expand Down Expand Up @@ -81,8 +81,7 @@ export function loadPrerenderManifest(nextDir: string) {
export function loadAppPathsManifest(nextDir: string) {
const appPathsManifestPath = path.join(
nextDir,
"server",
"app-paths-manifest.json",
"server/app-paths-manifest.json",
);
const appPathsManifestJson = fs.existsSync(appPathsManifestPath)
? fs.readFileSync(appPathsManifestPath, "utf-8")
Expand Down Expand Up @@ -120,7 +119,7 @@ export function loadAppPathsManifestKeys(nextDir: string) {
}

export function loadMiddlewareManifest(nextDir: string) {
const filePath = path.join(nextDir, "server", "middleware-manifest.json");
const filePath = path.join(nextDir, "server/middleware-manifest.json");
const json = fs.readFileSync(filePath, "utf-8");
return JSON.parse(json) as MiddlewareManifest;
}
Expand Down
31 changes: 10 additions & 21 deletions packages/open-next/src/build/copyTracedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
writeFileSync,
} from "node:fs";
import path from "node:path";
import type { NextConfig, PrerenderManifest } from "types/next-types";

import { loadConfig, loadPrerenderManifest } from "config/util.js";
import logger from "../logger.js";
import { MIDDLEWARE_TRACE_FILE } from "./constant.js";

Expand Down Expand Up @@ -47,6 +47,7 @@ export async function copyTracedFiles({
const dotNextDir = path.join(buildOutputPath, ".next");
const standaloneDir = path.join(dotNextDir, "standalone");
const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
const standaloneServerDir = path.join(standaloneNextDir, "server");
const outputNextDir = path.join(outputDir, packagePath, ".next");

const extractFiles = (files: string[], from = standaloneNextDir) =>
Expand Down Expand Up @@ -75,12 +76,12 @@ export async function copyTracedFiles({
});
}
// create directory for pages
if (existsSync(path.join(standaloneDir, ".next/server/pages"))) {
if (existsSync(path.join(standaloneServerDir, "pages"))) {
mkdirSync(path.join(outputNextDir, "server/pages"), {
recursive: true,
});
}
if (existsSync(path.join(standaloneDir, ".next/server/app"))) {
if (existsSync(path.join(standaloneServerDir, "app"))) {
mkdirSync(path.join(outputNextDir, "server/app"), {
recursive: true,
});
Expand Down Expand Up @@ -239,17 +240,15 @@ File ${fullFilePath} does not exist

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

readdirSync(path.join(standaloneNextDir, "server"))
readdirSync(standaloneServerDir)
.filter(
(fileOrDir) =>
!statSync(
path.join(standaloneNextDir, "server", fileOrDir),
).isDirectory(),
!statSync(path.join(standaloneServerDir, fileOrDir)).isDirectory(),
)
.filter((file) => file !== "server.js")
.forEach((file) =>
copyFileSync(
path.join(standaloneNextDir, "server", file),
path.join(standaloneServerDir, file),
path.join(path.join(outputNextDir, "server"), file),
),
);
Expand Down Expand Up @@ -285,24 +284,14 @@ File ${fullFilePath} does not exist
const staticFiles: Array<string> = Object.values(
JSON.parse(
readFileSync(
path.join(standaloneNextDir, "server/pages-manifest.json"),
path.join(standaloneServerDir, "pages-manifest.json"),
"utf8",
),
),
);
// Then we need to get all fallback: true dynamic routes html
const prerenderManifest = JSON.parse(
readFileSync(
path.join(standaloneNextDir, "prerender-manifest.json"),
"utf8",
),
) as PrerenderManifest;
const config = JSON.parse(
readFileSync(
path.join(standaloneNextDir, "required-server-files.json"),
"utf8",
),
).config as NextConfig;
const prerenderManifest = loadPrerenderManifest(standaloneNextDir);
const config = loadConfig(standaloneNextDir);
const locales = config.i18n?.locales;
Object.values(prerenderManifest.dynamicRoutes).forEach((route) => {
if (typeof route.fallback === "string") {
Expand Down
22 changes: 9 additions & 13 deletions packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from "node:fs";
import path from "node:path";

import { loadFunctionsConfigManifest } from "config/util.js";
import {
loadFunctionsConfigManifest,
loadMiddlewareManifest,
} from "config/util.js";
import logger from "../logger.js";
import type {
MiddlewareInfo,
MiddlewareManifest,
} from "../types/next-types.js";
import type { MiddlewareInfo } from "../types/next-types.js";
import { buildEdgeBundle } from "./edge/createEdgeBundle.js";
import * as buildHelper from "./helper.js";
import { installDependencies } from "./installDeps.js";
Expand All @@ -27,15 +27,11 @@ export async function createMiddleware(
) {
logger.info("Bundling middleware function...");

const { appBuildOutputPath, config, outputDir } = options;
const { config, outputDir } = options;
const buildOutputDotNextDir = path.join(options.appBuildOutputPath, ".next");

// Get middleware manifest
const middlewareManifest = JSON.parse(
fs.readFileSync(
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
"utf8",
),
) as MiddlewareManifest;
const middlewareManifest = loadMiddlewareManifest(buildOutputDotNextDir);

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

if (functionsConfigManifest?.functions["/_middleware"]) {
Expand Down
39 changes: 16 additions & 23 deletions packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdirSync } from "node:fs";
import fs from "node:fs";
import path from "node:path";
import { build } from "esbuild";
import type { MiddlewareInfo, MiddlewareManifest } from "types/next-types";
import type { MiddlewareInfo } from "types/next-types";
import type {
IncludedConverter,
IncludedOriginResolver,
Expand All @@ -13,6 +13,7 @@ import type {
SplittedFunctionOptions,
} from "types/open-next";

import { loadMiddlewareManifest } from "config/util.js";
import type { OriginResolver } from "types/overrides.js";
import logger from "../../logger.js";
import { openNextEdgePlugins } from "../../plugins/edge.js";
Expand Down Expand Up @@ -173,23 +174,19 @@ export async function generateEdgeBundle(
options: BuildOptions,
fnOptions: SplittedFunctionOptions,
) {
const { appBuildOutputPath, outputDir } = options;
logger.info(`Generating edge bundle for: ${name}`);

const buildOutputDotNextDir = path.join(options.appBuildOutputPath, ".next");

// Create output folder
const outputPath = path.join(outputDir, "server-functions", name);
fs.mkdirSync(outputPath, { recursive: true });
const outputDir = path.join(options.outputDir, "server-functions", name);
fs.mkdirSync(outputDir, { recursive: true });

// Copy open-next.config.mjs
copyOpenNextConfig(options.buildDir, outputPath, true);
copyOpenNextConfig(options.buildDir, outputDir, true);

// Load middleware manifest
const middlewareManifest = JSON.parse(
fs.readFileSync(
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
"utf8",
),
) as MiddlewareManifest;
const middlewareManifest = loadMiddlewareManifest(buildOutputDotNextDir);

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

//Copy wasm files
const wasmFiles = middlewareInfo.wasm;
mkdirSync(path.join(outputPath, "wasm"), { recursive: true });
mkdirSync(path.join(outputDir, "wasm"), { recursive: true });
for (const wasmFile of wasmFiles) {
fs.copyFileSync(
path.join(appBuildOutputPath, ".next", wasmFile.filePath),
path.join(outputPath, `wasm/${wasmFile.name}.wasm`),
path.join(buildOutputDotNextDir, wasmFile.filePath),
path.join(outputDir, `wasm/${wasmFile.name}.wasm`),
);
}

// Copy assets
const assets = middlewareInfo.assets;
mkdirSync(path.join(outputPath, "assets"), { recursive: true });
mkdirSync(path.join(outputDir, "assets"), { recursive: true });
for (const asset of assets) {
fs.copyFileSync(
path.join(appBuildOutputPath, ".next", asset.filePath),
path.join(outputPath, `assets/${asset.name}`),
path.join(buildOutputDotNextDir, asset.filePath),
path.join(outputDir, `assets/${asset.name}`),
);
}

await buildEdgeBundle({
middlewareInfo,
entrypoint: path.join(
options.openNextDistDir,
"adapters",
"edge-adapter.js",
),
outfile: path.join(outputPath, "index.mjs"),
entrypoint: path.join(options.openNextDistDir, "adapters/edge-adapter.js"),
outfile: path.join(outputDir, "index.mjs"),
options,
overrides: fnOptions.override,
additionalExternals: options.config.edgeExternals,
Expand Down
17 changes: 4 additions & 13 deletions packages/open-next/src/build/generateOutput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as fs from "node:fs";
import path from "node:path";

import type { NextConfig } from "types/next-types.js";
import type {
BaseOverride,
DefaultOverrideOptions,
Expand All @@ -10,6 +8,7 @@ import type {
OverrideOptions,
} from "types/open-next";

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

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

const defaultOriginCanstream = await canStream(config.default);

//Load required-server-files.json
const requiredServerFiles = JSON.parse(
fs.readFileSync(
path.join(appBuildOutputPath, ".next", "required-server-files.json"),
"utf-8",
),
).config as NextConfig;
const prefixer = prefixPattern(requiredServerFiles.basePath ?? "");
const nextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
const prefixer = prefixPattern(nextConfig.basePath ?? "");

// First add s3 origins and image optimization

Expand All @@ -206,9 +199,7 @@ export async function generateOutput(options: BuildOptions) {
copy: [
{
from: ".open-next/assets",
to: requiredServerFiles.basePath
? `_assets${requiredServerFiles.basePath}`
: "_assets",
to: nextConfig.basePath ? `_assets${nextConfig.basePath}` : "_assets",
cached: true,
versionedSubDir: prefixer("_next"),
},
Expand Down