Skip to content

Commit a6bc448

Browse files
committed
ref(nuxt): Use addVitePlugin instead of deprecated vite:extendConfig
1 parent b461991 commit a6bc448

4 files changed

Lines changed: 286 additions & 102 deletions

File tree

packages/nuxt/src/module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
addPluginTemplate,
44
addServerPlugin,
55
addTemplate,
6+
addVitePlugin,
67
createResolver,
78
defineNuxtModule,
89
} from '@nuxt/kit';
@@ -88,7 +89,7 @@ export default defineNuxtModule<ModuleOptions>({
8889
}
8990

9091
if (clientConfigFile || serverConfigFile) {
91-
setupSourceMaps(moduleOptions, nuxt);
92+
setupSourceMaps(moduleOptions, nuxt, addVitePlugin);
9293
}
9394

9495
addOTelCommonJSImportAlias(nuxt);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { Nuxt } from '@nuxt/schema';
2+
import { sentryVitePlugin } from '@sentry/vite-plugin';
3+
import type { ConfigEnv, Plugin, UserConfig } from 'vite';
4+
import type { SentryNuxtModuleOptions } from '../common/types';
5+
import { extractNuxtSourceMapSetting, getPluginOptions, validateDifferentSourceMapSettings } from './sourceMaps';
6+
7+
/**
8+
* Creates a Vite plugin that adds the Sentry Vite plugin and validates source map settings.
9+
*/
10+
export function createSentryViteConfigPlugin(options: {
11+
nuxt: Nuxt;
12+
moduleOptions: SentryNuxtModuleOptions;
13+
sourceMapsEnabled: boolean;
14+
shouldDeleteFilesFallback: { client: boolean; server: boolean };
15+
}): Plugin {
16+
const { nuxt, moduleOptions, sourceMapsEnabled, shouldDeleteFilesFallback } = options;
17+
const isDebug = moduleOptions.debug;
18+
19+
return {
20+
name: 'sentry-nuxt-vite-config',
21+
config(viteConfig: UserConfig, env: ConfigEnv) {
22+
// Only run in production builds
23+
if (!sourceMapsEnabled || env.mode === 'development' || nuxt.options?._prepare) {
24+
return;
25+
}
26+
27+
// Detect runtime from Vite config
28+
// In Nuxt, SSR builds have build.ssr: true, client builds don't
29+
const runtime = viteConfig.build?.ssr ? 'server' : 'client';
30+
31+
const nuxtSourceMapSetting = extractNuxtSourceMapSetting(nuxt, runtime);
32+
33+
// Initialize build config if needed
34+
viteConfig.build = viteConfig.build || {};
35+
const viteSourceMap = viteConfig.build.sourcemap;
36+
37+
// Vite source map options are the same as the Nuxt source map config options (unless overwritten)
38+
validateDifferentSourceMapSettings({
39+
nuxtSettingKey: `sourcemap.${runtime}`,
40+
nuxtSettingValue: nuxtSourceMapSetting,
41+
otherSettingKey: 'viteConfig.build.sourcemap',
42+
otherSettingValue: viteSourceMap,
43+
});
44+
45+
if (isDebug) {
46+
// eslint-disable-next-line no-console
47+
console.log(`[Sentry] Adding Sentry Vite plugin to the ${runtime} runtime.`);
48+
}
49+
50+
// Add Sentry plugin by mutating the config
51+
// Vite plugin is added on the client and server side (plugin runs for both builds)
52+
// Nuxt client source map is 'false' by default. Warning about this will be shown already in an earlier step, and it's also documented that `nuxt.sourcemap.client` needs to be enabled.
53+
viteConfig.plugins = viteConfig.plugins || [];
54+
viteConfig.plugins.push(sentryVitePlugin(getPluginOptions(moduleOptions, shouldDeleteFilesFallback)));
55+
},
56+
};
57+
}

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Nuxt } from '@nuxt/schema';
22
import { sentryRollupPlugin, type SentryRollupPluginOptions } from '@sentry/rollup-plugin';
3-
import { sentryVitePlugin, type SentryVitePluginOptions } from '@sentry/vite-plugin';
3+
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
44
import type { NitroConfig } from 'nitropack';
5+
import type { Plugin } from 'vite';
56
import type { SentryNuxtModuleOptions } from '../common/types';
7+
import { createSentryViteConfigPlugin } from './sentryVitePlugin';
68

