Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles }) => {
",
"sentry-cli-mock.json": "["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles }) => {
",
"sentry-cli-mock.json": "["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles }) => {
",
"sentry-cli-mock.json": "["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
21 changes: 18 additions & 3 deletions packages/bundler-plugins/src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { createRequire } from 'node:module';
import { finalizeRolldownDebugIds, getDebugIdForChunk, hasExistingDebugID } from './debug-id-injection';
import { stampDebugIds, type OutputBundle } from './debug-id-stamping';
import { createSourceMapHandling, type OutputDestinationOptions } from './source-map-handling';
import { createViteSourceMapHooks } from '../vite/source-map-hooks';

// The subset of Rollup's `TransformResult` that this plugin's `transform`
// hook actually returns. Defined locally instead of imported from `rollup`
Expand Down Expand Up @@ -316,15 +317,29 @@ export function _createRollupPluginInternals(
...(shouldTransform ? { transform: transformHook } : {}),
// Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin
// decides in its `config` hook instead.
...(buildTool === 'rollup' ? { outputOptions: sourceMapHandling.outputOptions } : {}),
...(buildTool === 'rollup'
? { outputOptions: sourceMapHandling.outputOptions }
: createViteSourceMapHooks(sentryBuildPluginManager, sourceMapHandling)),
renderChunk,
generateBundle: { order: 'pre' as const, handler: generateBundle },
writeBundle,
};

const post = {
name: `sentry-${buildTool}-upload-staging`,
generateBundle: { order: 'post' as const, handler: sourceMapHandling.stageSourceMaps },
name: buildTool === 'vite' ? 'sentry-vite-debug-id-sourcemaps' : 'sentry-rollup-upload-staging',
generateBundle: {
order: 'post' as const,
async handler(outputOptions: OutputDestinationOptions, bundle: OutputBundle, isWrite?: boolean): Promise<void> {
if (buildTool === 'vite' && options.sourcemaps?.disable === 'disable-upload') {
// Vite's module preload rewrite in `generateBundle` regenerates the source map of every
// chunk with a dynamic import, which drops the debug ID the main plugin stamped in its
// `pre` hook. Restore it without touching the chunk code, so file hashes stay valid.
stampDebugIds(bundle, false);
}

await sourceMapHandling.stageSourceMaps(outputOptions, bundle, isWrite);
},
},
};

return { main, post };
Expand Down
35 changes: 8 additions & 27 deletions packages/bundler-plugins/src/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import type { SentryRollupPluginOptions } from '../rollup';
import { _rollupPluginInternal } from '../rollup';
import { _createRollupPluginInternals } from '../rollup';
import { createRequire } from 'node:module';
import { stampDebugIds, type OutputBundle } from '../rollup/debug-id-stamping';

interface SentryVitePlugin {
name: string;
enforce: 'pre' | 'post';
generateBundle?: {
order: 'pre' | 'post';
handler: (_outputOptions: unknown, bundle: OutputBundle) => void;
};
[hook: string]: unknown;
}

function getViteMajorVersion(): string | undefined {
Expand All @@ -27,27 +23,12 @@ function getViteMajorVersion(): string | undefined {
}

export const sentryVitePlugin = (options?: SentryRollupPluginOptions): SentryVitePlugin[] => {
const plugins: SentryVitePlugin[] = [
{
enforce: 'pre',
..._rollupPluginInternal(options, 'vite', getViteMajorVersion()),
},
];

if (!options?.disable && options?.sourcemaps?.disable === 'disable-upload') {
plugins.push({
name: 'sentry-vite-debug-id-sourcemaps',
enforce: 'post',
generateBundle: {
order: 'post',
handler(_outputOptions, bundle) {
// Vite's module preload rewrite in `generateBundle` regenerates the source map of every
// chunk with a dynamic import, which drops the debug ID the main plugin stamped in its
// `pre` hook. Restore it without touching the chunk code, so file hashes stay valid.
stampDebugIds(bundle, false);
},
},
});
const { main, post } = _createRollupPluginInternals(options, 'vite', getViteMajorVersion());

const plugins: SentryVitePlugin[] = [{ enforce: 'pre', ...main }];

if (post) {
plugins.push({ enforce: 'post', ...post });
}

return plugins;
Expand Down
110 changes: 110 additions & 0 deletions packages/bundler-plugins/src/vite/source-map-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import * as path from 'node:path';
import type { SentryBuildPluginManager, SourceMapDecision } from '../core';
import type { RollupSourceMapSetting, SourceMapHandling } from '../rollup/source-map-handling';
import { sourceMapKindFromRollupSetting } from '../rollup/source-map-handling';

type BundlerOutputOptions = { dir?: string } | { dir?: string }[] | undefined;

type ViteBuildOptions = {
sourcemap?: RollupSourceMapSetting;
outDir?: string;
rollupOptions?: { output?: BundlerOutputOptions };
rolldownOptions?: { output?: BundlerOutputOptions };
};

export type ViteUserConfig = {
build?: ViteBuildOptions;
environments?: Record<string, { build?: ViteBuildOptions } | undefined>;
};

export type ViteResolvedConfig = {
root: string;
build: ViteBuildOptions;
environments?: Record<string, { build: ViteBuildOptions }>;
};

export type ViteSourceMapHooks = {
config: (userConfig: ViteUserConfig, env: { command: string }) => ViteUserConfig | null;
configResolved: (config: ViteResolvedConfig) => void;
};

const NO_CHANGE: SourceMapDecision = { enableHidden: false, deleteAfterUpload: false };

/**
* The directories an environment writes to: `build.outDir` (relative to the root) unless an output
* in `rollupOptions` or `rolldownOptions` sets its own `dir`, which the bundler resolves from the
* working directory.
*/
function getOutputDestinations(root: string, build: ViteBuildOptions): string[] {
const outputs = build.rolldownOptions?.output ?? build.rollupOptions?.output;
const outputDirs = (Array.isArray(outputs) ? outputs : [outputs])
.map(output => output?.dir)
.filter((dir): dir is string => typeof dir === 'string')
.map(dir => path.resolve(dir));

return outputDirs.length > 0 ? outputDirs : [path.resolve(root, build.outDir ?? 'dist')];
}

/**
* Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin decides on `build.sourcemap`
* in its `config` hook. The output directories are only known in `configResolved`, which then
* records the decision for each of them.
*/
export function createViteSourceMapHooks(
sentryBuildPluginManager: SentryBuildPluginManager,
sourceMapHandling: SourceMapHandling,
): ViteSourceMapHooks {
// `'*'` for the top-level setting, otherwise the name of an environment with its own.
const decisions = new Map<string, SourceMapDecision>();

return {
config: (userConfig, env) => {
if (env.command !== 'build') {
return null;
}

const decision = sentryBuildPluginManager.sourceMaps.decide(
sourceMapKindFromRollupSetting(userConfig.build?.sourcemap),
);
decisions.set('*', decision);

const result: ViteUserConfig = decision.enableHidden ? { build: { sourcemap: 'hidden' } } : {};

for (const [name, environment] of Object.entries(userConfig.environments ?? {})) {
const setting = environment?.build?.sourcemap;
if (setting === undefined) {
continue;
}

const environmentDecision = sentryBuildPluginManager.sourceMaps.decide(sourceMapKindFromRollupSetting(setting));
decisions.set(name, environmentDecision);

if (environmentDecision.enableHidden) {
result.environments = { ...result.environments, [name]: { build: { sourcemap: 'hidden' } } };
}
}

return result;
},

configResolved: config => {
const environments = config.environments ?? { client: { build: config.build } };

for (const [name, environment] of Object.entries(environments)) {
let decision = decisions.get(name) ?? decisions.get('*') ?? NO_CHANGE;

// A plugin that ran after ours may have changed the setting again.
if (decision.deleteAfterUpload && environment.build.sourcemap !== 'hidden') {
sentryBuildPluginManager.logger.debug(
`Source maps of the "${name}" environment are set to ${JSON.stringify(environment.build.sourcemap)}. Not removing them after upload.`,
);
decision = NO_CHANGE;
}

for (const destination of getOutputDestinations(config.root, environment.build)) {
sourceMapHandling.setDecision(destination, decision);
}
}
},
};
}
Loading
Loading