@sentry/remix
Version:
Official Sentry SDK for Remix
127 lines (124 loc) • 4.41 kB
JavaScript
import * as path from 'path';
import { sentryVitePlugin } from '@sentry/bundler-plugins/vite';
function makeAddSentryVitePlugin(options) {
const {
applicationKey,
authToken,
bundleSizeOptimizations,
debug,
errorHandler,
headers,
moduleMetadata,
org,
project,
release,
sentryUrl,
silent,
sourcemaps,
telemetry
} = options;
let resolveFilesToDeleteAfterUpload;
const filesToDeleteAfterUploadPromise = new Promise((resolve) => {
resolveFilesToDeleteAfterUpload = resolve;
});
const configPlugin = {
name: "sentry-remix-files-to-delete-after-upload",
apply: "build",
enforce: "post",
config(config) {
const userFilesToDelete = sourcemaps?.filesToDeleteAfterUpload;
if (typeof userFilesToDelete === "undefined" && typeof config.build?.sourcemap === "undefined" && !sourcemaps?.disable) {
const outDir = (config.build?.outDir || "build").replace(/\\/g, "/").replace(/\/+$/, "");
const isAbsoluteOutDir = path.posix.isAbsolute(outDir) || path.win32.isAbsolute(outDir);
const outDirGlob = isAbsoluteOutDir ? outDir : `./${outDir.replace(/^\.\//, "")}`;
const filesToDelete = [`${outDirGlob}/**/*.map`];
if (debug) {
console.log(
`[Sentry] Automatically setting \`sourcemaps.filesToDeleteAfterUpload: ${JSON.stringify(
filesToDelete
)}\` to delete generated source maps after they were uploaded to Sentry.`
);
}
resolveFilesToDeleteAfterUpload?.(filesToDelete);
} else {
resolveFilesToDeleteAfterUpload?.(userFilesToDelete);
}
}
};
const sentryPlugins = sentryVitePlugin({
applicationKey,
authToken: authToken ?? process.env.SENTRY_AUTH_TOKEN,
bundleSizeOptimizations,
debug: debug ?? false,
errorHandler,
headers,
moduleMetadata,
org: org ?? process.env.SENTRY_ORG,
project: project ?? process.env.SENTRY_PROJECT,
release,
silent,
sourcemaps: {
assets: sourcemaps?.assets,
disable: sourcemaps?.disable,
ignore: sourcemaps?.ignore,
rewriteSources: sourcemaps?.rewriteSources,
resolveSourceMap: sourcemaps?.resolveSourceMap,
filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise
},
telemetry: telemetry ?? true,
url: sentryUrl,
_metaOptions: {
telemetry: {
metaFramework: "remix"
}
}
});
return [configPlugin, ...sentryPlugins];
}
function makeEnableSourceMapsPlugin(options) {
return {
name: "sentry-remix-update-source-map-setting",
apply: "build",
enforce: "post",
// Returning only the changed key: Vite concatenates arrays when merging a `config` return
// value, so echoing the whole config back duplicates `ssr.noExternal`, `resolve.alias` etc.
config(viteConfig) {
return {
build: {
sourcemap: getUpdatedSourceMapSettings(viteConfig, options)
}
};
}
};
}
function getUpdatedSourceMapSettings(viteConfig, sentryPluginOptions) {
const viteUserSourceMapSetting = viteConfig.build?.sourcemap;
const settingKey = "vite.build.sourcemap";
const debug = sentryPluginOptions?.debug;
if (viteUserSourceMapSetting === false) {
if (debug) {
console.warn(
`[Sentry] Source map generation is currently disabled in your Vite configuration (\`${settingKey}: false\`). Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified.`
);
} else {
console.warn("[Sentry] Source map generation is disabled in your Vite configuration.");
}
return viteUserSourceMapSetting;
}
if (viteUserSourceMapSetting && ["hidden", "inline", true].includes(viteUserSourceMapSetting)) {
if (debug) {
console.log(
`[Sentry] We discovered \`${settingKey}\` is set to \`${viteUserSourceMapSetting.toString()}\`. Sentry will keep this source map setting.`
);
}
return viteUserSourceMapSetting;
}
if (debug) {
console.log(
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`
);
}
return "hidden";
}
export { getUpdatedSourceMapSettings, makeAddSentryVitePlugin, makeEnableSourceMapsPlugin };
//# sourceMappingURL=sourceMaps.js.map