Skip to content

Commit 8dc63cf

Browse files
committed
make comment-based replacement blocks with legal comment syntax
1 parent 0fe509a commit 8dc63cf

4 files changed

Lines changed: 18 additions & 13 deletions

File tree

dev-packages/rollup-utils/plugins/bundlePlugins.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,19 @@ export function makeIsDebugBuildPlugin(includeDebugging) {
6363
}
6464

6565
/**
66-
* Replaces the comment marker `/* __SENTRY_SDK_SOURCE__ *\/` in core's `getSDKSource()` with a
66+
* Replaces the comment marker `/*! __SENTRY_SDK_SOURCE__ *\/` in core's `getSDKSource()` with a
6767
* `return '<source>';` statement so the bundle reports the correct distribution channel.
6868
*
69-
* Because the marker is a block comment, esbuild would strip it during transpile — so the plugin
70-
* sort order in utils.mjs pins this name before `esbuild`.
69+
* The marker uses the `/*! ... *\/` legal-comment syntax so it survives esbuild's transpile
70+
* (esbuild strips ordinary block comments). The plugin sort order in utils.mjs also pins
71+
* this name before `esbuild`, in case it ever runs on un-transpiled source directly.
7172
*/
7273
export function makeSetSDKSourcePlugin(sdkSource) {
7374
const plugin = replace({
7475
preventAssignment: false,
7576
delimiters: ['', ''],
7677
values: {
77-
'/* __SENTRY_SDK_SOURCE__ */': `return ${JSON.stringify(sdkSource)};`,
78+
'/*! __SENTRY_SDK_SOURCE__ */': `return ${JSON.stringify(sdkSource)};`,
7879
},
7980
});
8081
plugin.name = 'replace-sdk-source';

dev-packages/rollup-utils/plugins/npmPlugins.mjs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ export function makeDebugBuildStatementReplacePlugin() {
121121
}
122122

123123
export function makeProductionReplacePlugin() {
124-
const pattern = /\/\* rollup-include-development-only \*\/[\s\S]*?\/\* rollup-include-development-only-end \*\/\s*/g;
125-
126-
// Must run as a `transform` (per-module) hook rather than `renderChunk`: esbuild
127-
// strips arbitrary block comments during transpile, so by the time `renderChunk`
128-
// would fire, the `rollup-include-development-only` marker comments are gone.
124+
// Markers use the `/*! ... */` legal-comment syntax so esbuild preserves them through
125+
// transpile. We still run as a `transform` (per-module) hook rather than `renderChunk`:
126+
// the block typically uses imports declared at the module top, and stripping it before
127+
// rollup analyses module-graph imports lets those now-unused imports be tree-shaken away.
129128
// The plugin sort order in utils.mjs pins this before `esbuild`.
129+
const pattern =
130+
/\/\*! rollup-include-development-only \*\/[\s\S]*?\/\*! rollup-include-development-only-end \*\/\s*/g;
131+
130132
return {
131133
name: 'remove-dev-mode-blocks',
132134
transform(code) {

packages/browser/src/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ export function init(options: BrowserOptions = {}): Client | undefined {
9898
let defaultIntegrations =
9999
options.defaultIntegrations == null ? getDefaultIntegrations(options) : options.defaultIntegrations;
100100

101-
/* rollup-include-development-only */
101+
/*! rollup-include-development-only */
102102
if (options.spotlight) {
103103
if (!defaultIntegrations) {
104104
defaultIntegrations = [];
105105
}
106106
const args = typeof options.spotlight === 'string' ? { sidecarUrl: options.spotlight } : undefined;
107107
defaultIntegrations.push(spotlightBrowserIntegration(args));
108108
}
109-
/* rollup-include-development-only-end */
109+
/*! rollup-include-development-only-end */
110110

111111
const clientOptions: BrowserClientOptions = {
112112
...options,

packages/core/src/utils/env.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export function isBrowserBundle(): boolean {
3030
* Get source of SDK.
3131
*/
3232
export function getSDKSource(): SdkSource {
33-
// This comment is used to identify this line in the CDN bundle build step and replace this with "return 'cdn';"
34-
/* __SENTRY_SDK_SOURCE__ */ return 'npm';
33+
// The `/*! ... */` marker is replaced by our CDN bundle build step with `return 'cdn';`.
34+
// It uses the `/*!` legal-comment syntax specifically so it survives esbuild's transpile
35+
// (which strips ordinary `/* ... */` block comments).
36+
/*! __SENTRY_SDK_SOURCE__ */ return 'npm';
3537
}

0 commit comments

Comments
 (0)