-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathsourceMaps.ts
More file actions
177 lines (159 loc) · 6.51 KB
/
sourceMaps.ts
File metadata and controls
177 lines (159 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { Options as BundlerPluginOptions } from '@sentry/bundler-plugin-core';
import { createSentryBuildPluginManager } from '@sentry/bundler-plugin-core';
import type { Nitro, NitroConfig } from 'nitro/types';
import type { SentryNitroOptions } from './config';
/**
* Registers a `compiled` hook to upload source maps after the build completes.
*/
export function setupSourceMaps(nitro: Nitro, options?: SentryNitroOptions, sentryEnabledSourcemaps?: boolean): void {
// The `compiled` hook fires on EVERY rebuild during `nitro dev` watch mode.
// nitro.options.dev is reliably set by the time module setup runs.
if (shouldSkipSourcemapUpload(nitro, options)) {
return;
}
nitro.hooks.hook('compiled', async (_nitro: Nitro) => {
await handleSourceMapUpload(_nitro, options, sentryEnabledSourcemaps);
});
}
/**
* Determines if sourcemap uploads should be skipped.
*/
function shouldSkipSourcemapUpload(nitro: Nitro, options?: SentryNitroOptions): boolean {
return !!(
nitro.options.dev ||
nitro.options.preset === 'nitro-prerender' ||
nitro.options.sourcemap === false ||
options?.sourcemaps?.disable === true
);
}
/**
* Handles the actual source map upload after the build completes.
*/
async function handleSourceMapUpload(
nitro: Nitro,
options?: SentryNitroOptions,
sentryEnabledSourcemaps?: boolean,
): Promise<void> {
const outputDir = nitro.options.output.serverDir;
const pluginOptions = getPluginOptions(options, sentryEnabledSourcemaps, outputDir);
const sentryBuildPluginManager = createSentryBuildPluginManager(pluginOptions, {
buildTool: 'nitro',
loggerPrefix: '[@sentry/nitro]',
});
await sentryBuildPluginManager.telemetry.emitBundlerPluginExecutionSignal();
await sentryBuildPluginManager.createRelease();
await sentryBuildPluginManager.injectDebugIds([outputDir]);
if (options?.sourcemaps?.disable !== 'disable-upload') {
await sentryBuildPluginManager.uploadSourcemaps([outputDir], {
// We don't prepare the artifacts because we injected debug IDs manually before
prepareArtifacts: false,
});
await sentryBuildPluginManager.deleteArtifacts();
}
}
/**
* Normalizes the beginning of a path from e.g. ../../../ to ./
*/
function normalizePath(path: string): string {
return path.replace(/^(\.\.\/)+/, './');
}
/**
* Removes a trailing slash from a path so glob patterns can be appended cleanly.
*/
function removeTrailingSlash(path: string): string {
return path.replace(/\/$/, '');
}
/**
* Builds the plugin options for `createSentryBuildPluginManager` from the Sentry Nitro options.
*
* Only exported for testing purposes.
*/
// oxlint-disable-next-line complexity
export function getPluginOptions(
options?: SentryNitroOptions,
sentryEnabledSourcemaps?: boolean,
outputDir?: string,
): BundlerPluginOptions {
const defaultFilesToDelete =
sentryEnabledSourcemaps && outputDir ? [`${removeTrailingSlash(outputDir)}/**/*.map`] : undefined;
if (options?.debug && defaultFilesToDelete && options?.sourcemaps?.filesToDeleteAfterUpload === undefined) {
// eslint-disable-next-line no-console
console.log(
`[@sentry/nitro] Setting \`sourcemaps.filesToDeleteAfterUpload: ["${defaultFilesToDelete[0]}"]\` to delete generated source maps after they were uploaded to Sentry.`,
);
}
return {
org: options?.org ?? process.env.SENTRY_ORG,
project: options?.project ?? process.env.SENTRY_PROJECT,
authToken: options?.authToken ?? process.env.SENTRY_AUTH_TOKEN,
url: options?.sentryUrl ?? process.env.SENTRY_URL,
headers: options?.headers,
telemetry: options?.telemetry ?? true,
debug: options?.debug ?? false,
silent: options?.silent ?? false,
errorHandler: options?.errorHandler,
sourcemaps: {
disable: options?.sourcemaps?.disable,
assets: options?.sourcemaps?.assets,
ignore: options?.sourcemaps?.ignore,
filesToDeleteAfterUpload: options?.sourcemaps?.filesToDeleteAfterUpload ?? defaultFilesToDelete,
rewriteSources: options?.sourcemaps?.rewriteSources ?? ((source: string) => normalizePath(source)),
},
release: options?.release,
bundleSizeOptimizations: options?.bundleSizeOptimizations,
_metaOptions: {
telemetry: {
metaFramework: 'nitro',
},
},
};
}
/* Source map configuration rules:
1. User explicitly disabled source maps (sourcemap: false)
- Keep their setting, emit a warning that errors won't be unminified in Sentry
- We will not upload anything
2. User enabled source map generation (true)
- Keep their setting (don't modify besides uploading)
3. User did not set source maps (undefined)
- We enable source maps for Sentry
- Configure `filesToDeleteAfterUpload` to clean up .map files after upload
*/
export function configureSourcemapSettings(
config: NitroConfig,
moduleOptions?: SentryNitroOptions,
): { sentryEnabledSourcemaps: boolean } {
const sourcemapUploadDisabled = moduleOptions?.sourcemaps?.disable === true;
if (sourcemapUploadDisabled) {
return { sentryEnabledSourcemaps: false };
}
if (config.sourcemap === false) {
// eslint-disable-next-line no-console
console.warn(
'[@sentry/nitro] You have explicitly disabled source maps (`sourcemap: false`). Sentry will not upload source maps, and errors will not be unminified. To let Sentry handle source maps, remove the `sourcemap` option from your Nitro config, or use `sourcemaps: { disable: true }` in your Sentry options to silence this warning.',
);
return { sentryEnabledSourcemaps: false };
}
let sentryEnabledSourcemaps = false;
if (config.sourcemap === true) {
if (moduleOptions?.debug) {
// eslint-disable-next-line no-console
console.log('[@sentry/nitro] Source maps are already enabled. Sentry will upload them for error unminification.');
}
} else {
// User did not explicitly set sourcemap — enable it for Sentry
config.sourcemap = true;
sentryEnabledSourcemaps = true;
if (moduleOptions?.debug) {
// eslint-disable-next-line no-console
console.log(
'[@sentry/nitro] Enabled source map generation for Sentry. Source map files will be deleted after upload.',
);
}
}
// Nitro v3 has a `sourcemapMinify` plugin that destructively deletes `sourcesContent`,
// `x_google_ignoreList`, and clears `mappings` for any chunk containing `node_modules`.
// This makes sourcemaps unusable for Sentry.
config.experimental = config.experimental || {};
config.experimental.sourcemapMinify = false;
return { sentryEnabledSourcemaps };
}