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/webpack.config.js | 73 +++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 17 deletions(-) (limited to 'packages/instant/webpack.config.js') 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 From d99bdcf036eebc6613274fecfdd19187eaf4aa1a Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 29 Nov 2018 15:13:55 -0800 Subject: cdn approach to identifyign environments --- packages/instant/webpack.config.js | 53 +++++++++++--------------------------- 1 file changed, 15 insertions(+), 38 deletions(-) (limited to 'packages/instant/webpack.config.js') diff --git a/packages/instant/webpack.config.js b/packages/instant/webpack.config.js index 284276c1a..020fab506 100644 --- a/packages/instant/webpack.config.js +++ b/packages/instant/webpack.config.js @@ -6,60 +6,37 @@ 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 CDNS_THAT_REQUIRE_HEAP = ['production', 'staging', 'dogfood']; +const getConfigForCdn = cdnName => { + return { + heapAnalyticsIdEnvName: + cdnName === 'production' ? 'INSTANT_HEAP_ANALYTICS_ID_PRODUCTION' : 'INSTANT_HEAP_ANALYTICS_ID_DEVELOPMENT', + heapAnalyticsIdRequired: CDNS_THAT_REQUIRE_HEAP.includes(cdnName), + }; }; const GIT_SHA = childProcess .execSync('git rev-parse HEAD') .toString() .trim(); -const generateConfig = (environmentName, configOptions) => { +const generateConfig = (cdnName, configOptions) => { const outputPath = process.env.WEBPACK_OUTPUT_PATH || 'umd'; 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}`, + `Must define heap analytics id in ENV var ${heapAnalyticsIdEnvName} when building for ${cdnName}`, ); } const envVars = { GIT_SHA: JSON.stringify(GIT_SHA), - INSTANT_ENVIRONMENT: JSON.stringify(environmentName), NPM_PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version), }; + if (cdnName) { + envVars.INSTANT_CDN = JSON.stringify(cdnName); + } if (heapAnalyticsId) { envVars.HEAP_ANALYTICS_ID = JSON.stringify(heapAnalyticsId); } @@ -114,7 +91,7 @@ const generateConfig = (environmentName, configOptions) => { }; module.exports = (env, _argv) => { - const environmentName = getEnvironmentName(env); - const configOptions = getConfigForEnv(environmentName); - return generateConfig(environmentName, configOptions); + const cdnName = env ? env.cdn : undefined; + const configOptions = getConfigForCdn(cdnName); + return generateConfig(cdnName, configOptions); }; -- cgit v1.2.3 From 09813cb1d83da4c571cf69a448c35c5f6af94dec Mon Sep 17 00:00:00 2001 From: fragosti Date: Fri, 30 Nov 2018 11:20:07 -0800 Subject: fix: address PR feedback --- packages/instant/webpack.config.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'packages/instant/webpack.config.js') diff --git a/packages/instant/webpack.config.js b/packages/instant/webpack.config.js index 41276809c..2a517bb59 100644 --- a/packages/instant/webpack.config.js +++ b/packages/instant/webpack.config.js @@ -3,9 +3,6 @@ const ip = require('ip'); const path = require('path'); 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 GIT_SHA = childProcess .execSync('git rev-parse HEAD') .toString() -- cgit v1.2.3 From ab631060a05fc6344ef6e2de7b0e6a0f0096e8ed Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 30 Nov 2018 12:42:40 -0800 Subject: cdn -> dischargeTarget, and report to heap --- packages/instant/webpack.config.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'packages/instant/webpack.config.js') diff --git a/packages/instant/webpack.config.js b/packages/instant/webpack.config.js index 020fab506..a6fe0535d 100644 --- a/packages/instant/webpack.config.js +++ b/packages/instant/webpack.config.js @@ -6,12 +6,14 @@ 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 CDNS_THAT_REQUIRE_HEAP = ['production', 'staging', 'dogfood']; -const getConfigForCdn = cdnName => { +const DISCHARGE_TARGETS_THAT_REQUIRED_HEAP = ['production', 'staging', 'dogfood']; +const getConfigForDischargeTarget = dischargeTarget => { return { heapAnalyticsIdEnvName: - cdnName === 'production' ? 'INSTANT_HEAP_ANALYTICS_ID_PRODUCTION' : 'INSTANT_HEAP_ANALYTICS_ID_DEVELOPMENT', - heapAnalyticsIdRequired: CDNS_THAT_REQUIRE_HEAP.includes(cdnName), + dischargeTarget === 'production' + ? 'INSTANT_HEAP_ANALYTICS_ID_PRODUCTION' + : 'INSTANT_HEAP_ANALYTICS_ID_DEVELOPMENT', + heapAnalyticsIdRequired: DISCHARGE_TARGETS_THAT_REQUIRED_HEAP.includes(dischargeTarget), }; }; @@ -19,14 +21,14 @@ const GIT_SHA = childProcess .execSync('git rev-parse HEAD') .toString() .trim(); -const generateConfig = (cdnName, configOptions) => { +const generateConfig = (dischargeTarget, configOptions) => { const outputPath = process.env.WEBPACK_OUTPUT_PATH || 'umd'; 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 ${cdnName}`, + `Must define heap analytics id in ENV var ${heapAnalyticsIdEnvName} when building for ${dischargeTarget}`, ); } @@ -34,12 +36,13 @@ const generateConfig = (cdnName, configOptions) => { GIT_SHA: JSON.stringify(GIT_SHA), NPM_PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version), }; - if (cdnName) { - envVars.INSTANT_CDN = JSON.stringify(cdnName); + if (dischargeTarget) { + envVars.INSTANT_DISCHARGE_TARGET = JSON.stringify(dischargeTarget); } if (heapAnalyticsId) { envVars.HEAP_ANALYTICS_ID = JSON.stringify(heapAnalyticsId); } + console.log(envVars); const config = { entry: { @@ -91,7 +94,7 @@ const generateConfig = (cdnName, configOptions) => { }; module.exports = (env, _argv) => { - const cdnName = env ? env.cdn : undefined; - const configOptions = getConfigForCdn(cdnName); - return generateConfig(cdnName, configOptions); + const dischargeTarget = env ? env.discharge_target : undefined; + const configOptions = getConfigForDischargeTarget(dischargeTarget); + return generateConfig(dischargeTarget, configOptions); }; -- cgit v1.2.3 From de8dcf9a72283711d41617fc2c0e849a4a8c7461 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 30 Nov 2018 12:47:16 -0800 Subject: takeout console.log --- packages/instant/webpack.config.js | 1 - 1 file changed, 1 deletion(-) (limited to 'packages/instant/webpack.config.js') diff --git a/packages/instant/webpack.config.js b/packages/instant/webpack.config.js index 917e84ff0..803240e76 100644 --- a/packages/instant/webpack.config.js +++ b/packages/instant/webpack.config.js @@ -39,7 +39,6 @@ const generateConfig = (dischargeTarget, configOptions) => { if (heapAnalyticsId) { envVars.HEAP_ANALYTICS_ID = JSON.stringify(heapAnalyticsId); } - console.log(envVars); const config = { entry: { -- cgit v1.2.3