Skip to content

Commit 7000bdf

Browse files
committed
feat(sveltekit): Streamline build logs
1 parent ce66380 commit 7000bdf

File tree

3 files changed

+169
-125
lines changed

3 files changed

+169
-125
lines changed

packages/sveltekit/src/vite/sourceMaps.ts

Lines changed: 85 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
6060
},
6161
};
6262

63-
const { promise: filesToDeleteAfterUpload, resolve: resolveFilesToDeleteAfterUpload } =
64-
createFilesToDeleteAfterUploadPromise();
65-
6663
const mergedOptions = {
6764
...defaultPluginOptions,
6865
...options,
@@ -72,7 +69,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
7269
},
7370
sourcemaps: {
7471
...options?.sourcemaps,
75-
filesToDeleteAfterUpload,
7672
},
7773
};
7874

@@ -99,9 +95,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
9995
'sentry-vite-debug-id-upload-plugin not found in sentryPlugins! Cannot modify plugin - returning default Sentry Vite plugins',
10096
);
10197

102-
// resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
103-
resolveFilesToDeleteAfterUpload(undefined);
104-
10598
return sentryPlugins;
10699
}
107100

@@ -112,9 +105,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
112105
'sentry-file-deletion-plugin not found in sentryPlugins! Cannot modify plugin - returning default Sentry Vite plugins',
113106
);
114107

115-
// resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
116-
resolveFilesToDeleteAfterUpload(undefined);
117-
118108
return sentryPlugins;
119109
}
120110

@@ -125,9 +115,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
125115
'sentry-release-management-plugin not found in sentryPlugins! Cannot modify plugin - returning default Sentry Vite plugins',
126116
);
127117

128-
// resolving filesToDeleteAfterUpload here, because we return the original deletion plugin which awaits the promise
129-
resolveFilesToDeleteAfterUpload(undefined);
130-
131118
return sentryPlugins;
132119
}
133120

@@ -153,65 +140,51 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
153140
name: 'sentry-sveltekit-update-source-map-setting-plugin',
154141
apply: 'build', // only apply this plugin at build time
155142
config: async (config: UserConfig) => {
156-
const settingKey = 'build.sourcemap';
157-
158-
const { updatedSourceMapSetting, previousSourceMapSetting } = getUpdatedSourceMapSetting(config);
143+
return {
144+
...config,
145+
build: {
146+
...config.build,
147+
sourcemap: _getUpdatedSourceMapSettings(config, options),
148+
},
149+
};
150+
},
151+
};
159152

