|
| 1 | +--- |
| 2 | +title: Angular CLI |
| 3 | +description: "Upload your source maps using the Angular CLI, a custom Angular builder and the Sentry Webpack plugin." |
| 4 | +sidebar_order: 0 |
| 5 | +supported: |
| 6 | + - javascript.angular |
| 7 | +--- |
| 8 | + |
| 9 | +You can use the Angular CLI together with our Sentry Webpack plugin to automatically create releases and upload source maps to Sentry when building your app (with `ng build`, for example). |
| 10 | +Due to Angular's closed build system, to register the Webpack plugin, you'll first need to configure a custom builder. |
| 11 | +In the end, you'll be able to automatically upload source maps whenever you're creating a production build of your app. |
| 12 | + |
| 13 | +<Alert level="warning"> |
| 14 | + |
| 15 | +Before you start configuring the source maps upload, make sure that you're [generating source maps](../../.#generating-source-maps) in your Angular project. |
| 16 | + |
| 17 | + </Alert> |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +Install the custom Webpack builder for Angular and the Sentry Webpack plugin: |
| 22 | + |
| 23 | +```shell {tabTitle:npm} |
| 24 | +npm install --save-dev @angular-builders/custom-webpack @sentry/webpack-plugin |
| 25 | +``` |
| 26 | + |
| 27 | +```shell {tabTitle:yarn} |
| 28 | +yarn add --dev @angular-builders/custom-webpack @sentry/webpack-plugin |
| 29 | +``` |
| 30 | + |
| 31 | +## Configure |
| 32 | + |
| 33 | +With these packages installed, configure your app to upload source maps in three steps: |
| 34 | + |
| 35 | +### 1. Set up custom Angular builder |
| 36 | + |
| 37 | +In your `angular.json`, replace the default builder (`@angular-devkit/build-angular`) with `@angular-builders/custom-webpack`: |
| 38 | + |
| 39 | +```javascript {filename:angular.json} |
| 40 | +{ |
| 41 | + "projects": { |
| 42 | + "my-project": { |
| 43 | + "architect": { |
| 44 | + "build": { |
| 45 | + "builder": "@angular-builders/custom-webpack:browser", |
| 46 | + "options": { |
| 47 | + "customWebpackConfig": { |
| 48 | + "path": "./webpack.config.js" |
| 49 | + }, |
| 50 | + // ... other options |
| 51 | + }, |
| 52 | + // ... other options |
| 53 | + }, |
| 54 | + // ... other options |
| 55 | + }, |
| 56 | + // ... other options |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +<Alert> |
| 63 | + |
| 64 | +This configuration has no effect on your development server if you're running `ng serve`. |
| 65 | +Only `ng build` is configured to use the custom builder and the Webpack plugin. |
| 66 | +If you're using an Angular version below 13, only `ng build --prod` will leverage the custom builder. |
| 67 | + |
| 68 | +</Alert> |
| 69 | + |
| 70 | +### 2. Register the Sentry Webpack Plugin |
| 71 | + |
| 72 | +Register the Sentry Webpack plugin in your `webpack.config.js`: |
| 73 | + |
| 74 | +```javascript {filename:webpack.config.js} |
| 75 | +const SentryWebpackPlugin = require("@sentry/webpack-plugin"); |
| 76 | + |
| 77 | +module.exports = { |
| 78 | + // ... other config above ... |
| 79 | + |
| 80 | + devtool: "source-map", // Source map generation must be turned on |
| 81 | + plugins: [ |
| 82 | + new SentryWebpackPlugin({ |
| 83 | + org: "___ORG_SLUG___", |
| 84 | + project: "___PROJECT_SLUG___", |
| 85 | + |
| 86 | + // Auth tokens can be obtained from https://sentry.io/settings/account/api/auth-tokens/ |
| 87 | + // and need the `project:releases` and `org:read` scopes |
| 88 | + authToken: process.env.SENTRY_AUTH_TOKEN, |
| 89 | + |
| 90 | + // Specify the directory containing build artifacts |
| 91 | + // By default, Angular emits build files in the `dist` directory but your |
| 92 | + // configuration may vary |
| 93 | + include: "./dist/my-project", |
| 94 | + |
| 95 | + // Optionally uncomment the line below to override automatic release name detection |
| 96 | + // release: process.env.RELEASE, |
| 97 | + }), |
| 98 | + ], |
| 99 | +}; |
| 100 | +``` |
| 101 | + |
| 102 | +Learn more about configuring the plugin in our [Sentry Webpack Plugin documentation](https://github.com/getsentry/sentry-webpack-plugin). |
| 103 | + |
| 104 | +### 3. Upload |
| 105 | + |
| 106 | +To upload the source maps, build your Angular application: |
| 107 | + |
| 108 | +```bash |
| 109 | +ng build # add --prod for Angular versions below 13 |
| 110 | +``` |
| 111 | + |
| 112 | +### Releases |
| 113 | + |
| 114 | +The Sentry Webpack plugin will automatically inject a release value into the SDK, so you should either omit the `release` option from `Sentry.init` or make sure `Sentry.init`'s `release` option matches the plugin's `release` option exactly: |
| 115 | + |
| 116 | +```javascript |
| 117 | +Sentry.init({ |
| 118 | + dsn: "___PUBLIC_DSN___", |
| 119 | + |
| 120 | + // When using the plugin, either remove the `release`` property here entirely or |
| 121 | + // make sure that the plugin's release option and the Sentry.init()'s release |
| 122 | + // option match exactly. |
| 123 | + // release: "my-example-release-1" |
| 124 | +}); |
| 125 | +``` |
0 commit comments