Skip to content

Commit 083c316

Browse files
committed
refactor: compile the config in a proper temp dir
1 parent 026ed20 commit 083c316

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

packages/open-next/src/build.ts

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cp from "node:child_process";
2-
import fs, { readFileSync } from "node:fs";
2+
import fs from "node:fs";
33
import { createRequire as topLevelCreateRequire } from "node:module";
44
import os from "node:os";
55
import path from "node:path";
@@ -47,9 +47,9 @@ export async function build(
4747
showWindowsWarning();
4848

4949
// Load open-next.config.ts
50-
const tempDir = initTempDir();
50+
const tempConfigDir = fs.mkdtempSync("open-next");
5151
let configPath = compileOpenNextConfigNode(
52-
tempDir,
52+
tempConfigDir,
5353
openNextConfigPath,
5454
nodeExternals,
5555
);
@@ -64,10 +64,10 @@ export async function build(
6464
}
6565
validateConfig(config);
6666

67-
compileOpenNextConfigEdge(tempDir, config, openNextConfigPath);
67+
compileOpenNextConfigEdge(tempConfigDir, config, openNextConfigPath);
6868

6969
// Initialize options
70-
const options = normalizeOptions(config);
70+
const options = normalizeOptions(config, tempConfigDir);
7171
logger.setLevel(options.debug ? "debug" : "info");
7272

7373
// Pre-build validation
@@ -82,7 +82,7 @@ export async function build(
8282

8383
// Generate deployable bundle
8484
printHeader("Generating bundle");
85-
initOutputDir(tempDir, options);
85+
initOutputDir(options);
8686

8787
// Compile cache.ts
8888
compileCache(options);
@@ -113,14 +113,6 @@ function showWindowsWarning() {
113113
);
114114
}
115115

116-
function initTempDir() {
117-
const dir = path.join(process.cwd(), ".open-next");
118-
const tempDir = path.join(dir, ".build");
119-
fs.rmSync(dir, { recursive: true, force: true });
120-
fs.mkdirSync(tempDir, { recursive: true });
121-
return tempDir;
122-
}
123-
124116
function checkRunningInsideNextjsApp(options: BuildOptions) {
125117
const { appPath } = options;
126118
const extension = ["js", "cjs", "mjs", "ts"].find((ext) =>
@@ -175,30 +167,26 @@ function printOpenNextVersion(options: BuildOptions) {
175167
logger.info(`OpenNext v${options.openNextVersion}`);
176168
}
177169

178-
function initOutputDir(srcTempDir: string, options: BuildOptions) {
170+
function initOutputDir(options: BuildOptions) {
179171
// We need to get the build relative to the cwd to find the compiled config
180172
// This is needed for the case where the app is a single-version monorepo and the package.json is in the root of the monorepo
181173
// where the build is in the app directory, but the compiled config is in the root of the monorepo.
182-
const openNextConfig = readFileSync(
183-
path.join(srcTempDir, "open-next.config.mjs"),
184-
"utf8",
185-
);
186-
let openNextConfigEdge: string | null = null;
187-
if (fs.existsSync(path.join(srcTempDir, "open-next.config.edge.mjs"))) {
188-
openNextConfigEdge = readFileSync(
189-
path.join(srcTempDir, "open-next.config.edge.mjs"),
190-
"utf8",
191-
);
192-
}
193174
fs.rmSync(options.outputDir, { recursive: true, force: true });
194175
const { buildDir } = options;
195176
fs.mkdirSync(buildDir, { recursive: true });
196-
fs.writeFileSync(path.join(buildDir, "open-next.config.mjs"), openNextConfig);
197-
if (openNextConfigEdge) {
198-
fs.writeFileSync(
177+
178+
fs.copyFileSync(
179+
path.join(options.tempConfigDir, "open-next.config.mjs"),
180+
path.join(buildDir, "open-next.config.mjs"),
181+
);
182+
183+
try {
184+
fs.copyFileSync(
185+
path.join(options.tempConfigDir, "open-next.config.edge.mjs"),
199186
path.join(buildDir, "open-next.config.edge.mjs"),
200-
openNextConfigEdge,
201187
);
188+
} catch {
189+
// The edge config does not always exist.
202190
}
203191
}
204192

@@ -692,7 +680,7 @@ async function createMiddleware(options: BuildOptions) {
692680

693681
// Get middleware manifest
694682
const middlewareManifest = JSON.parse(
695-
readFileSync(
683+
fs.readFileSync(
696684
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
697685
"utf8",
698686
),

packages/open-next/src/build/helper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
1717

1818
export type BuildOptions = ReturnType<typeof normalizeOptions>;
1919

20-
export function normalizeOptions(config: OpenNextConfig) {
20+
export function normalizeOptions(
21+
config: OpenNextConfig,
22+
tempConfigDir: string,
23+
) {
2124
const appPath = path.join(process.cwd(), config.appPath || ".");
2225
const buildOutputPath = path.join(
2326
process.cwd(),
@@ -44,14 +47,15 @@ export function normalizeOptions(config: OpenNextConfig) {
4447
appPackageJsonPath,
4548
appPath,
4649
appPublicPath: path.join(appPath, "public"),
50+
buildDir: path.join(outputDir, ".build"),
4751
config,
4852
debug: Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
4953
monorepoRoot,
5054
nextVersion: getNextVersion(appPath),
5155
openNextVersion: getOpenNextVersion(),
5256
outputDir,
5357
packager,
54-
buildDir: path.join(outputDir, ".build"),
58+
tempConfigDir,
5559
};
5660
}
5761

0 commit comments

Comments
 (0)