2017-09-17 47 views
0

我有一个使用html-webpack-plugin和webpack的问题,其中html-webpack-plugin正在将脚本注入到index.html的主体中。这显然会导致webpack不断刷新,并且每次都会生成新文件。首先,我如何防止这种情况发生,其次,有没有办法配置html-webpack-plugin来删除相应于不再存在的webpack生成的javascript文件的脚本(在这种情况下,过时的文件是相应的在下面的配置文件中输出static/bundle-[hash].js)?html-webpack插件注入和webpack导致持续刷新

这里是的WebPack配置文件:

const CleanWebpackPlugin = require('clean-webpack-plugin'); 
const HtmlWebpackPlugin = require('html-webpack-plugin'); 

module.exports = { 
    entry: "./js/main.js", 
    output: { 
     filename: "static/bundle-[hash].js", 
    }, 
    resolveLoader: { 
    moduleExtensions: ['-loader'] 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.jsx?$/, 
       exclude: /(node_modules|bower_components)/, 
       loader: 'babel', 
       query: { 
        presets: ['react', 'es2015', 'stage-0'] 
       } 
      }, 
      { 
       test: /\.css$/, 
       loader: 'style-loader', 
      }, 
      { 
       test: /\.css$/, 
       loader: 'css-loader', 
       query: { 
        modules: true, 
        localIdentName: '[name]__[local]___[hash:base64:5]' 
       } 
      } 
     ] 
    }, 
    plugins: [ 
     new CleanWebpackPlugin(['static/bundle*.js']), 
     new HtmlWebpackPlugin({ 
      filename: 'index.html', 
      template: 'index.html', 
      inject: 'body' 
     }) 
    ] 
}; 

回答

0

doc您可以让插件为您生成一个HTML文件,提供自己的模板

所以,要么你可以生成HTML文件通过给文件名称或者您可以通过一个模板,如index.html

0

在开发中,您应该删除文件名中的所有散列以防止此行为!

Webpack可以用热重新加载更新带有新内容的旧文件。