-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(sveltekit): Add sentrySvelteKitPlugin
#7788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './server'; | ||
export * from './config'; | ||
export * from './vite'; |
This file contains hidden or 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 hidden or 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,2 @@ | ||
export { withSentryViteConfig } from './withSentryViteConfig'; | ||
export { sentrySvelteKitPlugin } from './sentrySvelteKitPlugin'; |
This file contains hidden or 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 hidden or 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,54 @@ | ||
import type { Plugin, UserConfig } from 'vite'; | ||
|
||
import { injectSentryInitPlugin } from './injectInitPlugin'; | ||
import { hasSentryInitFiles } from './utils'; | ||
|
||
/** | ||
* Vite Plugin for the Sentry SvelteKit SDK, taking care of: | ||
* | ||
* - Creating Sentry releases and uploading source maps to Sentry | ||
* - Injecting Sentry.init calls if you use dedicated `sentry.(client|server).config.ts` files | ||
* | ||
* This plugin adds a few additional properties to your Vite config. | ||
* Make sure, it is registered before the SvelteKit plugin. | ||
*/ | ||
export function sentrySvelteKitPlugin(): Plugin { | ||
return { | ||
name: 'sentry-sveltekit', | ||
enforce: 'pre', // we want this plugin to run early enough | ||
config: originalConfig => { | ||
return addSentryConfig(originalConfig); | ||
}, | ||
}; | ||
} | ||
|
||
function addSentryConfig(originalConfig: UserConfig): UserConfig { | ||
const sentryPlugins = []; | ||
|
||
const shouldAddInjectInitPlugin = hasSentryInitFiles(); | ||
|
||
if (shouldAddInjectInitPlugin) { | ||
sentryPlugins.push(injectSentryInitPlugin); | ||
} | ||
|
||
const config = { | ||
...originalConfig, | ||
plugins: originalConfig.plugins ? [...sentryPlugins, ...originalConfig.plugins] : [...sentryPlugins], | ||
}; | ||
|
||
const mergedDevServerFileSystemConfig: UserConfig['server'] = shouldAddInjectInitPlugin | ||
? { | ||
fs: { | ||
...(config.server && config.server.fs), | ||
allow: [...((config.server && config.server.fs && config.server.fs.allow) || []), '.'], | ||
}, | ||
} | ||
: {}; | ||
|
||
config.server = { | ||
...config.server, | ||
...mergedDevServerFileSystemConfig, | ||
}; | ||
|
||
return config; | ||
} |
This file contains hidden or 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,28 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
/** | ||
* Checks if the user has a Sentry init file in the root of their project. | ||
* @returns true if the user has a Sentry init file, false otherwise. | ||
*/ | ||
export function hasSentryInitFiles(): boolean { | ||
const hasSentryServerInit = !!getUserConfigFile(process.cwd(), 'server'); | ||
const hasSentryClientInit = !!getUserConfigFile(process.cwd(), 'client'); | ||
return hasSentryServerInit || hasSentryClientInit; | ||
} | ||
|
||
/** | ||
* Looks up the sentry.{@param platform}.config.(ts|js) file | ||
* @returns the file path to the file or undefined if it doesn't exist | ||
*/ | ||
export function getUserConfigFile(projectDir: string, platform: 'server' | 'client'): string | undefined { | ||
const possibilities = [`sentry.${platform}.config.ts`, `sentry.${platform}.config.js`]; | ||
|
||
for (const filename of possibilities) { | ||
if (fs.existsSync(path.resolve(projectDir, filename))) { | ||
return filename; | ||
} | ||
} | ||
|
||
return undefined; | ||
} |
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...sveltekit/test/config/vitePlugins.test.ts → ...ltekit/test/vite/injectInitPlugin.test.ts
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
packages/sveltekit/test/vite/sentrySvelteKitPlugin.test.ts
This file contains hidden or 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,108 @@ | ||
import { vi } from 'vitest'; | ||
|
||
import { sentrySvelteKitPlugin } from './../../src/vite/sentrySvelteKitPlugin'; | ||
import * as utils from './../../src/vite/utils'; | ||
|
||
describe('sentrySvelteKitPlugin', () => { | ||
it('returns a Vite plugin with name, enforce, and config hook', () => { | ||
const plugin = sentrySvelteKitPlugin(); | ||
expect(plugin).toHaveProperty('name'); | ||
expect(plugin).toHaveProperty('enforce'); | ||
expect(plugin).toHaveProperty('config'); | ||
expect(plugin.name).toEqual('sentry-sveltekit'); | ||
expect(plugin.enforce).toEqual('pre'); | ||
}); | ||
|
||
describe('config hook', () => { | ||
const hasSentryInitFilesSpy = vi.spyOn(utils, 'hasSentryInitFiles').mockReturnValue(true); | ||
|
||
beforeEach(() => { | ||
hasSentryInitFilesSpy.mockClear(); | ||
}); | ||
|
||
it('adds the injectInitPlugin and adjusts the dev server config if init config files exist', () => { | ||
const plugin = sentrySvelteKitPlugin(); | ||
const originalConfig = {}; | ||
|
||
// @ts-ignore - plugin.config exists and is callable | ||
const modifiedConfig = plugin.config(originalConfig); | ||
|
||
expect(modifiedConfig).toEqual({ | ||
plugins: [ | ||
{ | ||
enforce: 'pre', | ||
name: 'sentry-init-injection-plugin', | ||
transform: expect.any(Function), | ||
}, | ||
], | ||
server: { | ||
fs: { | ||
allow: ['.'], | ||
}, | ||
}, | ||
}); | ||
expect(hasSentryInitFilesSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('merges user-defined options with Sentry-specifc ones', () => { | ||
const plugin = sentrySvelteKitPlugin(); | ||
const originalConfig = { | ||
test: { | ||
include: ['src/**/*.{test,spec}.{js,ts}'], | ||
}, | ||
build: { | ||
sourcemap: 'css', | ||
}, | ||
plugins: [{ name: 'some plugin' }], | ||
server: { | ||
fs: { | ||
allow: ['./build/**/*.{js}'], | ||
}, | ||
}, | ||
}; | ||
|
||
// @ts-ignore - plugin.config exists and is callable | ||
const modifiedConfig = plugin.config(originalConfig); | ||
|
||
expect(modifiedConfig).toEqual({ | ||
test: { | ||
include: ['src/**/*.{test,spec}.{js,ts}'], | ||
}, | ||
build: { | ||
sourcemap: 'css', | ||
}, | ||
plugins: [ | ||
{ | ||
enforce: 'pre', | ||
name: 'sentry-init-injection-plugin', | ||
transform: expect.any(Function), | ||
}, | ||
{ name: 'some plugin' }, | ||
], | ||
server: { | ||
fs: { | ||
allow: ['./build/**/*.{js}', '.'], | ||
}, | ||
}, | ||
}); | ||
expect(hasSentryInitFilesSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("doesn't add the injectInitPlugin if init config files don't exist", () => { | ||
hasSentryInitFilesSpy.mockReturnValue(false); | ||
const plugin = sentrySvelteKitPlugin(); | ||
const originalConfig = { | ||
plugins: [{ name: 'some plugin' }], | ||
}; | ||
|
||
// @ts-ignore - plugin.config exists and is callable | ||
const modifiedConfig = plugin.config(originalConfig); | ||
|
||
expect(modifiedConfig).toEqual({ | ||
plugins: [{ name: 'some plugin' }], | ||
server: {}, | ||
}); | ||
expect(hasSentryInitFilesSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we think about removing this injection behaviour all together then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was sort of debating if we should keep it as an alternative but I think the drawback is that we have to support both ways. Also the init location of hook vs. injected init calls differs slightly in the final bundles. Since IMO hooks are the better choice anyway, I'd say we remove it.
But I suggest we do it in a follow up PR, to make reverting easier if we have to, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's do it in a follow up PR!