简体   繁体   English

webpack-dev-server 热重载不工作

[英]webpack-dev-server hot reload not working

My file structure is:我的文件结构是:

dist
  css
    style.css
  index.html
  js
    bundle.js
src
  css
    style.css
  index.html
  js
    main.js
node_modules
webpack.config.js
package.json

My webpack.config.js looks like:我的 webpack.config.js 看起来像:

module.exports = {
    entry: './src/js/main.js',
    output: {
        path: __dirname,
        filename: './dist/js/bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules)/,
                loader: 'babel',
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.vue$/,
                loader: 'vue'
            }
        ]
    }
};

I run:我跑:

webpack-dev-server --content-base dist --hot

And it builds and seems like it's working.它建立起来并且看起来像是在工作。 localhost:8080 shows the expected result but hot-reload does just not work. localhost:8080 显示了预期的结果,但热重载不起作用。 When I change a file I can see in terminal that a rebuild is happening but nothing happens in the browser.当我更改文件时,我可以在终端中看到重建正在进行,但浏览器中没有任何反应。 Am I missing something in the config?我在配置中遗漏了什么吗?

What worked for me is to write <script src="bundle.js"> and not <script src="dist/bundle.js"> in my index.html file.对我有用的是在我的index.html文件中编写<script src="bundle.js">而不是<script src="dist/bundle.js">

// index.html
<script src="bundle.js"></script>      // works
<script src="dist/bundle.js"></script> // doesn't work!

Keeping dist/bundle.js as the output file works perfectly fine if you just build it using webpack .如果您只是使用webpack构建它,那么将dist/bundle.js为输出文件非常有效。 But when using webpack-dev-server , the static file already in the file system continues to be served, and not the latest hot replacement.但是当使用webpack-dev-server时,文件系统中已经存在的静态文件会继续被提供,而不是最新的热替换。 It seems webpack-dev-server gets confused when it sees dist/bundle.js in the html file and doesn't hot replace it, even though webpack.config.js is configured to that path.webpack-dev-server在 html 文件中看到dist/bundle.js并且没有热替换它时,它似乎感到困惑,即使webpack.config.js配置为该路径。

When using webpack-dev-server , it builds all files internally and does not spit them out into your output path.使用webpack-dev-server时,它会在内部构建所有文件,并且不会将它们吐出到您的输出路径中。 Running webpack alone, without the dev server, does the actual compilation to disk.单独运行webpack ,没有开发服务器,实际编译到磁盘。 The dev server does everything in memory which speeds up re-compilation by a lot.开发服务器在内存中完成所有工作,这大大加快了重新编译的速度。

To fix your hot reload issue, set the content base to your source directory and enable inline-mode要解决热重载问题,请将内容库设置为源目录并启用内联模式

Like so:像这样:

webpack-dev-server --content-base src --hot --inline

None of the options on this page worked for me.此页面上的所有选项都不适合我。 After changing the devServer section to:将 devServer 部分更改为:

devServer: {
    port: 8080,
    contentBase: ['./src', './public'], // both src and output dirs
    inline: true,
    hot: true
},

it worked.有效。

100% Working Solution 100% 工作解决方案

You have to just follow 3 steps and you will get your hot reloading as you expected您只需遵循 3 个步骤,即可按预期进行热重载

  1. Include "publicPath" key in "Output" property of webpack config.在 webpack 配置的“输出”属性中包含“publicPath”键。 "publicPath" should contain path to your bundle.js file so that dev-server will know if there is any change in your bundle.js file and it will reload your application. “publicPath”应该包含你的 bundle.js 文件的路径,以便开发服务器知道你的 bundle.js 文件是否有任何变化,它会重新加载你的应用程序。

Your config should look like this -您的配置应如下所示 -

 output: { path: __dirname, publicPath:"/dist/js/", filename: './dist/js/bundle.js' }
  1. Add "devServer" option in config file -在配置文件中添加“devServer”选项 -
 devServer:{ contentBase:"/src/", inline:true, stats:"errors-only" }

