Skip to content

Commit 06a4687

Browse files
committed
review
1 parent fa6b132 commit 06a4687

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

packages/cloudflare/src/cli/build/patches/plugins/instrumentation.spec.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,37 +117,37 @@ describe("getInstrumenationModule (Next154)", () => {
117117
test("patch when an instrumentation file is not present", async () => {
118118
expect(patchCode(code, getNext154Rule(null))).toMatchInlineSnapshot(`
119119
"async function getInstrumentationModule(projectDir, distDir) {
120-
if (cachedInstrumentationModule) {
121-
return cachedInstrumentationModule;
122-
}
123-
try {
124-
cachedInstrumentationModule = null;
125-
return cachedInstrumentationModule;
126-
} catch (err) {
127-
if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") {
128-
throw err;
120+
if (cachedInstrumentationModule) {
121+
return cachedInstrumentationModule;
122+
}
123+
try {
124+
cachedInstrumentationModule = null;
125+
return cachedInstrumentationModule;
126+
} catch (err) {
127+
if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") {
128+
throw err;
129+
}
130+
}
129131
}
130-
}
131-
}
132132
"
133133
`);
134134
});
135135

136136
test("patch when an instrumentation file is present", async () => {
137137
expect(patchCode(code, getNext154Rule("/_file_exists_/instrumentation.js"))).toMatchInlineSnapshot(`
138138
"async function getInstrumentationModule(projectDir, distDir) {
139-
if (cachedInstrumentationModule) {
140-
return cachedInstrumentationModule;
141-
}
142-
try {
143-
cachedInstrumentationModule = require('/_file_exists_/instrumentation.js');
144-
return cachedInstrumentationModule;
145-
} catch (err) {
146-
if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") {
147-
throw err;
139+
if (cachedInstrumentationModule) {
140+
return cachedInstrumentationModule;
141+
}
142+
try {
143+
cachedInstrumentationModule = require('/_file_exists_/instrumentation.js');
144+
return cachedInstrumentationModule;
145+
} catch (err) {
146+
if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") {
147+
throw err;
148+
}
149+
}
148150
}
149-
}
150-
}
151151
"
152152
`);
153153
});

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { join } from "node:path";
44
import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
55
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
66
import type { ContentUpdater, Plugin } from "@opennextjs/aws/plugins/content-updater.js";
7+
import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
78

89
import { normalizePath } from "../../utils/normalize-path.js";
910

@@ -13,7 +14,12 @@ export function patchInstrumentation(updater: ContentUpdater, buildOpts: BuildOp
1314
updater.updateContent("patch-instrumentation-next15-4", [
1415
{
1516
field: {
16-
filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
17+
filter: getCrossPlatformPathRegex(
18+
String.raw`/server/lib/router-utils/instrumentation-globals.external\.js$`,
19+
{
20+
escape: false,
21+
}
22+
),
1723
contentFilter: /async function getInstrumentationModule\(/,
1824
callback: ({ contents }) => patchCode(contents, getNext154Rule(builtInstrumentationPath)),
1925
},
@@ -49,23 +55,29 @@ export function patchInstrumentation(updater: ContentUpdater, buildOpts: BuildOp
4955
export function getNext154Rule(builtInstrumentationPath: string | null) {
5056
return `
5157
rule:
52-
kind: function_declaration
53-
any:
54-
- has: {field: name, regex: ^getInstrumentationModule$}
58+
kind: expression_statement
59+
has:
60+
kind: assignment_expression
61+
all:
62+
- has: {kind: identifier, pattern: cachedInstrumentationModule}
63+
has:
64+
kind: call_expression
65+
all:
66+
- has: {kind: arguments, regex: _constants.INSTRUMENTATION_HOOK_FILENAME}
67+
inside:
68+
kind: try_statement
69+
stopBy: end
70+
has:
71+
all:
72+
- has: {kind: return_statement, pattern: return cachedInstrumentationModule}
73+
inside:
74+
kind: statement_block
75+
inside:
76+
kind: function_declaration
77+
all:
78+
- has: {field: name, pattern: getInstrumentationModule}
5579
fix: |-
56-
async function getInstrumentationModule(projectDir, distDir) {
57-
if (cachedInstrumentationModule) {
58-
return cachedInstrumentationModule;
59-
}
60-
try {
6180
cachedInstrumentationModule = ${builtInstrumentationPath ? `require('${builtInstrumentationPath}')` : "null"};
62-
return cachedInstrumentationModule;
63-
} catch (err) {
64-
if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") {
65-
throw err;
66-
}
67-
}
68-
}
6981
`;
7082
}
7183

0 commit comments

Comments
 (0)