Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Upload sourcemaps generated by Nitro #13382

Merged
merged 10 commits into from
Sep 4, 2024
42 changes: 28 additions & 14 deletions packages/nuxt/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import type { Nuxt } from '@nuxt/schema';
import { type SentryRollupPluginOptions, sentryRollupPlugin } from '@sentry/rollup-plugin';
import { type SentryVitePluginOptions, sentryVitePlugin } from '@sentry/vite-plugin';
import type { NitroConfig } from 'nitropack';
import type { NullValue } from 'rollup';
import type { SentryNuxtModuleOptions } from '../common/types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isNullValue(value: any): value is NullValue {
return value === null || value === undefined;
}

/**
* Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro).
*/
Expand All @@ -27,21 +33,25 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu

nuxt.hook('nitro:config', (nitroConfig: NitroConfig) => {
if (sourceMapsEnabled && !nitroConfig.dev) {
if (nitroConfig.rollupConfig) {
// Add Sentry plugin
if (!Array.isArray(nitroConfig.rollupConfig.plugins)) {
nitroConfig.rollupConfig.plugins = nitroConfig.rollupConfig.plugins ? [nitroConfig.rollupConfig.plugins] : [];
}
if (!nitroConfig.rollupConfig) {
nitroConfig.rollupConfig = {};
}

if (isNullValue(nitroConfig.rollupConfig.plugins)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Personal preference, I would just do !== null etc instead of the helper because it is easier to parse mentally but I'll leave it up to you.

nitroConfig.rollupConfig.plugins = [];
} else if (!Array.isArray(nitroConfig.rollupConfig.plugins)) {
nitroConfig.rollupConfig.plugins = [nitroConfig.rollupConfig.plugins];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Maybe we can leave a comment saying something along the lines of "nitroConfig.rollupConfig.plugins can be a singular plugin, in the case of which we want to put it into an array so that we can push our own plugin"

}

nitroConfig.rollupConfig.plugins.push(sentryRollupPlugin(getPluginOptions(moduleOptions)));
// Add Sentry plugin
nitroConfig.rollupConfig.plugins.push(sentryRollupPlugin(getPluginOptions(moduleOptions, true)));

// Enable source maps
nitroConfig.rollupConfig.output = nitroConfig?.rollupConfig?.output || {};
nitroConfig.rollupConfig.output.sourcemap = true;
nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; // Adding "sourcesContent" to the source map (Nitro sets this eto `true`)
// Enable source maps
nitroConfig.rollupConfig.output = nitroConfig?.rollupConfig?.output || {};
nitroConfig.rollupConfig.output.sourcemap = true;
nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; // Adding "sourcesContent" to the source map (Nitro sets this eto `true`)

logDebugInfo(moduleOptions, nitroConfig.rollupConfig.output?.sourcemap);
}
logDebugInfo(moduleOptions, nitroConfig.rollupConfig.output?.sourcemap);
}
});
}
Expand All @@ -53,7 +63,10 @@ function normalizePath(path: string): string {
return path.replace(/^(\.\.\/)+/, './');
}

function getPluginOptions(moduleOptions: SentryNuxtModuleOptions): SentryVitePluginOptions | SentryRollupPluginOptions {
function getPluginOptions(
moduleOptions: SentryNuxtModuleOptions,
isNitro = false,
): SentryVitePluginOptions | SentryRollupPluginOptions {
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {};

return {
Expand All @@ -62,7 +75,8 @@ function getPluginOptions(moduleOptions: SentryNuxtModuleOptions): SentryVitePlu
authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN,
telemetry: sourceMapsUploadOptions.telemetry ?? true,
sourcemaps: {
assets: sourceMapsUploadOptions.sourcemaps?.assets ?? ['./.output/public/**/*', './.output/server/**/*'],
assets:
sourceMapsUploadOptions.sourcemaps?.assets ?? isNitro ? ['./.output/server/**/*'] : ['./.output/public/**/*'],
ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined,
filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined,
rewriteSources: (source: string) => normalizePath(source),
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8302,6 +8302,14 @@
"@sentry/cli-win32-i686" "2.33.1"
"@sentry/cli-win32-x64" "2.33.1"

"@sentry/rollup-plugin@2.22.3":
version "2.22.3"
resolved "https://registry.yarnpkg.com/@sentry/rollup-plugin/-/rollup-plugin-2.22.3.tgz#18ab4b7903ee723bee4cf789b38bb3febb05faae"
integrity sha512-I1UsnYzZm5W7/Pyah2yxuMRxmzgf5iDKoptFfMaerpRO5oBhFO3tMnKSLAlYMvuXKRoYkInNv6ckkUcSOF6jig==
dependencies:
"@sentry/bundler-plugin-core" "2.22.3"
unplugin "1.0.1"

"@sentry/vite-plugin@2.22.3", "@sentry/vite-plugin@^2.22.3":
version "2.22.3"
resolved "https://registry.yarnpkg.com/@sentry/vite-plugin/-/vite-plugin-2.22.3.tgz#b52802412b6f3d8e3e56742afc9624d9babae5b6"
Expand Down