简体   繁体   English

在Webpack中编辑html / jade文件时如何重新加载浏览器

[英]How to get browser reload when editing html/jade files in webpack

This is part of the code I have in my webpack.config.js 这是我在webpack.config.js包含的代码的一部分

const common = {
  entry: {
    style: PATHS.style,
    app: PATHS.jsComp
  },
  output: {
    path: PATHS.build,
    filename: '[name].js'
  },
  module: {
    loaders: [
      { test: /\.jade$/, loader: "jade" }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Webpack demo',
      template: PATHS.template, // Load a custom template (ejs by default but can be changed) 
    inject: 'body' // Inject all scripts into the body (this is the default so you can skip it) 
    })
  ]
};

I'm showing you the section where I think the problem is, I don't think you needed the entire code, but it's basically a slightly modified version of this git file 我向您展示我认为问题所在的部分,我认为您不需要整个代码,但这基本上是此git文件的略微修改版本

When I run webpack-dev-server everything works. 当我运行webpack-dev-server一切正常。 JavaScript and SASS changes do reload the browser but when I change by jade file, the browser doesn't reload. JavaScript和SASS的更改确实会重新加载浏览器,但是当我按jade文件进行更改时,浏览器不会重新加载。 In the terminal I have tried running these variations of webpack-dev-server 在终端中,我尝试运行这些webpack-dev-server变体

webpack-dev-server
webpack-dev-server --inline --hot
webpack-dev-server --inline --hot --colors
webpack-dev-server --inline --hot --colors --content-base app
webpack-dev-server --inline --hot --colors --content-base app --host 0.0.0.0
webpack-dev-server --inline --hot --colors --host 0.0.0.0

None of the above 6 variations get the browser reloading when I made changes to the jade file. 当我更改了jade文件时,以上6个变体都无法使浏览器重新加载。

Also, if I remember correctly, after every tutorial I have went through (before finding this setup), the browser never reloaded on html changes. 另外,如果我没记错的话,在完成所有教程之后(找到此设置之前),浏览器再也不会重新加载html更改了。

Is there anything else that I need to install (globaly or otherwise) 还有什么我需要安装的(全局或其他方式)

According to the documentation , you should do 3 things: 根据文档 ,您应该做三件事:

  • add an entry point to the webpack configuration: webpack/hot/dev-server. 将入口点添加到webpack配置中:webpack / hot / dev-server。
  • add the new webpack.HotModuleReplacementPlugin() to the webpack configuration. 将新的webpack.HotModuleReplacementPlugin()添加到webpack配置中。
  • add hot: true to the webpack-dev-server configuration to enable HMR on the server. add hot:对webpack-dev-server配置为true,以在服务器上启用HMR。

As far as I see, I think the only thing of those 3 that you already did is using the hot: true . 据我所知,我认为您已经做过的那三件事中唯一的事情就是使用hot: true I use to run the webpack-dev-server via Gulp, so my configurations stay in a JS object, not in command line arguments. 我曾经通过Gulp运行webpack-dev-server,所以我的配置保留在JS对象中,而不是在命令行参数中。

Try this: 尝试这个:

import webpack from 'webpack';

const common = {
  entry: {
    style: PATHS.style,
    app: [
       PATHS.jsComp,
       'webpack/hot/dev-server', `webpack-dev-server/client?http://localhost:3000`
    ]
  },
  output: {
    path: PATHS.build,
    filename: '[name].js'
  },
  module: {
    loaders: [
      { test: /\.jade$/, loader: "jade" }
    ]
  },
  devServer: {
    hot: true
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebpackPlugin({
      title: 'Webpack demo',
      template: PATHS.template, // Load a custom template (ejs by default but can be changed) 
    inject: 'body' // Inject all scripts into the body (this is the default so you can skip it) 
    })
  ]
};

Edit: 编辑:

After discussing in the comments, I think that my answer does not address your case. 在评论中讨论之后,我认为我的回答没有解决您的情况。 I thought you were trying to reload the application after some Jade module (say, some AngularJS component were using import with jade-loader to load a Javascript template string from a Jade file) were changed. 我以为您在更改了某些Jade 模块 (例如,某些AngularJS组件正在使用import with jade-loader来从Jade文件加载Javascript模板字符串)后尝试重新加载应用程序。 But as it turns out, you are trying to reload when your base HTML (well, Jade, but the base of your page) file is changed. 但事实证明,当您的基本HTML(Jade,但页面的基础)文件更改时,您正尝试重新加载。

If you are using some modern Javascript framework (React, Angular etc), chances are your base HTML will be very small, and the real thing happens in Javascript. 如果您使用的是现代Javascript框架(React,Angular等),则基本HTML可能很小,而真正的事情发生在Javascript中。

Anyway, I don't think that your need is currently supported neither by webpack-dev-server or html-webpack-plugin. 无论如何,我不认为webpack-dev-server或html-webpack-plugin目前都不支持您的需求。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM