简体   繁体   English

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

[英]index.html using webpack-dev-server not reload

When I change my app.js and main.css while webpack-dev-server is running, the bundle is updated.当我在webpack-dev-server运行时更改我的app.jsmain.css时,包会更新。 But when i change the index.html the content is not refreshed;但是当我更改index.html时,内容不会刷新; if I add a line to the HTML, the webpack-dev-server does not refresh anything on the page.如果我在 HTML 中添加一行, webpack-dev-server不会刷新页面上的任何内容。 Here are my webpack.config.js and package.json files.这是我的webpack.config.jspackage.json文件。 I hope you can help me.我希望你能帮助我。

webpack.config.js : 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 :包.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"
  }
}

My directory structure:我的目录结构:

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

Every file inside the dist directory is generated by webpack. dist目录中的每个文件都是由 webpack 生成的。

add the HtmlWebpackPlugin link: https://www.npmjs.com/package/html-webpack-plugin add添加 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'
        })
    ]
};

It can be due to your IDE -- see webpack dev server这可能是由于您的 IDE——请参阅webpack dev server

Working with editors/IDEs supporting “safe write”... This behaviour causes file watcher to lose the track because the original file is removed.使用支持“安全写入”的编辑器/IDE...此行为会导致文件观察器丢失轨道,因为原始文件已被删除。 In order to prevent this issue, you have to disable “safe write” feature in your editor.为了防止这个问题,你必须在你的编辑器中禁用“安全写入”功能。

You can try this workaround for auto reloading while developing an app.您可以在开发应用程序时尝试使用此解决方法来自动重新加载。 Add to your entry point ('./src/app.js'):添加到您的入口点('./src/app.js'):

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

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

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