Skip to content

Commit 0ce0730

Browse files
committed
Cleanup helpers
1 parent c8f9826 commit 0ce0730

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

packages/esbuild-plugin-node/src/plugin.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
124127
function 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
}

packages/esbuild-plugin-node/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import type { OnLoadArgs as EsbuildOnLoadArgs } from 'esbuild';
1818
import type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
1919

2020
export interface ExtractedModule {
21-
package: string | null;
22-
path: string | null;
21+
package: string;
22+
path: string;
2323
}
2424

2525
export type OnLoadArgs = Omit<EsbuildOnLoadArgs, 'pluginData'> & {

0 commit comments

Comments
 (0)