Skip to content

Commit 68ccc71

Browse files
committed
chore: make dev overrides work in monorepo
1 parent 68a13a4 commit 68ccc71

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ async function generateBundle(
302302
outfile: path.join(outputPath, packagePath, `index.${outfileExt}`),
303303
banner: {
304304
js: [
305-
`globalThis.monorepoPackagePath = "${packagePath}";`,
305+
needsGlobalOutputDir(options)
306+
? `globalThis.outputDir = "${outputDir}"`
307+
: "",
306308
"import process from 'node:process';",
307309
"import { Buffer } from 'node:buffer';",
308310
"import { createRequire as topLevelCreateRequire } from 'module';",
@@ -395,3 +397,13 @@ async function minifyServerBundle(outputDir: string) {
395397
mangle: true,
396398
});
397399
}
400+
401+
// Check if we need the outputDir in any dev override
402+
// Remember to update this if you add a new override that needs the outputDir
403+
function needsGlobalOutputDir(options: buildHelper.BuildOptions) {
404+
return (
405+
options.config.default?.override?.wrapper === "express-dev" ||
406+
options.config.default.override?.incrementalCache === "fs-dev" ||
407+
options.config.default.override?.tagCache === "fs-dev"
408+
);
409+
}

packages/open-next/src/overrides/incrementalCache/fs-dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from "node:fs/promises";
44
import path from "node:path";
55

66
const buildId = process.env.NEXT_BUILD_ID;
7-
const basePath = path.resolve(process.cwd(), `../../cache/${buildId}`);
7+
const basePath = path.join(globalThis.outputDir, `cache/${buildId}`);
88

99
const getCacheKey = (key: string) => {
1010
return path.join(basePath, `${key}.cache`);

packages/open-next/src/overrides/tagCache/fs-dev.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { TagCache } from "types/overrides";
22

33
import fs from "node:fs";
4+
import path from "node:path";
45

5-
// TODO: fix this for monorepo
6-
const tagFile = "../../dynamodb-provider/dynamodb-cache.json";
7-
6+
const tagFile = path.join(
7+
globalThis.outputDir,
8+
"dynamodb-provider/dynamodb-cache.json",
9+
);
810
const tagContent = fs.readFileSync(tagFile, "utf-8");
911

1012
let tags = JSON.parse(tagContent) as {

packages/open-next/src/overrides/wrappers/express-dev.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from "node:path";
12
import express from "express";
23

34
import type { StreamCreator } from "types/open-next.js";
@@ -6,9 +7,13 @@ import type { WrapperHandler } from "types/overrides.js";
67
const wrapper: WrapperHandler = async (handler, converter) => {
78
const app = express();
89
// To serve static assets
9-
app.use(express.static("../../assets"));
10+
app.use(express.static(path.join(globalThis.outputDir, "assets")));
11+
12+
const imageHandlerPath = path.join(
13+
globalThis.outputDir,
14+
"image-optimization-function/index.mjs",
15+
);
1016

11-
const imageHandlerPath = "../../image-optimization-function/index.mjs";
1217
const imageHandler = await import(imageHandlerPath).then((m) => m.handler);
1318

1419
app.all("/_next/image", async (req, res) => {

packages/open-next/src/types/global.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,9 @@ declare global {
220220
var __next_route_preloader: (
221221
stage: "waitUntil" | "start" | "warmerEvent" | "onDemand",
222222
) => Promise<void>;
223+
224+
/**
225+
* This is for dev overrides that needs the output directory.
226+
*/
227+
var outputDir: string;
223228
}

0 commit comments

Comments
 (0)