Skip to content

Commit d2a3bfe

Browse files
konraddysputKonrad Dysputrick-bt
authored
Backtrace: Upload automatically source maps to Backtrace (#3075)
* Backtrace: Upload automatically source maps to Backtrace * Update docs/error-reporting/language-integrations/react-native.md Co-authored-by: Rick Foster <[email protected]> * Update docs/error-reporting/language-integrations/react-native.md Co-authored-by: Rick Foster <[email protected]> --------- Co-authored-by: Konrad Dysput <[email protected]> Co-authored-by: Rick Foster <[email protected]>
1 parent db60495 commit d2a3bfe

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/error-reporting/language-integrations/react-native.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and easy, after which you can explore the rich set of Backtrace features.
3636
- [BacktraceClient Options](#backtraceclient)
3737
- [Manually send an error](#manually-send-an-error)
3838
- [Modify/skip error reports](#modifyskip-error-reports)
39+
- [Automatically upload source maps](#automatically-upload-source-maps)
3940
- [SDK Method Overrides](#sdk-method-overrides)
4041

4142
## Basic Integration
@@ -440,6 +441,81 @@ const client = BacktraceClient.initialize({
440441
});
441442
```
442443
444+
### Automatically upload source maps
445+
446+
@Backtrace/react provides CI tooling to easily post source maps to Backtrace. Posting source maps enables Backtrace to symbolicate minified code, allowing error reports to be displayed with the original source code highlighting the correct faulting line.
447+
448+
To integrate an application source maps in react-native with Backtrace you need:
449+
- create a `.backtracejsrc` file in the main application folder,
450+
- install the `@backtrace/javascript-cli` and `@backtrace/sourcemap-tools` packages,
451+
- modify `metro.config.js` to attach debug identifier to the generated javascript code,
452+
- modify the build system in the android or xcode project.
453+
454+
#### Modifying `metro.config.js`
455+
456+
Backtrace is compatible with the metro build system. To enable source map support, set a `customSerializer` method in the `metro.config.js` file to the `processSourceMap` function available in `@backtrace/react-native/scripts/processSourceMap`.
457+
458+
```
459+
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
460+
const backtraceSourceMapProcessor = require('@backtrace/react-native/scripts/processSourceMap');
461+
462+
const config = {
463+
serializer: {
464+
customSerializer: backtraceSourceMapProcessor.processSourceMap
465+
},
466+
};
467+
module.exports = mergeConfig(getDefaultConfig(__dirname), config);
468+
469+
```
470+
471+
Add Backtrace to build automation to ensure every build has source map support.
472+
473+
In order to upload source maps to Backtrace, you can:
474+
475+
**On Android:**
476+
477+
Enable source map support in `app/build.gradle` by uncommenting hermes source map flags. With additional parameters, source maps will be generated. To automatically upload them to Backtrace, you can use the gradle task available the @backtrace/react-native library.
478+
479+
`apply from: "$rootDir/../node_modules/@backtrace/react-native/android/upload-sourcemaps.gradle"`
480+
481+
Once you import the gradle task, you can simply add it to your flow for any build/assemble tasks.
482+
483+
```gradle
484+
tasks.matching {
485+
it.name.startsWith("assemble") || it.name.startsWith("build")
486+
}.configureEach { task ->
487+
task.finalizedBy("uploadSourceMapsToBacktrace")
488+
}
489+
```
490+
491+
**On iOS.**
492+
493+
Modify the code in the `Bundle React Native code and images` step in the `Build Phases` of your xcode project setting. In the end of the script, you can include the code below, to upload source maps directly to Backtrace after generating the applicaiton.
494+
495+
```bash
496+
project_directory="$(pwd)/.."
497+
# enable source map support
498+
export SOURCEMAP_FILE="$project_directory/main.jsbundle.map"
499+
500+
...
501+
502+
# upload source maps to Backtrace
503+
source_map_upload="$project_directory/node_modules/@backtrace/react-native/scripts/ios-sourcemap-upload.sh"
504+
backtrace_js_config="$project_directory/.backtracejsrc"
505+
506+
/bin/sh -c "$source_map_upload $SOURCEMAP_FILE $TARGET_BUILD_DIR/.backtrace-sourcemap-id $backtrace_js_config $project_directory"
507+
508+
```
509+
510+
#### Advanced use cases
511+
512+
Backtrace generates `.backtrace-sourcemap-id` in the application build directory. The file contains debug-id attached to each source file and source file. The debug id file path can be modified by setting an environment variable `DEBUG_ID_PATH` to the path to the file. For example:
513+
514+
```
515+
DEBUG_ID_PATH=/path/to/backtrace/debug/id/backtrace-javascript/.debug_id
516+
```
517+
The file directory should be created before building the application.
518+
443519
### SDK Method Overrides
444520
445521
BacktraceClient.builder is used to override default BacktraceClient methods. File and http operation overrides, for

0 commit comments

Comments
 (0)