【问题标题】:WebPack disable HMRWebPack 禁用 HMR
【发布时间】:2017-06-26 14:16:02
【问题描述】:

我正在尝试运行一个简单的webpack-dev-server,当相关源 JavaScript 文件发生更改时,它会编译 .bundle.js 文件。我现在不想启用热模块更换 (HMR)。

我的服务器正在运行,但它会在 JavaScript 控制台上打印以下错误:

GET https://monkey.transposit.com:3000/sockjs-node/info?t=1486581439029 net::ERR_CONNECTION_CLOSED
    AbstractXHRObject._start @ home.bundle.js:3182
    (anonymous) @ home.bundle.js:3071
[WDS] Disconnected!
    log @ home.bundle.js:3684
    close @ home.bundle.js:3753
    sock.onclose @ home.bundle.js:3980
    EventTarget.dispatchEvent @ home.bundle.js:2917
    (anonymous) @ home.bundle.js:6021
GET https://monkey.transposit.com:3000/sockjs-node/info?t=1486581439029 net::ERR_CONNECTION_CLOSED
    AbstractXHRObject._start @ home.bundle.js:3182
    (anonymous) @ home.bundle.js:3071
GET https://monkey.transposit.com:3000/sockjs-node/info?t=1486581440063 net::ERR_CONNECTION_CLOSED
    AbstractXHRObject._start @ home.bundle.js:3182
    (anonymous) @ home.bundle.js:3071

我不清楚浏览器试图做什么,我看到了这些错误。 (特别是因为捆绑包正在成功编译和提供服务)。

这是我的 webpack.config.js

const path = require('path');
module.exports = {
  entry: {
    project_console: './src/console/console',
    …
  },
  output: {
    filename: '[name].bundle.js',
    path: path.join(__dirname, 'dist'),
    publicPath: '/js/',
    library: '[name]',
    libraryTarget: 'var'
  },
  module: {
    rules: [
      {test: /\.js$/, use: ['babel-loader'], include: path.join(__dirname, 'src')},
      {test: /\.scss/, use: ['style-loader', 'css-loader', 'sass-loader']}
    ]
  },
  devServer: {
    host: '0.0.0.0',
    port: 3000,
    hot: false
  }
};

这是我的 package.json

{
  …
  "files": [
    "src/"
  ],
  "scripts": {
    "start": "webpack-dev-server”,
    …
  },
  "dependencies": {
    "react": "^15.4.2",
    "react-dom": "^15.4.2”,
    …
  },
  "devDependencies": {
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.3.0”,
    …
  }
  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  }
  …
}

感谢您的帮助!

【问题讨论】:

  • 尝试将hot设置为false:webpack.github.io/docs/…
  • @lux 我在 webpack.config.jsdevServer.hot 中将 hot 设置为 false。是这个意思吗?

标签: reactjs webpack webpack-dev-server webpack-hmr


【解决方案1】:

我在调用 webpack-dev-server 时添加了 --no-line,这解决了我的问题。

这是我的 package.json

{
  "scripts": {
    "start": "webpack-dev-server --no-inline”,
    …
  }
}

【讨论】:

  • 我在使用 --no-inline 时停止获取所有控制台输出
【解决方案2】:

您可以将hotreload=false 放在查询字符串中的任何位置,即使#hotreload=false 也可以。

你仍然会得到:

[WDS] 应用已更新。重新编译...

在控制台日志中,但页面实际上不会重新加载。

此行为可能会发生变化。我刚刚通过在我的 vendor.js 文件中搜索 WDS 并向后工作找到它:-)

【讨论】:

  • 在 webpack 5 中:webpack-dev-server-hot=false&webpack-dev-server-live-reload=false 在查询字符串中 (WDS source)。
【解决方案3】:

要禁用 HMR,您需要将以下内容添加到您的 webpack 配置中

{
  devServer: {
    hot: false, // disable HMR
  },
  plugins: [],
}

更多here

【讨论】: