Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sveltekit): Add source maps support for Vercel (lambda) (#8256)
Adjust our automatic source maps upload setup in the SvelteKit SDK to support SvelteKit apps deployed to Vercel. This will only work for Lambda functions/Node runtime; not for the Vercel Edge runtime. This required a few changes in our custom vite plugin as well as on the server side of the SDK: * Based on the used adapter (manually set or detected via #8193) and the `svelte.config.js` we determine the output directory where the generated JS emitted to. * The determined output directory is injected into the global object on the server side * When an error occurs on the server side, we strip the absolute filename of each stack frame so that the relative path of the server-side code within the output directory is left. * We also use the determined output directory to build the correct `include` entries for the source map upload plugin. With this change, source maps upload should work for auto and Vercel adapters, as well as for the Node adapter. As for the Node adapter, the stackframe rewrite behaviour was also changed but it is now more in line with all supported adapters.
- Loading branch information
Showing
11 changed files
with
257 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { InternalGlobal } from '@sentry/utils'; | ||
|
||
export type GlobalSentryValues = { | ||
__sentry_sveltekit_output_dir?: string; | ||
}; | ||
|
||
/** | ||
* Extend the `global` type with custom properties that are | ||
* injected by the SvelteKit SDK at build time. | ||
* @see packages/sveltekit/src/vite/sourcemaps.ts | ||
*/ | ||
export type GlobalWithSentryValues = InternalGlobal & GlobalSentryValues; | ||
|
||
export const VIRTUAL_GLOBAL_VALUES_FILE = '\0sentry-inject-global-values-file'; | ||
|
||
/** | ||
* @returns code that injects @param globalSentryValues into the global object. | ||
*/ | ||
export function getGlobalValueInjectionCode(globalSentryValues: GlobalSentryValues): string { | ||
if (Object.keys(globalSentryValues).length === 0) { | ||
return ''; | ||
} | ||
|
||
const sentryGlobal = '_global'; | ||
|
||
const globalCode = `var ${sentryGlobal} = | ||
typeof window !== 'undefined' ? | ||
window : | ||
typeof globalThis !== 'undefined' ? | ||
globalThis : | ||
typeof global !== 'undefined' ? | ||
global : | ||
typeof self !== 'undefined' ? | ||
self : | ||
{};`; | ||
const injectedValuesCode = Object.entries(globalSentryValues) | ||
.map(([key, value]) => `${sentryGlobal}["${key}"] = ${JSON.stringify(value)};`) | ||
.join('\n'); | ||
|
||
return `${globalCode}\n${injectedValuesCode}\n`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { getGlobalValueInjectionCode } from '../../src/vite/injectGlobalValues'; | ||
|
||
describe('getGlobalValueInjectionCode', () => { | ||
it('returns code that injects values into the global object', () => { | ||
const injectionCode = getGlobalValueInjectionCode({ | ||
// @ts-ignore - just want to test this with multiple values | ||
something: 'else', | ||
__sentry_sveltekit_output_dir: '.svelte-kit/output', | ||
}); | ||
expect(injectionCode).toEqual(`var _global = | ||
typeof window !== 'undefined' ? | ||
window : | ||
typeof globalThis !== 'undefined' ? | ||
globalThis : | ||
typeof global !== 'undefined' ? | ||
global : | ||
typeof self !== 'undefined' ? | ||
self : | ||
{}; | ||
_global["something"] = "else"; | ||
_global["__sentry_sveltekit_output_dir"] = ".svelte-kit/output"; | ||
`); | ||
|
||
// Check that the code above is in fact valid and works as expected | ||
// The return value of eval here is the value of the last expression in the code | ||
expect(eval(`${injectionCode}`)).toEqual('.svelte-kit/output'); | ||
|
||
delete globalThis.__sentry_sveltekit_output_dir; | ||
}); | ||
|
||
it('returns empty string if no values are passed', () => { | ||
expect(getGlobalValueInjectionCode({})).toEqual(''); | ||
}); | ||
}); |
Oops, something went wrong.