-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmakeEnableSourceMapsPlugin.ts
83 lines (74 loc) · 3.08 KB
/
makeEnableSourceMapsPlugin.ts
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
import { consoleSandbox } from '@sentry/core';
import type { Plugin, UserConfig } from 'vite';
import type { SentryReactRouterBuildOptions } from './types';
/**
* A Sentry plugin for React Router to enable "hidden" source maps if they are unset.
*/
export function makeEnableSourceMapsPlugin(options: SentryReactRouterBuildOptions): Plugin {
return {
name: 'sentry-react-router-update-source-map-setting',
apply: 'build',
enforce: 'post',
config(viteConfig) {
return {
...viteConfig,
build: {
...viteConfig.build,
sourcemap: getUpdatedSourceMapSettings(viteConfig, options),
},
};
},
};
}
/** There are 3 ways to set up source map generation
*
* 1. User explicitly disabled source maps
* - keep this setting (emit a warning that errors won't be unminified in Sentry)
* - we won't upload anything
*
* 2. Users enabled source map generation (true, 'hidden', 'inline').
* - keep this setting (don't do anything - like deletion - besides uploading)
*
* 3. Users didn't set source maps generation
* - we enable 'hidden' source maps generation
* - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this)
*
* --> only exported for testing
*/
export function getUpdatedSourceMapSettings(
viteConfig: UserConfig,
sentryPluginOptions?: SentryReactRouterBuildOptions,
): boolean | 'inline' | 'hidden' {
viteConfig.build = viteConfig.build || {};
const viteSourceMap = viteConfig?.build?.sourcemap;
let updatedSourceMapSetting = viteSourceMap;
const settingKey = 'vite.build.sourcemap';
if (viteSourceMap === false) {
updatedSourceMapSetting = viteSourceMap;
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] Source map generation is currently disabled in your Vite configuration (\`${settingKey}: false \`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`,
);
});
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
updatedSourceMapSetting = viteSourceMap;
if (sentryPluginOptions?.debug) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`,
);
});
}
} else {
updatedSourceMapSetting = 'hidden';
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.log(
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
);
});
}
return updatedSourceMapSetting;
}