160-
const userProvidedFilesToDeleteAfterUpload = await options?.sourcemaps?.filesToDeleteAfterUpload;
153+
const filesToDeleteAfterUploadConfigPlugin: Plugin = {
154+
name: 'sentry-sveltekit-files-to-delete-after-upload-setting-plugin',
155+
apply: 'build', // only apply this plugin at build time
156+
config: async (config: UserConfig) => {
157+
const filesToDeleteAfterUpload = mergedOptions.sourcemaps?.filesToDeleteAfterUpload;
158+
159+
if (
160+
typeof filesToDeleteAfterUpload === 'undefined' &&
161+
// Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden")
162+
typeof config.build?.sourcemap === 'undefined'
163+
) {
164+
// Including all hidden (`.*`) directories by default so that folders like .vercel,
165+
// .netlify, etc are also cleaned up. Additionally, we include the adapter output
166+
// dir which could be a non-hidden directory, like `build` for the Node adapter.
167+
const defaultFileDeletionGlob = ['./.*/**/*.map', `./${adapterOutputDir}/**/*.map`];
161168

162-
if (previousSourceMapSetting === 'unset') {
163169
consoleSandbox(() => {
164-
// eslint-disable-next-line no-console
165-
console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: "hidden"\`.`);
166-
});
167-
168-
if (userProvidedFilesToDeleteAfterUpload) {
169-
resolveFilesToDeleteAfterUpload(userProvidedFilesToDeleteAfterUpload);
170-
} else {
171-
// Including all hidden (`.*`) directories by default so that folders like .vercel,
172-
// .netlify, etc are also cleaned up. Additionally, we include the adapter output
173-
// dir which could be a non-hidden directory, like `build` for the Node adapter.
174-
const defaultFileDeletionGlob = ['./.*/**/*.map', `./${adapterOutputDir}/**/*.map`];
175-
176-
consoleSandbox(() => {
170+
debug &&
177171
// eslint-disable-next-line no-console
178-
console.warn(
172+
console.info(
179173
`[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${defaultFileDeletionGlob
180174
.map(file => `"${file}"`)
181175
.join(', ')}]\` to delete generated source maps after they were uploaded to Sentry.`,
182176
);
183-
});
184-
185-
// In case we enabled source maps and users didn't specify a glob patter to delete, we set a default pattern:
186-
resolveFilesToDeleteAfterUpload(defaultFileDeletionGlob);
187-
}
177+
});
188178

189179
return {
190180
...config,
191-
build: { ...config.build, sourcemap: updatedSourceMapSetting },
181+
build: {
182+
...config.build,
183+
filesToDeleteAfterUpload: defaultFileDeletionGlob,
184+
},
192185
};
193186
}
194187

195-
if (previousSourceMapSetting === 'disabled') {
196-
consoleSandbox(() => {
197-
// eslint-disable-next-line no-console
198-
console.warn(
199-
`[Sentry] Parts of source map generation are 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\`).`,
200-
);
201-
});
202-
} else if (previousSourceMapSetting === 'enabled') {
203-
if (mergedOptions?.debug) {
204-
consoleSandbox(() => {
205-
// eslint-disable-next-line no-console
206-
console.log(
207-
`[Sentry] We discovered you enabled source map generation in your Vite configuration (\`${settingKey}\`). Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`,
208-
);
209-
});
210-
}
211-
}
212-
213-
resolveFilesToDeleteAfterUpload(userProvidedFilesToDeleteAfterUpload);
214-
215188
return config;
216189
},
217190
};
@@ -390,18 +363,30 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
390363
return [
391364
...unchangedSentryVitePlugins,
392365
sourceMapSettingsPlugin,
366+
filesToDeleteAfterUploadConfigPlugin,
393367
customReleaseManagementPlugin,
394368
customDebugIdUploadPlugin,
395369
customFileDeletionPlugin,
396370
];
397371
}
398372

399-
/**
400-
* Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps
373+
/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993)
374+
*
375+
* 1. User explicitly disabled source maps
376+
* - keep this setting (emit a warning that errors won't be unminified in Sentry)
377+
* - We won't upload anything
378+
*
379+
* 2. Users enabled source map generation (true, 'hidden', 'inline').
380+
* - keep this setting (don't do anything - like deletion - besides uploading)
381+
*
382+
* 3. Users didn't set source maps generation
383+
* - we enable 'hidden' source maps generation
384+
* - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this)
385+
*
386+
* --> only exported for testing
401387
*/
402-
type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
403388

