0

Build once, deploy many

I have a React app that references a config file with variables that are different for each environment. In keeping with the "build once, deploy many" concept, I'd like to exclude the config file from my webpack build process and instead import a different config file for each environment.

How can I accomplish this?

Here's my webpack.config.js as it stands.

const path = require('path');
const webpack = require('webpack');

const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const srcRoot = path.resolve(__dirname, 'src');
const appRoot = path.resolve(srcRoot, 'app');
const configRoot = path.resolve(srcRoot, 'fs_config.js');

module.exports = (env) => {

  const isDev = env == 'development';

  return {
    context: path.resolve(__dirname),
    entry: {
      main: './src/app/main.js',
      vendor: [
        'react', 'react-dom', 'jquery', 'moment',
        'react-bootstrap', 'lodash'
      ]
    },
    output: {
      path: path.resolve(__dirname, './dist'),
      filename: isDev ? 'js/[name].bundle.js' : 'js/[name].[hash].bundle.js',
      sourceMapFilename: isDev ? 'js/[name].bundle.map' : 'js/[name].[chunkhash].bundle.map',
      chunkFilename: isDev ? 'js/[id].chunk.js' : 'js/[id].[chunkhash].chunk.js',

      publicPath: '/'
    },
    module: {
      rules: [
        {
          test: /\.jsx?$/,
          loader: 'babel-loader',

          query:{
            "presets": [
              ["es2015", {"modules": false}],
              "stage-2",
              "react"
            ],
            "plugins": [
              "react-hot-loader/babel"
            ]
          },

          exclude: [
            /node_modules/
          ],
        },

        {test: /\.css$/, loader: "style-loader!css-loader"},

        {test: /\.json$/, loader: "json-loader"},

        {
          test: /\.(jpe?g|png|gif)$/,
          loader: 'file-loader',
          query:{
            name: 'assets/img/[name].[ext]'
          }
        },
      ]

    },
    resolve: {
      extensions: [".js", ".jsx"],

      modules: [
        appRoot,
        'node_modules'
      ],
    },
    devServer: {
      contentBase: path.join(__dirname, "dist"),
      port: 2200,
      disableHostCheck: true,
      compress:true,
      publicPath: '/',
      stats: "minimal"

    },
    stats: "minimal",
    performance: {
      hints: false
    },
    devtool: isDev ? 'eval' : 'cheap-source-map',

    plugins: [
      new CleanWebpackPlugin(['dist']),
      new CopyWebpackPlugin([
        {from: './src/index.html'},
        {from: './src/assets', to: './assets'},

      ]),
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),

      new HtmlWebpackPlugin({
        template: path.resolve(srcRoot, 'index.html'),
        chunksSortMode: 'dependency'
      }),

      new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        filename: 'js/[hash].vendor.js',
        minChunks: Infinity,
      }),

      new webpack.DefinePlugin({
        process: {
          env: {
            NODE_ENV: isDev ? '"development"' : '"production"'
          }
        }
      }),


    ].concat(
      !isDev
        ?
        [
          new webpack.optimize.UglifyJsPlugin({
            compress: {
              warnings: false
            }
          }),
        ]
        :
        []
    ),
    node: {
      console: true,
      fs: 'empty',
      tls: 'empty',
      net: 'empty',
    },
    externals: [{
        xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}'
      }
    ]
  }
};
skink
  • 5,133
  • 6
  • 37
  • 58
Milk
  • 2,469
  • 5
  • 31
  • 54
  • I've had a look at the related questions e.g https://stackoverflow.com/questions/45259464/handling-config-files-with-webpack but they don't appear to have a complete solution – Milk Oct 23 '17 at 20:17
  • Take a look at how create-react-app does this same thing by creating a new app with it and then ejecting so you can see their configs... That's where i'd start at least. – rimraf Oct 23 '17 at 23:23
  • @archae0pteryx `create-react-app` doesn't use webpack or a config file so not sure how this would help – Milk Oct 23 '17 at 23:29
  • i’m sorry. i misunderstood. – rimraf Oct 23 '17 at 23:30

0 Answers0