Skip to content

Commit 5c6bd72

Browse files
authored
Refactoring (#554)
- rename BuildOptions#tempDir to BuildOptions#buildDir - simplify the default OpenNext config - rename a few arguments for consistency / clarity - compile the configuration into the OS temp dir - pin the Next version used in CI to npm latest
1 parent 5d03b5d commit 5c6bd72

File tree

6 files changed

+37
-73
lines changed

6 files changed

+37
-73
lines changed

.github/workflows/e2e.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Get Latest Next Version
3434
id: get_latest_version
3535
run: |
36-
latest_version=$(curl -s https://api.github.com/repos/vercel/next.js/releases/latest | jq -r '.tag_name')
36+
latest_version=$(curl -s https://registry.npmjs.org/-/package/next/dist-tags | jq -r '.latest')
3737
echo "Latest version: $latest_version"
3838
echo "LATEST_VERSION=$latest_version" >> $GITHUB_ENV
3939
echo "LATEST_VERSION=$latest_version" >> $GITHUB_OUTPUT

packages/open-next/src/build.ts

Lines changed: 18 additions & 46 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 tempBuildDir = fs.mkdtempSync(path.join(os.tmpdir(), "open-next-tmp"));
5151
let configPath = compileOpenNextConfigNode(
52-
tempDir,
52+
tempBuildDir,
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(tempBuildDir, config, openNextConfigPath);
6868

6969
// Initialize options
70-
const options = normalizeOptions(config);
70+
const options = normalizeOptions(config, tempBuildDir);
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,34 +167,14 @@ 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 });
194-
const destTempDir = options.tempDir;
195-
fs.mkdirSync(destTempDir, { recursive: true });
196-
fs.writeFileSync(
197-
path.join(destTempDir, "open-next.config.mjs"),
198-
openNextConfig,
199-
);
200-
if (openNextConfigEdge) {
201-
fs.writeFileSync(
202-
path.join(destTempDir, "open-next.config.edge.mjs"),
203-
openNextConfigEdge,
204-
);
205-
}
175+
const { buildDir } = options;
176+
fs.mkdirSync(buildDir, { recursive: true });
177+
fs.cpSync(options.tempBuildDir, buildDir, { recursive: true });
206178
}
207179

208180
async function createWarmerBundle(options: BuildOptions) {
@@ -215,7 +187,7 @@ async function createWarmerBundle(options: BuildOptions) {
215187
fs.mkdirSync(outputPath, { recursive: true });
216188

217189
// Copy open-next.config.mjs into the bundle
218-
copyOpenNextConfig(options.tempDir, outputPath);
190+
copyOpenNextConfig(options.buildDir, outputPath);
219191

220192
// Build Lambda code
221193
// note: bundle in OpenNext package b/c the adatper relys on the
@@ -258,7 +230,7 @@ async function createRevalidationBundle(options: BuildOptions) {
258230
fs.mkdirSync(outputPath, { recursive: true });
259231

260232
//Copy open-next.config.mjs into the bundle
261-
copyOpenNextConfig(options.tempDir, outputPath);
233+
copyOpenNextConfig(options.buildDir, outputPath);
262234

263235
// Build Lambda code
264236
await esbuildAsync(
@@ -297,7 +269,7 @@ async function createImageOptimizationBundle(options: BuildOptions) {
297269
fs.mkdirSync(outputPath, { recursive: true });
298270

299271
// Copy open-next.config.mjs into the bundle
300-
copyOpenNextConfig(options.tempDir, outputPath);
272+
copyOpenNextConfig(options.buildDir, outputPath);
301273

302274
const plugins = [
303275
openNextResolvePlugin({
@@ -640,7 +612,7 @@ async function createCacheAssets(options: BuildOptions) {
640612
);
641613

642614
//Copy open-next.config.mjs into the bundle
643-
copyOpenNextConfig(options.tempDir, providerPath);
615+
copyOpenNextConfig(options.buildDir, providerPath);
644616

645617
// TODO: check if metafiles doesn't contain duplicates
646618
fs.writeFileSync(
@@ -664,7 +636,7 @@ export function compileCache(
664636
) {
665637
const { config } = options;
666638
const ext = format === "cjs" ? "cjs" : "mjs";
667-
const outfile = path.join(options.outputDir, ".build", `cache.${ext}`);
639+
const outfile = path.join(options.buildDir, `cache.${ext}`);
668640

669641
const isAfter15 = compareSemver(options.nextVersion, "15.0.0") >= 0;
670642

@@ -699,7 +671,7 @@ async function createMiddleware(options: BuildOptions) {
699671

700672
// Get middleware manifest
701673
const middlewareManifest = JSON.parse(
702-
readFileSync(
674+
fs.readFileSync(
703675
path.join(appBuildOutputPath, ".next/server/middleware-manifest.json"),
704676
"utf8",
705677
),
@@ -725,7 +697,7 @@ async function createMiddleware(options: BuildOptions) {
725697

726698
// Copy open-next.config.mjs
727699
copyOpenNextConfig(
728-
options.tempDir,
700+
options.buildDir,
729701
outputPath,
730702
config.middleware.override?.wrapper === "cloudflare",
731703
);
@@ -743,7 +715,7 @@ async function createMiddleware(options: BuildOptions) {
743715
} else {
744716
await buildEdgeBundle({
745717
entrypoint: path.join(__dirname, "core", "edgeFunctionHandler.js"),
746-
outfile: path.join(outputDir, ".build", "middleware.mjs"),
718+
outfile: path.join(options.buildDir, "middleware.mjs"),
747719
...commonMiddlewareOptions,
748720
onlyBuildOnce: true,
749721
});

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,21 @@ import { OpenNextConfig } from "types/open-next.js";
77
import logger from "../logger.js";
88

99
export function compileOpenNextConfigNode(
10-
tempDir: string,
10+
outputDir: string,
1111
openNextConfigPath?: string,
1212
nodeExternals?: string,
1313
) {
1414
const sourcePath = path.join(
1515
process.cwd(),
1616
openNextConfigPath ?? "open-next.config.ts",
1717
);
18-
const outputPath = path.join(tempDir, "open-next.config.mjs");
18+
const outputPath = path.join(outputDir, "open-next.config.mjs");
1919

2020
//Check if open-next.config.ts exists
2121
if (!fs.existsSync(sourcePath)) {
2222
//Create a simple open-next.config.mjs file
2323
logger.debug("Cannot find open-next.config.ts. Using default config.");
24-
fs.writeFileSync(
25-
outputPath,
26-
[
27-
"var config = { default: { } };",
28-
"var open_next_config_default = config;",
29-
"export { open_next_config_default as default };",
30-
].join("\n"),
31-
);
24+
fs.writeFileSync(outputPath, "export default { default: { } };");
3225
} else {
3326
buildSync({
3427
entryPoints: [sourcePath],

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ async function generateBundle(
144144

145145
const ext = fnOptions.runtime === "deno" ? "mjs" : "cjs";
146146
fs.copyFileSync(
147-
path.join(outputDir, ".build", `cache.${ext}`),
147+
path.join(options.buildDir, `cache.${ext}`),
148148
path.join(outputPath, packagePath, "cache.cjs"),
149149
);
150150

@@ -158,24 +158,21 @@ async function generateBundle(
158158
await bundleNextServer(path.join(outputPath, packagePath), appPath);
159159
}
160160

161-
// // Copy middleware
161+
// Copy middleware
162162
if (
163163
!config.middleware?.external &&
164-
existsSync(path.join(outputDir, ".build", "middleware.mjs"))
164+
existsSync(path.join(options.buildDir, "middleware.mjs"))
165165
) {
166166
fs.copyFileSync(
167-
path.join(outputDir, ".build", "middleware.mjs"),
167+
path.join(options.buildDir, "middleware.mjs"),
168168
path.join(outputPath, packagePath, "middleware.mjs"),
169169
);
170170
}
171171

172172
// Copy open-next.config.mjs
173-
copyOpenNextConfig(
174-
path.join(outputDir, ".build"),
175-
path.join(outputPath, packagePath),
176-
);
173+
copyOpenNextConfig(options.buildDir, path.join(outputPath, packagePath));
177174

178-
//Copy env files
175+
// Copy env files
179176
copyEnvFile(appBuildOutputPath, packagePath, outputPath);
180177

181178
// Copy all necessary traced files

packages/open-next/src/build/edge/createEdgeBundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export async function generateEdgeBundle(
179179
fs.mkdirSync(outputPath, { recursive: true });
180180

181181
// Copy open-next.config.mjs
182-
copyOpenNextConfig(path.join(outputDir, ".build"), outputPath, true);
182+
copyOpenNextConfig(options.buildDir, outputPath, true);
183183

184184
// Load middleware manifest
185185
const middlewareManifest = JSON.parse(

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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(config: OpenNextConfig, tempBuildDir: string) {
2121
const appPath = path.join(process.cwd(), config.appPath || ".");
2222
const buildOutputPath = path.join(
2323
process.cwd(),
@@ -44,14 +44,15 @@ export function normalizeOptions(config: OpenNextConfig) {
4444
appPackageJsonPath,
4545
appPath,
4646
appPublicPath: path.join(appPath, "public"),
47+
buildDir: path.join(outputDir, ".build"),
4748
config,
4849
debug: Boolean(process.env.OPEN_NEXT_DEBUG) ?? false,
4950
monorepoRoot,
5051
nextVersion: getNextVersion(appPath),
5152
openNextVersion: getOpenNextVersion(),
5253
outputDir,
5354
packager,
54-
tempDir: path.join(outputDir, ".build"),
55+
tempBuildDir,
5556
};
5657
}
5758

@@ -77,6 +78,7 @@ function findMonorepoRoot(appPath: string) {
7778
// note: a lock file (package-lock.json, yarn.lock, or pnpm-lock.yaml) is
7879
// not found in the app's directory or any of its parent directories.
7980
// We are going to assume that the app is not part of a monorepo.
81+
logger.warn("No lockfile found");
8082
return { root: appPath, packager: "npm" as const };
8183
}
8284

@@ -273,17 +275,17 @@ export function compareSemver(v1: string, v2: string): number {
273275
}
274276

275277
export function copyOpenNextConfig(
276-
tempDir: string,
277-
outputPath: string,
278+
inputDir: string,
279+
outputDir: string,
278280
isEdge = false,
279281
) {
280282
// Copy open-next.config.mjs
281283
fs.copyFileSync(
282284
path.join(
283-
tempDir,
285+
inputDir,
284286
isEdge ? "open-next.config.edge.mjs" : "open-next.config.mjs",
285287
),
286-
path.join(outputPath, "open-next.config.mjs"),
288+
path.join(outputDir, "open-next.config.mjs"),
287289
);
288290
}
289291

0 commit comments

Comments
 (0)