webpack.config.prod.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(base, {
  9. mode: 'production',
  10. // Enable a full source map. Generates a comment linking to the source map
  11. devtool: 'source-map',
  12. output: {
  13. // Override filename to include hash for immutable caching
  14. filename: 'js/[name]-[chunkhash].js'
  15. },
  16. module: {
  17. rules: [
  18. {
  19. // Override base font loading to add hash to filename so that we can
  20. // use "immutable" caching
  21. test: /\.(woff|woff2)$/,
  22. use: [
  23. {
  24. loader: 'file-loader',
  25. options: {
  26. outputPath: 'fonts',
  27. publicPath: '/fonts/',
  28. name: '[name]-[hash].[ext]'
  29. }
  30. }
  31. ]
  32. }
  33. ]
  34. },
  35. optimization: {
  36. // Minify JS (with Terser) and CSS (with cssnano)
  37. minimizer: [new TerserPlugin(), new OptimizeCssAssetsPlugin()]
  38. },
  39. plugins: [
  40. // Extract CSS to a separate file (rather than inlining to a <style> tag)
  41. new MiniCssExtractPlugin({
  42. // Output to public/stylesheets directory and append hash for immutable caching
  43. filename: 'stylesheets/[name]-[chunkhash].css'
  44. })
  45. ]
  46. })