Skip to content

Commit

Permalink
Cleanup helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcorlin1 committed May 27, 2024
1 parent c8f9826 commit 0ce0730
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
38 changes: 18 additions & 20 deletions packages/esbuild-plugin-node/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export function openTelemetryPlugin(
args.resolveDir
);

// If it's a local import, don't patch it
if (!extractedModule) return;

// We'll rely on the OTel auto-instrumentation at runtime to patch builtin modules
if (isBuiltIn(args.path, extractedModule)) return;

Expand Down Expand Up @@ -122,42 +125,37 @@ export function openTelemetryPlugin(
* output: { package: '@co/stuff', path: 'foo/bar/baz.js' }
*/
function extractPackageAndModulePath(
path: string,
originalPath: string,
resolveDir: string
): { path: string; extractedModule: ExtractedModule } {
): { path: string; extractedModule: ExtractedModule | null } {
// @see https://github.com/nodejs/node/issues/47000
const fullPath = require.resolve(
path === '.' ? './' : path === '..' ? '../' : path,
const path = require.resolve(
originalPath === '.' ? './' : originalPath === '..' ? '../' : originalPath,
{ paths: [resolveDir] }
);

const nodeModulesIndex = fullPath.lastIndexOf(NODE_MODULES);
if (nodeModulesIndex < 0) {
return {
path: fullPath,
extractedModule: { package: null, path: null },
};
}
const nodeModulesIndex = path.lastIndexOf(NODE_MODULES);
if (nodeModulesIndex < 0) return { path, extractedModule: null };

const subPath = fullPath.substring(nodeModulesIndex + NODE_MODULES.length);
const firstSlash = subPath.indexOf('/');
const subPath = path.substring(nodeModulesIndex + NODE_MODULES.length);
const firstSlashIndex = subPath.indexOf('/');

if (!subPath.startsWith('@')) {
return {
path: fullPath,
path,
extractedModule: {
package: subPath.substring(0, firstSlash),
path: subPath.substring(firstSlash + 1),
package: subPath.substring(0, firstSlashIndex),
path: subPath.substring(firstSlashIndex + 1),
},
};
}

const secondSlash = subPath.substring(firstSlash + 1).indexOf('/');
const secondSlash = subPath.substring(firstSlashIndex + 1).indexOf('/');
return {
path: fullPath,
path,
extractedModule: {
package: subPath.substring(0, firstSlash + 1 + secondSlash),
path: subPath.substring(firstSlash + 1 + secondSlash + 1),
package: subPath.substring(0, firstSlashIndex + secondSlash + 1),
path: subPath.substring(firstSlashIndex + secondSlash + 2),
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/esbuild-plugin-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type { OnLoadArgs as EsbuildOnLoadArgs } from 'esbuild';
import type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';

export interface ExtractedModule {
package: string | null;
path: string | null;
package: string;
path: string;
}

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

0 comments on commit 0ce0730

Please sign in to comment.