簡體   English   中英

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

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

當我在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;

包.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 生成的。

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

這可能是由於您的 IDE——請參閱webpack dev server

使用支持“安全寫入”的編輯器/IDE...此行為會導致文件觀察器丟失軌道,因為原始文件已被刪除。 為了防止這個問題,你必須在你的編輯器中禁用“安全寫入”功能。

您可以在開發應用程序時嘗試使用此解決方法來自動重新加載。 添加到您的入口點('./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