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: 7 additions & 0 deletions .changeset/three-oranges-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/aws": patch
---

refactor: `compileOpenNextConfig` now takes `openNextConfigPath` only and no more `baseDir`.

`openNextConfigPath` is now in line with fs APIs: it is either absolute or relative to the working directory (`cwd`).
4 changes: 2 additions & 2 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import url from "node:url";

import {
Expand Down Expand Up @@ -32,8 +33,7 @@ export async function build(
const openNextDistDir = url.fileURLToPath(new URL(".", import.meta.url));

const { config, buildDir } = await compileOpenNextConfig(
baseDir,
openNextConfigPath,
path.join(baseDir, openNextConfigPath ?? "open-next.config.ts"),
{ nodeExternals },
);

Expand Down
47 changes: 31 additions & 16 deletions packages/open-next/src/build/compileConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,18 @@ import { validateConfig } from "./validateConfig.js";
*
* The configuration is always compiled for Node.js and for the edge only if needed.
*
* @param baseDir Directory where to look for the configuration.
* @param openNextConfigPath Override the default configuration when provided. Relative to baseDir.
* @param openNextConfigPath Path to the configuration file. Absolute or relative to cwd.
* @param nodeExternals Coma separated list of Externals for the Node.js compilation.
* @param compileEdge Force compiling for the edge runtime when true
* @return The configuration and the build directory.
*/
export async function compileOpenNextConfig(
baseDir: string,
openNextConfigPath?: string,
openNextConfigPath: string,
{ nodeExternals = "", compileEdge = false } = {},
) {
const sourcePath = path.join(
baseDir,
openNextConfigPath ?? "open-next.config.ts",
);

const buildDir = fs.mkdtempSync(path.join(os.tmpdir(), "open-next-tmp"));
let configPath = compileOpenNextConfigNode(
sourcePath,
openNextConfigPath,
buildDir,
nodeExternals.split(","),
);
Expand All @@ -54,7 +47,11 @@ export async function compileOpenNextConfig(
(config.middleware?.external && config.middleware.runtime !== "node") ||
Object.values(config.functions || {}).some((fn) => fn.runtime === "edge");
if (usesEdgeRuntime || compileEdge) {
compileOpenNextConfigEdge(sourcePath, buildDir, config.edgeExternals ?? []);
compileOpenNextConfigEdge(
openNextConfigPath,
buildDir,
config.edgeExternals ?? [],
);
} else {
// Skip compiling for the edge runtime.
logger.debug(
Expand All @@ -65,22 +62,30 @@ export async function compileOpenNextConfig(
return { config, buildDir };
}

/**
* Compiles the OpenNext configuration for Node.
*
* @param openNextConfigPath Path to the configuration file. Absolute or relative to cwd.
* @param outputDir Folder where to output the compiled config file (`open-next.config.mjs`).
* @param externals List of packages that should not be bundled.
* @return Path to the compiled config.
*/
export function compileOpenNextConfigNode(
sourcePath: string,
openNextConfigPath: string,
outputDir: string,
externals: string[],
) {
const outputPath = path.join(outputDir, "open-next.config.mjs");
logger.debug("Compiling open-next.config.ts for Node.", outputPath);

//Check if open-next.config.ts exists
if (!fs.existsSync(sourcePath)) {
if (!fs.existsSync(openNextConfigPath)) {
//Create a simple open-next.config.mjs file
logger.debug("Cannot find open-next.config.ts. Using default config.");
fs.writeFileSync(outputPath, "export default { default: { } };");
} else {
buildSync({
entryPoints: [sourcePath],
entryPoints: [openNextConfigPath],
outfile: outputPath,
bundle: true,
format: "esm",
Expand All @@ -101,16 +106,24 @@ export function compileOpenNextConfigNode(
return outputPath;
}

/**
* Compiles the OpenNext configuration for Edge.
*
* @param openNextConfigPath Path to the configuration file. Absolute or relative to cwd.
* @param outputDir Folder where to output the compiled config file (`open-next.config.edge.mjs`).
* @param externals List of packages that should not be bundled.
* @return Path to the compiled config.
*/
export function compileOpenNextConfigEdge(
sourcePath: string,
openNextConfigPath: string,
outputDir: string,
externals: string[],
) {
const outputPath = path.join(outputDir, "open-next.config.edge.mjs");
logger.debug("Compiling open-next.config.ts for edge runtime.", outputPath);

buildSync({
entryPoints: [sourcePath],
entryPoints: [openNextConfigPath],
outfile: outputPath,
bundle: true,
format: "esm",
Expand All @@ -123,4 +136,6 @@ export function compileOpenNextConfigEdge(
"process.env.NODE_ENV": '"production"',
},
});

return outputPath;
}
Loading