Skip to content

Commit 026ed20

Browse files
committed
refactor: consistently retrieve the build folder from the options
And a few other minor changes: - rename BuildOptions#tempDir to BuildOptions#buildDir - simplify the default OpenNext config - rename a few arguments for consistency / clarity
1 parent 4671c8e commit 026ed20

File tree

5 files changed

+26
-39
lines changed

5 files changed

+26
-39
lines changed

packages/open-next/src/build.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,12 @@ function initOutputDir(srcTempDir: string, options: BuildOptions) {
191191
);
192192
}
193193
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-
);
194+
const { buildDir } = options;
195+
fs.mkdirSync(buildDir, { recursive: true });
196+
fs.writeFileSync(path.join(buildDir, "open-next.config.mjs"), openNextConfig);
200197
if (openNextConfigEdge) {
201198
fs.writeFileSync(
202-
path.join(destTempDir, "open-next.config.edge.mjs"),
199+
path.join(buildDir, "open-next.config.edge.mjs"),
203200
openNextConfigEdge,
204201
);
205202
}
@@ -215,7 +212,7 @@ async function createWarmerBundle(options: BuildOptions) {
215212
fs.mkdirSync(outputPath, { recursive: true });
216213

217214
// Copy open-next.config.mjs into the bundle
218-
copyOpenNextConfig(options.tempDir, outputPath);
215+
copyOpenNextConfig(options.buildDir, outputPath);
219216

220217
// Build Lambda code
221218
// note: bundle in OpenNext package b/c the adatper relys on the
@@ -258,7 +255,7 @@ async function createRevalidationBundle(options: BuildOptions) {
258255
fs.mkdirSync(outputPath, { recursive: true });
259256

260257
//Copy open-next.config.mjs into the bundle
261-
copyOpenNextConfig(options.tempDir, outputPath);
258+
copyOpenNextConfig(options.buildDir, outputPath);
262259

263260
// Build Lambda code
264261
await esbuildAsync(
@@ -297,7 +294,7 @@ async function createImageOptimizationBundle(options: BuildOptions) {
297294
fs.mkdirSync(outputPath, { recursive: true });
298295

299296
// Copy open-next.config.mjs into the bundle
300-
copyOpenNextConfig(options.tempDir, outputPath);
297+
copyOpenNextConfig(options.buildDir, outputPath);
301298

302299
const plugins = [
303300
openNextResolvePlugin({
@@ -636,7 +633,7 @@ async function createCacheAssets(options: BuildOptions) {
636633
);
637634

638635
//Copy open-next.config.mjs into the bundle
639-
copyOpenNextConfig(options.tempDir, providerPath);
636+
copyOpenNextConfig(options.buildDir, providerPath);
640637

641638
// TODO: check if metafiles doesn't contain duplicates
642639
fs.writeFileSync(
@@ -660,7 +657,7 @@ export function compileCache(
660657
) {
661658
const { config } = options;
662659
const ext = format === "cjs" ? "cjs" : "mjs";
663-
const outfile = path.join(options.outputDir, ".build", `cache.${ext}`);
660+
const outfile = path.join(options.buildDir, `cache.${ext}`);
664661

665662
const isAfter15 = compareSemver(options.nextVersion, "15.0.0") >= 0;
666663

@@ -721,7 +718,7 @@ async function createMiddleware(options: BuildOptions) {
721718

722719
// Copy open-next.config.mjs
723720
copyOpenNextConfig(
724-
options.tempDir,
721+
options.buildDir,
725722
outputPath,
726723
config.middleware.override?.wrapper === "cloudflare",
727724
);
@@ -739,7 +736,7 @@ async function createMiddleware(options: BuildOptions) {
739736
} else {
740737
await buildEdgeBundle({
741738
entrypoint: path.join(__dirname, "core", "edgeFunctionHandler.js"),
742-
outfile: path.join(outputDir, ".build", "middleware.mjs"),
739+
outfile: path.join(options.buildDir, "middleware.mjs"),
743740
...commonMiddlewareOptions,
744741
onlyBuildOnce: true,
745742
});

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function normalizeOptions(config: OpenNextConfig) {
5151
openNextVersion: getOpenNextVersion(),
5252
outputDir,
5353
packager,
54-
tempDir: path.join(outputDir, ".build"),
54+
buildDir: path.join(outputDir, ".build"),
5555
};
5656
}
5757

@@ -273,17 +273,17 @@ export function compareSemver(v1: string, v2: string): number {
273273
}
274274

275275
export function copyOpenNextConfig(
276-
tempDir: string,
277-
outputPath: string,
276+
inputDir: string,
277+
outputDir: string,
278278
isEdge = false,
279279
) {
280280
// Copy open-next.config.mjs
281281
fs.copyFileSync(
282282
path.join(
283-
tempDir,
283+
inputDir,
284284
isEdge ? "open-next.config.edge.mjs" : "open-next.config.mjs",
285285
),
286-
path.join(outputPath, "open-next.config.mjs"),
286+
path.join(outputDir, "open-next.config.mjs"),
287287
);
288288
}
289289

0 commit comments

Comments
 (0)