Skip to content

Commit

Permalink
fix: significantly reduce client bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Mar 24, 2023
1 parent 1b9952d commit 3516984
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function buildHook (nuxt: Nuxt, moduleOptions: ModuleConfiguration,

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

const pluginOptionClient = clientSentryEnabled(moduleOptions) ? (moduleOptions.lazy ? 'lazy' : 'client') : 'mocked'
const pluginOptionClient = clientSentryEnabled(moduleOptions) && canInitialize(moduleOptions) ? (moduleOptions.lazy ? 'lazy' : 'client') : 'mocked'
const clientOptions: ResolvedClientOptions = defu({ config: { release } }, await resolveClientOptions(nuxt, moduleOptions, logger))
addPluginTemplate({
src: resolve(templateDir, `plugin.${pluginOptionClient}.js`),
Expand Down
5 changes: 1 addition & 4 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Integrations as ServerIntegrations } from '@sentry/node'
import type Sentry from '@sentry/node'
import * as PluggableIntegrations from '@sentry/integrations'
import type { Options } from '@sentry/types'
import type { NuxtOptions } from '@nuxt/types'
import type { AllIntegrations, LazyConfiguration, TracingConfiguration } from './types/configuration'
import type { ModuleConfiguration } from './types'
import { Nuxt, resolveAlias } from './kit-shim'
Expand Down Expand Up @@ -104,14 +103,12 @@ function resolveLazyOptions (options: ModuleConfiguration, apiMethods: string[],
} else if (Array.isArray(options.lazy.mockApiMethods)) {
const mockMethods = options.lazy.mockApiMethods
options.lazy.mockApiMethods = mockMethods.filter(method => apiMethods.includes(method))

const notfoundMethods = mockMethods.filter(method => !apiMethods.includes(method))
if (notfoundMethods.length) {
logger.warn('Some specified methods to mock weren\'t found in @sentry/vue:', notfoundMethods)
}

if (!options.lazy.mockApiMethods.includes('captureException')) {
// always add captureException if a sentry mock is requested
// always add captureException if a sentry mock is requested
options.lazy.mockApiMethods.push('captureException')
}
}
Expand Down
35 changes: 17 additions & 18 deletions src/templates/plugin.client.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/* eslint-disable import/order */
import Vue from 'vue'
import merge from '~lodash.mergewith'
import * as Sentry from '~@sentry/vue'
<% if (options.tracing) { %>import { BrowserTracing } from '~@sentry/tracing'<% } %>
<%
if (options.initialize) {
let integrations = options.BROWSER_PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations'
const vueImports = ['getCurrentHub', 'init', 'Integrations', ...(options.tracing ? ['vueRouterInstrumentation'] : [])]
%>import { <%= vueImports.join(', ') %> } from '~@sentry/vue'
<%
if (options.tracing) {%>import { BrowserTracing } from '~@sentry/tracing'
<%}
if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>'
let integrations = options.BROWSER_PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations'
<%}
if (options.customClientIntegrations) {%>import getCustomIntegrations from '<%= options.customClientIntegrations %>'
if (options.clientConfigPath) {%>import getClientConfig from '<%= options.clientConfigPath %>'
<%}
integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations)
if (integrations.length) {%>
const { <%= integrations.join(', ') %> } = Sentry.Integrations
if (options.customClientIntegrations) {%>import getCustomIntegrations from '<%= options.customClientIntegrations %>'
<%}
integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations)
if (integrations.length) {%>
const { <%= integrations.join(', ') %> } = Integrations
<%}
}
%>

export default async function (ctx, inject) {
<% if (options.initialize) { %>
/* eslint-disable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
const config = {
Vue,
Expand Down Expand Up @@ -53,7 +53,7 @@ export default async function (ctx, inject) {
// eslint-disable-next-line prefer-regex-literals
const { browserTracing, vueOptions, ...tracingOptions } = <%= serialize(options.tracing) %>
config.integrations.push(new BrowserTracing({
...(ctx.app.router ? { routingInstrumentation: Sentry.vueRouterInstrumentation(ctx.app.router) } : {}),
...(ctx.app.router ? { routingInstrumentation: vueRouterInstrumentation(ctx.app.router) } : {}),
...browserTracing,
}))
merge(config, vueOptions, tracingOptions)
Expand All @@ -79,9 +79,8 @@ export default async function (ctx, inject) {
}

/* eslint-enable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
Sentry.init(config)
<% } %>

inject('sentry', Sentry)
ctx.$sentry = Sentry
init(config)
const sentryClient = getCurrentHub().getClient()
inject('sentry', sentryClient)
ctx.$sentry = sentryClient
}
3 changes: 1 addition & 2 deletions test/fixture/lazy/nuxt.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const config = {
],
sentry: {
lazy: true,
// dsn: 'https://[email protected]/1429779',
config: {},
dsn: 'https://[email protected]/1429779',
clientIntegrations: {
// Integration from @Sentry/browser package.
TryCatch: { eventTarget: false },
Expand Down

0 comments on commit 3516984

Please sign in to comment.