Skip to content

Commit 396dc33

Browse files
konraddysputKonrad Dysputperf2711
authored
react-native: simplify source map processing flow (#333)
* react-native: Simplify react-native source map injection flow * react-native: Flow adjustements & Code review * react-native: Demo source map description * react-native: Bring standard serialization method, formatting and fix environment variable read * react-native: override correctly path to the debug id file in the source map upload script * react-native: move upload script to the react-native directory and process source map via node, not via jq * react-native: use basename of the script Co-authored-by: Sebastian Alex <sebapotok@gmail.com> * react-native: uplaod script- remove shebang from addDebugIdToSourceMap * react-native: move scripts into the script directory and adjust paths to them in readme/xcode --------- Co-authored-by: Konrad Dysput <konrad.dysput@saucelabs.com> Co-authored-by: Sebastian Alex <sebapotok@gmail.com>
1 parent 0226393 commit 396dc33

File tree

13 files changed

+298
-168
lines changed

13 files changed

+298
-168
lines changed

examples/sdk/reactNative/README.md

+43-32
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,64 @@ This example app shows features available in the @backtrace/react-native package
88
2. `npm install`. If you're on iOS, navigate to the `ios` directory and run `pod install`
99
3. `npm run start` and pick desired platform
1010

11-
#### Source maps
11+
### Source maps
12+
13+
This example application is integrated with the source map support. Once you change the .backtracejsrc file, source maps will be automatically uploaded to your project.
1214

1315
Before executing any step:
1416

15-
- Please update .backtracejsrc file with your symbols submission URL and your sourcemap settings.
17+
> Please update .backtracejsrc file with your symbols submission URL and your sourcemap settings.
1618
17-
On Android: You can verify our example app with the source map support. In order to do that, please use the
18-
android-sourcemap.sh script.
19+
Backtrace is compatible with 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`.
1920

20-
```bash
21-
./android-sourcemap.sh ./optional-path-to-directory
2221
```
22+
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
23+
const backtraceSourceMapProcessor = require('@backtrace/react-native/scripts/processSourceMap');
2324
24-
The script will prepare a release APK version of your React Native application with the sourcemap and Hermes support.
25-
The APK can be found in the ./android/app/build/outputs/apk/release/ directory.
25+
const config = {
26+
serializer: {
27+
customSerializer: backtraceSourceMapProcessor.processSourceMap
28+
},
29+
};
30+
module.exports = mergeConfig(getDefaultConfig(__dirname), config);
2631
27-
On iOS: Backtrace simplify the flow needed to upload source maps by instructing hermesc how to generate and prepare
28-
source maps for your application. Without this additional step, react-native will not instruct your application to
29-
support Backtrace source maps.
32+
```
3033

31-
In order to prepare your application for source maps and automatically upload them to Backtrace, modify your "Build
32-
Phase" with the following code:
34+
Add Backtrace to build automation to ensure every build has source map support.
3335

34-
```bash
35-
set -e
36+
In order to upload source maps to Backtrace, you can:
3637

37-
# destination source map directory
38-
SOURCE_MAP_DIR="$(pwd)/../build"
39-
mkdir -p $SOURCE_MAP_DIR
38+
**On Android:**
4039

41-
export SOURCEMAP_FILE="$SOURCE_MAP_DIR/main.js.map";
42-
WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
43-
REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"
40+
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.
4441

45-
# use hermesc script provided by Backtrace to populate source maps
46-
# if you dont use hermes support, please skip this step.
47-
export HERMES_CLI_PATH="$(pwd)/../ios-hermesc.sh"
42+
`apply from: "$rootDir/../node_modules/@backtrace/react-native/android/upload-sourcemaps.gradle"`
4843

49-
/bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"
44+
Once you import the gradle task, you can simply add it to your flow for any build/assemble tasks.
5045

51-
# copy javascript build output to the build directory
52-
cp "$CONFIGURATION_BUILD_DIR/main.jsbundle" $SOURCE_MAP_DIR
46+
```gradle
47+
tasks.matching {
48+
it.name.startsWith("assemble") || it.name.startsWith("build")
49+
}.configureEach { task ->
50+
task.finalizedBy("uploadSourceMapsToBacktrace")
51+
}
52+
```
5353

54-
# process source map with javascript code
55-
npx --yes @backtrace/javascript-cli run --config "$(pwd)/../.backtracejsrc" --path "$SOURCE_MAP_DIR/main.jsbundle"
54+
**On iOS.**
5655

57-
```
56+
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.
5857

59-
Note: this modification copy the output of the javascript build into the build directory created in your application
60-
folder. Please also put ios-hermesc.
58+
```bash
59+
project_directory="$(pwd)/.."
60+
# enable source map support
61+
export SOURCEMAP_FILE="$project_directory/main.jsbundle.map"
62+
63+
...
64+
65+
# upload source maps to Backtrace
66+
source_map_upload="$project_directory/node_modules/@backtrace/react-native/scripts/ios-sourcemap-upload.sh"
67+
backtrace_js_config="$project_directory/.backtracejsrc"
68+
69+
/bin/sh -c "$source_map_upload $SOURCEMAP_FILE $TARGET_BUILD_DIR/.backtrace-sourcemap-id $backtrace_js_config $project_directory"
70+
71+
```

examples/sdk/reactNative/android-sourcemap.sh

-66
This file was deleted.

examples/sdk/reactNative/android/app/build.gradle

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
apply plugin: "com.android.application"
22
apply plugin: "com.facebook.react"
3+
apply from: "$rootDir/../node_modules/@backtrace/react-native/android/upload-sourcemaps.gradle"
4+
35

46
/**
57
* This is the configuration block to customize your React Native Android app.
@@ -47,7 +49,7 @@ react {
4749
// hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
4850
//
4951
// The list of flags to pass to the Hermes compiler. By default is "-O", "-output-source-map"
50-
// hermesFlags = ["-O", "-output-source-map"]
52+
hermesFlags = ["-O", "-output-source-map"]
5153

5254
/* Autolinking */
5355
autolinkLibrariesWithApp()
@@ -116,3 +118,9 @@ dependencies {
116118
implementation jscFlavor
117119
}
118120
}
121+
122+
tasks.matching {
123+
it.name.startsWith("assemble") || it.name.startsWith("build")
124+
}.configureEach { task ->
125+
task.finalizedBy("uploadSourceMapsToBacktrace")
126+
}

