1- import { existsSync } from "node:fs" ;
21import { mkdir , writeFile } from "node:fs/promises" ;
32import { join } from "node:path" ;
43
@@ -7,6 +6,7 @@ import type { CompileAgentResult } from "#compiler/compile-agent.js";
76import { createCompiledModuleMapSource } from "#compiler/module-map.js" ;
87import { getWorldImport } from "@workflow/utils" ;
98import { stringifyEsmImportSpecifier } from "#internal/application/import-specifier.js" ;
9+ import { resolveInstrumentationModule } from "#internal/application/instrumentation-module.js" ;
1010import {
1111 resolvePackageCompiledFilePath ,
1212 resolvePackageSourceFilePath ,
@@ -25,16 +25,8 @@ export interface GeneratedCompiledArtifactsFiles {
2525 bootstrapPath : string ;
2626 /** Nitro plugin that installs the selected vendored Workflow world. */
2727 workflowWorldPluginPath : string ;
28- /**
29- * Optional Nitro plugin that imports the authored instrumentation module
30- * from the application when present.
31- */
32- instrumentationPluginPath ?: string ;
33- /**
34- * Absolute path to the authored instrumentation module when present.
35- * Nitro uses this to preserve the module's side effects during bundling.
36- */
37- instrumentationSourcePath ?: string ;
28+ /** Nitro plugin that imports authored instrumentation when present. */
29+ instrumentationPluginPath : string ;
3830}
3931
4032/**
@@ -71,45 +63,20 @@ export async function writeCompiledArtifactsFiles(input: {
7163 ) ,
7264 ) ;
7365
74- if ( instrumentationPath !== undefined ) {
75- await writeFile (
76- instrumentationPluginPath ,
77- createInstrumentationPluginSource ( {
78- agentName : input . compileResult . manifest . config . name ,
79- instrumentationPath,
80- registerConfigPath : resolvePackageSourceFilePath ( "src/harness/instrumentation-config.ts" ) ,
81- } ) ,
82- ) ;
83- }
66+ await writeFile (
67+ instrumentationPluginPath ,
68+ createInstrumentationPluginSource ( {
69+ agentName : input . compileResult . manifest . config . name ,
70+ instrumentationPath,
71+ registerConfigPath : resolvePackageSourceFilePath ( "src/harness/instrumentation-config.ts" ) ,
72+ } ) ,
73+ ) ;
8474
85- const generatedArtifacts : GeneratedCompiledArtifactsFiles = {
75+ return {
8676 bootstrapPath,
77+ instrumentationPluginPath,
8778 workflowWorldPluginPath,
8879 } ;
89-
90- if ( instrumentationPath !== undefined ) {
91- generatedArtifacts . instrumentationPluginPath = instrumentationPluginPath ;
92- generatedArtifacts . instrumentationSourcePath = instrumentationPath ;
93- }
94-
95- return generatedArtifacts ;
96- }
97-
98- const INSTRUMENTATION_EXTENSIONS = [ ".ts" , ".mts" , ".js" , ".mjs" ] ;
99-
100- /**
101- * Resolves the optional `agent/instrumentation` module from the agent root
102- * directory. Returns the absolute path if found, `undefined` otherwise.
103- */
104- function resolveInstrumentationModule ( agentRoot : string ) : string | undefined {
105- for ( const ext of INSTRUMENTATION_EXTENSIONS ) {
106- const candidate = join ( agentRoot , `instrumentation${ ext } ` ) ;
107- if ( existsSync ( candidate ) ) {
108- return candidate ;
109- }
110- }
111-
112- return undefined ;
11380}
11481
11582function stripCompiledModuleMapExports ( source : string ) : string {
@@ -212,18 +179,22 @@ export function createWorkflowWorldPluginSource(
212179
213180function createInstrumentationPluginSource ( input : {
214181 agentName : string ;
215- instrumentationPath : string ;
182+ instrumentationPath : string | undefined ;
216183 registerConfigPath : string ;
217184} ) : string {
218185 return [
219186 "// Generated by eve. Do not edit by hand." ,
220- `import * as instrumentationModule from ${ stringifyEsmImportSpecifier ( input . instrumentationPath ) } ;` ,
221- `import { registerInstrumentationConfig } from ${ stringifyEsmImportSpecifier ( input . registerConfigPath ) } ;` ,
222- "" ,
223- "if (instrumentationModule.default != null) {" ,
224- ` registerInstrumentationConfig(instrumentationModule.default, { agentName: ${ JSON . stringify ( input . agentName ) } });` ,
225- "}" ,
226- "" ,
187+ ...( input . instrumentationPath === undefined
188+ ? [ ]
189+ : [
190+ `import * as instrumentationModule from ${ stringifyEsmImportSpecifier ( input . instrumentationPath ) } ;` ,
191+ `import { registerInstrumentationConfig } from ${ stringifyEsmImportSpecifier ( input . registerConfigPath ) } ;` ,
192+ "" ,
193+ "if (instrumentationModule.default != null) {" ,
194+ ` registerInstrumentationConfig(instrumentationModule.default, { agentName: ${ JSON . stringify ( input . agentName ) } });` ,
195+ "}" ,
196+ "" ,
197+ ] ) ,
227198 "// Default export satisfies the Nitro plugin contract so this file" ,
228199 "// can be used directly as a Nitro plugin without a separate wrapper." ,
229200 "export default function installInstrumentationPlugin() {}" ,
0 commit comments