@@ -4,6 +4,7 @@ import { join } from "node:path";
44import { type BuildOptions , getPackagePath } from "@opennextjs/aws/build/helper.js" ;
55import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js" ;
66import type { ContentUpdater , Plugin } from "@opennextjs/aws/plugins/content-updater.js" ;
7+ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js" ;
78
89import { 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 : / \. ( j s | m j s | c j s | j s x | t s | t s x ) $ / ,
17+ filter : getCrossPlatformPathRegex (
18+ String . raw `/server/lib/router-utils/instrumentation-globals.external\.js$` ,
19+ {
20+ escape : false ,
21+ }
22+ ) ,
1723 contentFilter : / a s y n c f u n c t i o n g e t I n s t r u m e n t a t i o n M o d u l e \( / ,
1824 callback : ( { contents } ) => patchCode ( contents , getNext154Rule ( builtInstrumentationPath ) ) ,
1925 } ,
@@ -49,23 +55,29 @@ export function patchInstrumentation(updater: ContentUpdater, buildOpts: BuildOp
4955export function getNext154Rule ( builtInstrumentationPath : string | null ) {
5056 return `
5157rule:
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}
5579fix: |-
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