diff --git a/.changeset/three-oranges-whisper.md b/.changeset/three-oranges-whisper.md new file mode 100644 index 000000000..fc42525b7 --- /dev/null +++ b/.changeset/three-oranges-whisper.md @@ -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`). diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index e926bb093..f4a3a5b25 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -1,3 +1,4 @@ +import path from "node:path"; import url from "node:url"; import { @@ -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 }, ); diff --git a/packages/open-next/src/build/compileConfig.ts b/packages/open-next/src/build/compileConfig.ts index dd33a6d95..9b5647ff5 100644 --- a/packages/open-next/src/build/compileConfig.ts +++ b/packages/open-next/src/build/compileConfig.ts @@ -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(","), ); @@ -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( @@ -65,8 +62,16 @@ 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[], ) { @@ -74,13 +79,13 @@ export function compileOpenNextConfigNode( 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", @@ -101,8 +106,16 @@ 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[], ) { @@ -110,7 +123,7 @@ export function compileOpenNextConfigEdge( logger.debug("Compiling open-next.config.ts for edge runtime.", outputPath); buildSync({ - entryPoints: [sourcePath], + entryPoints: [openNextConfigPath], outfile: outputPath, bundle: true, format: "esm", @@ -123,4 +136,6 @@ export function compileOpenNextConfigEdge( "process.env.NODE_ENV": '"production"', }, }); + + return outputPath; }