Skip to content

Commit 69db608

Browse files
authored
chore: Remove runtime ESM/CJS switching (#21761)
This PR uses the build time code comments to remove the runtime ESM/CJS switching.
1 parent 5730d1b commit 69db608

13 files changed

Lines changed: 102 additions & 57 deletions

File tree

packages/google-cloud-serverless/src/sdk.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import type { Integration, Options } from '@sentry/core';
22
import { applySdkMetadata } from '@sentry/core';
33
import type { NodeClient, NodeOptions } from '@sentry/node';
44
import { getDefaultIntegrationsWithoutPerformance, init as initNode } from '@sentry/node';
5-
import { isCjs } from '@sentry/node-core';
65
import { gcpContextIntegration } from './integrations/gcp-context';
76
import { googleCloudGrpcIntegration } from './integrations/google-cloud-grpc';
87
import { googleCloudHttpIntegration } from './integrations/google-cloud-http';
98

109
function getCjsOnlyIntegrations(): Integration[] {
11-
return isCjs()
12-
? [
13-
googleCloudHttpIntegration({ optional: true }), // We mark this integration optional since '@google-cloud/common' module could be missing.
14-
googleCloudGrpcIntegration({ optional: true }), // We mark this integration optional since 'google-gax' module could be missing.
15-
]
16-
: [];
10+
/*! rollup-include-cjs-only */
11+
return [
12+
googleCloudHttpIntegration({ optional: true }), // We mark this integration optional since '@google-cloud/common' module could be missing.
13+
googleCloudGrpcIntegration({ optional: true }), // We mark this integration optional since 'google-gax' module could be missing.
14+
];
15+
/*! rollup-include-cjs-only-end */
16+
/*! rollup-include-esm-only */
17+
return [];
18+
/*! rollup-include-esm-only-end */
1719
}
1820

1921
/** Get the default integrations for the GCP SDK. */

packages/node-core/src/common-exports.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export { getSentryRelease, defaultStackParser } from './sdk/api';
3434
export { createGetModuleFromFilename } from './utils/module';
3535
export { addOriginToSpan } from './utils/addOriginToSpan';
3636
export { initializeEsmLoader } from './sdk/esmLoader';
37-
export { isCjs } from './utils/isCjs';
3837
export { createMissingInstrumentationContext } from './utils/createMissingInstrumentationContext';
3938
export { makeNodeTransport, type NodeTransportOptions } from './transports';
4039
export type { HTTPModuleRequestIncomingMessage } from './transports/http-module';

packages/node-core/src/integrations/modules.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { existsSync, readFileSync } from 'node:fs';
22
import { dirname, join } from 'node:path';
33
import { GLOBAL_OBJ, type IntegrationFn } from '@sentry/core';
4-
import { isCjs } from '../utils/isCjs';
54

65
type ModuleInfo = Record<string, string>;
76

@@ -68,7 +67,9 @@ function collectModules(): ModuleInfo {
6867
return {
6968
...getServerModules(),
7069
...getModulesFromPackageJson(),
71-
...(isCjs() ? collectRequireModules() : {}),
70+
/*! rollup-include-cjs-only */
71+
...collectRequireModules(),
72+
/*! rollup-include-cjs-only-end */
7273
};
7374
}
7475

packages/node-core/src/light/sdk.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { systemErrorIntegration } from '../integrations/systemError';
2828
import { defaultStackParser, getSentryRelease } from '../sdk/api';
2929
import { makeNodeTransport } from '../transports';
3030
import type { NodeClientOptions, NodeOptions } from '../types';
31-
import { isCjs } from '../utils/isCjs';
3231
import { getSpotlightConfig } from '../utils/spotlight';
3332
import { setAsyncLocalStorageAsyncContextStrategy } from './asyncLocalStorageStrategy';
3433
import { LightNodeClient } from './client';
@@ -120,7 +119,12 @@ function _init(
120119

121120
client.init();
122121

123-
debug.log(`SDK initialized from ${isCjs() ? 'CommonJS' : 'ESM'} (light mode)`);
122+
/*! rollup-include-cjs-only */
123+
debug.log(`SDK initialized from CommonJS (light mode)`);
124+
/*! rollup-include-cjs-only-end */
125+
/*! rollup-include-esm-only */
126+
debug.log(`SDK initialized from ESM (light mode)`);
127+
/*! rollup-include-esm-only-end */
124128

125129
client.startClientReportTracking();
126130

packages/node-core/src/sdk/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { consoleIntegration } from '../integrations/console';
3737
import { systemErrorIntegration } from '../integrations/systemError';
3838
import { makeNodeTransport } from '../transports';
3939
import type { NodeClientOptions, NodeOptions } from '../types';
40-
import { isCjs } from '../utils/isCjs';
4140
import { getSpotlightConfig } from '../utils/spotlight';
4241
import { defaultStackParser, getSentryRelease } from './api';
4342
import { NodeClient } from './client';
@@ -134,7 +133,12 @@ function _init(
134133

135134
client.init();
136135

137-
debug.log(`SDK initialized from ${isCjs() ? 'CommonJS' : 'ESM'}`);
136+
/*! rollup-include-cjs-only */
137+
debug.log(`SDK initialized from CommonJS`);
138+
/*! rollup-include-cjs-only-end */
139+
/*! rollup-include-esm-only */
140+
debug.log(`SDK initialized from ESM`);
141+
/*! rollup-include-esm-only-end */
138142

139143
client.startClientReportTracking();
140144

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import type { MissingInstrumentationContext } from '@sentry/core';
2-
import { isCjs } from './isCjs';
32

4-
export const createMissingInstrumentationContext = (pkg: string): MissingInstrumentationContext => ({
5-
package: pkg,
6-
'javascript.is_cjs': isCjs(),
7-
});
3+
export const createMissingInstrumentationContext = (pkg: string): MissingInstrumentationContext => {
4+
let isCjs: boolean;
5+
/*! rollup-include-cjs-only */
6+
isCjs = true;
7+
/*! rollup-include-cjs-only-end */
8+
/*! rollup-include-esm-only */
9+
isCjs = false;
10+
/*! rollup-include-esm-only-end */
11+
12+
return {
13+
package: pkg,
14+
'javascript.is_cjs': isCjs,
15+
};
16+
};

packages/node-core/src/utils/detection.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { consoleSandbox } from '@sentry/core';
22
import { NODE_MAJOR, NODE_MINOR } from '../nodeVersion';
3-
import { isCjs } from './isCjs';
3+
4+
function isCjs(): boolean {
5+
/*! rollup-include-cjs-only */
6+
return true;
7+
/*! rollup-include-cjs-only-end */
8+
9+
/*! rollup-include-esm-only */
10+
return false;
11+
/*! rollup-include-esm-only-end */
12+
}
413

514
let hasWarnedAboutNodeVersion: boolean | undefined;
615

packages/node-core/src/utils/ensureIsWrapped.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '@sentry/core';
1111
import type { NodeClient } from '../sdk/client';
1212
import { createMissingInstrumentationContext } from './createMissingInstrumentationContext';
13-
import { isCjs } from './isCjs';
1413

1514
/**
1615
* Checks and warns if a framework isn't wrapped by opentelemetry.
@@ -30,17 +29,19 @@ export function ensureIsWrapped(
3029
hasSpansEnabled(clientOptions)
3130
) {
3231
consoleSandbox(() => {
33-
if (isCjs()) {
34-
// eslint-disable-next-line no-console
35-
console.warn(
36-
`[Sentry] ${name} is not instrumented. This is likely because you required/imported ${name} before calling \`Sentry.init()\`.`,
37-
);
38-
} else {
39-
// eslint-disable-next-line no-console
40-
console.warn(
41-
`[Sentry] ${name} is not instrumented. Please make sure to initialize Sentry in a separate file that you \`--import\` when running node, see: https://docs.sentry.io/platforms/javascript/guides/${name}/install/esm/.`,
42-
);
43-
}
32+
/*! rollup-include-cjs-only */
33+
// eslint-disable-next-line no-console
34+
console.warn(
35+
`[Sentry] ${name} is not instrumented. This is likely because you required/imported ${name} before calling \`Sentry.init()\`.`,
36+
);
37+
/*! rollup-include-cjs-only-end */
38+
39+
/*! rollup-include-esm-only */
40+
// eslint-disable-next-line no-console
41+
console.warn(
42+
`[Sentry] ${name} is not instrumented. Please make sure to initialize Sentry in a separate file that you \`--import\` when running node, see: https://docs.sentry.io/platforms/javascript/guides/${name}/install/esm/.`,
43+
);
44+
/*! rollup-include-esm-only-end */
4445
});
4546

4647
getGlobalScope().setContext('missing_instrumentation', createMissingInstrumentationContext(name));

packages/node-core/src/utils/isCjs.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/node-core/test/utils/ensureIsWrapped.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('ensureIsWrapped', () => {
2828

2929
expect(spyWarn).toHaveBeenCalledTimes(1);
3030
expect(spyWarn).toHaveBeenCalledWith(
31-
'[Sentry] express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.',
31+
'[Sentry] express is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/express/install/esm/.',
3232
);
3333
});
3434

0 commit comments

Comments
 (0)