如何使用Webpack打包“生产"我的React网站? [英] How to package to 'production' my react website with Webpack?
问题描述
我设法使用此 react-hot-boilerplate 配置脚本来创建和测试一个简单的React Flux网络应用.
I managed to use this react-hot-boilerplate configuration script to create and test a simple React Flux webapp.
现在,当我运行npm start
时,我有一个喜欢的网站,在配置中添加生产版本的最简单/最佳方法是什么?当我使用"package"命令时,我想得到一个prod
文件夹,其中包含我需要的所有最终html和缩小的js文件,这是我应该期待的吗?
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?
这是我的 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
命令,但是我不确定在那里写什么.
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.
还有我的 Webpack.config.js ,以防万一我可能不得不对产品配置使用相似但截然不同的副本:
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...
推荐答案
您将要使用--optimize-minimize
选项(最小化)运行Webpack构建,并确保将NODE_ENV
设置为production
(此操作有效地禁用/剥离React的仅开发代码,例如道具类型检查)
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)
您可以通过向package.json
(例如NODE_ENV=production webpack --optimize-minimize
将此添加到package.json的scripts
Add this to your package.json's scripts
"build:production": "NODE_ENV=production webpack --optimize-minimize"
并通过npm run build:production
注意:这是假设您已经正确配置了Webpack,并且可以通过从命令行运行webpack
来构建"
Note: this is assuming you have already correctly configured Webpack and can "build" by running webpack
from the command line
您可能必须查看一下webpack.config,我建议在此样板之外了解Webpack. Egghead.io上有一些关于该主题的精彩短片(以及许多其他视频):) egghead.io/search?q=Webpack,尤其是
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
这篇关于如何使用Webpack打包“生产"我的React网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!