Please note that contentBase should point to the path where you put your index.html file which contain your script tag in your case it will be "/src/"请注意, contentBase应该指向您放置包含脚本标签的 index.html 文件的路径,在您的情况下,它将是“/src/”

  1. At last you have to make sure 'src' attribute of your 'script' tag in index.html points to bundle.js starting from " http://localhost:port " as follow -最后,您必须确保 index.html 中的 'script' 标记的 'src' 属性指向从“ http://localhost:port ”开始的 bundle.js,如下所示 -

<script src="http://localhost:portnumber + value in publicPath + bundle.js"></script>

in your case it will look like this -在你的情况下,它看起来像这样 -

<script src="http://localhost:8080/js/dist/bundle.js" type="text/javascript"></script>

And finally remember webpack-dev-server doesn't compile your js file or make build or watch on your js file it dose everything in memory to watch on your js file you have to run webpack --watch in seprate window最后请记住webpack-dev-server doesn't compile your js file or make build or watch on your js文件它会在内存中执行所有操作以查看您的 js 文件您必须在单独的窗口中运行webpack --watch

--inline --hot wasn't an issue for me --inline --hot 对我来说不是问题

If you are using redux can try this.如果你使用 redux 可以试试这个。

For some random reason redux-devtools was not allowing hot reload for me.由于某种随机原因redux-devtools不允许我进行热重载。 Try removing it from root component and redux compose config.尝试从根组件和redux compose config 中删除它。

Note: Use redux devtool browser extension with this config in your store configuration: window.devToolsExtension ? window.devToolsExtension() : f => f注意:在您的商店配置中使用带有此配置的 redux devtool 浏览器扩展: window.devToolsExtension ? window.devToolsExtension() : f => f window.devToolsExtension ? window.devToolsExtension() : f => f

Also, must read: https://medium.com/@rajaraodv/webpacks-hmr-react-hot-loader-the-missing-manual-232336dc0d96#.ejpsmve8f另外,必须阅读: https ://medium.com/@rajaraodv/webpacks-hmr-react-hot-loader-the-missing-manual-232336dc0d96#.ejpsmve8f

Or try hot reload 3: example: https://github.com/gaearon/redux-devtools/commit/64f58b7010a1b2a71ad16716eb37ac1031f93915或尝试热重载 3:示例: https ://github.com/gaearon/redux-devtools/commit/64f58b7010a1b2a71ad16716eb37ac1031f93915

The only thing that works for me is apply the " hotOnly " inside the configuration of the "devServer" (on the "webpack.config.js"), like this:唯一对我有用的是在“ devServer ”的配置中应用“hotOnly”(在“webpack.config.js”上),如下所示:

devServer: {
    hotOnly: true,
},

After reload the "Webpack Dev Server", the "Hot Reload" works, at least, after saving any change in CSS or JS files.重新加载“Webpack 开发服务器”后,“热重载”至少在保存 CSS 或 JS 文件中的任何更改后有效。

For Webpack ^5.72.1对于 Webpack ^5.72.1

This worked for me,这对我有用,

 devServer: {
    port: 3000,
    hot: false,
    liveReload: true,
  },

Note: liveReload will reload the whole application on file save but it will lose application state.注意:liveReload 将在文件保存时重新加载整个应用程序,但它会丢失应用程序状态。 Hot reloading preserves state.热重载会保留状态。 But liveReload seemed to fix the issue for me with css/html not loading on the browser after save, without having to refresh the browser.但是 liveReload 似乎为我解决了保存后 css/html 未在浏览器上加载的问题,而无需刷新浏览器。

Webpack hot reloading stopped working for me as well. Webpack 热重载也停止了对我的工作。 The solution for me was to delete the node_modules folder and fresh install all dependencies.我的解决方案是删除node_modules文件夹并全新安装所有依赖项。 Just open the parent folder of node_modules in your terminal and run npm install只需在终端中打开node_modules的父文件夹并运行npm install

所有受苦的人:不要忘记公共路径前的斜杠: publicPath:'/assets/scripts/'不是publicPath:'assets/scripts/'因为路径字符串中的 1 个正斜杠而丢失了三天

