1- import * as path from 'path' ;
21import { sentryVitePlugin } from '@sentry/bundler-plugins/vite' ;
32import type { Plugin , UserConfig } from 'vite' ;
43import type { SentryRemixVitePluginOptions } from './types' ;
54
6- type FilesToDeleteAfterUpload = string | string [ ] | undefined ;
5+ /** Shared by the two plugins of one build. */
6+ export type SourceMapState = {
7+ /** The SDK turned on hidden source maps because the user did not set `build.sourcemap`. */
8+ sdkEnabledSourceMaps : boolean ;
9+ } ;
710
811/**
912 * Adds the Sentry bundler plugin, which injects debug IDs and uploads source maps to Sentry.
1013 */
11- export function makeAddSentryVitePlugin ( options : SentryRemixVitePluginOptions ) : Plugin [ ] {
14+ export function makeAddSentryVitePlugin (
15+ options : SentryRemixVitePluginOptions ,
16+ state : SourceMapState = { sdkEnabledSourceMaps : false } ,
17+ ) : Plugin [ ] {
1218 const {
1319 applicationKey,
1420 authToken,
@@ -26,59 +32,7 @@ export function makeAddSentryVitePlugin(options: SentryRemixVitePluginOptions):
2632 telemetry,
2733 } = options ;
2834
29- // The default depends on `build.sourcemap`, which is only known once Vite resolves the config.
30- let resolveFilesToDeleteAfterUpload : ( ( value : FilesToDeleteAfterUpload ) => void ) | undefined ;
31- const filesToDeleteAfterUploadPromise = new Promise < FilesToDeleteAfterUpload > ( resolve => {
32- resolveFilesToDeleteAfterUpload = resolve ;
33- } ) ;
34-
35- const configPlugin : Plugin = {
36- name : 'sentry-remix-files-to-delete-after-upload' ,
37- apply : 'build' ,
38- enforce : 'post' ,
39- config ( config ) {
40- const userFilesToDelete = sourcemaps ?. filesToDeleteAfterUpload ;
41-
42- // Only clean up after ourselves: if the user asked for source maps, they are theirs to keep.
43- // Scoped to the build output rather than `./**/*.map`, which the bundler plugin globs without
44- // ignoring `node_modules` and then deletes with `force: true`.
45- //
46- // `disable: 'disable-upload'` injects debug IDs but leaves uploading to the user, and the
47- // bundler plugin deletes in a `finally` block even when it skipped the upload - so defaulting
48- // the deletion there would remove the very maps they still have to upload by hand.
49- if (
50- typeof userFilesToDelete === 'undefined' &&
51- typeof config . build ?. sourcemap === 'undefined' &&
52- ! sourcemaps ?. disable
53- ) {
54- // Derived from the configured `outDir` rather than hardcoding `build`, so a custom Remix
55- // `buildDirectory` still gets cleaned up. Remix runs a client and an SSR build, each with
56- // its own `outDir`, so each pass deletes only what it emitted.
57- //
58- // Separators are normalised because `glob` reads a backslash as an escape, and both path
59- // flavours are checked because `path.isAbsolute` only recognises the host platform's form.
60- // Only a relative path may take the `./` prefix: `.//abs/path/**/*.map` matches nothing.
61- const outDir = ( config . build ?. outDir || 'build' ) . replace ( / \\ / g, '/' ) . replace ( / \/ + $ / , '' ) ;
62- const isAbsoluteOutDir = path . posix . isAbsolute ( outDir ) || path . win32 . isAbsolute ( outDir ) ;
63- const outDirGlob = isAbsoluteOutDir ? outDir : `./${ outDir . replace ( / ^ \. \/ / , '' ) } ` ;
64- const filesToDelete = [ `${ outDirGlob } /**/*.map` ] ;
65-
66- if ( debug ) {
67- // eslint-disable-next-line no-console
68- console . log (
69- `[Sentry] Automatically setting \`sourcemaps.filesToDeleteAfterUpload: ${ JSON . stringify (
70- filesToDelete ,
71- ) } \` to delete generated source maps after they were uploaded to Sentry.`,
72- ) ;
73- }
74- resolveFilesToDeleteAfterUpload ?.( filesToDelete ) ;
75- } else {
76- resolveFilesToDeleteAfterUpload ?.( userFilesToDelete ) ;
77- }
78- } ,
79- } ;
80-
81- const sentryPlugins = sentryVitePlugin ( {
35+ return sentryVitePlugin ( {
8236 applicationKey,
8337 authToken : authToken ?? process . env . SENTRY_AUTH_TOKEN ,
8438 bundleSizeOptimizations,
@@ -96,7 +50,13 @@ export function makeAddSentryVitePlugin(options: SentryRemixVitePluginOptions):
9650 ignore : sourcemaps ?. ignore ,
9751 rewriteSources : sourcemaps ?. rewriteSources ,
9852 resolveSourceMap : sourcemaps ?. resolveSourceMap ,
99- filesToDeleteAfterUpload : filesToDeleteAfterUploadPromise ,
53+ filesToDeleteAfterUpload : sourcemaps ?. filesToDeleteAfterUpload ,
54+ // Called when the bundler plugin decides, after `makeEnableSourceMapsPlugin` ran.
55+ // With `disable: 'disable-upload'` the user uploads the maps by hand, so they stay.
56+ deleteSourcemapsAfterUpload : ( ) =>
57+ sourcemaps ?. deleteSourcemapsAfterUpload ??
58+ ( state . sdkEnabledSourceMaps && ! sourcemaps ?. disable ? true : undefined ) ,
59+ sdkEnabledSourceMaps : ( ) => state . sdkEnabledSourceMaps ,
10060 } ,
10161 telemetry : telemetry ?? true ,
10262 url : sentryUrl ,
@@ -106,25 +66,26 @@ export function makeAddSentryVitePlugin(options: SentryRemixVitePluginOptions):
10666 } ,
10767 } ,
10868 } ) ;
109-
110- return [ configPlugin , ...sentryPlugins ] ;
11169}
11270
11371/**
11472 * Enables "hidden" source maps if the user did not configure them.
11573 */
116- export function makeEnableSourceMapsPlugin ( options : SentryRemixVitePluginOptions ) : Plugin {
74+ export function makeEnableSourceMapsPlugin (
75+ options : SentryRemixVitePluginOptions ,
76+ state : SourceMapState = { sdkEnabledSourceMaps : false } ,
77+ ) : Plugin {
11778 return {
11879 name : 'sentry-remix-update-source-map-setting' ,
11980 apply : 'build' ,
12081 enforce : 'post' ,
12182 // Returning only the changed key: Vite concatenates arrays when merging a `config` return
12283 // value, so echoing the whole config back duplicates `ssr.noExternal`, `resolve.alias` etc.
12384 config ( viteConfig ) {
85+ const sourcemap = getUpdatedSourceMapSettings ( viteConfig , options ) ;
86+ state . sdkEnabledSourceMaps = sourcemap !== viteConfig . build ?. sourcemap ;
12487 return {
125- build : {
126- sourcemap : getUpdatedSourceMapSettings ( viteConfig , options ) ,
127- } ,
88+ build : { sourcemap } ,
12889 } ;
12990 } ,
13091 } ;
@@ -141,7 +102,7 @@ export function makeEnableSourceMapsPlugin(options: SentryRemixVitePluginOptions
141102 *
142103 * 3. Users didn't set source maps generation
143104 * - we enable 'hidden' source maps generation
144- * - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this)
105+ * - pass `deleteSourcemapsAfterUpload: true`, so the maps never reach the build output
145106 *
146107 * --> only exported for testing
147108 */
0 commit comments