How to package to 'production' my react we

2019-04-07 04:03发布

I managed to use this react-hot-boilerplate configuration script to create and test a simple React Flux webapp.

Now that I have a website I like when I run npm start, what would be the easiest/best way to add a production build in the configuration? When I use that 'package' command I would like to get a little prod folder with all the final html and minified js files that I need in it, is that what I should expect?

This is my package.json :

{
  "name": "react-hot-boilerplate",
  "version": "1.0.0",
  "description": "Boilerplate for ReactJS project with hot code reloading",
  "scripts": {
    "start": "node server.js",
    "lint": "eslint src"
  },
  "author": "Dan Abramov <dan.abramov@me.com> (http://github.com/gaearon)",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/gaearon/react-hot-boilerplate/issues"
  },
  "homepage": "https://github.com/gaearon/react-hot-boilerplate",
  "devDependencies": {
    "babel-core": "^5.4.7",
    "babel-eslint": "^3.1.9",
    "babel-loader": "^5.1.2",
    "eslint-plugin-react": "^2.3.0",
    "react-hot-loader": "^1.2.7",
    "webpack": "^1.9.6",
    "webpack-dev-server": "^1.8.2"
  },
  "dependencies": {
    "react": "^0.13.0",
    "flux": "^2.0.2",
    "events": "^1.0.2",
    "object-assign": "^3.0.0",
    "jquery": "^2.1.4",
    "imports-loader": "^0.6.4",
    "url-loader": "^0.5.6",
    "numeral": "^1.5.3",
    "bootstrap": "^3.3.5",
    "d3": "^3.5.6",
    "zeroclipboard": "^2.2.0",
    "react-toastr": "^1.5.1",
    "d3-legend": "^1.0.0"
  }
}

I think I want to add a new script in the scripts list so I can use a new npm xyz command but I am not sure what to write there.


Also my Webpack.config.js, in case I might(?) have to use a similar but distinct copy for the prod config :

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

  module.exports = {
    devtool: 'eval',
    entry: [
      'webpack-dev-server/client?http://localhost:3000',
      'webpack/hot/only-dev-server',
      './src/index'
    ],
    output: {
      path: path.join(__dirname, 'dist'),
      filename: 'bundle.js',
      publicPath: '/static/'
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new webpack.NoErrorsPlugin(),
      new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery" })
    ],
    externales: { "jquery": "jQuery", "$": 'jQuery' },
    resolve: {
      extensions: ['', '.js', '.jsx']
    },
    module: {
      loaders: [
        {
          test: /\.jsx?$/,
          loaders: ['react-hot', 'babel'],
          include: path.join(__dirname, 'src')
        },
        { test: /\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders
        { test: /\.css$/, loader: 'style-loader!css-loader' },
        {test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192'} // inline base64 URLs for <=8k images, direct URLs for the rest
      ]
    }
  };

And I'm not sure which parts to keep, alter (prod config) or add if a copy is required...

1条回答
乱世女痞
2楼-- · 2019-04-07 04:42

You'll want to run your Webpack build with the --optimize-minimize option (minifies) and make sure that NODE_ENV is set to production (this effectively disables/strips out React's development only code such as prop types checking)

You can accomplish this as an npm command by adding a build:production (you can name this whatever you like) command to your package.json such as NODE_ENV=production webpack --optimize-minimize

Add this to your package.json's scripts

"build:production": "NODE_ENV=production webpack --optimize-minimize"

And run the command via npm run build:production

Note: this is assuming you have already correctly configured Webpack and can "build" by running webpack from the command line

You may have to look at your webpack.config I suggest getting to know Webpack outside of this boilerplate. Egghead.io has some great, short videos on the topic (and many others) :) egghead.io/search?q=Webpack and specifically https://egghead.io/lessons/javascript-intro-to-webpack

查看更多
登录 后发表回答