Try this:尝试这个:

In your package.json file, delete the line that contains "test" "echo \"Error: no test specified\" && exit 1" under the scripts object, and replace it with:在您的package.json文件中,删除 scripts 对象下包含"test" "echo \"Error: no test specified\" && exit 1"的行,并将其替换为:

...
"scripts": {
    "start": "webpack-dev-server --hot"
  },

...

Then to restart your project, just use npm start .然后重新启动您的项目,只需使用npm start

This worked for me when I ran into this problem.当我遇到这个问题时,这对我有用。

Edit: Can you share your package.json file?编辑:你能分享你的package.json文件吗?

Try adding this to webpack.config.js as well尝试将其添加到 webpack.config.js 以及

devServer: {
  inline: true,
  port: 3000,
  hot: true
},

Check your console logs if it has below error then add cors to your webpack dev server file检查您的控制台日志是否有以下错误,然后将 cors 添加到您的 webpack 开发服务器文件

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin react hot reload

Ideally add below entry in you dev server js理想情况下,在您的开发服务器 js 中添加以下条目

headers: { "Access-Control-Allow-Origin": "*" },

I increased the max number of file changes that can be watched and it worked for me.我增加了可以观看的文件更改的最大数量,它对我有用。 I guess the issue for me was too many files.我想我的问题是文件太多。

echo fs.inotify.max_user_watches=1999999 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

source 资源

You can try adding this to your config:您可以尝试将其添加到您的配置中:

module.exports = {
...
  devServer: {
    contentBase: './dist/', // Since your index.html is in the "dist" dir
    open: true,             // Automatically open the browser
    hot: true,              // Automatically refresh the page whenever bundle.js changes
                            // (You will have to refresh manually if other files change)
  },
...
};

And then running (no options):然后运行(无选项):
webpack-dev-server

With the above settings, while using webpack-dev-server for static page building I add bundle js or whatever js file configured as output to the html page.通过上述设置,在使用webpack-dev-server进行静态页面构建时,我将 bundle js 或任何配置为输出的 js 文件添加到 html 页面。 This way I get refresh on my static page after any changes.这样,我在进行任何更改后都会刷新我的静态页面。

For webpack version 5, the module.loaders is now module.rules.对于 webpack 版本 5,module.loaders 现在是 module.rules。 And for the presets change query to options.对于预设,将查询更改为选项。

Make sure in development mode you are not using a plugin that extract css into files, example MiniCssExtractPlugin.确保在开发模式下您没有使用将 css 提取到文件中的插件,例如 MiniCssExtractPlugin。 It messes the things up.它把事情搞砸了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 webpack-dev-server热重装无法正常工作? - webpack-dev-server hot reload not working? webpack-dev-server 拒绝热重载 html 文件 - webpack-dev-server refuses to hot-reload html file webpack-dev-server 不热重载(webpack 5) - webpack-dev-server not hot reloading (webpack 5) webpack-dev-server和webpack-&gt;将文件输出到父目录,并使webpack-dev-server热重装仍然有效 - webpack-dev-server and webpack -> output file to parent directory and have webpack-dev-server hot reloading still working Webpack Dev Server(webpack-dev-server)热模块替换(HMR)不起作用 - Webpack Dev Server (webpack-dev-server) Hot Module Replacement (HMR) Not Working CLI热模块重新加载页面上的webpack-dev-server内联模式将刷新以更改dom - webpack-dev-server Inline mode on CLI hot module reload page will refresh for change dom webpack-dev-server 电子角度 1.6.1 热重载空白屏幕 - webpack-dev-server electron angular 1.6.1 hot reload blank screen webpack-dev-server with hot reload重新加载整个页面并进行css更改 - webpack-dev-server with hot reload reloading entire page with css changes Webpack-dev-server无法重新加载.html - Webpack-dev-server not reload .html Webpack 3 webpack-dev-server热重装不起作用 - Webpack 3 webpack-dev-server hot reloading does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM