Skip to content

Commit 2297dbc

Browse files
authored
fix: significantly reduce client bundle size (#532)
1 parent 1b9952d commit 2297dbc

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

src/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function buildHook (nuxt: Nuxt, moduleOptions: ModuleConfiguration,
2020

2121
const templateDir = fileURLToPath(new URL('./templates', import.meta.url))
2222

23-
const pluginOptionClient = clientSentryEnabled(moduleOptions) ? (moduleOptions.lazy ? 'lazy' : 'client') : 'mocked'
23+
const pluginOptionClient = clientSentryEnabled(moduleOptions) && canInitialize(moduleOptions) ? (moduleOptions.lazy ? 'lazy' : 'client') : 'mocked'
2424
const clientOptions: ResolvedClientOptions = defu({ config: { release } }, await resolveClientOptions(nuxt, moduleOptions, logger))
2525
addPluginTemplate({
2626
src: resolve(templateDir, `plugin.${pluginOptionClient}.js`),

src/options.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Integrations as ServerIntegrations } from '@sentry/node'
55
import type Sentry from '@sentry/node'
66
import * as PluggableIntegrations from '@sentry/integrations'
77
import type { Options } from '@sentry/types'
8-
import type { NuxtOptions } from '@nuxt/types'
98
import type { AllIntegrations, LazyConfiguration, TracingConfiguration } from './types/configuration'
109
import type { ModuleConfiguration } from './types'
1110
import { Nuxt, resolveAlias } from './kit-shim'
@@ -104,14 +103,12 @@ function resolveLazyOptions (options: ModuleConfiguration, apiMethods: string[],
104103
} else if (Array.isArray(options.lazy.mockApiMethods)) {
105104
const mockMethods = options.lazy.mockApiMethods
106105
options.lazy.mockApiMethods = mockMethods.filter(method => apiMethods.includes(method))
107-
108106
const notfoundMethods = mockMethods.filter(method => !apiMethods.includes(method))
109107
if (notfoundMethods.length) {
110108
logger.warn('Some specified methods to mock weren\'t found in @sentry/vue:', notfoundMethods)
111109
}
112-
113110
if (!options.lazy.mockApiMethods.includes('captureException')) {
114-
// always add captureException if a sentry mock is requested
111+
// always add captureException if a sentry mock is requested
115112
options.lazy.mockApiMethods.push('captureException')
116113
}
117114
}

src/templates/plugin.client.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/* eslint-disable import/order */
22
import Vue from 'vue'
33
import merge from '~lodash.mergewith'
4-
import * as Sentry from '~@sentry/vue'
5-
<% if (options.tracing) { %>import { BrowserTracing } from '~@sentry/tracing'<% } %>
64
<%
7-
if (options.initialize) {
8-
let integrations = options.BROWSER_PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
9-
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations'
5+
const vueImports = ['getCurrentHub', 'init', 'Integrations', ...(options.tracing ? ['vueRouterInstrumentation'] : [])]
6+
%>import { <%= vueImports.join(', ') %> } from '~@sentry/vue'
7+
<%
8+
if (options.tracing) {%>import { BrowserTracing } from '~@sentry/tracing'
109
<%}
11-
if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>'
10+
let integrations = options.BROWSER_PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
11+
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations'
1212
<%}
13-
if (options.customClientIntegrations) {%>import getCustomIntegrations from '<%= options.customClientIntegrations %>'
13+
if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>'
1414
<%}
15-
integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations)
16-
if (integrations.length) {%>
17-
const { <%= integrations.join(', ') %> } = Sentry.Integrations
15+
if (options.customClientIntegrations) {%>import getCustomIntegrations from '<%= options.customClientIntegrations %>'
16+
<%}
17+
integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations)
18+
if (integrations.length) {%>
19+
const { <%= integrations.join(', ') %> } = Integrations
1820
<%}
19-
}
2021
%>
2122

2223
export default async function (ctx, inject) {
23-
<% if (options.initialize) { %>
2424
/* eslint-disable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
2525
const config = {
2626
Vue,
@@ -53,7 +53,7 @@ export default async function (ctx, inject) {
5353
// eslint-disable-next-line prefer-regex-literals
5454
const { browserTracing, vueOptions, ...tracingOptions } = <%= serialize(options.tracing) %>
5555
config.integrations.push(new BrowserTracing({
56-
...(ctx.app.router ? { routingInstrumentation: Sentry.vueRouterInstrumentation(ctx.app.router) } : {}),
56+
...(ctx.app.router ? { routingInstrumentation: vueRouterInstrumentation(ctx.app.router) } : {}),
5757
...browserTracing,
5858
}))
5959
merge(config, vueOptions, tracingOptions)
@@ -79,9 +79,8 @@ export default async function (ctx, inject) {
7979
}
8080

8181
/* eslint-enable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
82-
Sentry.init(config)
83-
<% } %>
84-
85-
inject('sentry', Sentry)
86-
ctx.$sentry = Sentry
82+
init(config)
83+
const sentryClient = getCurrentHub().getClient()
84+
inject('sentry', sentryClient)
85+
ctx.$sentry = sentryClient
8786
}

test/fixture/lazy/nuxt.config.cjs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const config = {
1313
],
1414
sentry: {
1515
lazy: true,
16-
// dsn: 'https://fe8b7df6ea7042f69d7a97c66c2934f7@sentry.io.nuxt/1429779',
17-
config: {},
16+
dsn: 'https://fe8b7df6ea7042f69d7a97c66c2934f7@sentry.io.nuxt/1429779',
1817
clientIntegrations: {
1918
// Integration from @Sentry/browser package.
2019
TryCatch: { eventTarget: false },

0 commit comments

Comments
 (0)