What is HtmlWebpackPlugin and why is it useful?

Beginner

Answer

HtmlWebpackPlugin automatically generates HTML files that include your Webpack bundles. It's useful because:

  • Automatic injection: Automatically adds script and link tags for your bundles
  • Template support: Can use HTML templates with variables
  • Multiple pages: Can generate multiple HTML files
  • Cache busting: Works with filename hashing for cache invalidation
new HtmlWebpackPlugin({
  template: './src/index.html',
  filename: 'index.html',
  chunks: ['app'], // Only include specific chunks
  minify: process.env.NODE_ENV === 'production'
})