源码映射

了解如何在 Sentry 错误中启用可读的堆栈跟踪。

要使 Sentry 错误中的堆栈跟踪可读,您需要将源映射上传到 Sentry。 通过观看此视频或阅读以下逐步说明来学习如何解压缩您的 JavaScript 代码。

This guide assumes you are using the @sentry/svelte SDK on version 7.47.0 or higher.

If you are on an older version and you want to upload source maps we recommend upgrading your SDK to the newest version.

The easiest way to configure uploading source maps is by using the Sentry Wizard:

Copied
npx @sentry/wizard@latest -i sourcemaps

向导将引导您完成以下步骤:

  • 登录 Sentry 并选择一个项目
  • 安装必要的 Sentry 包
  • 配置您的构建工具以生成和上传源映射
  • 配置您的 CI 以上传源映射

If you want to configure source maps upload manually, follow the guide for your bundler or build tool below.

To generate source maps with your Svelte project, you need to set the source map compiler options in your Svelte config:

svelte.config.js
Copied
import sveltePreprocess from "svelte-preprocess";

const config = {
  compilerOptions: {
    enableSourcemap: true,
  },
  preprocess: sveltePreprocess({
    sourceMap: true,
  }),
};

export default config;

If you're using Vite in you Svelte project, you can use Sentry's Vite plugin for convenience:

Copied
npm install @sentry/vite-plugin --save-dev

To upload source maps you have to configure an Organization Auth Token.

Alternatively, you can also use a User Auth Token, with the "Project: Read & Write" and "Release: Admin" permissions.

Auth tokens can be passed to the plugin explicitly with the authToken option, with a SENTRY_AUTH_TOKEN environment variable, or with a .env.sentry-build-plugin file in the working directory when building your project. You likely want to add the auth token as an environment variable to your CI/CD environment.

.env.sentry-build-plugin
Copied
SENTRY_AUTH_TOKEN=sntrys_YOUR_TOKEN_HERE

Configure Vite to emit source maps and use the Sentry Vite plugin:

vite.config.js
Copied
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { sentryVitePlugin } from "@sentry/vite-plugin";

export default defineConfig({
  build: {
    sourcemap: true, // Source map generation must be turned on
  },
  plugins: [
    svelte(),

    // Put the Sentry vite plugin after all other plugins
    sentryVitePlugin({
      org: "example-org",
      project: "example-project",
      authToken: process.env.SENTRY_AUTH_TOKEN,
    }),
  ],
});

The Sentry Vite plugin doesn't upload source maps in watch-mode/development-mode. We recommend running a production build to test your implementation.

If you're using a bundler other than Vite, check out our general guide on how to upload source maps, or refer to your bundler's documentation.

By default, if Sentry can't find the uploaded files it needs, it will attempt to download them from the URLs in the stack trace. To disable this, turn off "Enable JavaScript source fetching" in either your organization's "Security & Privacy" settings or your project's general settings.