Skip to content
Open
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
5 changes: 4 additions & 1 deletion packages/open-next/src/adapters/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from "node:path";

Check failure on line 1 in packages/open-next/src/adapters/config/index.ts

View workflow job for this annotation

GitHub Actions / validate

format

File content differs from formatting output

import { debug } from "../logger";
import {
Expand All @@ -16,7 +16,10 @@
loadRoutesManifest,
} from "./util.js";

export const NEXT_DIR = path.join(__dirname, ".next");
export const NEXT_DIR = path.join(
__dirname,
globalThis.nextDistDir || ".next",
);
export const OPEN_NEXT_DIR = path.join(__dirname, ".open-next");

debug({ NEXT_DIR, OPEN_NEXT_DIR });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { optimizeImage } from "./plugins/image-optimization/image-optimization.j
import { setNodeEnv } from "./util.js";

setNodeEnv();
const nextDir = path.join(__dirname, ".next");
const nextDir = path.join(__dirname, globalThis.nextDistDir || ".next");
const config = loadConfig(nextDir);
const buildId = loadBuildId(nextDir);
const nextConfig = {
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function build(
);

// Initialize options
const options = buildHelper.normalizeOptions(
const options = await buildHelper.normalizeOptions(
config,
openNextDistDir,
buildDir,
Expand Down
8 changes: 5 additions & 3 deletions packages/open-next/src/build/copyTracedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ interface CopyTracedFilesOptions {
routes: string[];
bundledNextServer: boolean;
skipServerFiles?: boolean;
nextDistDir?: string;
}

export function getManifests(nextDir: string) {
Expand All @@ -92,13 +93,14 @@ export async function copyTracedFiles({
routes,
bundledNextServer,
skipServerFiles,
nextDistDir = ".next",
}: CopyTracedFilesOptions) {
const tsStart = Date.now();
const dotNextDir = path.join(buildOutputPath, ".next");
const dotNextDir = path.join(buildOutputPath, nextDistDir);
const standaloneDir = path.join(dotNextDir, "standalone");
const standaloneNextDir = path.join(standaloneDir, packagePath, ".next");
const standaloneNextDir = path.join(standaloneDir, packagePath, nextDistDir);
const standaloneServerDir = path.join(standaloneNextDir, "server");
const outputNextDir = path.join(outputDir, packagePath, ".next");
const outputNextDir = path.join(outputDir, packagePath, nextDistDir);

// Files to copy
// Map from files in the `.next/standalone` to files in the `.open-next` folder
Expand Down
24 changes: 15 additions & 9 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export function createStaticAssets(
) {
logger.info("Bundling static assets...");

const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options;
const { appBuildOutputPath, appPublicPath, outputDir, appPath, nextDistDir } =
options;

const NextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
const NextConfig = loadConfig(path.join(appBuildOutputPath, nextDistDir));
const basePath = useBasePath ? (NextConfig.basePath ?? "") : "";

// Create output folder
Expand All @@ -47,12 +48,12 @@ export function createStaticAssets(
* Note: BUILD_ID is used by the SST infra.
*/
fs.copyFileSync(
path.join(appBuildOutputPath, ".next/BUILD_ID"),
path.join(appBuildOutputPath, nextDistDir, "BUILD_ID"),
path.join(outputPath, "BUILD_ID"),
);

fs.cpSync(
path.join(appBuildOutputPath, ".next/static"),
path.join(appBuildOutputPath, nextDistDir, "static"),
path.join(outputPath, "_next", "static"),
{ recursive: true },
);
Expand Down Expand Up @@ -82,25 +83,29 @@ export function createStaticAssets(
export function createCacheAssets(options: buildHelper.BuildOptions) {
logger.info("Bundling cache assets...");

const { appBuildOutputPath, outputDir } = options;
const { appBuildOutputPath, outputDir, nextDistDir } = options;
const packagePath = buildHelper.getPackagePath(options);
const buildId = buildHelper.getBuildId(options);
let useTagCache = false;

const dotNextPath = path.join(
appBuildOutputPath,
".next/standalone",
nextDistDir,
"standalone",
packagePath,
);

const outputCachePath = path.join(outputDir, "cache", buildId);
fs.mkdirSync(outputCachePath, { recursive: true });

const sourceDirs = [".next/server/pages", ".next/server/app"]
const sourceDirs = [
path.join(nextDistDir, "server/pages"),
path.join(nextDistDir, "server/app"),
]
.map((dir) => path.join(dotNextPath, dir))
.filter(fs.existsSync);

const htmlPages = buildHelper.getHtmlPages(dotNextPath);
const htmlPages = buildHelper.getHtmlPages(dotNextPath, nextDistDir);

const isFileSkipped = (relativePath: string) =>
relativePath.endsWith(".js") ||
Expand Down Expand Up @@ -219,7 +224,8 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
// Copy fetch-cache to cache folder
const fetchCachePath = path.join(
appBuildOutputPath,
".next/cache/fetch-cache",
nextDistDir,
"cache/fetch-cache",
);
if (fs.existsSync(fetchCachePath)) {
const fetchOutputPath = path.join(outputDir, "cache", "__fetch", buildId);
Expand Down
10 changes: 5 additions & 5 deletions packages/open-next/src/build/createImageOptimizationBundle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from "node:fs";

Check failure on line 1 in packages/open-next/src/build/createImageOptimizationBundle.ts

View workflow job for this annotation

GitHub Actions / validate

format

File content differs from formatting output
import { createRequire } from "node:module";
import os from "node:os";
import path from "node:path";
Expand Down Expand Up @@ -97,14 +97,14 @@
);

// Copy over .next/required-server-files.json file and BUILD_ID
fs.mkdirSync(path.join(outputPath, ".next"));
fs.mkdirSync(path.join(outputPath, options.nextDistDir));
fs.copyFileSync(
path.join(appBuildOutputPath, ".next/required-server-files.json"),
path.join(outputPath, ".next/required-server-files.json"),
path.join(appBuildOutputPath, options.nextDistDir, "required-server-files.json"),
path.join(outputPath, options.nextDistDir, "required-server-files.json"),
);
fs.copyFileSync(
path.join(appBuildOutputPath, ".next/BUILD_ID"),
path.join(outputPath, ".next/BUILD_ID"),
path.join(appBuildOutputPath, options.nextDistDir, "BUILD_ID"),
path.join(outputPath, options.nextDistDir, "BUILD_ID"),
);

// Sharp provides pre-build binaries for all platforms. https://github.com/lovell/sharp/blob/main/docs/install.md#cross-platform
Expand Down
5 changes: 4 additions & 1 deletion packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export async function createMiddleware(
logger.info("Bundling middleware function...");

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

// Get middleware manifest
const middlewareManifest = loadMiddlewareManifest(buildOutputDotNextDir);
Expand Down
2 changes: 1 addition & 1 deletion packages/open-next/src/build/createRevalidationBundle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from "node:fs";

Check failure on line 1 in packages/open-next/src/build/createRevalidationBundle.ts

View workflow job for this annotation

GitHub Actions / validate

format

File content differs from formatting output
import path from "node:path";

import logger from "../logger.js";
Expand Down Expand Up @@ -46,7 +46,7 @@

// Copy over .next/prerender-manifest.json file
fs.copyFileSync(
path.join(appBuildOutputPath, ".next", "prerender-manifest.json"),
path.join(appBuildOutputPath, options.nextDistDir, "prerender-manifest.json"),
path.join(outputPath, "prerender-manifest.json"),
);
}
18 changes: 13 additions & 5 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ export async function createServerBundle(

const remainingRoutes = new Set<string>();

const { appBuildOutputPath } = options;
const { appBuildOutputPath, nextDistDir } = options;

// Find remaining routes
const serverPath = path.join(
appBuildOutputPath,
".next/standalone",
nextDistDir,
"standalone",
buildHelper.getPackagePath(options),
".next/server",
nextDistDir,
"server",
);

// Find app dir routes
Expand Down Expand Up @@ -171,7 +173,7 @@ async function generateBundle(
);

const middlewareManifest = loadMiddlewareManifest(
path.join(options.appBuildOutputPath, ".next"),
path.join(options.appBuildOutputPath, options.nextDistDir),
);

copyMiddlewareResources(
Expand All @@ -185,7 +187,12 @@ async function generateBundle(
buildHelper.copyOpenNextConfig(options.buildDir, outPackagePath);

// Copy env files
buildHelper.copyEnvFile(appBuildOutputPath, packagePath, outputPath);
buildHelper.copyEnvFile(
appBuildOutputPath,
options.nextDistDir,
packagePath,
outputPath,
);

// Copy all necessary traced files
const { tracedFiles, manifests } = await copyTracedFiles({
Expand All @@ -194,6 +201,7 @@ async function generateBundle(
outputDir: outputPath,
routes: fnOptions.routes ?? ["app/page.tsx"],
bundledNextServer: isBundled,
nextDistDir: options.nextDistDir,
});

const additionalCodePatches = codeCustomization?.additionalCodePatches ?? [];
Expand Down
12 changes: 8 additions & 4 deletions packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ export async function buildEdgeBundle({
),
openNextEdgePlugins({
middlewareInfo,
nextDir: path.join(options.appBuildOutputPath, ".next"),
nextDir: path.join(options.appBuildOutputPath, options.nextDistDir),
isInCloudflare,
distDir: options.nextDistDir,
}),
...additionalPlugins,
// The content updater plugin must be the last plugin
Expand Down Expand Up @@ -188,7 +189,10 @@ export async function generateEdgeBundle(
) {
logger.info(`Generating edge bundle for: ${name}`);

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

// Create output folder
const outputDir = path.join(options.outputDir, "server-functions", name);
Expand Down Expand Up @@ -235,15 +239,15 @@ export function copyMiddlewareResources(
mkdirSync(path.join(destDir, "wasm"), { recursive: true });
for (const file of middlewareInfo?.wasm ?? []) {
fs.copyFileSync(
path.join(options.appBuildOutputPath, ".next", file.filePath),
path.join(options.appBuildOutputPath, options.nextDistDir, file.filePath),
path.join(destDir, `wasm/${file.name}.wasm`),
);
}

mkdirSync(path.join(destDir, "assets"), { recursive: true });
for (const file of middlewareInfo?.assets ?? []) {
fs.copyFileSync(
path.join(options.appBuildOutputPath, ".next", file.filePath),
path.join(options.appBuildOutputPath, options.nextDistDir, file.filePath),
path.join(destDir, `assets/${file.name}`),
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/open-next/src/build/generateOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export async function generateOutput(options: BuildOptions) {

const defaultOriginCanstream = await canStream(config.default);

const nextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
const nextConfig = loadConfig(
path.join(appBuildOutputPath, options.nextDistDir),
);
const prefixer = prefixPattern(nextConfig.basePath ?? "");

// First add s3 origins and image optimization
Expand Down
Loading
Loading