Skip to content

Commit fd49d0b

Browse files
authored
fix(nuxt): Add vue to excludeEsmLoaderHooks array (#13346)
fixes #13304
1 parent 6c0f01a commit fd49d0b

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

packages/nuxt/src/client/sdk.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getDefaultIntegrations as getBrowserDefaultIntegrations, init as initBrowser } from '@sentry/browser';
22
import { applySdkMetadata } from '@sentry/core';
33
import type { Client } from '@sentry/types';
4-
import type { SentryNuxtOptions } from '../common/types';
4+
import type { SentryNuxtClientOptions } from '../common/types';
55

66
/**
77
* Initializes the client-side of the Nuxt SDK
88
*
99
* @param options Configuration options for the SDK.
1010
*/
11-
export function init(options: SentryNuxtOptions): Client | undefined {
11+
export function init(options: SentryNuxtClientOptions): Client | undefined {
1212
const sentryOptions = {
1313
/* BrowserTracing is added later with the Nuxt client plugin */
1414
defaultIntegrations: [...getBrowserDefaultIntegrations(options)],

packages/nuxt/src/common/types.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { init } from '@sentry/vue';
1+
import type { init as initNode } from '@sentry/node';
2+
import type { init as initVue } from '@sentry/vue';
23

34
// Omitting 'app' as the Nuxt SDK will add the app instance in the client plugin (users do not have to provide this)
4-
export type SentryNuxtOptions = Omit<Parameters<typeof init>[0] & object, 'app'>;
5+
export type SentryNuxtClientOptions = Omit<Parameters<typeof initVue>[0] & object, 'app'>;
6+
export type SentryNuxtServerOptions = Omit<Parameters<typeof initNode>[0] & object, 'app'>;
57

68
type SourceMapsOptions = {
79
/**

packages/nuxt/src/server/sdk.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import { init as initNode } from '@sentry/node';
33
import type { Client, EventProcessor } from '@sentry/types';
44
import { logger } from '@sentry/utils';
55
import { DEBUG_BUILD } from '../common/debug-build';
6-
import type { SentryNuxtOptions } from '../common/types';
6+
import type { SentryNuxtServerOptions } from '../common/types';
77

88
/**
99
* Initializes the server-side of the Nuxt SDK
1010
*
1111
* @param options Configuration options for the SDK.
1212
*/
13-
export function init(options: SentryNuxtOptions): Client | undefined {
13+
export function init(options: SentryNuxtServerOptions): Client | undefined {
1414
const sentryOptions = {
1515
...options,
16+
registerEsmLoaderHooks: mergeRegisterEsmLoaderHooks(options),
1617
};
1718

1819
applySdkMetadata(sentryOptions, 'nuxt', ['nuxt', 'node']);
@@ -44,3 +45,22 @@ export function init(options: SentryNuxtOptions): Client | undefined {
4445

4546
return client;
4647
}
48+
49+
/**
50+
* Adds /vue/ to the registerEsmLoaderHooks options and merges it with the old values in the array if one is defined.
51+
* If the registerEsmLoaderHooks option is already a boolean, nothing is changed.
52+
*
53+
* Only exported for Testing purposes.
54+
*/
55+
export function mergeRegisterEsmLoaderHooks(
56+
options: SentryNuxtServerOptions,
57+
): SentryNuxtServerOptions['registerEsmLoaderHooks'] {
58+
if (typeof options.registerEsmLoaderHooks === 'object' && options.registerEsmLoaderHooks !== null) {
59+
return {
60+
exclude: Array.isArray(options.registerEsmLoaderHooks.exclude)
61+
? [...options.registerEsmLoaderHooks.exclude, /vue/]
62+
: options.registerEsmLoaderHooks.exclude ?? [/vue/],
63+
};
64+
}
65+
return options.registerEsmLoaderHooks ?? { exclude: [/vue/] };
66+
}

packages/nuxt/test/server/sdk.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import * as SentryNode from '@sentry/node';
22
import type { NodeClient } from '@sentry/node';
33
import { SDK_VERSION } from '@sentry/node';
44
import { beforeEach, describe, expect, it, vi } from 'vitest';
5+
import type { SentryNuxtServerOptions } from '../../src/common/types';
56
import { init } from '../../src/server';
7+
import { mergeRegisterEsmLoaderHooks } from '../../src/server/sdk';
68

79
const nodeInit = vi.spyOn(SentryNode, 'init');
810

@@ -82,4 +84,42 @@ describe('Nuxt Server SDK', () => {
8284
);
8385
});
8486
});
87+
88+
describe('mergeRegisterEsmLoaderHooks', () => {
89+
it('merges exclude array when registerEsmLoaderHooks is an object with an exclude array', () => {
90+
const options: SentryNuxtServerOptions = {
91+
registerEsmLoaderHooks: { exclude: [/test/] },
92+
};
93+
const result = mergeRegisterEsmLoaderHooks(options);
94+
expect(result).toEqual({ exclude: [/test/, /vue/] });
95+
});
96+
97+
it('sets exclude array when registerEsmLoaderHooks is an object without an exclude array', () => {
98+
const options: SentryNuxtServerOptions = {
99+
registerEsmLoaderHooks: {},
100+
};
101+
const result = mergeRegisterEsmLoaderHooks(options);
102+
expect(result).toEqual({ exclude: [/vue/] });
103+
});
104+
105+
it('returns boolean when registerEsmLoaderHooks is a boolean', () => {
106+
const options1: SentryNuxtServerOptions = {
107+
registerEsmLoaderHooks: true,
108+
};
109+
const result1 = mergeRegisterEsmLoaderHooks(options1);
110+
expect(result1).toBe(true);
111+
112+
const options2: SentryNuxtServerOptions = {
113+
registerEsmLoaderHooks: false,
114+
};
115+
const result2 = mergeRegisterEsmLoaderHooks(options2);
116+
expect(result2).toBe(false);
117+
});
118+
119+
it('sets exclude array when registerEsmLoaderHooks is undefined', () => {
120+
const options: SentryNuxtServerOptions = {};
121+
const result = mergeRegisterEsmLoaderHooks(options);
122+
expect(result).toEqual({ exclude: [/vue/] });
123+
});
124+
});
85125
});

0 commit comments

Comments
 (0)