After spending hours on looking for an example or quick copy&paste solution I had no other option as to write it myself. So here is how I achieved this, hope this helps someone time.
This is my first shell script for Mac, so it's not perfect for sure. I guess it's likely need to be adjusted for usage on other platforms, but it's a good start anyway.
- Create a folder called "sourceMaps" in a root folder
- Add a new script to your package.json and modify build script
"build": "REACT_APP_GIT_SHA=`git rev-parse HEAD` react-scripts build && npm run upload-source-maps",
"upload-source-maps": "rm sourceMaps/* && mv build/static/js/*.map sourceMaps/ && sh ./upload-script.sh"
This script does a few things:
a) it cleans up source maps from previous build
b) moves newly generated source maps from build/static/js/ to sourceMaps/ folder - so that they are not deployed publicly
c) it calls ./upload-script.sh script that does all the work
- Now create a file called "upload-script.sh" in your root folder with following code
version=$(git rev-parse HEAD)
for filename in ./sourceMaps/*; do
sliced=${filename//.\/sourceMaps/""}
without_map=${sliced//.map/""}
minified_url=//YOUR_DOMAIN/static/js$without_map
source_map=@${filename//.\//""}
curl https://api.rollbar.com/api/1/sourcemap \
-F access_token=YOUR_TOKEN \
-F version="$version" \
-F minified_url=$minified_url \
-F source_map="$source_map"
done
this script does next things:
a) takes latest git commit as version (so that rollbar can understand which source map version it needs
b) iterates over each source map file in your sourceMaps folder, replaces symbols we don't need to follow rollbar format
c) make curl request to rollbar api
The last thing you need to do is to set rollbar code version inside code, as you could notice i pass an variable called REACT_APP_GIT_SHA=git rev-parse HEAD
that you can access inside app with process.env.REACT_APP_GIT_SHA
Here is an example of your rollbarConfig
const rollbarConfig = {
accessToken: YOUR_ACCESS_TOKEN,
captureUncaught: true,
payload: {
environment: process.env.REACT_APP_NODE_ENV,
client: {
javascript: {
source_map_enabled: true,
code_version: process.env.REACT_APP_GIT_SHA,
}
}
}
}