diff options
author | Steve Klebanoff <steve@0xproject.com> | 2018-12-04 06:24:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-04 06:24:58 +0800 |
commit | 50df67e7511460f051f91785bb4384485077ef60 (patch) | |
tree | 3caa4fac28a2af99e7e7d0beff7a661d26f8665e /packages/instant/webpack.config.js | |
parent | f076cdb8329693e9d7351ec88d2ac01ad42a2a3a (diff) | |
parent | 91b0fd951708b0fff0733c2e1eed45a403f3cc73 (diff) | |
download | dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.tar dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.tar.gz dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.tar.bz2 dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.tar.lz dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.tar.xz dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.tar.zst dexon-sol-tools-50df67e7511460f051f91785bb4384485077ef60.zip |
Merge pull request #1295 from 0xProject/feature/instant/rollbar-env
[instant] Rollbar
Diffstat (limited to 'packages/instant/webpack.config.js')
-rw-r--r-- | packages/instant/webpack.config.js | 108 |
1 files changed, 93 insertions, 15 deletions
diff --git a/packages/instant/webpack.config.js b/packages/instant/webpack.config.js index 803240e76..a1db01db9 100644 --- a/packages/instant/webpack.config.js +++ b/packages/instant/webpack.config.js @@ -1,10 +1,16 @@ const childProcess = require('child_process'); const ip = require('ip'); const path = require('path'); +const RollbarSourceMapPlugin = require('rollbar-sourcemap-webpack-plugin'); const webpack = require('webpack'); +const GIT_SHA = childProcess + .execSync('git rev-parse HEAD') + .toString() + .trim(); + const DISCHARGE_TARGETS_THAT_REQUIRED_HEAP = ['production', 'staging', 'dogfood']; -const getConfigForDischargeTarget = dischargeTarget => { +const getHeapConfigForDischargeTarget = dischargeTarget => { return { heapAnalyticsIdEnvName: dischargeTarget === 'production' @@ -14,14 +20,51 @@ const getConfigForDischargeTarget = dischargeTarget => { }; }; -const GIT_SHA = childProcess - .execSync('git rev-parse HEAD') - .toString() - .trim(); -const generateConfig = (dischargeTarget, configOptions) => { +const DISCHARGE_TARGETS_THAT_REQUIRE_ROLLBAR = ['production', 'staging', 'dogfood']; +const getRollbarConfigForDischargeTarget = dischargeTarget => { + if (DISCHARGE_TARGETS_THAT_REQUIRE_ROLLBAR.includes(dischargeTarget)) { + const rollbarSourceMapPublicPath = + dischargeTarget === 'production' + ? 'https://instant.0xproject.com' + : `http://0x-instant-${dischargeTarget}.s3-website-us-east-1.amazonaws.com`; + + return { + rollbarSourceMapPublicPath, + rollbarRequired: true, + }; + } + + return { + rollbarRequired: false, + }; +}; + +const ROLLBAR_CLIENT_TOKEN_ENV_VAR_NAME = 'INSTANT_ROLLBAR_CLIENT_TOKEN'; +const ROLLBAR_PUBLISH_TOKEN_ENV_VAR_NAME = 'INSTANT_ROLLBAR_PUBLISH_TOKEN'; +const getRollbarTokens = (dischargeTarget, rollbarRequired) => { + const clientToken = process.env[ROLLBAR_CLIENT_TOKEN_ENV_VAR_NAME]; + const publishToken = process.env[ROLLBAR_PUBLISH_TOKEN_ENV_VAR_NAME]; + + if (rollbarRequired) { + if (!clientToken) { + throw new Error( + `Rollbar client token required for ${dischargeTarget}, please set env var ${ROLLBAR_CLIENT_TOKEN_ENV_VAR_NAME}`, + ); + } + if (!publishToken) { + throw new Error( + `Rollbar publish token required for ${dischargeTarget}, please set env var ${ROLLBAR_PUBLISH_TOKEN_ENV_VAR_NAME}`, + ); + } + } + + return { clientToken, publishToken }; +}; + +const generateConfig = (dischargeTarget, heapConfigOptions, rollbarConfigOptions, nodeEnv) => { const outputPath = process.env.WEBPACK_OUTPUT_PATH || 'umd'; - const { heapAnalyticsIdEnvName, heapAnalyticsIdRequired } = configOptions; + const { heapAnalyticsIdEnvName, heapAnalyticsIdRequired } = heapConfigOptions; const heapAnalyticsId = process.env[heapAnalyticsIdEnvName]; if (heapAnalyticsIdRequired && !heapAnalyticsId) { throw new Error( @@ -29,9 +72,26 @@ const generateConfig = (dischargeTarget, configOptions) => { ); } + const rollbarTokens = getRollbarTokens(dischargeTarget, rollbarConfigOptions.rollbarRequired); + const rollbarEnabled = + rollbarTokens.clientToken && (nodeEnv !== 'development' || process.env.INSTANT_ROLLBAR_FORCE_DEVELOPMENT); + + let rollbarPlugin; + if (rollbarConfigOptions.rollbarRequired) { + if (!rollbarEnabled || !rollbarTokens.publishToken || !rollbarConfigOptions.rollbarSourceMapPublicPath) { + throw new Error(`Rollbar required for ${dischargeTarget} but not configured`); + } + rollbarPlugin = new RollbarSourceMapPlugin({ + accessToken: rollbarTokens.publishToken, + version: GIT_SHA, + publicPath: rollbarConfigOptions.rollbarSourceMapPublicPath, + }); + } + const envVars = { GIT_SHA: JSON.stringify(GIT_SHA), NPM_PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version), + ROLLBAR_ENABLED: rollbarEnabled, }; if (dischargeTarget) { envVars.INSTANT_DISCHARGE_TARGET = JSON.stringify(dischargeTarget); @@ -39,6 +99,18 @@ const generateConfig = (dischargeTarget, configOptions) => { if (heapAnalyticsId) { envVars.HEAP_ANALYTICS_ID = JSON.stringify(heapAnalyticsId); } + if (rollbarTokens.clientToken) { + envVars.ROLLBAR_CLIENT_TOKEN = JSON.stringify(rollbarTokens.clientToken); + } + + const plugins = [ + new webpack.DefinePlugin({ + 'process.env': envVars, + }), + ]; + if (rollbarPlugin) { + plugins.push(rollbarPlugin); + } const config = { entry: { @@ -50,11 +122,7 @@ const generateConfig = (dischargeTarget, configOptions) => { library: 'zeroExInstant', libraryTarget: 'umd', }, - plugins: [ - new webpack.DefinePlugin({ - 'process.env': envVars, - }), - ], + plugins, devtool: 'source-map', resolve: { extensions: ['.js', '.json', '.ts', '.tsx'], @@ -69,6 +137,15 @@ const generateConfig = (dischargeTarget, configOptions) => { test: /\.svg$/, loader: 'svg-react-loader', }, + { + test: /\.js$/, + loader: 'source-map-loader', + exclude: [ + // instead of /\/node_modules\// + path.join(process.cwd(), 'node_modules'), + path.join(process.cwd(), '../..', 'node_modules'), + ], + }, ], }, devServer: { @@ -89,8 +166,9 @@ const generateConfig = (dischargeTarget, configOptions) => { return config; }; -module.exports = (env, _argv) => { +module.exports = (env, argv) => { const dischargeTarget = env ? env.discharge_target : undefined; - const configOptions = getConfigForDischargeTarget(dischargeTarget); - return generateConfig(dischargeTarget, configOptions); + const heapConfigOptions = getHeapConfigForDischargeTarget(dischargeTarget); + const rollbarConfigOptions = getRollbarConfigForDischargeTarget(dischargeTarget); + return generateConfig(dischargeTarget, heapConfigOptions, rollbarConfigOptions, argv.mode); }; |