我正在使用sails.js进行一个项目并做出反应.我希望能够使用Webpack的热模块替换,这样我就可以编辑我的代码并立即在浏览器上进行更改.但是,我怎么能把它连接起来似乎并不明显.
我希望能够使用$ sails lift
并让它完全正常工作.
如果这是一个node.js项目,我只需配置webpack即可使用react-transform-hmr
并webpack-dev-server
从package.json 开始(例如,如此处所述).但是,这似乎不是一个非常风帆的东西.
我看到模块webpack-hot-middleware声称能够"在没有的情况下将热重新加载到现有服务器中webpack-dev-server
".但是,我不确定在Sails> 0.10中添加Express中间件配置的适当位置.
任何人都可以推荐一个好的方法来设置它吗?
好吧,经过一些好的方法,看起来是使用sails customMiddleware
的http
中间件配置的旧选项,但仅限于保留的开发环境config/env/development.js
.
1)安装反应和反应(如果你还没有):
$ npm install react react-dom --save
2)安装webpack,热模块重载(&ES6)支持帆:
$ npm install sails-webpack babel-core babel-loader \ babel-plugin-syntax-class-properties babel-plugin-syntax-decorators \ babel-plugin-syntax-object-rest-spread \ babel-plugin-transform-class-properties \ babel-plugin-transform-decorators-legacy \ babel-plugin-transform-object-rest-spread \ babel-preset-es2015 babel-preset-react \ copy-webpack-plugin file-loader --save
3)安装反应变换和中间件以进行热模块重新加载:
$ npm install babel-plugin-react-transform react-transform-catch-errors react-transform-hmr \ webpack-dev-middleware webpack-hot-middleware --save-dev
4)禁用通常链接应用程序的内置grunt钩子:
// .sailsrc { "hooks": { "grunt": false } }
5)配置sails webpack配置:
// config/webpack.js var webpack = require('webpack'); var CopyWebpackPlugin = require('copy-webpack-plugin'); var path = require('path'); // compile js assets into a single bundle file module.exports.webpack = { options: { context: path.join(__dirname, '..'), devtool: 'eval', entry: [ './assets/js', 'webpack-hot-middleware/client' ], output: { path: path.resolve(__dirname, '../.tmp/public'), publicPath: "/", filename: 'bundle.js' }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), /* Copy sails.io.js unmolested: */ new CopyWebpackPlugin([ { from: 'assets/js/dependencies', to: 'dependencies', force: true } ]), ], resolve: { extensions: ['', '.js', '.jsx'] }, module: { loaders: [ { test: /\.jsx?$/, exclude: /(bower_components|node_modules)/, loader: 'babel', }, { test: /\.css$/, loader: 'style!css' }, { test: /\.jpe?g$|\.gif$|\.png$|\.svg$|\.woff$|\.ttf$|\.wav$|\.mp3$/, loader: "file" } ] } }, // docs: https://webpack.github.io/docs/node.js-api.html#compiler watchOptions: { aggregateTimeout: 300 } };
6).babelrc
在开发模式下配置项目范围以使用热模块重新加载:
{ "presets": [ "es2015", "react", ], "plugins": [ "syntax-class-properties", "syntax-decorators", "syntax-object-rest-spread", "transform-class-properties", "transform-decorators-legacy", "transform-object-rest-spread" ], "env": { "development": { "plugins": [["react-transform", { "transforms": [{ "transform": "react-transform-hmr", "imports": ["react"], "locals": ["module"] }] }]] } } }
7)最后,添加http.customMiddleware
配置到帆config/env/development.js
:
module.exports = { /* ... */ /* * Enable webpack hotloading while in development mode: */ http: { customMiddleware: function (app) { var webpack = require('webpack'); var webpackConfig = require('../webpack').webpack.options; var compiler = webpack(webpackConfig); app.use(require("webpack-dev-middleware")(compiler, { noInfo: true, publicPath: webpackConfig.output.publicPath } )); app.use(require("webpack-hot-middleware")(compiler, { reload: true } )); }, } /* ... */ };
假设你有一个assets/js/index.jsx
(或类似的)反应应用程序和一个包含你的bundle.js
文件的视图,你应该能够简单地$ sails lift
在你的浏览器的开发控制台上看到以下内容:
|> Now connected to Sails. \___/ For help, see: http://bit.ly/1DmTvgK (using browser SDK @v0.11.0) client.js:51 [HMR] connected
和繁荣,你应该在业务!