From e8f22219b0a7f8aed53a986f0190f8ad13fb90ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Thu, 25 Nov 2021 23:51:35 +0100 Subject: [PATCH] feat: make it possible to disable NewRelic globally By defining the `ENABLE_NEW_RELIC='false'` environment variable, the new relic logging integration will be disabled, which is a must-have for most non-edX platforms. Close https://github.com/openedx/frontend-wg/issues/14 --- config/webpack.prod.config.js | 46 ++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/config/webpack.prod.config.js b/config/webpack.prod.config.js index 4047498b1..a4ee694c5 100644 --- a/config/webpack.prod.config.js +++ b/config/webpack.prod.config.js @@ -7,13 +7,13 @@ const { merge } = require('webpack-merge'); const CssNano = require('cssnano'); const Dotenv = require('dotenv-webpack'); const dotenv = require('dotenv'); -const HtmlWebpackNewRelicPlugin = require('html-webpack-new-relic-plugin'); -const NewRelicSourceMapPlugin = require('@edx/new-relic-source-map-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const path = require('path'); const PostCssAutoprefixerPlugin = require('autoprefixer'); const PostCssRTLCSS = require('postcss-rtlcss'); +const NewRelicSourceMapPlugin = require('@edx/new-relic-source-map-webpack-plugin'); +const HtmlWebpackNewRelicPlugin = require('html-webpack-new-relic-plugin'); const commonConfig = require('./webpack.common.config.js'); const presets = require('../lib/presets'); @@ -23,6 +23,28 @@ dotenv.config({ path: path.resolve(process.cwd(), '.env'), }); +const extraPlugins = []; +if (process.env.ENABLE_NEW_RELIC !== 'false') { + // Enable NewRelic logging only if the account ID is properly defined + extraPlugins.push(new HtmlWebpackNewRelicPlugin({ + // This plugin fixes an issue where the newrelic script will break if + // not added directly to the HTML. + // We use non empty strings as defaults here to prevent errors for empty configs + accountID: process.env.NEW_RELIC_ACCOUNT_ID || 'undefined_account_id', + agentID: process.env.NEW_RELIC_AGENT_ID || 'undefined_agent_id', + trustKey: process.env.NEW_RELIC_TRUST_KEY || 'undefined_trust_key', + licenseKey: process.env.NEW_RELIC_LICENSE_KEY || 'undefined_license_key', + applicationID: process.env.NEW_RELIC_APP_ID || 'undefined_application_id', + })); + extraPlugins.push(new NewRelicSourceMapPlugin({ + applicationId: process.env.NEW_RELIC_APP_ID, + apiKey: process.env.NEW_RELIC_ADMIN_KEY, + staticAssetUrl: process.env.BASE_URL, + // upload source maps in prod builds only + noop: typeof process.env.NEW_RELIC_ADMIN_KEY === 'undefined', + })); +} + module.exports = merge(commonConfig, { mode: 'production', devtool: 'source-map', @@ -172,26 +194,10 @@ module.exports = merge(commonConfig, { path: path.resolve(process.cwd(), '.env'), systemvars: true, }), - new HtmlWebpackNewRelicPlugin({ - // This plugin fixes an issue where the newrelic script will break if - // not added directly to the HTML. - // We use non empty strings as defaults here to prevent errors for empty configs - accountID: process.env.NEW_RELIC_ACCOUNT_ID || 'undefined_account_id', - agentID: process.env.NEW_RELIC_AGENT_ID || 'undefined_agent_id', - trustKey: process.env.NEW_RELIC_TRUST_KEY || 'undefined_trust_key', - licenseKey: process.env.NEW_RELIC_LICENSE_KEY || 'undefined_license_key', - applicationID: process.env.NEW_RELIC_APP_ID || 'undefined_application_id', - }), - new NewRelicSourceMapPlugin({ - applicationId: process.env.NEW_RELIC_APP_ID, - apiKey: process.env.NEW_RELIC_ADMIN_KEY, - staticAssetUrl: process.env.BASE_URL, - // upload source maps in prod builds only - noop: typeof process.env.NEW_RELIC_ADMIN_KEY === 'undefined', - }), + ].concat(extraPlugins).concat([ new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false, }), - ], + ]), });