Skip to content

Commit 43d4d33

Browse files
authored
feat(aws-serverless): Fix tree-shaking for aws-serverless package (#12017)
The prior fix was incomplete, because we were still using `getDefaultIntegrations()` inside of node's `init`, so the deps where pulled in anyhow. Furthermore, it seems that `preserveModules: false` for `@sentry/node` also prevented this from working as expected. So this PR does two things: 1. Set `preserveModules: true` so that tree-shaking can work as expected (😿 ) 2. Expose a new `initWithoutDefaultIntegrations` method from `@sentry/node` which AWS uses, which avoids including any integrations by default. You have to pass your own `defaultIntegrations` to it. This is not the prettiest solution, but I couldn't think of anything much better 😬 I also added a size-limit entry to keep track of this.
1 parent c0753f0 commit 43d4d33

File tree

7 files changed

+63
-17
lines changed

7 files changed

+63
-17
lines changed

.size-limit.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,33 @@ module.exports = [
211211
gzip: true,
212212
limit: '180 KB',
213213
},
214+
// AWS SDK (ESM)
215+
{
216+
name: '@sentry/aws-serverless',
217+
path: 'packages/aws-serverless/build/npm/esm/index.js',
218+
import: createImport('init'),
219+
ignore: [
220+
'node:http',
221+
'node:https',
222+
'node:diagnostics_channel',
223+
'async_hooks',
224+
'child_process',
225+
'perf_hooks',
226+
'fs',
227+
'os',
228+
'path',
229+
'inspector',
230+
'worker_threads',
231+
'http',
232+
'stream',
233+
'zlib',
234+
'net',
235+
'tls',
236+
'module',
237+
],
238+
gzip: true,
239+
limit: '140 KB',
240+
},
214241
];
215242

216243
function createImport(...args) {

dev-packages/e2e-tests/test-applications/node-exports-test-app/scripts/consistentExports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const NODE_EXPORTS_IGNORE = [
1717
// Only required from the Node package
1818
'setNodeAsyncContextStrategy',
1919
'getDefaultIntegrationsWithoutPerformance',
20+
'initWithoutDefaultIntegrations',
2021
];
2122

2223
const nodeExports = Object.keys(SentryNode).filter(e => !NODE_EXPORTS_IGNORE.includes(e));

packages/aws-serverless/src/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
flush,
1212
getCurrentScope,
1313
getDefaultIntegrationsWithoutPerformance,
14-
init as initNode,
14+
initWithoutDefaultIntegrations,
1515
startSpanManual,
1616
withScope,
1717
} from '@sentry/node';
@@ -93,7 +93,7 @@ export function init(options: NodeOptions = {}): void {
9393
version: SDK_VERSION,
9494
};
9595

96-
initNode(opts);
96+
initWithoutDefaultIntegrations(opts);
9797
}
9898

9999
/** */

packages/aws-serverless/test/sdk.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jest.mock('@sentry/node', () => {
2424
const original = jest.requireActual('@sentry/node');
2525
return {
2626
...original,
27-
init: (options: unknown) => {
27+
initWithoutDefaultIntegrations: (options: unknown) => {
2828
mockInit(options);
2929
},
3030
startInactiveSpan: (...args: unknown[]) => {

packages/node/rollup.npm.config.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ export default [
2323
output: {
2424
// set exports to 'named' or 'auto' so that rollup doesn't warn
2525
exports: 'named',
26-
// set preserveModules to false because we want to bundle everything into one file.
27-
preserveModules:
28-
process.env.SENTRY_BUILD_PRESERVE_MODULES === undefined
29-
? false
30-
: Boolean(process.env.SENTRY_BUILD_PRESERVE_MODULES),
26+
preserveModules: true,
3127
},
3228
plugins: [
3329
replace({

packages/node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export {
3030
init,
3131
getDefaultIntegrations,
3232
getDefaultIntegrationsWithoutPerformance,
33+
initWithoutDefaultIntegrations,
3334
} from './sdk/init';
3435
export { initOpenTelemetry } from './sdk/initOtel';
3536
export { getAutoPerformanceIntegrations } from './integrations/tracing';

packages/node/src/sdk/init.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,24 @@ declare const __IMPORT_META_URL_REPLACEMENT__: string;
8888
* Initialize Sentry for Node.
8989
*/
9090
export function init(options: NodeOptions | undefined = {}): void {
91-
const clientOptions = getClientOptions(options);
91+
return _init(options, getDefaultIntegrations);
92+
}
93+
94+
/**
95+
* Initialize Sentry for Node, without any integrations added by default.
96+
*/
97+
export function initWithoutDefaultIntegrations(options: NodeOptions | undefined = {}): void {
98+
return _init(options, () => []);
99+
}
100+
101+
/**
102+
* Initialize Sentry for Node, without performance instrumentation.
103+
*/
104+
function _init(
105+
options: NodeOptions | undefined = {},
106+
getDefaultIntegrationsImpl: (options: Options) => Integration[],
107+
): void {
108+
const clientOptions = getClientOptions(options, getDefaultIntegrationsImpl);
92109

93110
if (clientOptions.debug === true) {
94111
if (DEBUG_BUILD) {
@@ -191,7 +208,10 @@ function validateOpenTelemetrySetup(): void {
191208
}
192209
}
193210

194-
function getClientOptions(options: NodeOptions): NodeClientOptions {
211+
function getClientOptions(
212+
options: NodeOptions,
213+
getDefaultIntegrationsImpl: (options: Options) => Integration[],
214+
): NodeClientOptions {
195215
const release = getRelease(options.release);
196216

197217
const autoSessionTracking =
@@ -215,17 +235,18 @@ function getClientOptions(options: NodeOptions): NodeClientOptions {
215235
tracesSampleRate,
216236
});
217237

238+
const mergedOptions = {
239+
...baseOptions,
240+
...options,
241+
...overwriteOptions,
242+
};
243+
218244
if (options.defaultIntegrations === undefined) {
219-
options.defaultIntegrations = getDefaultIntegrations({
220-
...options,
221-
...overwriteOptions,
222-
});
245+
options.defaultIntegrations = getDefaultIntegrationsImpl(mergedOptions);
223246
}
224247

225248
const clientOptions: NodeClientOptions = {
226-
...baseOptions,
227-
...options,
228-
...overwriteOptions,
249+
...mergedOptions,
229250
stackParser: stackParserFromStackParserOptions(options.stackParser || defaultStackParser),
230251
integrations: getIntegrationsToSetup({
231252
defaultIntegrations: options.defaultIntegrations,

0 commit comments

Comments
 (0)