Skip to content

Commit df0d758

Browse files
committed
fix(profiling) shim __dirname for esm
1 parent 4626a4e commit df0d758

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

packages/profiling-node/rollup.npm.config.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import commonjs from '@rollup/plugin-commonjs';
22
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
33

44
export const ESMImportShim = `
5-
import cjsModule from 'node:module';
5+
import {createRequire} from 'node:module';
6+
import {fileURLToPath} from 'node:url';
7+
import {dirname } from 'node:path';
68
`;
79

810
const ESMRequireShim = `
9-
const require = cjsModule.createRequire(import.meta.url);
11+
const require = createRequire(import.meta.url);
1012
`;
1113

14+
const ESMDirnameShim = `
15+
const filename = fileURLToPath(import.meta.url);
16+
const __dirname = dirname(__filename);
17+
`
18+
1219
function makeESMImportShimPlugin(shim) {
1320
return {
1421
transform(code) {
@@ -27,6 +34,15 @@ function makeESMRequireShimPlugin(shim) {
2734
};
2835
}
2936

37+
function makeESMDirnameShimPlugin(shim){
38+
return {
39+
transform(code){
40+
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_DIRNAME_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_DIRNAME_SHIM/
41+
return code.replace(SHIM_REGEXP, shim);
42+
}
43+
}
44+
}
45+
3046
const variants = makeNPMConfigVariants(
3147
makeBaseNPMConfig({
3248
packageSpecificConfig: {
@@ -40,10 +56,12 @@ for (const variant of variants) {
4056
if (variant.output.format === 'esm') {
4157
variant.plugins.push(makeESMImportShimPlugin(ESMImportShim));
4258
variant.plugins.push(makeESMRequireShimPlugin(ESMRequireShim));
59+
variant.plugins.push(makeESMDirnameShimPlugin(ESMDirnameShim));
4360
} else {
4461
// Remove the ESM shim comment
4562
variant.plugins.push(makeESMImportShimPlugin(''));
4663
variant.plugins.push(makeESMRequireShimPlugin(''));
64+
variant.plugins.push(makeESMDirnameShimPlugin(''));
4765
}
4866
}
4967

packages/profiling-node/src/cpu_profiler.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { arch as _arch, platform as _platform } from 'node:os';
2-
import { join, resolve } from 'node:path';
2+
import { join, resolve} from 'node:path';
33
import { env, versions } from 'node:process';
44
import { threadId } from 'node:worker_threads';
55
import { familySync } from 'detect-libc';
@@ -27,17 +27,13 @@ const arch = process.env['BUILD_ARCH'] || _arch();
2727
const abi = getAbi(versions.node, 'node');
2828
const identifier = [platform, arch, stdlib, abi].filter(c => c !== undefined && c !== null).join('-');
2929

30-
const built_from_source_path = resolve(__dirname, '..', `./sentry_cpu_profiler-${identifier}`);
31-
3230
/**
3331
* Imports cpp bindings based on the current platform and architecture.
3432
*/
3533
// eslint-disable-next-line complexity
3634
export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
3735
// #START_SENTRY_ESM_REQUIRE_SHIM
38-
// When building for ESM, we shim require to use createRequire and __dirname.
39-
// We need to do this because .node extensions in esm are not supported.
40-
// The comment below this line exists as a placeholder for where to insert the shim.
36+
// When building for ESM, we shim require to use createRequire because .node extensions in esm are not supported.
4137
// #END_SENTRY_ESM_REQUIRE_SHIM
4238

4339
// If a binary path is specified, use that.
@@ -165,6 +161,12 @@ export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
165161
}
166162
}
167163
}
164+
165+
// #START_SENTRY_ESM_DIRNAME_SHIM
166+
// const filename = fileURLToPath(import.meta.url);
167+
// const __dirname = dirname(filename);
168+
// #END_SENTRY_ESM_DIRNAME_SHIM
169+
const built_from_source_path = resolve(__dirname, '..', `./sentry_cpu_profiler-${identifier}`);
168170
return require(`${built_from_source_path}.node`);
169171
}
170172

0 commit comments

Comments
 (0)