Skip to content

Commit 7b96613

Browse files
committed
make globalSentryValues type safe
1 parent f0445e7 commit 7b96613

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

packages/sveltekit/src/server/utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import type { RequestEvent } from '@sveltejs/kit';
99

1010
import { WRAPPED_MODULE_SUFFIX } from '../vite/autoInstrument';
1111

12+
export type GlobalSentryValues = {
13+
__sentry_sveltekit_output_dir?: string;
14+
};
15+
1216
/**
1317
* Extend the `global` type with custom properties that are
1418
* injected by the SvelteKit SDK at build time.
1519
* @see packages/sveltekit/src/vite/sourcemaps.ts
1620
*/
17-
export type GlobalWithSentryValues = typeof globalThis & {
18-
__sentry_sveltekit_output_dir?: string;
19-
};
21+
export type GlobalWithSentryValues = typeof globalThis & GlobalSentryValues;
2022

2123
/**
2224
* Takes a request event and extracts traceparent and DSC data

packages/sveltekit/src/vite/sourceMaps.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as path from 'path';
1010
import * as sorcery from 'sorcery';
1111
import type { Plugin } from 'vite';
1212

13+
import type { GlobalSentryValues } from '../server/utils';
1314
import { WRAPPED_MODULE_SUFFIX } from './autoInstrument';
1415
import type { SupportedSvelteKitAdapters } from './detectAdapter';
1516
import { getAdapterOutputDir, getHooksFileName, loadSvelteConfig } from './svelteConfig';
@@ -78,6 +79,10 @@ export async function makeCustomSentryVitePlugin(options?: CustomSentryVitePlugi
7879

7980
const serverHooksFile = getHooksFileName(svelteConfig, 'server');
8081

82+
const globalSentryValues: GlobalSentryValues = {
83+
__sentry_sveltekit_output_dir: outputDir,
84+
};
85+
8186
const customPlugin: Plugin = {
8287
name: 'sentry-upload-source-maps',
8388
apply: 'build', // only apply this plugin at build time
@@ -117,8 +122,10 @@ export async function makeCustomSentryVitePlugin(options?: CustomSentryVitePlugi
117122
const isServerHooksFile = new RegExp(`/${escapeStringForRegex(serverHooksFile)}(.(js|ts|mjs|mts))?`).test(id);
118123

119124
if (isServerHooksFile) {
120-
const injectedCode = `global.__sentry_sveltekit_output_dir = "${outputDir || 'undefined'}";\n`;
121-
modifiedCode = `${code}\n${injectedCode}`;
125+
const injectedCode = Object.entries(globalSentryValues)
126+
.map(([key, value]) => `globalThis["${key}"] = ${JSON.stringify(value)};`)
127+
.join('\n');
128+
modifiedCode = `${code}\n${injectedCode}\n`;
122129
}
123130
// @ts-ignore - this hook exists on the plugin!
124131
return sentryPlugin.transform(modifiedCode, id);

packages/sveltekit/test/vite/sourceMaps.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('makeCustomSentryVitePlugin()', () => {
5858
const plugin = await makeCustomSentryVitePlugin();
5959
// @ts-ignore this function exists!
6060
const transformedCode = await plugin.transform('foo', '/src/hooks.server.ts');
61-
const expectedtransformedCode = 'foo\nglobal.__sentry_sveltekit_output_dir = ".svelte-kit/output";\n';
61+
const expectedtransformedCode = 'foo\nglobalThis["__sentry_sveltekit_output_dir"] = ".svelte-kit/output";\n';
6262
expect(mockedSentryVitePlugin.transform).toHaveBeenCalledWith(expectedtransformedCode, '/src/hooks.server.ts');
6363
expect(transformedCode).toEqual(expectedtransformedCode);
6464
});

0 commit comments

Comments
 (0)