第一種方式:webpack 配置
參考文檔:https://docs.sentry.io/platforms/javascript/sourcemaps/
- 安裝 @sentry/webpack-plugin 和 clean-webpack-plugin
yarn add @sentry/webpack-plugin clean-webpack-plugin -D
- 獲取 authToken(兩種獲取方式)
- Organization Settings -> Developer Settings -> New Internal Integration -> Save Changes
note: PERMISSIONS 列表中需要選中 Releases -> Admin
- API keys -> Auth Tokens -> Create New Token
- 在 webpack.config.js 中添加以下代碼:
const SentryWebpackPlugin = require("@sentry/webpack-plugin");
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
// other configuration
plugins: [
// 多次構建后會因文件過多無法上傳,先清理一下
new CleanWebpackPlugin(),
new SentryWebpackPlugin({
// 自托管 sentry 地址
url: "",
authToken: "",
org: "example-org",
project: "example-project",
// 本地 sourcemap 所在目錄
include: "",
// js 訪問路徑
urlPrefix: ""
}),
],
};
第二種方式:sentry-cli
參考文檔:https://docs.sentry.io/product/cli/configuration/
https://docs.sentry.io/platforms/javascript/sourcemaps/uploading/
- 配置和認證
(1)獲取 authToken,至少具有以下范圍:
- project:read
- project:releases
- org:read
(2)配置(四種方式)
- 創建 .sentryclirc
[defaults]
url = https://mysentry.invalid/
[auth]
token=your-auth-token
- 導出為環境變量
// .env
export SENTRY_URL=https://mysentry.invalid/
export SENTRY_AUTH_TOKEN=your-auth-token
- 使用 Properties
// .env
export SENTRY_PROPERTIES=./sentry.properties
// sentry.properties
defaults.url=https://mysentry.invalid/
auth.token=your-auth-token
- 作為命令行參數
sentry-cli --url https://mysentry.invalid/ --auth-token your-auth-token
- 創建新版本
sentry-cli releases -o example-org -p example-project new <release_name>
note: release_name 需要在 example-org 內唯一,且與 Sentry.init 中的 release 選項保持一致
- 上傳 sourcemap
sentry-cli releases -o example-org -p example-project files <release_name> upload-sourcemaps /path/to/files --url-prefix example-url
