diff --git a/packages/utils/src/env.ts b/packages/utils/src/env.ts index 101be14e6e2c..8d622f8836cd 100644 --- a/packages/utils/src/env.ts +++ b/packages/utils/src/env.ts @@ -7,7 +7,7 @@ * * Attention: * This file should not be used to define constants/flags that are intended to be used for tree-shaking conducted by - * users. These fags should live in their respective packages, as we identified user tooling (specifically webpack) + * users. These flags should live in their respective packages, as we identified user tooling (specifically webpack) * having issues tree-shaking these constants across package boundaries. * An example for this is the __SENTRY_DEBUG__ constant. It is declared in each package individually because we want * users to be able to shake away expressions that it guards. @@ -15,6 +15,8 @@ declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined; +type SdkSource = 'npm' | 'cdn' | 'loader'; + /** * Figures out if we're building a browser bundle. * @@ -23,3 +25,11 @@ declare const __SENTRY_BROWSER_BUNDLE__: boolean | undefined; export function isBrowserBundle(): boolean { return typeof __SENTRY_BROWSER_BUNDLE__ !== 'undefined' && !!__SENTRY_BROWSER_BUNDLE__; } + +/** + * Get source of SDK. + */ +export function getSDKSource(): SdkSource { + // @ts-ignore __SENTRY_SDK_SOURCE__ is injected by rollup during build process + return __SENTRY_SDK_SOURCE__; +} diff --git a/rollup/bundleHelpers.js b/rollup/bundleHelpers.js index 5e1772b99ea1..43a5f26563d9 100644 --- a/rollup/bundleHelpers.js +++ b/rollup/bundleHelpers.js @@ -17,6 +17,7 @@ import { makeTerserPlugin, makeTSPlugin, makeExcludeReplayPlugin, + makeSetSDKSourcePlugin, } from './plugins/index.js'; import { mergePlugins } from './utils'; @@ -141,6 +142,7 @@ export function makeBundleConfigVariants(baseConfig, options = {}) { const includeDebuggingPlugin = makeIsDebugBuildPlugin(true); const stripDebuggingPlugin = makeIsDebugBuildPlugin(false); const terserPlugin = makeTerserPlugin(); + const setSdkSourcePlugin = makeSetSDKSourcePlugin('cdn'); // The additional options to use for each variant we're going to create. const variantSpecificConfigMap = { @@ -148,21 +150,21 @@ export function makeBundleConfigVariants(baseConfig, options = {}) { output: { entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.js`, }, - plugins: [includeDebuggingPlugin], + plugins: [includeDebuggingPlugin, setSdkSourcePlugin], }, '.min.js': { output: { entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`, }, - plugins: [stripDebuggingPlugin, terserPlugin], + plugins: [stripDebuggingPlugin, setSdkSourcePlugin, terserPlugin], }, '.debug.min.js': { output: { entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.debug.min.js`, }, - plugins: [includeDebuggingPlugin, terserPlugin], + plugins: [includeDebuggingPlugin, setSdkSourcePlugin, terserPlugin], }, }; diff --git a/rollup/npmHelpers.js b/rollup/npmHelpers.js index 0b79fb6edc03..be6a900b2115 100644 --- a/rollup/npmHelpers.js +++ b/rollup/npmHelpers.js @@ -13,6 +13,7 @@ import { makeCleanupPlugin, makeSucrasePlugin, makeDebugBuildStatementReplacePlugin, + makeSetSDKSourcePlugin, } from './plugins/index.js'; import { mergePlugins } from './utils'; @@ -31,6 +32,7 @@ export function makeBaseNPMConfig(options = {}) { const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin(); const cleanupPlugin = makeCleanupPlugin(); const extractPolyfillsPlugin = makeExtractPolyfillsPlugin(); + const setSdkSourcePlugin = makeSetSDKSourcePlugin('npm'); const defaultBaseConfig = { input: entrypoints, @@ -83,6 +85,7 @@ export function makeBaseNPMConfig(options = {}) { plugins: [ nodeResolvePlugin, + setSdkSourcePlugin, sucrasePlugin, debugBuildStatementReplacePlugin, cleanupPlugin, diff --git a/rollup/plugins/bundlePlugins.js b/rollup/plugins/bundlePlugins.js index d6521e53a614..5e483dfddc46 100644 --- a/rollup/plugins/bundlePlugins.js +++ b/rollup/plugins/bundlePlugins.js @@ -63,6 +63,15 @@ export function makeIsDebugBuildPlugin(includeDebugging) { }); } +export function makeSetSDKSourcePlugin(sdkSource) { + return replace({ + preventAssignment: false, + values: { + __SENTRY_SDK_SOURCE__: JSON.stringify(sdkSource), + }, + }); +} + /** * Create a plugin to set the value of the `__SENTRY_BROWSER_BUNDLE__` magic string. *