ericpalakovichcarr / drf-react-redux

This repository shows how to use Django, Django Rest Framework, React, and Redux together.
MIT License
81 stars 18 forks source link

Production Environment #3

Open jamesattard opened 7 years ago

jamesattard commented 7 years ago

How can I use this on the production env without HMR so as to avoid having a separate Node server? Just reconf of webpack to just bundle static js?

ericpalakovichcarr commented 7 years ago

I currently have a separate webpack.prod.config.js for building production bundles. I've updated to webpack 2 (this repository was based on webpack 1), but my prod config applied to the config in this repository would look something like this (FYI: untested, so beware):

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  context: __dirname,

  entry: [
    './client/index'
  ],

  output: {
    path: path.resolve('./static/'),
    filename: "bundle.js",
    publicPath: '/static/',
  },

  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new ExtractTextPlugin("[name]-bundle.css")
  ],

  module: {
    rules: [
      { test: /\.js$/, exclude: /node_modules/, use: ['babel-loader']},
      { test: /\.css$/, use: ExtractTextPlugin.extract({
        fallback: "style-loader",
        use: "css-loader",
      })}
    ],
  },

  resolve: {
    modules: ["node_modules"],
    extensions: ['.js'],
  },
};

Then update the scripts section in package.json like so:

"scripts": {
  "server": "node server.js",
  "build_bundles": "webpack --config=webpack.prod.config.js -p"
}

Then run node run build_bundles whenever you want to build your production bundle. It'll get built and added to Django's static directory, which will then get picked up by whatever processes you have in place for static assets (e.g. manage.py collectstatic).