79
/**
810
* Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps
@@ -15,7 +17,11 @@ export type SourceMapSetting = boolean | 'hidden' | 'inline';
1517
/**
1618
* Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro).
1719
*/
18-
export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void {
20+
export function setupSourceMaps(
21+
moduleOptions: SentryNuxtModuleOptions,
22+
nuxt: Nuxt,
23+
addVitePlugin: (plugin: Plugin | (() => Plugin), options?: { dev?: boolean; build?: boolean }) => void,
24+
): void {
1925
// TODO(v11): remove deprecated options (also from SentryNuxtModuleOptions type)
2026

2127
const isDebug = moduleOptions.debug;
@@ -76,39 +82,16 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu
7682
}
7783
});
7884

79-
nuxt.hook('vite:extendConfig', async (viteConfig, env) => {
80-
if (sourceMapsEnabled && viteConfig.mode !== 'development' && !nuxt.options?._prepare) {
81-
const runtime = env.isServer ? 'server' : env.isClient ? 'client' : undefined;
82-
const nuxtSourceMapSetting = extractNuxtSourceMapSetting(nuxt, runtime);
83-
84-
viteConfig.build = viteConfig.build || {};
85-
const viteSourceMap = viteConfig.build.sourcemap;
86-
87-
// Vite source map options are the same as the Nuxt source map config options (unless overwritten)
88-
validateDifferentSourceMapSettings({
89-
nuxtSettingKey: `sourcemap.${runtime}`,
90-
nuxtSettingValue: nuxtSourceMapSetting,
91-
otherSettingKey: 'viteConfig.build.sourcemap',
92-
otherSettingValue: viteSourceMap,
93-
});
94-
95-
if (isDebug) {
96-
if (!runtime) {
97-
// eslint-disable-next-line no-console
98-
console.log("[Sentry] Cannot detect runtime (client/server) inside hook 'vite:extendConfig'.");
99-
} else {
100-
// eslint-disable-next-line no-console
101-
console.log(`[Sentry] Adding Sentry Vite plugin to the ${runtime} runtime.`);
102-
}
103-
}
104-
105-
// Add Sentry plugin
106-
// Vite plugin is added on the client and server side (hook runs twice)
107-
// Nuxt client source map is 'false' by default. Warning about this will be shown already in an earlier step, and it's also documented that `nuxt.sourcemap.client` needs to be enabled.
108-
viteConfig.plugins = viteConfig.plugins || [];
109-
viteConfig.plugins.push(sentryVitePlugin(getPluginOptions(moduleOptions, shouldDeleteFilesFallback)));
110-
}
111-
});
85+
addVitePlugin(
86+
createSentryViteConfigPlugin({
87+
nuxt,
88+
moduleOptions,
89+
sourceMapsEnabled,
90+
shouldDeleteFilesFallback,
91+
}),
92+
// Only add source map plugin during build
93+
{ dev: false, build: true },
94+
);
11295

11396
nuxt.hook('nitro:config', (nitroConfig: NitroConfig) => {
11497
if (sourceMapsEnabled && !nitroConfig.dev && !nuxt.options?._prepare) {
@@ -379,7 +362,13 @@ export function validateNitroSourceMapSettings(
379362
}
380363
}
381364

382-
function validateDifferentSourceMapSettings({
365+
/**
366+
* Validates that source map settings are consistent between Nuxt and Vite/Nitro configurations.
367+
* Logs a warning if conflicting settings are detected.
368+
*
369+
* @internal Only exported for testing.
370+
*/
371+
export function validateDifferentSourceMapSettings({
383372
nuxtSettingKey,
384373
nuxtSettingValue,
385374
otherSettingKey,

0 commit comments

Comments
 (0)