Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e27cd2e

Browse files
add getBuiltInstrumentationPath utility
1 parent 6e590ab commit e27cd2e

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

packages/cloudflare/src/cli/build/patches/plugins/load-instrumentation.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22
* `loadInstrumentationModule` uses a dynamic require which is not supported.
33
*/
44

5-
import { existsSync } from "node:fs";
6-
import { join } from "node:path";
7-
8-
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
5+
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
96

7+
import { getBuiltInstrumentationPath } from "../../utils/get-built-instrumentation-path.js";
108
import { patchCode } from "../ast/util.js";
119
import type { ContentUpdater } from "./content-updater.js";
1210

1311
export function patchLoadInstrumentation(updater: ContentUpdater, buildOpts: BuildOptions) {
14-
const { outputDir } = buildOpts;
15-
16-
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
17-
const dotNextDir = join(baseDir, ".next");
18-
const maybeBuiltInstrumentationPath = join(dotNextDir, "server", `${INSTRUMENTATION_HOOK_FILENAME}.js`);
19-
const builtInstrumentationPath = existsSync(maybeBuiltInstrumentationPath)
20-
? maybeBuiltInstrumentationPath
21-
: null;
12+
const builtInstrumentationPath = getBuiltInstrumentationPath(buildOpts);
2213

2314
return updater.updateContent(
2415
"patch-load-instrumentation",
@@ -42,9 +33,3 @@ export async function getRule(builtInstrumentationPath: string | null) {
4233
}
4334
`;
4435
}
45-
46-
/**
47-
* Pattern to detect instrumentation hooks file
48-
* (taken from Next.js source: https://github.com/vercel/next.js/blob/1d5820563/packages/next/src/lib/constants.ts#L46-L47)
49-
*/
50-
const INSTRUMENTATION_HOOK_FILENAME = "instrumentation";

packages/cloudflare/src/cli/build/patches/plugins/prepare-impl.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@
44
* `prepareImpl` is the method that sets up instrumentation in Next 14 (this is `loadInstrumentationModule` in Next 15).
55
*/
66

7-
import { existsSync } from "node:fs";
8-
import { join } from "node:path";
9-
10-
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
7+
import { type BuildOptions } from "@opennextjs/aws/build/helper.js";
118

9+
import { getBuiltInstrumentationPath } from "../../utils/get-built-instrumentation-path.js";
1210
import { patchCode } from "../ast/util.js";
1311
import type { ContentUpdater } from "./content-updater.js";
1412

1513
export function patchPrepareImpl(updater: ContentUpdater, buildOpts: BuildOptions) {
16-
const { outputDir } = buildOpts;
17-
18-
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
19-
const dotNextDir = join(baseDir, ".next");
20-
const maybeBuiltInstrumentationPath = join(dotNextDir, "server", `${INSTRUMENTATION_HOOK_FILENAME}.js`);
21-
const builtInstrumentationPath = existsSync(maybeBuiltInstrumentationPath)
22-
? maybeBuiltInstrumentationPath
23-
: null;
14+
const builtInstrumentationPath = getBuiltInstrumentationPath(buildOpts);
2415

2516
return updater.updateContent(
2617
"patch-prepareImpl",
@@ -46,9 +37,3 @@ export async function getRule(builtInstrumentationPath: string | null) {
4637
}
4738
`;
4839
}
49-
50-
/**
51-
* Pattern to detect instrumentation hooks file
52-
* (taken from Next.js source: https://github.com/vercel/next.js/blob/1d5820563/packages/next/src/lib/constants.ts#L46-L47)
53-
*/
54-
const INSTRUMENTATION_HOOK_FILENAME = "instrumentation";
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { existsSync } from "node:fs";
2+
import { join } from "node:path";
3+
4+
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
5+
6+
/**
7+
* Gets the instrumentation.js file that the Next.js build process generates when an
8+
* instrumentation hook is provided in the app's source
9+
*
10+
* @param buildOpts the open-next build options
11+
* @returns a string pointing to the instrumentation.js file location, or null if such file is not found
12+
*/
13+
export function getBuiltInstrumentationPath(buildOpts: BuildOptions): string | null {
14+
const { outputDir } = buildOpts;
15+
16+
const baseDir = join(outputDir, "server-functions/default", getPackagePath(buildOpts));
17+
const dotNextDir = join(baseDir, ".next");
18+
const maybeBuiltInstrumentationPath = join(dotNextDir, "server", `${INSTRUMENTATION_HOOK_FILENAME}.js`);
19+
const builtInstrumentationPath = existsSync(maybeBuiltInstrumentationPath)
20+
? maybeBuiltInstrumentationPath
21+
: null;
22+
23+
return builtInstrumentationPath;
24+
}
25+
26+
/**
27+
* Pattern to detect instrumentation hooks file
28+
* (taken from Next.js source: https://github.com/vercel/next.js/blob/1d5820563/packages/next/src/lib/constants.ts#L46-L47)
29+
*/
30+
const INSTRUMENTATION_HOOK_FILENAME = "instrumentation";

0 commit comments

Comments
 (0)