diff --git a/packages/open-next/src/adapters/config/util.ts b/packages/open-next/src/adapters/config/util.ts index 8c083fc21..fb64ae1ca 100644 --- a/packages/open-next/src/adapters/config/util.ts +++ b/packages/open-next/src/adapters/config/util.ts @@ -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")) @@ -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") @@ -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; } diff --git a/packages/open-next/src/build/copyTracedFiles.ts b/packages/open-next/src/build/copyTracedFiles.ts index 65588b438..9de488629 100644 --- a/packages/open-next/src/build/copyTracedFiles.ts +++ b/packages/open-next/src/build/copyTracedFiles.ts @@ -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"; @@ -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) => @@ -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, }); @@ -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), ), ); @@ -285,24 +284,14 @@ File ${fullFilePath} does not exist const staticFiles: Array = 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") { diff --git a/packages/open-next/src/build/createMiddleware.ts b/packages/open-next/src/build/createMiddleware.ts index 5f4856be0..914acce12 100644 --- a/packages/open-next/src/build/createMiddleware.ts +++ b/packages/open-next/src/build/createMiddleware.ts @@ -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"; @@ -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 @@ -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"]) { diff --git a/packages/open-next/src/build/edge/createEdgeBundle.ts b/packages/open-next/src/build/edge/createEdgeBundle.ts index 22a7b4efc..84d5c46d7 100644 --- a/packages/open-next/src/build/edge/createEdgeBundle.ts +++ b/packages/open-next/src/build/edge/createEdgeBundle.ts @@ -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, @@ -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"; @@ -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) => @@ -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, diff --git a/packages/open-next/src/build/generateOutput.ts b/packages/open-next/src/build/generateOutput.ts index b8d80e2f2..769e106e7 100644 --- a/packages/open-next/src/build/generateOutput.ts +++ b/packages/open-next/src/build/generateOutput.ts @@ -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, @@ -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 = { @@ -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 @@ -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"), },