Skip to content

Commit

Permalink
DRY up webpack config
Browse files Browse the repository at this point in the history
  • Loading branch information
roryabraham committed Mar 25, 2021
1 parent 714ccaf commit 15354b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
29 changes: 29 additions & 0 deletions config/webpack/productionConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const webpack = require('webpack');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');

/**
* Get the production webpack configuration, given an environment object.
*
* @param {Object} env
* @returns {Object}
*/
function getProductionConfig(env) {
return ({
mode: 'production',
devtool: 'source-map',
plugins: [
// This allows us to interactively inspect JS bundle contents
...(process.env.ANALYZE_BUNDLE === 'true' ? [new BundleAnalyzerPlugin()] : []),
new webpack.DefinePlugin({
__REACT_WEB_CONFIG__: JSON.stringify(env),

// React Native JavaScript environment requires the global __DEV__ variable to be accessible.
// react-native-render-html uses variable to log exclusively during development.
// See https://reactnative.dev/docs/javascript-environment
__DEV__: false,
}),
],
});
}

module.exports = getProductionConfig;
20 changes: 2 additions & 18 deletions config/webpack/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
const path = require('path');
const webpack = require('webpack');
const {merge} = require('webpack-merge');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const dotenv = require('dotenv');
const common = require('./webpack.common.js');
const getProductionConfig = require('./productionConfig');

const env = dotenv.config({path: path.resolve(__dirname, '../../.env.production')}).parsed;

module.exports = merge(common, {
mode: 'production',
devtool: 'source-map',
plugins: [
// This allows us to interactively inspect JS bundle contents
...(process.env.ANALYZE_BUNDLE === 'true' ? [new BundleAnalyzerPlugin()] : []),
new webpack.DefinePlugin({
__REACT_WEB_CONFIG__: JSON.stringify(env),

// React Native JavaScript environment requires the global __DEV__ variable to be accessible.
// react-native-render-html uses variable to log exclusively during development.
// See https://reactnative.dev/docs/javascript-environment
__DEV__: false,
}),
],
});
module.exports = merge(common, getProductionConfig(env));
20 changes: 2 additions & 18 deletions config/webpack/webpack.staging.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
const path = require('path');
const webpack = require('webpack');
const {merge} = require('webpack-merge');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const dotenv = require('dotenv');
const common = require('./webpack.common.js');
const getProductionConfig = require('./productionConfig');

const env = dotenv.config({path: path.resolve(__dirname, '../../.env.staging')}).parsed;

module.exports = merge(common, {
mode: 'production',
devtool: 'source-map',
plugins: [
// This allows us to interactively inspect JS bundle contents
...(process.env.ANALYZE_BUNDLE === 'true' ? [new BundleAnalyzerPlugin()] : []),
new webpack.DefinePlugin({
__REACT_WEB_CONFIG__: JSON.stringify(env),

// React Native JavaScript environment requires the global __DEV__ variable to be accessible.
// react-native-render-html uses variable to log exclusively during development.
// See https://reactnative.dev/docs/javascript-environment
__DEV__: false,
}),
],
});
module.exports = merge(common, getProductionConfig(env));

0 comments on commit 15354b8

Please sign in to comment.