Skip to content

Commit d3236bd

Browse files
authored
feat: Track major version for Vite and Rollup (#867)
* feat: Track major version for Vite and Rollup * Fix types
1 parent 8357f82 commit d3236bd

File tree

11 files changed

+71
-37
lines changed

11 files changed

+71
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@
3737
},
3838
"volta": {
3939
"node": "18.20.8",
40-
"yarn": "1.22.19"
40+
"yarn": "1.22.22"
4141
}
4242
}

packages/bundler-plugin-core/src/build-plugin-manager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export function createSentryBuildPluginManager(
124124
* E.g. `webpack` or `nextjs` or `turbopack`
125125
*/
126126
buildTool: string;
127+
/**
128+
* E.g. `5` for webpack v5 or `4` for Rollup v4
129+
*/
130+
buildToolMajorVersion?: string;
127131
/**
128132
* E.g. `[sentry-webpack-plugin]` or `[@sentry/nextjs]`
129133
*/
@@ -195,7 +199,8 @@ export function createSentryBuildPluginManager(
195199
const { sentryScope, sentryClient } = createSentryInstance(
196200
options,
197201
shouldSendTelemetry,
198-
bundlerPluginMetaContext.buildTool
202+
bundlerPluginMetaContext.buildTool,
203+
bundlerPluginMetaContext.buildToolMajorVersion
199204
);
200205

201206
const { release, environment = DEFAULT_ENVIRONMENT } = sentryClient.getOptions();

packages/bundler-plugin-core/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ interface SentryUnpluginFactoryOptions {
4747
webpack_forceExitOnBuildComplete?: boolean
4848
) => UnpluginOptions;
4949
bundleSizeOptimizationsPlugin: (buildFlags: SentrySDKBuildFlags) => UnpluginOptions;
50+
getBundlerMajorVersion?: () => string | undefined;
5051
}
5152

5253
/**
@@ -57,13 +58,15 @@ export function sentryUnpluginFactory({
5758
componentNameAnnotatePlugin,
5859
debugIdUploadPlugin,
5960
bundleSizeOptimizationsPlugin,
61+
getBundlerMajorVersion,
6062
}: SentryUnpluginFactoryOptions): UnpluginInstance<Options | undefined, true> {
6163
return createUnplugin<Options | undefined, true>((userOptions = {}, unpluginMetaContext) => {
6264
const sentryBuildPluginManager = createSentryBuildPluginManager(userOptions, {
6365
loggerPrefix:
6466
userOptions._metaOptions?.loggerPrefixOverride ??
6567
`[sentry-${unpluginMetaContext.framework}-plugin]`,
6668
buildTool: unpluginMetaContext.framework,
69+
buildToolMajorVersion: getBundlerMajorVersion?.(),
6770
});
6871

6972
const {

packages/bundler-plugin-core/src/options-mapping.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export type NormalizedOptions = {
7676
_metaOptions: {
7777
telemetry: {
7878
metaFramework: string | undefined;
79-
bundlerMajorVersion: string | undefined;
8079
};
8180
};
8281
applicationKey: string | undefined;

packages/bundler-plugin-core/src/sentry/telemetry.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const stackParser = createStackParser(nodeStackLineParser());
1414
export function createSentryInstance(
1515
options: NormalizedOptions,
1616
shouldSendTelemetry: Promise<boolean>,
17-
buildTool: string
17+
buildTool: string,
18+
buildToolMajorVersion: string | undefined
1819
): { sentryScope: Scope; sentryClient: Client } {
1920
const clientOptions: ServerRuntimeClientOptions = {
2021
platform: "node",
@@ -56,15 +57,16 @@ export function createSentryInstance(
5657
const scope = new Scope();
5758
scope.setClient(client);
5859

59-
setTelemetryDataOnScope(options, scope, buildTool);
60+
setTelemetryDataOnScope(options, scope, buildTool, buildToolMajorVersion);
6061

6162
return { sentryScope: scope, sentryClient: client };
6263
}
6364

6465
export function setTelemetryDataOnScope(
6566
options: NormalizedOptions,
6667
scope: Scope,
67-
buildTool: string
68+
buildTool: string,
69+
buildToolMajorVersion?: string
6870
): void {
6971
const { org, project, release, errorHandler, sourcemaps, reactComponentAnnotation } = options;
7072

@@ -111,7 +113,9 @@ export function setTelemetryDataOnScope(
111113
bundler: buildTool,
112114
});
113115

114-
scope.setTag("bundler-major-version", options._metaOptions.telemetry.bundlerMajorVersion);
116+
if (buildToolMajorVersion) {
117+
scope.setTag("bundler-major-version", buildToolMajorVersion);
118+
}
115119

116120
scope.setUser({ id: org });
117121
}

packages/integration-tests/fixtures/telemetry/telemetry.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ test("rollup bundle telemetry", async () => {
112112
"react-annotate": false,
113113
"meta-framework": "none",
114114
"application-key-set": false,
115+
"bundler-major-version": "2",
115116
bundler: "rollup",
116117
}),
117118
sdk: expect.objectContaining({

packages/integration-tests/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ module.exports = {
33
transform: {
44
"^.+\\.(t|j)sx?$": ["@swc/jest"],
55
},
6+
transformIgnorePatterns: ["!node_modules/"],
67
};

packages/rollup-plugin/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
createComponentNameAnnotateHooks,
1010
Logger,
1111
} from "@sentry/bundler-plugin-core";
12+
import { createRequire } from "node:module";
1213
import type { UnpluginOptions } from "unplugin";
1314

1415
function rollupComponentNameAnnotatePlugin(
@@ -48,11 +49,26 @@ function rollupBundleSizeOptimizationsPlugin(
4849
};
4950
}
5051

52+
function getRollupMajorVersion(): string | undefined {
53+
try {
54+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
55+
// @ts-ignore - Rollup already transpiles this for us
56+
const req = createRequire(import.meta.url);
57+
const rollup = req("rollup") as { VERSION?: string };
58+
return rollup.VERSION?.split(".")[0];
59+
} catch (err) {
60+
// do nothing, we'll just not report a version
61+
}
62+
63+
return undefined;
64+
}
65+
5166
const sentryUnplugin = sentryUnpluginFactory({
5267
injectionPlugin: rollupInjectionPlugin,
5368
componentNameAnnotatePlugin: rollupComponentNameAnnotatePlugin,
5469
debugIdUploadPlugin: rollupDebugIdUploadPlugin,
5570
bundleSizeOptimizationsPlugin: rollupBundleSizeOptimizationsPlugin,
71+
getBundlerMajorVersion: getRollupMajorVersion,
5672
});
5773

5874
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/vite-plugin/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
createComponentNameAnnotateHooks,
1010
Logger,
1111
} from "@sentry/bundler-plugin-core";
12+
import { createRequire } from "node:module";
1213
import { UnpluginOptions, VitePlugin } from "unplugin";
1314

1415
function viteInjectionPlugin(injectionCode: CodeInjection, debugIds: boolean): UnpluginOptions {
@@ -52,11 +53,26 @@ function viteBundleSizeOptimizationsPlugin(
5253
};
5354
}
5455

56+
function getViteMajorVersion(): string | undefined {
57+
try {
58+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
59+
// @ts-ignore - Rollup already transpiles this for us
60+
const req = createRequire(import.meta.url);
61+
const vite = req("vite") as { version?: string };
62+
return vite.version?.split(".")[0];
63+
} catch (err) {
64+
// do nothing, we'll just not report a version
65+
}
66+
67+
return undefined;
68+
}
69+
5570
const sentryUnplugin = sentryUnpluginFactory({
5671
injectionPlugin: viteInjectionPlugin,
5772
componentNameAnnotatePlugin: viteComponentNameAnnotatePlugin,
5873
debugIdUploadPlugin: viteDebugIdUploadPlugin,
5974
bundleSizeOptimizationsPlugin: viteBundleSizeOptimizationsPlugin,
75+
getBundlerMajorVersion: getViteMajorVersion,
6076
});
6177

6278
export const sentryVitePlugin = (options?: Options): VitePlugin[] => {

packages/webpack-plugin/src/index.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,14 @@ const BannerPlugin = webpack4or5?.BannerPlugin || webpack4or5?.default?.BannerPl
88

99
const DefinePlugin = webpack4or5?.DefinePlugin || webpack4or5?.default?.DefinePlugin;
1010

11-
// Detect webpack major version for telemetry (helps differentiate webpack 4 vs 5 usage)
12-
function getWebpackMajorVersion(): string | undefined {
13-
try {
14-
const webpack = webpack4or5 as unknown as
15-
| { version?: string; default?: { version?: string } }
16-
| undefined;
17-
const version = webpack?.version ?? webpack?.default?.version;
18-
const webpackMajorVersion = version?.split(".")[0]; // "4" or "5"
19-
return webpackMajorVersion;
20-
} catch (error) {
21-
return undefined;
22-
}
23-
}
24-
25-
const webpackMajorVersion = getWebpackMajorVersion();
26-
2711
const sentryUnplugin = sentryWebpackUnpluginFactory({
2812
BannerPlugin,
2913
DefinePlugin,
3014
});
3115

3216
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33-
export const sentryWebpackPlugin: (options?: SentryWebpackPluginOptions) => any = (options) => {
34-
const enhancedOptions: SentryWebpackPluginOptions = {
35-
...options,
36-
_metaOptions: {
37-
...options?._metaOptions,
38-
telemetry: {
39-
...options?._metaOptions?.telemetry,
40-
bundlerMajorVersion:
41-
options?._metaOptions?.telemetry?.bundlerMajorVersion ?? webpackMajorVersion,
42-
},
43-
},
44-
};
45-
return sentryUnplugin.webpack(enhancedOptions);
46-
};
17+
export const sentryWebpackPlugin: (options?: SentryWebpackPluginOptions) => any =
18+
sentryUnplugin.webpack;
4719

4820
export { sentryCliBinaryExists } from "@sentry/bundler-plugin-core";
4921

0 commit comments

Comments
 (0)