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

时间:2016-05-13 03:30:52

标签: javascript json webpack webpack-dev-server webpack-hmr

当我在运行webpack-dev-server时,我吟唱我的app.js和main.css时,捆绑包会更新。 但是当我更改index.html服务器没有重新加载时,如果我向html添加一些行,webpack-dev-server不会重新加载页面。 这是我的webpack.config.js和package.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 个答案:

答案 0 :(得分:4)

添加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 :(得分:1)

这可能是由于您的IDE - 请参阅webpack dev server

  

使用支持“安全写入”的编辑器/ IDE ...此行为导致文件监视器丢失轨道,因为原始文件已被删除。为了防止出现此问题,您必须在编辑器中禁用“安全写入”功能。

答案 2 :(得分:1)

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

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