Skip to content

Commit 63e698a

Browse files
authored
feat: support plugin path for clientConfig and serverConfig (#477)
1 parent acb2aaf commit 63e698a

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

docs/content/en/sentry/options.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,40 @@ export default function () {
354354

355355
- Type: `Object`
356356
- Default: `{}`
357-
- Server-specific Sentry SDK options.
357+
- Sentry SDK [Basic Server Options](https://docs.sentry.io/platforms/node/configuration/options/).
358358
- The specified keys will override common options set in the `config` key.
359+
- The value can be a string in which case it needs to be a file path (can use [webpack aliases](https://nuxtjs.org/docs/2.x/directory-structure/assets#aliases)) pointing to a javascript file whose default export (a function) returns the configuration object. This is necessary in case some of the options rely on imported values or can't be serialized. The function can be `async`. Artificial example that switches out the `transport`:
360+
```js [~/config/sentry-server-config.js]
361+
import { makeNodeTransport } from '@sentry/node'
362+
363+
export default function() {
364+
return {
365+
transport: makeNodeTransport,
366+
}
367+
}
368+
```
359369

360370
### clientConfig
361371

362-
- Type: `Object`
372+
- Type: `Object` or `String`
363373
- Default: `{}`
364-
- Browser-specific Sentry SDK options.
374+
- Sentry SDK [Basic Browser Options](https://docs.sentry.io/platforms/javascript/guides/vue/configuration/options/).
365375
- The specified keys will override common options set in the `config` key.
376+
- The value can be a string in which case it needs to be a file path (can use [webpack aliases](https://nuxtjs.org/docs/2.x/directory-structure/assets#aliases)) pointing to a javascript file whose default export (a function) returns the configuration object. This is necessary in case some of the options rely on imported values or can't be serialized. The function is passed a `Nuxt Context` argument and can be `async`. Example of how to enable [User Feedback](https://docs.sentry.io/platforms/javascript/enriching-events/user-feedback/) dialog:
377+
```js [~/config/sentry-client-config.js]
378+
import { showReportDialog } from '@sentry/vue'
379+
380+
export default function(context) {
381+
return {
382+
beforeSend (event, hint) {
383+
if (event.exception) {
384+
showReportDialog({ eventId: event.event_id })
385+
}
386+
return event
387+
},
388+
}
389+
}
390+
```
366391

367392
### requestHandlerConfig
368393

lib/core/options.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ function resolveTracingOptions (options, config) {
119119
export async function resolveClientOptions (moduleContainer, moduleOptions, logger) {
120120
/** @type {import('../../types/sentry').ResolvedModuleConfiguration} */
121121
const options = merge({}, moduleOptions)
122+
options.config = merge({}, options.config)
122123

123-
options.config = merge({}, options.config, options.clientConfig)
124+
let clientConfigPath
125+
if (typeof (options.clientConfig) === 'string') {
126+
clientConfigPath = moduleContainer.nuxt.resolver.resolveAlias(options.clientConfig)
127+
} else {
128+
options.config = merge(options.config, options.clientConfig)
129+
}
124130

125131
const apiMethods = await getApiMethods('@sentry/vue')
126132
resolveLazyOptions(options, apiMethods, logger)
@@ -151,6 +157,7 @@ export async function resolveClientOptions (moduleContainer, moduleOptions, logg
151157
dsn: options.dsn,
152158
...options.config,
153159
},
160+
clientConfigPath,
154161
lazy: options.lazy,
155162
apiMethods,
156163
customClientIntegrations,
@@ -186,9 +193,13 @@ export async function resolveServerOptions (moduleContainer, moduleOptions, logg
186193
let customIntegrations = []
187194
if (options.customServerIntegrations) {
188195
const resolvedPath = moduleContainer.nuxt.resolver.resolveAlias(options.customServerIntegrations)
189-
customIntegrations = (await import(resolvedPath).then(m => m.default || m))()
190-
if (!Array.isArray(customIntegrations)) {
191-
logger.error(`Invalid value returned from customServerIntegrations plugin. Expected an array, got "${typeof (customIntegrations)}".`)
196+
try {
197+
customIntegrations = (await import(resolvedPath).then(m => m.default || m))()
198+
if (!Array.isArray(customIntegrations)) {
199+
logger.error(`Invalid value returned from customServerIntegrations plugin. Expected an array, got "${typeof (customIntegrations)}".`)
200+
}
201+
} catch (error) {
202+
logger.error(`Error handling the customServerIntegrations plugin:\n${error}`)
192203
}
193204
}
194205

@@ -205,7 +216,17 @@ export async function resolveServerOptions (moduleContainer, moduleOptions, logg
205216
],
206217
}
207218

208-
options.config = merge(defaultConfig, options.config, options.serverConfig, getRuntimeConfig(moduleContainer, options))
219+
let serverConfig = options.serverConfig
220+
if (typeof (serverConfig) === 'string') {
221+
const resolvedPath = moduleContainer.nuxt.resolver.resolveAlias(options.serverConfig)
222+
try {
223+
serverConfig = (await import(resolvedPath).then(m => m.default || m))()
224+
} catch (error) {
225+
logger.error(`Error handling the serverConfig plugin:\n${error}`)
226+
}
227+
}
228+
229+
options.config = merge(defaultConfig, options.config, serverConfig, getRuntimeConfig(moduleContainer, options))
209230

210231
const apiMethods = await getApiMethods('@sentry/node')
211232
resolveLazyOptions(options, apiMethods, logger)

lib/plugin.client.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as Sentry from '@sentry/vue'
66
if (options.initialize) {
77
let integrations = options.PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
88
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '@sentry/integrations'
9+
<%}
10+
if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>'
911
<%}
1012
if (options.customClientIntegrations) {%>import getCustomIntegrations from '<%= options.customClientIntegrations %>'
1113
<%}
@@ -60,6 +62,12 @@ export default async function (ctx, inject) {
6062
}))
6163
merge(config, vueOptions, tracingOptions)
6264
<% } %>
65+
66+
<% if (options.clientConfigPath) { %>
67+
const clientConfig = await getClientConfig(ctx)
68+
clientConfig ? merge(config, clientConfig) : console.error(`[@nuxtjs/sentry] Invalid value returned from the clientConfig plugin.`)
69+
<% } %>
70+
6371
<% if (options.customClientIntegrations) { %>
6472
const customIntegrations = await getCustomIntegrations(ctx)
6573
if (Array.isArray(customIntegrations)) {

lib/plugin.lazy.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ async function loadSentry (ctx, inject) {
174174
}))
175175
<% } %>
176176

177+
<% if (options.clientConfigPath) { %>
178+
const clientConfig = (await import(/* <%= magicComments.join(', ') %> */ '<%= options.clientConfigPath %>').then(m => m.default || m))(ctx)
179+
const { default: merge } = await import(/* <%= magicComments.join(', ') %> */ 'lodash.mergewith')
180+
clientConfig ? merge(config, clientConfig) : console.error(`[@nuxtjs/sentry] Invalid value returned from the clientConfig plugin.`)
181+
<% } %>
182+
177183
<%if (options.customClientIntegrations) {%>
178184
const customIntegrations = (await import(/* <%= magicComments.join(', ') %> */ '<%= options.customClientIntegrations %>').then(m => m.default || m))(ctx)
179185
if (Array.isArray(customIntegrations)) {

types/sentry.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface TracingConfiguration extends Pick<SentryOptions, 'tracesSampleR
2929
}
3030

3131
export interface ModuleConfiguration {
32-
clientConfig?: Partial<SentryVueOptions>
32+
clientConfig?: Partial<SentryVueOptions> | string
3333
clientIntegrations?: IntegrationsConfiguration
3434
config?: SentryOptions
3535
customClientIntegrations?: string
@@ -47,7 +47,7 @@ export interface ModuleConfiguration {
4747
/** See available options at https://github.com/getsentry/sentry-webpack-plugin */
4848
publishRelease?: boolean | Partial<SentryCliPluginOptions>
4949
runtimeConfigKey?: string
50-
serverConfig?: SentryOptions
50+
serverConfig?: SentryOptions | string
5151
serverIntegrations?: IntegrationsConfiguration
5252
sourceMapStyle?: WebpackOptions.Devtool
5353
requestHandlerConfig?: Handlers.RequestHandlerOptions

0 commit comments

Comments
 (0)