Skip to content
Merged
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
84 changes: 41 additions & 43 deletions packages/cli/src/lib/wizard/sourcemaps/tools/vite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Source maps setup for Vite: add the Sentry Vite plugin to the Vite config
* and turn on `build.sourcemap`.
* Source maps setup for Vite: add the Sentry Vite plugin to the Vite config.
* `build.sourcemap` stays unset, so the plugin generates hidden source maps
* and removes them from the build output after uploading them.
*/

import type { namedTypes as t } from "ast-types";
Expand All @@ -9,7 +10,6 @@ import { generateCode, parseModule } from "magicast";
import { addVitePlugin } from "magicast/helpers";
import { promises } from "node:fs";
import path from "node:path";
import recast from "recast";

import { Sentry } from "../../telemetry.js";
import {
Expand All @@ -29,6 +29,7 @@ import {
import { clack } from "../../utils/clack/prompts.js";
import { debug } from "../../utils/debug.js";
import { ensureBundlerPlugin } from "../utils/bundler-plugin.js";
import { askShouldRemoveDisabledSourceMaps } from "../utils/disabled-source-maps.js";
import type {
SourceMapUploadToolConfigurationFunction,
SourceMapUploadToolConfigurationOptions,
Expand All @@ -44,9 +45,8 @@ const getViteConfigSnippet = (
${plus(`import { sentryVitePlugin } from "${importPath}";`)}

export default defineConfig({
build: {
${plus("sourcemap: true, // Source map generation must be turned on")}
},
// Leave \`build.sourcemap\` unset. The Sentry plugin then generates hidden source
// maps and removes them from the build output after uploading them.
plugins: [
// Put the Sentry vite plugin after all other plugins
${plus(`sentryVitePlugin({
Expand Down Expand Up @@ -144,8 +144,11 @@ export async function addVitePluginToConfig(
}
}

const enabledSourcemaps = enableSourcemapGeneration(mod.$ast as t.Program);
if (!enabledSourcemaps) {
const handledSourcemaps = await handleDisabledSourcemaps(
mod.$ast as t.Program,
prettyViteConfigFilename,
);
if (!handledSourcemaps) {
Sentry.setTag("ast-mod-fail-reason", "insertion-fail");
return false;
}
Expand All @@ -171,7 +174,7 @@ export async function addVitePluginToConfig(
await promises.writeFile(viteConfigPath, code);

clack.log.success(
`Added the Sentry Vite plugin to ${prettyViteConfigFilename} and enabled source maps`,
`Added the Sentry Vite plugin to ${prettyViteConfigFilename}.`,
);

return true;
Expand All @@ -182,57 +185,52 @@ export async function addVitePluginToConfig(
}
}

function enableSourcemapGeneration(program: t.Program): boolean {
/**
* Offers to remove `build.sourcemap: false`. All other values are left alone:
* if the option is unset, the Sentry plugin generates hidden source maps and
* removes them after the upload.
*
* @returns false if the config object could not be found
*/
async function handleDisabledSourcemaps(
program: t.Program,
prettyConfigFilename: string,
): Promise<boolean> {
const configObj = getViteConfigObject(program);

if (!configObj) {
return false;
}

const b = recast.types.builders;

const buildProp = getObjectProperty(configObj, "build");

// case 1: build property doesn't exist yet, so we can just add it
if (!buildProp) {
configObj.properties.push(
b.objectProperty(
b.identifier("build"),
b.objectExpression([
b.objectProperty(b.identifier("sourcemap"), b.booleanLiteral(true)),
]),
),
);
// A `build` value we can't analyze doesn't block adding the plugin. If it
// turns off source maps, the plugin warns about it during the build.
if (buildProp?.value.type !== "ObjectExpression") {
return true;
}

if (buildProp.value.type !== "ObjectExpression") {
return false;
}

const buildObj = buildProp.value;
const sourceMapsProp = getObjectProperty(buildObj, "sourcemap");

// case 2: build.sourcemap property doesn't exist yet, so we just add it
if (!sourceMapsProp) {
buildObj.properties.push(
b.objectProperty(b.identifier("sourcemap"), b.booleanLiteral(true)),
);
return true;
}
const sourcemapProp = getObjectProperty(buildObj, "sourcemap");

// case 3: build.sourcemap property exists, and it's set to 'hidden'
if (
sourceMapsProp.value.type === "StringLiteral" &&
sourceMapsProp.value.value === "hidden"
sourcemapProp?.value.type === "BooleanLiteral" &&
!sourcemapProp.value.value &&
(await askShouldRemoveDisabledSourceMaps(
"build.sourcemap: false",
prettyConfigFilename,
))
) {
// nothing to do for us
return true;
buildObj.properties = buildObj.properties.filter(
(p) => p !== sourcemapProp,
);
clack.log.success(
`Removed ${chalk.cyan(
"build.sourcemap: false",
)} from ${prettyConfigFilename}.`,
);
}

// case 4: build.sourcemap property exists, but it's not enabled, so we set it to true
// or it is already true in which case this is a noop
sourceMapsProp.value = b.booleanLiteral(true);
return true;
}

Expand Down
82 changes: 42 additions & 40 deletions packages/cli/src/lib/wizard/sourcemaps/tools/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Source maps setup for webpack: add the Sentry webpack plugin to a CommonJS
* webpack config and turn on `devtool: "source-map"`.
* webpack config. `devtool` stays unset, so the plugin generates hidden source
* maps and removes them from the build output after uploading them.
*/

import type { namedTypes as t } from "ast-types";
Expand All @@ -24,6 +25,7 @@ import {
import { clack } from "../../utils/clack/prompts.js";
import { debug } from "../../utils/debug.js";
import { ensureBundlerPlugin } from "../utils/bundler-plugin.js";
import { askShouldRemoveDisabledSourceMaps } from "../utils/disabled-source-maps.js";
import type {
SourceMapUploadToolConfigurationFunction,
SourceMapUploadToolConfigurationOptions,
Expand All @@ -41,7 +43,8 @@ const getCodeSnippet = (

module.exports = {
// ... other options
${plus('devtool: "source-map", // Source map generation must be turned on')}
// Leave \`devtool\` unset for production. The Sentry plugin then generates hidden
// source maps and removes them from the build output after uploading them.
plugins: [
// Put the Sentry Webpack plugin after all other plugins
${plus(`sentryWebpackPlugin({
Expand Down Expand Up @@ -102,7 +105,7 @@ export const configureWebPackPlugin: SourceMapUploadToolConfigurationFunction =
};

/**
* Modifies a webpack config file to enable source map generation and add the Sentry webpack plugin
* Modifies a webpack config file to add the Sentry webpack plugin
* exported only for testing
*/
export async function modifyWebpackConfig(
Expand Down Expand Up @@ -141,19 +144,7 @@ export async function modifyWebpackConfig(
return false;
}

const enabledSourcemaps = enableSourcemapsGeneration(configObject);

if (enabledSourcemaps) {
clack.log.success(
`Enabled source map generation in ${prettyConfigFilename}.`,
);
} else {
clack.log.warn(
`Couldn't enable source maps generation in ${prettyConfigFilename} Please follow the instructions below.`,
);
Sentry.setTag("ast-mod-fail-reason", "insertion-fail");
return false;
}
await handleDisabledSourcemaps(configObject, prettyConfigFilename);

const addedPlugin = addSentryWebpackPlugin(
program,
Expand Down Expand Up @@ -311,40 +302,51 @@ function addSentryWebpackPluginImport(program: t.Program, importPath: string) {
return b;
}

function enableSourcemapsGeneration(configObj: t.ObjectExpression): boolean {
const b = recast.types.builders;

/**
* Offers to remove a `devtool` value that disables source maps. All other
* values are left alone: if the option is unset, the Sentry plugin generates
* hidden source maps and removes them after the upload.
*/
async function handleDisabledSourcemaps(
configObj: t.ObjectExpression,
prettyConfigFilename: string,
): Promise<void> {
const devtoolProp = configObj.properties.find(
(p): p is t.Property =>
p.type === "Property" &&
p.key.type === "Identifier" &&
p.key.name === "devtool",
);

if (devtoolProp) {
// devtool can have quite a lot of source maps values.
// see: https://webpack.js.org/configuration/devtool/#devtool
// For Sentry to work best, we should set it to "source-map" or "hidden-source-map"
// Heuristic:
// - all values that contain "hidden" will be set to "hidden-source-map"
// - all other values will be set to "source-map"
if (
(devtoolProp.value.type === "Literal" ||
devtoolProp.value.type === "StringLiteral") &&
devtoolProp.value.value?.toString().startsWith("hidden-")
) {
devtoolProp.value = b.stringLiteral("hidden-source-map");
} else {
devtoolProp.value = b.stringLiteral("source-map");
}
return true;
if (devtoolProp?.value.type !== "Literal") {
return;
}

configObj.properties.push(
b.objectProperty(b.identifier("devtool"), b.stringLiteral("source-map")),
);
const devtool = devtoolProp.value.value;
// Values like "eval" don't produce source maps either
const isDisabled =
devtool === false ||
(typeof devtool === "string" && !devtool.includes("source-map"));

// With SourceMapDevToolPlugin, users set `devtool: false` to configure source
// maps themselves
if (
!isDisabled ||
recast.print(configObj).code.includes("SourceMapDevToolPlugin")
) {
return;
}

return true;
const setting = `devtool: ${JSON.stringify(devtool)}`;

if (await askShouldRemoveDisabledSourceMaps(setting, prettyConfigFilename)) {
configObj.properties = configObj.properties.filter(
(p) => p !== devtoolProp,
);
clack.log.success(
`Removed ${chalk.cyan(setting)} from ${prettyConfigFilename}.`,
);
}
}

function getWebpackConfigObject(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Asks whether a bundler setting that disables source maps should be removed.
*
* If the setting is unset, the Sentry bundler plugins generate hidden source
* maps and remove them from the build output after uploading them.
*/

import chalk from "chalk";

import { Sentry } from "../../telemetry.js";
import { abortIfCancelled } from "../../utils/clack/index.js";
import { clack } from "../../utils/clack/prompts.js";

export async function askShouldRemoveDisabledSourceMaps(
setting: string,
prettyConfigFilename: string,
): Promise<boolean> {
const shouldRemove = await abortIfCancelled(
clack.select({
message: `Source maps are disabled by ${chalk.cyan(
setting,
)} in ${prettyConfigFilename}. Should the wizard remove this setting so that Sentry can un-minify your stack traces?`,
options: [
{
label: "Yes, remove it",
hint: "source maps are not exposed, Sentry removes them from the build output after the upload",
value: true,
},
{
label: "No, keep source maps disabled",
value: false,
},
],
initialValue: true,
}),
);

Sentry.setTag("removed-disabled-source-maps", shouldRemove);

if (!shouldRemove) {
clack.log.warn(
`Source maps stay disabled. Stack traces in Sentry will be minified until you remove ${chalk.cyan(
setting,
)} from ${prettyConfigFilename}.`,
);
}

return shouldRemove;
}
Loading
Loading