Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add sourcemap in React Native for Production?

I received error log like the following while the app crashed:

Fatal Exception: com.facebook.react.modules.core.JavascriptException: onSelect index.android.bundle:20:7148 onPress index.android.bundle:20:2435

But it's not really helpful for me to trouble shoot. How could I enable source map so that I could track down where the issue is ?

UPDATE 2018 https://docs.expo.io/versions/latest/guides/using-sentry.html Looks promising !

like image 759
noooooooob Avatar asked Jan 11 '16 06:01

noooooooob


People also ask

What is Sourcemap in React Native?

Using our source map library you can upload source maps to unminify stack traces and get human-readable method names, files, and line numbers. To configure your project to upload source maps and native mappings automatically, follow the showing full stacktraces guide for React Native.

What is a Sourcemap file?

A source map is a file that maps from the transformed source to the original source, enabling the browser to reconstruct the original source and present the reconstructed original in the debugger. To enable the debugger to work with a source map, you must: generate the source map.


2 Answers

For source mapping here is the way I go about it:

In my bundle command for my production build I tell it to generate a source map:

iOS:

react-native bundle --platform ios --entry-file index.ios.js --dev false --bundle-output ./ios/main.jsbundle --assets-dest ./ios --sourcemap-output ./sourcemap.js 

Android - I had to actually modify the android/app/react.gradle file to get source maps generating on release compile. There might be an easier way but basically you find where it builds up the bundle command in the bundleReleaseJsAndAssets method and add the source map bit to it:

if (Os.isFamily(Os.FAMILY_WINDOWS)) {     commandLine "cmd","/c", "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",         entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease, "--sourcemap-output", file("$buildDir/../../../sourcemap.js") } else {     commandLine "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",         entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease, "--sourcemap-output", file("$buildDir/../../../sourcemap.js") } 

The output path looks a bit odd but that puts it at your root level (same spot as iOS. I wanted it that way. You can obviously put it anywhere).

Then once you have an error with the line number that means nothing you run it through the "source-map" NPM package. You could probably get very elaborate with your approach but I simply went with:

var sourceMap = require('source-map'); var fs = require('fs');  fs.readFile('./sourcemap.js', 'utf8', function (err, data) {     var smc = new sourceMap.SourceMapConsumer(data);      console.log(smc.originalPositionFor({         line: 16,         column: 29356     })); }); 

Where line and column should be replaced withe line and column number from your example output above.

This obviously works best if you have the source maps stored somewhere as the line and column numbers change from build to build as your code changes. It should get pretty close though if you can use you source control setup of choice to go back to the commit that was used to build the app in question and re-generate the bundle with the additional bits to the command to generate the source map.

like image 154
rmevans9 Avatar answered Oct 05 '22 12:10

rmevans9


Android inspired by @chetstone's answer

Starting on v0.32 for android, you can modify your android/app/build.gradle to accomplish this. Look for the line

apply from: "../../node_modules/react-native/react.gradle"

Just above this, you will see something like:

project.ext.react = [     entryFile: "index.js", ] 

Modify it to match the following

project.ext.react = [     entryFile: "index.js",     extraPackagerArgs: ["--sourcemap-output", file("$buildDir/../../../sourcemap.android.js")] ] 

On iOS

Go to your build phases in Xcode for the "Bundle React Native code and images" phase and add:

export EXTRA_PACKAGER_ARGS="--sourcemap-output sourcemap.ios.js" 
like image 42
otusweb Avatar answered Oct 05 '22 14:10

otusweb