production.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Note: You must restart bin/webpack-dev-server for changes to take effect
  2. const { createHash } = require('crypto');
  3. const { readFileSync } = require('fs');
  4. const { resolve } = require('path');
  5. const CompressionPlugin = require('compression-webpack-plugin');
  6. const TerserPlugin = require('terser-webpack-plugin');
  7. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  8. const { merge } = require('webpack-merge');
  9. const { InjectManifest } = require('workbox-webpack-plugin');
  10. const sharedConfig = require('./shared');
  11. const root = resolve(__dirname, '..', '..');
  12. module.exports = merge(sharedConfig, {
  13. mode: 'production',
  14. devtool: 'source-map',
  15. stats: 'normal',
  16. bail: true,
  17. optimization: {
  18. minimize: true,
  19. minimizer: [
  20. new TerserPlugin({
  21. cache: true,
  22. parallel: true,
  23. sourceMap: true,
  24. }),
  25. ],
  26. },
  27. plugins: [
  28. new CompressionPlugin({
  29. filename: '[path][base].gz[query]',
  30. cache: true,
  31. test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/,
  32. }),
  33. new CompressionPlugin({
  34. filename: '[path][base].br[query]',
  35. algorithm: 'brotliCompress',
  36. cache: true,
  37. test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/,
  38. }),
  39. new BundleAnalyzerPlugin({ // generates report.html
  40. analyzerMode: 'static',
  41. openAnalyzer: false,
  42. logLevel: 'silent', // do not bother Webpacker, who runs with --json and parses stdout
  43. }),
  44. new InjectManifest({
  45. additionalManifestEntries: ['1f602.svg', 'sheet_13.png'].map((filename) => {
  46. const path = resolve(root, 'public', 'emoji', filename);
  47. const body = readFileSync(path);
  48. const md5 = createHash('md5');
  49. md5.update(body);
  50. return {
  51. revision: md5.digest('hex'),
  52. url: `/emoji/${filename}`,
  53. };
  54. }),
  55. exclude: [
  56. /(?:base|extra)_polyfills-.*\.js$/,
  57. /locale_.*\.js$/,
  58. /mailer-.*\.(?:css|js)$/,
  59. ],
  60. include: [/\.js$/, /\.css$/],
  61. maximumFileSizeToCacheInBytes: 2 * 1_024 * 1_024, // 2 MiB
  62. swDest: resolve(root, 'public', 'packs', 'sw.js'),
  63. swSrc: resolve(root, 'app', 'javascript', 'mastodon', 'service_worker', 'entry.js'),
  64. }),
  65. ],
  66. });