Skip to content

Commit

Permalink
feat: make it possible to disable NewRelic globally
Browse files Browse the repository at this point in the history
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 openedx/wg-frontend#14
  • Loading branch information
regisb committed Nov 26, 2021
1 parent 9a51fb2 commit e8f2221
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions config/webpack.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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',
Expand Down Expand Up @@ -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,
}),
],
]),
});

0 comments on commit e8f2221

Please sign in to comment.