Last active
March 27, 2019 06:12
-
-
Save nuc/ed956a040b89764c9c3944f4e5a52e15 to your computer and use it in GitHub Desktop.
Upload sourcemaps to Sentry
This file contains hidden or 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
/* eslint-disable no-sync */ | |
const request = require('superagent') | |
const path = require('path') | |
const fs = require('fs') | |
const ACCOUNT = 'account' | |
const PROJECT = 'project-name' | |
const TOKEN = 'your-token' | |
const ASSET_URL = 'https://example.com/assets' | |
const SOURCEMAPS_PATH = './temp' | |
const LOCAL_ASSET_PATH = './dist/static' | |
const currentVersion = () => { | |
const pf = path.resolve(__dirname, './package.json') | |
const { version } = JSON.parse(fs.readFileSync(pf, 'utf8')) | |
return version | |
} | |
const getSourceMapFileName = () => { | |
const pf = path.resolve(__dirname, `${LOCAL_ASSET_PATH}/stats.json`) | |
const { | |
assetsByChunkName: { main } | |
} = JSON.parse(fs.readFileSync(pf, 'utf8')) | |
const jsSourceMaps = main.filter(filename => /\.js\.map/.test(filename)) | |
return jsSourceMaps[0] | |
} | |
const uploadSourceMap = (filename, version) => { | |
console.log(`Uploading sourcemap: ${filename} for ${version}`) | |
const sourceMapPath = path.resolve( | |
__dirname, `${SOURCEMAPS_PATH}/${filename}` | |
) | |
request | |
.post( | |
`https://app.getsentry.com/api/0/projects/${ACCOUNT}/${PROJECT}/releases/${version}/files/` //eslint-disable-line max-len | |
) | |
.set({ | |
'Accept': 'application/json', | |
'Authorization': `Bearer ${TOKEN}`, | |
'Content-Type': 'application/json' | |
}) | |
.attach('file', sourceMapPath) | |
.field('name', `${ASSET_URL}/${filename}`) | |
.end((err, res) => { | |
if (err) { | |
console.error(`Error uploading sourcemap: ${err}`) | |
} else { | |
switch (res.statusCode) { | |
case 201: // created | |
console.log('Sourcemap uploaded!') | |
break | |
default: | |
console.error(`Error uploading sourcemap: ${res.statusCode}`) | |
break | |
} | |
} | |
}) | |
} | |
const createRelease = (version, filename) => { | |
console.log(`Creating release '${version}'...`) | |
request | |
.post( | |
`https://app.getsentry.com/api/0/projects/${ACCOUNT}/${PROJECT}/releases/` //eslint-disable-line max-len | |
) | |
.set({ | |
'Accept': 'application/json', | |
'Authorization': `Bearer ${TOKEN}`, | |
'Content-Type': 'application/json' | |
}) | |
.send({ version }) | |
.end((err, res) => { | |
if (err) { | |
console.error(`Error creating release ${err}`) | |
} else { | |
switch (res.statusCode) { | |
case 400: // already exists | |
case 200: // all good | |
console.log('Release already exists.') | |
uploadSourceMap(filename, version) | |
break | |
case 201: // created | |
console.log('Release created') | |
uploadSourceMap(filename, version) | |
break | |
default: | |
console.error(`Error creating release: ${res.statusCode}`) | |
break | |
} | |
} | |
}) | |
} | |
const version = currentVersion() | |
const filename = getSourceMapFileName() | |
createRelease(version, filename) |
This file contains hidden or 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
const StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin | |
module.exports = { | |
devtool: 'source-map', | |
output: { | |
path: path.join(__dirname, 'dist/static'), | |
filename: '[name].[hash].js', | |
sourceMapFilename: '../temp/[file].map', | |
publicPath: '/assets/' | |
}, | |
plugins: [ | |
new StatsWriterPlugin({ | |
filename: 'stats.json' // Default | |
}), | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks.