@@ -59,6 +59,9 @@ export function openTelemetryPlugin(
5959 args . resolveDir
6060 ) ;
6161
62+ // If it's a local import, don't patch it
63+ if ( ! extractedModule ) return ;
64+
6265 // We'll rely on the OTel auto-instrumentation at runtime to patch builtin modules
6366 if ( isBuiltIn ( args . path , extractedModule ) ) return ;
6467
@@ -122,42 +125,37 @@ export function openTelemetryPlugin(
122125 * output: { package: '@co /stuff', path: 'foo/bar/baz.js' }
123126 */
124127function extractPackageAndModulePath (
125- path : string ,
128+ originalPath : string ,
126129 resolveDir : string
127- ) : { path : string ; extractedModule : ExtractedModule } {
130+ ) : { path : string ; extractedModule : ExtractedModule | null } {
128131 // @see https://github.com/nodejs/node/issues/47000
129- const fullPath = require . resolve (
130- path === '.' ? './' : path === '..' ? '../' : path ,
132+ const path = require . resolve (
133+ originalPath === '.' ? './' : originalPath === '..' ? '../' : originalPath ,
131134 { paths : [ resolveDir ] }
132135 ) ;
133136
134- const nodeModulesIndex = fullPath . lastIndexOf ( NODE_MODULES ) ;
135- if ( nodeModulesIndex < 0 ) {
136- return {
137- path : fullPath ,
138- extractedModule : { package : null , path : null } ,
139- } ;
140- }
137+ const nodeModulesIndex = path . lastIndexOf ( NODE_MODULES ) ;
138+ if ( nodeModulesIndex < 0 ) return { path, extractedModule : null } ;
141139
142- const subPath = fullPath . substring ( nodeModulesIndex + NODE_MODULES . length ) ;
143- const firstSlash = subPath . indexOf ( '/' ) ;
140+ const subPath = path . substring ( nodeModulesIndex + NODE_MODULES . length ) ;
141+ const firstSlashIndex = subPath . indexOf ( '/' ) ;
144142
145143 if ( ! subPath . startsWith ( '@' ) ) {
146144 return {
147- path : fullPath ,
145+ path,
148146 extractedModule : {
149- package : subPath . substring ( 0 , firstSlash ) ,
150- path : subPath . substring ( firstSlash + 1 ) ,
147+ package : subPath . substring ( 0 , firstSlashIndex ) ,
148+ path : subPath . substring ( firstSlashIndex + 1 ) ,
151149 } ,
152150 } ;
153151 }
154152
155- const secondSlash = subPath . substring ( firstSlash + 1 ) . indexOf ( '/' ) ;
153+ const secondSlash = subPath . substring ( firstSlashIndex + 1 ) . indexOf ( '/' ) ;
156154 return {
157- path : fullPath ,
155+ path,
158156 extractedModule : {
159- package : subPath . substring ( 0 , firstSlash + 1 + secondSlash ) ,
160- path : subPath . substring ( firstSlash + 1 + secondSlash + 1 ) ,
157+ package : subPath . substring ( 0 , firstSlashIndex + secondSlash + 1 ) ,
158+ path : subPath . substring ( firstSlashIndex + secondSlash + 2 ) ,
161159 } ,
162160 } ;
163161}
0 commit comments