如何用Webpack打包到'生产'我的反应网站?

时间:2016-01-01 17:19:22

标签: javascript reactjs npm webpack packaging

我设法使用这个react-hot-boilerplate配置脚本来创建和测试一个简单的React Flux webapp。

现在,当我运行npm start时,我有一个我喜欢的网站,在配置中添加生产版本的最简单/最好的方法是什么?当我使用'package'命令时,我想得到一个小prod文件夹,其中包含我需要的所有最终html和缩小的js文件,这是我应该期待的吗?

这是我的 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"
  }
}

我想我想在脚本列表中添加一个新脚本,这样我就可以使用新的npm xyz命令,但我不知道该写什么。

我的 Webpack.config.js ,如果我可能(?)必须为prod配置使用类似但不同的副本:

  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
      ]
    }
  };

我不确定要保留哪些部分,改变(prod config)或添加是否需要副本......

1 个答案:

答案 0 :(得分:5)

您需要使用--optimize-minimize选项(缩小)运行Webpack构建,并确保NODE_ENV设置为production(这有效地禁用/删除了React的开发仅代码类型检查等代码

您可以通过向build:production package.json NODE_ENV=production webpack --optimize-minimize添加scripts(您可以将此名称命名)命令添加为npm命令来完成此操作,例如"build:production": "NODE_ENV=production webpack --optimize-minimize"

将此添加到package.json的npm run build:production

webpack

通过t.integer :recipient_id

运行命令

注意:假设您已经正确配置了Webpack,并且可以通过从命令行运行class Notification < ActiveRecord::Base belongs_to :recipient, class_name: 'User' end 来“构建”

你可能需要查看你的webpack.config我建议在这个样板之外了解Webpack。 Egghead.io有一些关于这个主题的很棒的短视频(还有很多其他的):) egghead.io/search?q=Webpack,特别是https://egghead.io/lessons/javascript-intro-to-webpack