Skip to content

Commit c2da3a8

Browse files
sommeeeerconico974
andauthored
chore: make dev overrides work in monorepo (#857)
* chore: make dev overrides work in monorepo * semicolon * review * review * refactor * rename function * beautify * Create weak-swans-grin.md --------- Co-authored-by: conico974 <nicodorseuil@yahoo.fr>
1 parent 68a13a4 commit c2da3a8

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

.changeset/weak-swans-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: make dev overrides work in monorepo

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import type { IncrementalCache } from "types/overrides.js";
2-
31
import fs from "node:fs/promises";
42
import path from "node:path";
53

4+
import type { IncrementalCache } from "types/overrides.js";
5+
import { getMonorepoRelativePath } from "utils/normalize-path";
6+
67
const buildId = process.env.NEXT_BUILD_ID;
7-
const basePath = path.resolve(process.cwd(), `../../cache/${buildId}`);
8+
const basePath = path.join(getMonorepoRelativePath(), `cache/${buildId}`);
89

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

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import type { TagCache } from "types/overrides";
2-
31
import fs from "node:fs";
2+
import path from "node:path";
43

5-
// TODO: fix this for monorepo
6-
const tagFile = "../../dynamodb-provider/dynamodb-cache.json";
4+
import type { TagCache } from "types/overrides";
5+
import { getMonorepoRelativePath } from "utils/normalize-path";
76

7+
const tagFile = path.join(
8+
getMonorepoRelativePath(),
9+
"dynamodb-provider/dynamodb-cache.json",
10+
);
811
const tagContent = fs.readFileSync(tagFile, "utf-8");
912

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

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

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

34
import type { StreamCreator } from "types/open-next.js";
45
import type { WrapperHandler } from "types/overrides.js";
6+
import { getMonorepoRelativePath } from "utils/normalize-path";
57

68
const wrapper: WrapperHandler = async (handler, converter) => {
79
const app = express();
810
// To serve static assets
9-
app.use(express.static("../../assets"));
11+
app.use(express.static(path.join(getMonorepoRelativePath(), "assets")));
12+
13+
const imageHandlerPath = path.join(
14+
getMonorepoRelativePath(),
15+
"image-optimization-function/index.mjs",
16+
);
1017

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

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,10 @@ declare global {
220220
var __next_route_preloader: (
221221
stage: "waitUntil" | "start" | "warmerEvent" | "onDemand",
222222
) => Promise<void>;
223+
224+
/**
225+
* This is the relative package path of the monorepo. It will be an empty string "" in normal repos.
226+
* ex. `packages/web`
227+
*/
228+
var monorepoPackagePath: string;
223229
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
import path from "node:path";
2+
13
export function normalizePath(path: string) {
24
return path.replace(/\\/g, "/");
35
}
6+
7+
export function getMonorepoRelativePath(relativePath = "../.."): string {
8+
return path.join(
9+
globalThis.monorepoPackagePath
10+
.split("/")
11+
.filter(Boolean)
12+
.map(() => "..")
13+
.join("/"),
14+
relativePath,
15+
);
16+
}

0 commit comments

Comments
 (0)