Skip to content

Commit 8eba1e9

Browse files
committed
feat(core): split exports by browser/server for bundle size
Split the exports from `@sentry/core` into three options: - `@sentry/core`, the default (unchanged) - `@sentry/core/browser`, containing _only_ shared and browser-specific functionality, nothing server-specific. - `@sentry/core/server`, containing _only_ shared and server-specific functionality, nothing browser-specific. This should allow us to make the bundle sizes quite a bit smaller in our browser SDKs where this is important, while adding more functionality to our server-specific SDKs, in `@sentry/core` where they can be easily shared across runtimes. Integration may require updating our `tsconfig` settings so that tsc knows it is allowed to look on `package.json` exports. fix: #20434 fix: JS-2243
1 parent 94d4229 commit 8eba1e9

154 files changed

Lines changed: 985 additions & 834 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev-packages/e2e-tests/test-applications/react-router-6/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"forceConsistentCasingInFileNames": true,
1111
"noFallthroughCasesInSwitch": true,
1212
"module": "esnext",
13-
"moduleResolution": "node",
13+
"moduleResolution": "bundler",
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,

dev-packages/e2e-tests/test-applications/react-send-to-sentry/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"forceConsistentCasingInFileNames": true,
1111
"noFallthroughCasesInSwitch": true,
1212
"module": "esnext",
13-
"moduleResolution": "node",
13+
"moduleResolution": "bundler",
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,

dev-packages/rollup-utils/npmHelpers.mjs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
makeSucrasePlugin,
2222
} from './plugins/index.mjs';
2323
import { makePackageNodeEsm } from './plugins/make-esm-plugin.mjs';
24-
import { mergePlugins } from './utils.mjs';
24+
import { mergeExternals, mergePlugins } from './utils.mjs';
2525

2626
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2727

@@ -100,17 +100,26 @@ export function makeBaseNPMConfig(options = {}) {
100100
plugins: [nodeResolvePlugin, sucrasePlugin, debugBuildStatementReplacePlugin, rrwebBuildPlugin, cleanupPlugin],
101101

102102
// don't include imported modules from outside the package in the final output
103-
external: [
104-
...builtinModules.filter(m => !bundledBuiltins.includes(m)),
105-
...Object.keys(packageDotJSON.dependencies || {}),
106-
...Object.keys(packageDotJSON.peerDependencies || {}),
107-
...Object.keys(packageDotJSON.optionalDependencies || {}),
108-
],
103+
// also treat subpath exports (e.g. `@sentry/core/browser`) as external
104+
external: id => {
105+
const deps = [
106+
...builtinModules.filter(m => !bundledBuiltins.includes(m)),
107+
...Object.keys(packageDotJSON.dependencies || {}),
108+
...Object.keys(packageDotJSON.peerDependencies || {}),
109+
...Object.keys(packageDotJSON.optionalDependencies || {}),
110+
];
111+
// treat subpath exports as external if the package is external
112+
return deps.some(dep => id === dep || id.startsWith(`${dep}/`));
113+
},
109114
};
110115

111116
return deepMerge(defaultBaseConfig, packageSpecificConfig, {
112117
// Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
113-
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
118+
customMerge: key => {
119+
if (key === 'plugins') return mergePlugins;
120+
if (key === 'external') return mergeExternals;
121+
return undefined;
122+
},
114123
});
115124
}
116125

dev-packages/rollup-utils/utils.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ export const insertAt = (arr, index, ...insertees) => {
1010
return newArr;
1111
};
1212

13+
/**
14+
* Turn a list of module IDs into a test function
15+
* Includes submodule exports by checking that it starts with the name
16+
* plus a / character.
17+
*/
18+
function toFilterFunction(list) {
19+
return Array.isArray(list) ? id => list.some(test => id === test || test.startsWith(`${id}/`)) : list;
20+
}
21+
22+
/**
23+
* Merge two external configs (function or array), returning a function that handles both.
24+
*/
25+
export function mergeExternals(base, specific) {
26+
const baseFn = toFilterFunction(base);
27+
const specificFn = toFilterFunction(specific);
28+
return id => baseFn(id) || specificFn(id);
29+
}
30+
1331
/**
1432
* Merge two arrays of plugins, making sure they're sorted in the correct order.
1533
*/

packages/astro/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"include": ["src/**/*"],
55

66
"compilerOptions": {
7+
"module": "esnext",
8+
"moduleResolution": "bundler"
79
// package-specific options
810
}
911
}

packages/browser/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import type {
88
ParameterizedString,
99
Scope,
1010
SeverityLevel,
11-
} from '@sentry/core';
11+
} from '@sentry/core/browser';
1212
import {
1313
_INTERNAL_flushLogsBuffer,
1414
_INTERNAL_flushMetricsBuffer,
1515
addAutoIpAddressToSession,
1616
applySdkMetadata,
1717
Client,
1818
getSDKSource,
19-
} from '@sentry/core';
19+
} from '@sentry/core/browser';
2020
import { eventFromException, eventFromMessage } from './eventbuilder';
2121
import { WINDOW } from './helpers';
2222
import type { BrowserTransportOptions } from './transports/types';

packages/browser/src/diagnose-sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getClient, suppressTracing } from '@sentry/core';
1+
import { getClient, suppressTracing } from '@sentry/core/browser';
22

33
/**
44
* A function to diagnose why the SDK might not be successfully sending data.

packages/browser/src/eventbuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
SeverityLevel,
77
StackFrame,
88
StackParser,
9-
} from '@sentry/core';
9+
} from '@sentry/core/browser';
1010
import {
1111
_INTERNAL_enhanceErrorWithSentryInfo,
1212
addExceptionMechanism,
@@ -22,7 +22,7 @@ import {
2222
isPlainObject,
2323
normalizeToSize,
2424
resolvedSyncPromise,
25-
} from '@sentry/core';
25+
} from '@sentry/core/browser';
2626

2727
type Prototype = { constructor: (...args: unknown[]) => unknown };
2828

packages/browser/src/exports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type {
2020
ExclusiveEventHintOrCaptureContext,
2121
Log,
2222
LogSeverityLevel,
23-
} from '@sentry/core';
23+
} from '@sentry/core/browser';
2424

2525
export type { BrowserOptions } from './client';
2626

@@ -72,14 +72,14 @@ export {
7272
updateSpanName,
7373
withStreamedSpan,
7474
metrics,
75-
} from '@sentry/core';
75+
} from '@sentry/core/browser';
7676

7777
export {
7878
SEMANTIC_ATTRIBUTE_SENTRY_OP,
7979
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
8080
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
8181
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
82-
} from '@sentry/core';
82+
} from '@sentry/core/browser';
8383

8484
export { WINDOW } from './helpers';
8585
export { BrowserClient } from './client';

packages/browser/src/feedbackAsync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import { lazyLoadIntegration } from './utils/lazyLoadIntegration';
55
* An integration to add user feedback to your application,
66
* while loading most of the code lazily only when it's needed.
77
*/
8-
export const feedbackAsyncIntegration = buildFeedbackIntegration({
8+
export const feedbackAsyncIntegration: ReturnType<typeof buildFeedbackIntegration> = buildFeedbackIntegration({
99
lazyLoadIntegration,
1010
});

0 commit comments

Comments
 (0)