examples/sdk/reactNative/ios-hermesc.sh

-28
This file was deleted.

examples/sdk/reactNative/ios-sourcemaps.sh

-32
This file was deleted.

examples/sdk/reactNative/ios/reactNative.xcodeproj/project.pbxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
);
270270
runOnlyForDeploymentPostprocessing = 0;
271271
shellPath = /bin/sh;
272-
shellScript = "set -e\nset -x\n# destination source map directory\nSOURCE_MAP_DIR=\"$(pwd)/../build\"\nmkdir -p $SOURCE_MAP_DIR\n\nexport SOURCEMAP_FILE=\"$SOURCE_MAP_DIR/main.js.map\";\nWITH_ENVIRONMENT=\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\"\nREACT_NATIVE_XCODE=\"$REACT_NATIVE_PATH/scripts/react-native-xcode.sh\"\n\n# use hermesc script provided by Backtrace to populate source maps\n# if you dont use hermes support, please skip this step.\nexport HERMES_CLI_PATH=\"$(pwd)/../ios-hermesc.sh\"\n\n/bin/sh -c \"$WITH_ENVIRONMENT $REACT_NATIVE_XCODE\"\n\n# copy javascript build output to the build directory\ncp \"$CONFIGURATION_BUILD_DIR/main.jsbundle\" $SOURCE_MAP_DIR \n\nPROCESS_SOURCEMAPS_SCRIPT=\"$(pwd)/../ios-sourcemaps.sh\"\nexport BACKTRACE_JS_CONFIG=\"$(pwd)/../.backtracejsrc\"\nexport BACKTRACE_JS_BUNDLE_PATH=\"$SOURCE_MAP_DIR/main.jsbundle\"\n \n# process source map with javascript code\n/bin/sh -c \"$WITH_ENVIRONMENT $PROCESS_SOURCEMAPS_SCRIPT\"\n";
272+
shellScript = "set -e\nproject_directory=\"$(pwd)/..\"\n# enable source map support\nexport SOURCEMAP_FILE=\"$project_directory/main.jsbundle.map\"\n\nWITH_ENVIRONMENT=\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\"\nREACT_NATIVE_XCODE=\"$REACT_NATIVE_PATH/scripts/react-native-xcode.sh\"\n\n/bin/sh -c \"$WITH_ENVIRONMENT $REACT_NATIVE_XCODE\"\n\n# upload source maps to Backtrace\nsource_map_upload=\"$project_directory/node_modules/@backtrace/react-native/scripts/ios-sourcemap-upload.sh\"\nbacktrace_js_config=\"$project_directory/.backtracejsrc\"\n\n/bin/sh -c \"$source_map_upload $SOURCEMAP_FILE $TARGET_BUILD_DIR/.backtrace-sourcemap-id $backtrace_js_config $project_directory\" \n\n";
273273
};
274274
00EEFC60759A1932668264C0 /* [CP] Embed Pods Frameworks */ = {
275275
isa = PBXShellScriptBuildPhase;
+11-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
1+
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
22
const path = require('path');
3+
const backtraceSourceMapProcessor = require('@backtrace/react-native/scripts/processSourceMap');
34

45
/**
56
* Metro configuration
@@ -8,12 +9,14 @@ const path = require('path');
89
* @type {import('metro-config').MetroConfig}
910
*/
1011
const config = {
11-
watchFolders: [
12-
path.resolve('../../../packages/react-native'),
13-
path.resolve('../../../packages/react-native/node_modules'),
14-
path.resolve('../../../node_modules'),
15-
path.resolve('../../../packages/sdk-core'),
16-
],
12+
watchFolders: [
13+
path.resolve('../../../packages/react-native'),
14+
path.resolve('../../../packages/react-native/node_modules'),
15+
path.resolve('../../../node_modules'),
16+
path.resolve('../../../packages/sdk-core'),
17+
],
18+
serializer: {
19+
customSerializer: backtraceSourceMapProcessor.processSourceMap,
20+
},
1721
};
18-
1922
module.exports = mergeConfig(getDefaultConfig(__dirname), config);

examples/sdk/reactNative/package-lock.json

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/sdk/reactNative/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@babel/preset-env": "^7.20.0",
1919
"@babel/runtime": "^7.20.0",
2020
"@backtrace/javascript-cli": "file:../../../tools/cli",
21+
"@backtrace/sourcemap-tools": "file:../../../tools/sourcemap-tools",
2122
"@react-native/babel-preset": "0.75.3",
2223
"@react-native/eslint-config": "0.75.3",
2324
"@react-native/metro-config": "0.75.3",

0 commit comments

Comments
 (0)