index.html使用webpack-dev-server不重新加载

9
当我在运行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生成的。

3个回答

5

添加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'
        })
    ]
};

1

可能是由于您的IDE引起的--请参见webpack dev server

与支持“安全写入”的编辑器/IDE一起工作...此行为会导致文件监视器失去跟踪,因为原始文件被删除。为了防止这个问题,您需要在编辑器中禁用“安全写入”功能。


-1

在开发应用程序时,您可以尝试此解决方法以实现自动重新加载。将以下代码添加到您的入口点('./src/app.js')中:

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

require('file!../index.html'); 如果您没有为HTML文件设置加载程序。 - David Burrows
不再起作用 - jremydeaton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接