webpack.config.prod.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const merge = require('webpack-merge')
  2. const TerserPlugin = require('terser-webpack-plugin')
  3. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  5. const base = require('./webpack.config')
  6. // Use "smart" merge: attempts to combine loaders targeting the same file type,
  7. // overriding the base config
  8. module.exports = merge.smart(
  9. base,
  10. {
  11. mode: 'production',
  12. // Enable a full source map. Generates a comment linking to the source map
  13. devtool: 'hidden-source-map',
  14. output: {
  15. // Override filename to include hash for immutable caching
  16. filename: 'js/[name]-[chunkhash].js'
  17. },
  18. module: {
  19. rules: [
  20. {
  21. // Override base font loading to add hash to filename so that we can
  22. // use "immutable" caching
  23. test: /\.(woff|woff2)$/,
  24. use: [
  25. {
  26. loader: 'file-loader',
  27. options: {
  28. outputPath: 'fonts',
  29. publicPath: '/fonts/',
  30. name: '[name]-[hash].[ext]'
  31. }
  32. }
  33. ]
  34. }
  35. ]
  36. },
  37. optimization: {
  38. // Minify JS (with Terser) and CSS (with cssnano)
  39. minimizer: [new TerserPlugin(), new OptimizeCssAssetsPlugin()]
  40. },
  41. plugins: [
  42. // Extract CSS to a separate file (rather than inlining to a <style> tag)
  43. new MiniCssExtractPlugin({
  44. // Output to public/stylesheets directory and append hash for immutable
  45. // caching
  46. filename: 'stylesheets/[name]-[chunkhash].css'
  47. })
  48. ]
  49. },
  50. {}
  51. )