Skip to content

Commit 049be11

Browse files
committed
patch instr
fix instr fix
1 parent 79305a1 commit 049be11

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
22
import { describe, expect, test } from "vitest";
33

4-
import { getNext14Rule, getNext15Rule } from "./instrumentation.js";
4+
import { getNext14Rule, getNext15Rule, getNext154Rule } from "./instrumentation.js";
55

66
describe("LoadInstrumentationModule (Next15)", () => {
77
const code = `
@@ -96,3 +96,59 @@ describe("prepareImpl (Next14)", () => {
9696
`);
9797
});
9898
});
99+
100+
describe("getInstrumenationModule (Next154)", () => {
101+
const code = `
102+
async function getInstrumentationModule(projectDir, distDir) {
103+
if (cachedInstrumentationModule) {
104+
return cachedInstrumentationModule;
105+
}
106+
try {
107+
cachedInstrumentationModule = (0, _interopdefault.interopDefault)(await require(_nodepath.default.join(projectDir, distDir, "server", \`\${_constants.INSTRUMENTATION_HOOK_FILENAME}.js\`\)));
108+
return cachedInstrumentationModule;
109+
} catch (err) {
110+
if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") {
111+
throw err;
112+
}
113+
}
114+
}
115+
`;
116+
117+
test("patch when an instrumentation file is not present", async () => {
118+
expect(patchCode(code, getNext154Rule(null))).toMatchInlineSnapshot(`
119+
"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;
129+
}
130+
}
131+
}
132+
"
133+
`);
134+
});
135+
136+
test("patch when an instrumentation file is present", async () => {
137+
expect(patchCode(code, getNext154Rule("/_file_exists_/instrumentation.js"))).toMatchInlineSnapshot(`
138+
"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;
148+
}
149+
}
150+
}
151+
"
152+
`);
153+
});
154+
});

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ import { normalizePath } from "../../utils/normalize-path.js";
1010
export function patchInstrumentation(updater: ContentUpdater, buildOpts: BuildOptions): Plugin {
1111
const builtInstrumentationPath = getBuiltInstrumentationPath(buildOpts);
1212

13+
updater.updateContent("patch-instrumentation-next15-4", [
14+
{
15+
field: {
16+
filter: /\.(js|mjs|cjs|jsx|ts|tsx)$/,
17+
contentFilter: /async function getInstrumentationModule\(/,
18+
callback: ({ contents }) => patchCode(contents, getNext154Rule(builtInstrumentationPath)),
19+
},
20+
},
21+
]);
22+
1323
updater.updateContent("patch-instrumentation-next15", [
1424
{
1525
field: {
@@ -36,6 +46,29 @@ export function patchInstrumentation(updater: ContentUpdater, buildOpts: BuildOp
3646
};
3747
}
3848

49+
export function getNext154Rule(builtInstrumentationPath: string | null) {
50+
return `
51+
rule:
52+
kind: function_declaration
53+
any:
54+
- has: {field: name, regex: ^getInstrumentationModule$}
55+
fix: |-
56+
async function getInstrumentationModule(projectDir, distDir) {
57+
if (cachedInstrumentationModule) {
58+
return cachedInstrumentationModule;
59+
}
60+
try {
61+
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+
}
69+
`;
70+
}
71+
3972
export function getNext15Rule(builtInstrumentationPath: string | null) {
4073
return `
4174
rule:

0 commit comments

Comments
 (0)