【问题标题】:index.html using webpack-dev-server not reloadindex.html 使用 webpack-dev-server 不重新加载
【发布时间】:2016-05-13 03:30:52
【问题描述】:

当我在 webpack-dev-server 运行时更改我的 app.jsmain.css 时,捆绑包会更新。 但是当我更改index.html 时,内容不会刷新;如果我在 HTML 中添加一行,webpack-dev-server 不会刷新页面上的任何内容。 这是我的webpack.config.jspackage.json 文件。 我希望你能帮助我。

webpack.config.js

var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server'); 
var CleanWebpackPlugin = require('clean-webpack-plugin'); 
var chalk = require('chalk');  
var env = process.env.WEBPACK_ENV;

var host = 'localhost';
var port = '8080';

var config = {
  devtool: 'source-map',
  entry: [
    'webpack/hot/dev-server',
    'webpack-dev-server/client?http://' + host + ':' + port +'/',
    './src/app.js'
  ],
  output: {
    path: __dirname + '/dist',
    filename: 'bundle.js'
  },
  module : {
    loaders: [
      { test: /\.css$/, loader: 'style-loader!css-loader' },
      { test: /\.html$/,loader: 'file?name=[name].[ext]' }
    ]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new CleanWebpackPlugin(['dist'], {
      root: __dirname,
      verbose: true,
      dry: false
    })
  ]
};

if (env === 'dev') {
  new WebpackDevServer(webpack(config), {
    contentBase: './dist/',
    stats: {colors: true},
    hot: true,
    debug: true
  }).listen(port, host, function (err, result) {
    if (err) {
      console.log(err);
    }
  });

  console.log('-------------------------');
  console.log(chalk.bold.white('Local web server runs at ') + chalk.green('http://' + host + ':' + port));
  console.log('-------------------------\n\n');
}

module.exports = config;

package.json

{
  "name": "webpack-skeleton",
  "version": "1.0.0",
  "description": "webpack skeleton",
  "main": "bundle.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "WEBPACK_ENV=dev ./node_modules/.bin/webpack --watch --inline"
  },
  "author": "Jose Roa",
  "license": "ISC",
  "devDependencies": {
    "chalk": "^1.1.3",
    "clean-webpack-plugin": "^0.1.9",
    "css-loader": "^0.23.1",
    "file-loader": "^0.8.5",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.0",
    "webpack-dev-server": "^1.14.1"
  }
}

我的目录结构:

css  
  main.css
dist
  bundle.js
  bundle.js.map
  index.html
node_modules
src
  app.js
  sum.js
package.json
index.html
node_modules
webpack.config.js

dist 目录下的每个文件都是由 webpack 生成的。

【问题讨论】:

    标签: javascript webpack webpack-dev-server html-webpack-plugin webpack-hmr


    【解决方案1】:

    添加 HtmlWebpackPlugin 链接:https://www.npmjs.com/package/html-webpack-plugin 添加

    // webpack.config.js
    const HtmlWebpackPlugin =  require('html-webpack-plugin');
    module.exports = {
        plugins: [
            new HtmlWebpackPlugin({
                template: './index.html'
            })
        ]
    };
    

    【讨论】:

      【解决方案2】:

      这可能是由于您的 IDE 造成的——请参阅 webpack dev server

      使用支持“安全写入”的编辑器/IDE...此行为会导致文件观察程序丢失轨道,因为原始文件已被删除。为了防止这个问题,你必须在你的编辑器中禁用“安全写入”功能。

      【讨论】:

        【解决方案3】:

        您可以在开发应用时尝试使用此解决方法来自动重新加载。添加到您的入口点('./src/app.js'):

        require('../index.html'); //workround to reload page on index.html changes
        

        【讨论】:

        • require('file!../index.html'); 如果您没有为 html 文件设置加载器