404-
/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993)
389+
/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-j avascript/issues/13993)
405390
*
406391
* 1. User explicitly disabled source maps
407392
* - keep this setting (emit a warning that errors won't be unminified in Sentry)
@@ -416,30 +401,50 @@ type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
416401
*
417402
* --> only exported for testing
418403
*/
419-
export function getUpdatedSourceMapSetting(viteConfig: {
420-
build?: {
421-
sourcemap?: boolean | 'inline' | 'hidden';
422-
};
423-
}): { updatedSourceMapSetting: boolean | 'inline' | 'hidden'; previousSourceMapSetting: UserSourceMapSetting } {
404+
export function _getUpdatedSourceMapSettings(
405+
viteConfig: UserConfig,
406+
sentryPluginOptions?: CustomSentryVitePluginOptions,
407+
): boolean | 'inline' | 'hidden' {
424408
viteConfig.build = viteConfig.build || {};
425409

426-
const originalSourcemapSetting = viteConfig.build.sourcemap;
410+
const viteSourceMap = viteConfig?.build?.sourcemap;
411+
let updatedSourceMapSetting = viteSourceMap;
427412

428-
if (originalSourcemapSetting === false) {
429-
return {
430-
previousSourceMapSetting: 'disabled',
431-
updatedSourceMapSetting: originalSourcemapSetting,
432-
};
433-
}
413+
const settingKey = 'build.sourcemap';
414+
const debug = sentryPluginOptions?.debug;
415+
416+
if (viteSourceMap === false) {
417+
updatedSourceMapSetting = viteSourceMap;
434418

435-
if (originalSourcemapSetting && ['hidden', 'inline', true].includes(originalSourcemapSetting)) {
436-
return { previousSourceMapSetting: 'enabled', updatedSourceMapSetting: originalSourcemapSetting };
419+
if (debug) {
420+
// Longer debug message with more details
421+
// eslint-disable-next-line no-console
422+
console.warn(
423+
`[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\`).`,
424+
);
425+
} else {
426+
// eslint-disable-next-line no-console
427+
console.warn('[Sentry] Source map generation is disabled in your Vite configuration.');
428+
}
429+
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
430+
updatedSourceMapSetting = viteSourceMap;
431+
432+
debug &&
433+
// eslint-disable-next-line no-console
434+
console.log(
435+
`[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.`,
436+
);
437+
} else {
438+
updatedSourceMapSetting = 'hidden';
439+
440+
debug &&
441+
// eslint-disable-next-line no-console
442+
console.log(
443+
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
444+
);
437445
}
438446

439-
return {
440-
previousSourceMapSetting: 'unset',
441-
updatedSourceMapSetting: 'hidden',
442-
};
447+
return updatedSourceMapSetting;
443448
}
444449

445450
function getFiles(dir: string): string[] {
@@ -475,22 +480,3 @@ function detectSentryRelease(): string {
475480

476481
return release;
477482
}
478-
479-
/**
480-
* Creates a deferred promise that can be resolved/rejected by calling the
481-
* `resolve` or `reject` function.
482-
* Inspired by: https://stackoverflow.com/a/69027809
483-
*/
484-
function createFilesToDeleteAfterUploadPromise(): {
485-
promise: Promise<string | string[] | undefined>;
486-
resolve: (value: string | string[] | undefined) => void;
487-
reject: (reason?: unknown) => void;
488-
} {
489-
let resolve!: (value: string | string[] | undefined) => void;
490-
let reject!: (reason?: unknown) => void;
491-
const promise = new Promise<string | string[] | undefined>((res, rej) => {
492-
resolve = res;
493-
reject = rej;
494-
});
495-
return { resolve, reject, promise };
496-
}

packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('sentrySvelteKit()', () => {
4242

4343
expect(plugins).toBeInstanceOf(Array);
4444
// 1 auto instrument plugin + 5 source maps plugins
45-
expect(plugins).toHaveLength(8);
45+
expect(plugins).toHaveLength(9);
4646
});
4747

4848
it('returns the custom sentry source maps upload plugin, unmodified sourcemaps plugins and the auto-instrument plugin by default', async () => {
@@ -56,6 +56,7 @@ describe('sentrySvelteKit()', () => {
5656
'sentry-vite-release-injection-plugin',
5757
'sentry-vite-debug-id-injection-plugin',
5858
'sentry-sveltekit-update-source-map-setting-plugin',
59+
'sentry-sveltekit-files-to-delete-after-upload-setting-plugin',
5960
// custom release plugin:
6061
'sentry-sveltekit-release-management-plugin',
6162
// custom source maps plugin:
@@ -86,7 +87,7 @@ describe('sentrySvelteKit()', () => {
8687
it("doesn't return the auto instrument plugin if autoInstrument is `false`", async () => {
8788
const plugins = await getSentrySvelteKitPlugins({ autoInstrument: false });
8889
const pluginNames = plugins.map(plugin => plugin.name);
89-
expect(plugins).toHaveLength(7);
90+
expect(plugins).toHaveLength(8);
9091
expect(pluginNames).not.toContain('sentry-upload-source-maps');
9192
});
9293

@@ -184,7 +185,7 @@ describe('sentrySvelteKit()', () => {
184185
// just to ignore the source maps plugin:
185186
autoUploadSourceMaps: false,
186187
});
187-
const plugin = plugins[0];
188+
const plugin = plugins[0]!;
188189

189190
expect(plugin.name).toEqual('sentry-auto-instrumentation');
190191
expect(makePluginSpy).toHaveBeenCalledWith({

0 commit comments

Comments
 (0)