webpack.config.prod.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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: [
  40. new TerserPlugin({
  41. terserOptions: {
  42. keep_classnames: /Error$/,
  43. keep_fnames: /Error$/,
  44. },
  45. }),
  46. new OptimizeCssAssetsPlugin(),
  47. ],
  48. },
  49. plugins: [
  50. // Extract CSS to a separate file (rather than inlining to a <style> tag)
  51. new MiniCssExtractPlugin({
  52. // Output to public/stylesheets directory and append hash for immutable
  53. // caching
  54. filename: 'stylesheets/[name]-[chunkhash].css',
  55. }),
  56. ],
  57. },
  58. {}
  59. )