Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { sentryVitePlugin } from "@sentry/bundler-plugins/vite";
import { defineConfig } from "vite";
import { sentryConfig } from "../configs/sourcemaps-assets-auto.config.js";

export default defineConfig({
build: {
minify: false,
rollupOptions: {
input: ["src/entry1.js", "src/entry2.js"],
output: {
dir: "out/sourcemaps-assets-auto",
chunkFileNames: "[name].js",
entryFileNames: "[name].js",
},
},
},
plugins: [sentryVitePlugin(sentryConfig)],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from "vitest";
import { test } from "./utils";

test(import.meta.url, ({ runBundler, readOutputFiles }) => {
runBundler({ SENTRY_TEST_RECORD_UPLOADED_SOURCES: "1" });
const files = readOutputFiles();

expect(Object.keys(files).sort()).toMatchInlineSnapshot(`
[
"common.js",
"common.js.map",
"entry1.js",
"entry2.js",
"entry2.js.map",
"sentry-cli-mock.json",
"sentry-uploaded-sources.json",
]
`);
expect(JSON.parse(files["sentry-uploaded-sources.json"]!)).toMatchInlineSnapshot(`
[
[
"../../src/entry1.js",
],
]
`);
expect(files["sentry-cli-mock.json"]).toMatchInlineSnapshot(`
"["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"],
"
`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { sentryVitePlugin } from "@sentry/bundler-plugins/vite";
import { defineConfig } from "vite";
import { sentryConfig } from "../configs/sourcemaps-ignore-auto.config.js";

export default defineConfig({
build: {
minify: false,
rollupOptions: {
input: ["src/entry1.js", "src/entry2.js"],
output: {
dir: "out/sourcemaps-ignore-auto",
chunkFileNames: "[name].js",
entryFileNames: "[name].js",
},
},
},
plugins: [sentryVitePlugin(sentryConfig)],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from "vitest";
import { test } from "./utils";

test(import.meta.url, ({ runBundler, readOutputFiles }) => {
runBundler({ SENTRY_TEST_RECORD_UPLOADED_SOURCES: "1" });
const files = readOutputFiles();

expect(Object.keys(files).sort()).toMatchInlineSnapshot(`
[
"common.js",
"entry1.js",
"entry2.js",
"entry2.js.map",
"sentry-cli-mock.json",
"sentry-uploaded-sources.json",
]
`);
expect(JSON.parse(files["sentry-uploaded-sources.json"]!)).toMatchInlineSnapshot(`
[
[
"../../src/common.js",
],
[
"../../src/entry1.js",
],
]
`);
expect(files["sentry-cli-mock.json"]).toMatchInlineSnapshot(`
"["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"],
"
`);
});
1 change: 1 addition & 0 deletions packages/bundler-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"@types/node": "^20.19.0",
"premove": "^4.0.0",
"rolldown": "^1.0.0",
"vite": "^8.3.1",
"vitest": "^4.1.11",
"webpack": "5.112.2"
},
Expand Down
31 changes: 26 additions & 5 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 @@ -315,21 +316,41 @@ export function _createRollupPluginInternals(
}
: transform;

// Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin
// decides in a `config` hook instead.
const viteSourceMapHooks =
buildTool === 'vite' ? createViteSourceMapHooks(sentryBuildPluginManager, sourceMapHandling) : undefined;

const main = {
name,
buildStart,
...(shouldTransform ? { transform: transformHook } : {}),
// Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin
// decides in its `config` hook instead.
...(buildTool === 'rollup' ? { outputOptions: sourceMapHandling.outputOptions } : {}),
...(viteSourceMapHooks
? { configResolved: viteSourceMapHooks.configResolved }
: { outputOptions: sourceMapHandling.outputOptions }),
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',
// On the post plugin, so framework plugins that set `build.sourcemap`
// themselves run first and the decision sees the merged setting.
...(viteSourceMapHooks ? { config: viteSourceMapHooks.config } : {}),
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
Loading
Loading