production.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 { merge } = require('webpack-merge');
  6. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  7. const TerserPlugin = require('terser-webpack-plugin');
  8. const CompressionPlugin = require('compression-webpack-plugin');
  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 BundleAnalyzerPlugin({ // generates report.html
  34. analyzerMode: 'static',
  35. openAnalyzer: false,
  36. logLevel: 'silent', // do not bother Webpacker, who runs with --json and parses stdout
  37. }),
  38. new InjectManifest({
  39. additionalManifestEntries: ['1f602.svg', 'sheet_13.png'].map((filename) => {
  40. const path = resolve(root, 'public', 'emoji', filename);
  41. const body = readFileSync(path);
  42. const md5 = createHash('md5');
  43. md5.update(body);
  44. return {
  45. revision: md5.digest('hex'),
  46. url: `/emoji/${filename}`,
  47. };
  48. }),
  49. exclude: [
  50. /(?:base|extra)_polyfills-.*\.js$/,
  51. /locale_.*\.js$/,
  52. /mailer-.*\.(?:css|js)$/,
  53. ],
  54. include: [/\.js$/, /\.css$/],
  55. maximumFileSizeToCacheInBytes: 2 * 1_024 * 1_024, // 2 MiB
  56. swDest: resolve(root, 'public', 'packs', 'sw.js'),
  57. swSrc: resolve(root, 'app', 'javascript', 'mastodon', 'service_worker', 'entry.js'),
  58. }),
  59. ],
  60. });