From bd03df8af02169ef88207eb14051bc79231927aa Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 29 Nov 2018 12:05:20 -0800 Subject: feat(instant): Cleaner config-specific setup --- packages/instant/.dogfood.discharge.json | 2 +- packages/instant/.production.discharge.json | 2 +- packages/instant/.staging.discharge.json | 2 +- packages/instant/package.json | 7 +-- packages/instant/src/constants.ts | 6 +++ packages/instant/webpack.config.js | 73 ++++++++++++++++++++++------- 6 files changed, 69 insertions(+), 23 deletions(-) (limited to 'packages') diff --git a/packages/instant/.dogfood.discharge.json b/packages/instant/.dogfood.discharge.json index 6e669ebaa..5a11f8a1d 100644 --- a/packages/instant/.dogfood.discharge.json +++ b/packages/instant/.dogfood.discharge.json @@ -1,6 +1,6 @@ { "domain": "0x-instant-dogfood", - "build_command": "WEBPACK_OUTPUT_PATH=public yarn build --env.dogfood", + "build_command": "WEBPACK_OUTPUT_PATH=public yarn build:prod --env.dogfood", "upload_directory": "public", "index_key": "index.html", "error_key": "index.html", diff --git a/packages/instant/.production.discharge.json b/packages/instant/.production.discharge.json index b2369d43e..ecd3a12d4 100644 --- a/packages/instant/.production.discharge.json +++ b/packages/instant/.production.discharge.json @@ -1,6 +1,6 @@ { "domain": "instant.0xproject.com", - "build_command": "yarn build --env.production_cdn", + "build_command": "yarn build:prod --env.production_cdn", "upload_directory": "umd", "index_key": "instant.js", "error_key": "404.html", diff --git a/packages/instant/.staging.discharge.json b/packages/instant/.staging.discharge.json index 6df436f73..ba2ca5f99 100644 --- a/packages/instant/.staging.discharge.json +++ b/packages/instant/.staging.discharge.json @@ -1,6 +1,6 @@ { "domain": "0x-instant-staging", - "build_command": "WEBPACK_OUTPUT_PATH=public yarn build:prod --env.staging", + "build_command": "WEBPACK_OUTPUT_PATH=public yarn build:prod:prod --env.staging", "upload_directory": "public", "index_key": "index.html", "error_key": "index.html", diff --git a/packages/instant/package.json b/packages/instant/package.json index bc46aa732..964e5c9b3 100644 --- a/packages/instant/package.json +++ b/packages/instant/package.json @@ -8,10 +8,11 @@ "main": "umd/instant.js", "private": true, "scripts": { - "build": "webpack --mode production --env.production_standalone", - "build:ci": "yarn build", + "build": "yarn build:prod --env.production_standalone", + "build:prod": "webpack --mode production", + "build:ci": "build", "watch_without_deps": "tsc -w", - "dev": "webpack-dev-server --mode development", + "dev": "webpack-dev-server --mode development --env.development", "lint": "tslint --format stylish --project .", "test": "jest", "test:coverage": "jest --coverage", diff --git a/packages/instant/src/constants.ts b/packages/instant/src/constants.ts index be6077ca9..e70b3f1d8 100644 --- a/packages/instant/src/constants.ts +++ b/packages/instant/src/constants.ts @@ -20,6 +20,12 @@ export const HEAP_ANALYTICS_ID = process.env.HEAP_ANALYTICS_ID; export const COINBASE_API_BASE_URL = 'https://api.coinbase.com/v2'; export const PROGRESS_STALL_AT_WIDTH = '95%'; export const PROGRESS_FINISH_ANIMATION_TIME_MS = 200; +export const INSTANT_ENVIRONMENT = process.env.INSTANT_ENVIRONMENT as + | 'dogfood' + | 'staging' + | 'development' + | 'production_cdn' + | 'production_standalone'; export const COINBASE_WALLET_IOS_APP_STORE_URL = 'https://itunes.apple.com/us/app/coinbase-wallet/id1278383455?mt=8'; export const COINBASE_WALLET_ANDROID_APP_STORE_URL = 'https://play.google.com/store/apps/details?id=org.toshi&hl=en'; export const COINBASE_WALLET_SITE_URL = 'https://wallet.coinbase.com/'; diff --git a/packages/instant/webpack.config.js b/packages/instant/webpack.config.js index 41276809c..284276c1a 100644 --- a/packages/instant/webpack.config.js +++ b/packages/instant/webpack.config.js @@ -6,27 +6,64 @@ const webpack = require('webpack'); // The common js bundle (not this one) is built using tsc. // The umd bundle (this one) has a different entrypoint. +const ACCEPTABLE_ENV_NAMES = ['production_standalone', 'production_cdn', 'staging', 'dogfood', 'development']; +const getEnvironmentName = env => { + if (!env) { + throw new Error('Please specify env via --env to webpack'); + } + const foundName = ACCEPTABLE_ENV_NAMES.find(e => (env[e] ? e : false)); + if (!foundName) { + throw new Error( + `Couldn't find env name, please specify via one of the following CLI arguments: ${acceptableEnvNames.map( + i => `--env.${i}`, + )}`, + ); + } + return foundName; +}; + +const getConfigForEnv = environmentName => { + switch (environmentName) { + case 'production_standalone': + case 'production_cdn': + return { + heapAnalyticsIdEnvName: 'INSTANT_HEAP_ANALYTICS_ID_PRODUCTION', + heapAnalyticsIdRequired: environmentName !== 'production_standalone', + }; + case 'staging': + case 'dogfood': + case 'development': + return { + heapAnalyticsIdEnvName: 'INSTANT_HEAP_ANALYTICS_ID_DEVELOPMENT', + heapAnalyticsIdRequired: environmentName !== 'development', + }; + } +}; + const GIT_SHA = childProcess .execSync('git rev-parse HEAD') .toString() .trim(); +const generateConfig = (environmentName, configOptions) => { + const outputPath = process.env.WEBPACK_OUTPUT_PATH || 'umd'; -const HEAP_PRODUCTION_ENV_VAR_NAME = 'INSTANT_HEAP_ANALYTICS_ID_PRODUCTION'; -const HEAP_DEVELOPMENT_ENV_VAR_NAME = 'INSTANT_HEAP_ANALYTICS_ID_DEVELOPMENT'; -const getHeapAnalyticsId = modeName => { - if (modeName === 'production') { - return process.env[HEAP_PRODUCTION_ENV_VAR_NAME]; + const { heapAnalyticsIdEnvName, heapAnalyticsIdRequired } = configOptions; + const heapAnalyticsId = process.env[heapAnalyticsIdEnvName]; + if (heapAnalyticsIdRequired && !heapAnalyticsId) { + throw new Error( + `Must define heap analytics id in ENV var ${heapAnalyticsIdEnvName} when building for ${environmentName}`, + ); } - if (modeName === 'development') { - return process.env[HEAP_DEVELOPMENT_ENV_VAR_NAME]; + const envVars = { + GIT_SHA: JSON.stringify(GIT_SHA), + INSTANT_ENVIRONMENT: JSON.stringify(environmentName), + NPM_PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version), + }; + if (heapAnalyticsId) { + envVars.HEAP_ANALYTICS_ID = JSON.stringify(heapAnalyticsId); } - return undefined; -}; - -module.exports = (env, argv) => { - const outputPath = process.env.WEBPACK_OUTPUT_PATH || 'umd'; const config = { entry: { instant: './src/index.umd.ts', @@ -39,11 +76,7 @@ module.exports = (env, argv) => { }, plugins: [ new webpack.DefinePlugin({ - 'process.env': { - GIT_SHA: JSON.stringify(GIT_SHA), - HEAP_ANALYTICS_ID: getHeapAnalyticsId(argv.mode), - NPM_PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version), - }, + 'process.env': envVars, }), ], devtool: 'source-map', @@ -79,3 +112,9 @@ module.exports = (env, argv) => { }; return config; }; + +module.exports = (env, _argv) => { + const environmentName = getEnvironmentName(env); + const configOptions = getConfigForEnv(environmentName); + return generateConfig(environmentName, configOptions); +}; -- cgit v1.2.3