我可以使用webpack的热模块重新加载sailsjs吗?

时间:2016-01-04 04:38:34

标签: node.js reactjs sails.js webpack webpack-hmr

我正在使用sails.jsreact开展项目。我希望能够使用Webpack的hot module replacement,这样我就可以编辑我的代码并立即在浏览器上进行更改。但是,我怎么能把它连接起来似乎并不明显。

我希望能够使用$ sails lift并让它完全正常工作。

如果这是一个node.js项目,我只需将webpack配置为使用react-transform-hmr并从package.json启动webpack-dev-server(例如as described here)。但是,这似乎不是一个非常 sails-y 的东西。

我看到模块webpack-hot-middleware声称能够“将热重新加载到没有webpack-dev-server的现有服务器中。”但是,我不确定在Sails> 0.10中添加Express中间件配置的适当位置。

有人可以推荐一个好方法来设置吗?

3 个答案:

答案 0 :(得分:8)

好的,经过一些好的方法后,看起来是使用sails customMiddleware中间件配置的旧http选项,但仅限于config/env/development.js中保留的开发环境。< / p>

1)安装反应和反应(如果你还没有):

$ npm install react react-dom --save

2)安装webpack,热模块重新加载(&amp; 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

热潮你应该做生意!

答案 1 :(得分:2)

您的解决方案应该运行良好,但我想为其他人提供另一种快速解决方案:

您可以完全删除Sails.js构建管道并单独运行webpack。在子shell中同时运行这两个命令应该可以解决问题。

( webpack & sails lift ; )

两个命令都将运行,您将看到两者的输出在终端中合并。 Ctrl + C也会正确地杀死它们,因为它在子shell中运行。

您可以创建一个npm脚本,以避免每次都写出两个命令。

答案 2 :(得分:1)

我们创建了自己的Sails挂钩来内部处理Webpack,目前工作得很好。配置需要一些清理:https://github.com/teamfa/sails-hook-webpack