Created
May 29, 2020 17:18
-
-
Save ShuvoHabib/2e1db313672d3fbba168910a9e15c714 to your computer and use it in GitHub Desktop.
Bugsnag integration with React.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const { upload } = require('bugsnag-sourcemaps'); | |
const glob = require('glob'); | |
const fs = require('fs'); | |
const reportBuild = require('bugsnag-build-reporter'); | |
function findSourceMaps(callback) { | |
return glob('build/**/*/*.map', callback); | |
} | |
/** | |
* Uploads the source map with accompanying sources | |
* @param sourceMap - single .map file | |
* @returns {Promise<string>} - upload to Bugsnag | |
*/ | |
function uploadSourceMap(sourceMap) { | |
// Remove .map from the file to get the js filename | |
const minifiedFile = sourceMap.replace('.map', ''); | |
// Remove absolute path to the static assets folder | |
const minifiedFileRelativePath = minifiedFile.split('build/')[1]; | |
return upload({ | |
apiKey: process.env.REACT_APP_BUGSNAG_KEY, | |
appVersion: process.env.REACT_APP_PWA_VERSION, | |
overwrite: true, | |
minifiedUrl: `http*://${process.env.REACT_APP_SITE_HOSTNAME}/${minifiedFileRelativePath}`, | |
sourceMap, | |
minifiedFile, | |
projectRoot: __dirname, | |
uploadSources: true, | |
}); | |
} | |
/** | |
* Delete the .map files | |
* We do this to protect our source | |
* @param files - array of sourcemap files | |
*/ | |
function deleteFiles(files) { | |
files.forEach(file => { | |
const path = `${__dirname}/${file}`; | |
fs.unlinkSync(path); | |
}); | |
} | |
/** | |
* Notifies Bugsnag of the new release | |
*/ | |
function notifyRelease() { | |
reportBuild({ | |
apiKey: process.env.REACT_APP_BUGSNAG_KEY, | |
appVersion: process.env.REACT_APP_PWA_VERSION, | |
releaseStage: process.env.REACT_APP_ENVIRONMENT, | |
}) | |
.then(() => console.log('Bugsnag build reported')) | |
.catch(err => console.log('Reporting Bugsnag build failed', err.messsage)); | |
} | |
/** | |
* Find, upload and delete Source Maps | |
*/ | |
function processSourceMaps() { | |
findSourceMaps((error, files) => | |
Promise.all(files.map(uploadSourceMap)) | |
.then(() => { | |
deleteFiles(files); | |
notifyRelease(); | |
}) | |
.catch(e => { | |
console.log(e); | |
}) | |
); | |
} | |
processSourceMaps(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment