我设法使用这个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(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)或添加如果需要副本......
您将希望使用--optimize-minimize
选项(缩小)运行Webpack构建并确保将NODE_ENV
其设置为production
(这有效地禁用/删除了React的仅开发代码,例如prop类型检查)
你可以做到这一点可以通过添加一个命令NPM build:production
(你能说出这个不管你喜欢)命令在package.json
如NODE_ENV=production webpack --optimize-minimize
将它添加到你的package.json中 scripts
"build:production": "NODE_ENV=production webpack --optimize-minimize"
并通过命令运行 npm run build:production
注意:这是假设您已经正确配置了Webpack并且可以通过webpack
从命令行运行来"构建"
您可能需要查看您的webpack.config我建议您在此样板之外了解Webpack.Egghead.io有一些关于这个主题的很棒的短视频(还有很多其他的):) egghead.io/search?q=Webpack,特别是https://egghead.io/lessons/javascript-intro-to-webpack