production.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Note: You must restart bin/webpack-dev-server for changes to take effect
  2. const path = require('path');
  3. const { URL } = require('url');
  4. const merge = require('webpack-merge');
  5. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  6. const OfflinePlugin = require('offline-plugin');
  7. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  8. const CompressionPlugin = require('compression-webpack-plugin');
  9. const { output } = require('./configuration');
  10. const sharedConfig = require('./shared');
  11. let attachmentHost;
  12. if (process.env.S3_ENABLED === 'true') {
  13. if (process.env.S3_ALIAS_HOST || process.env.S3_CLOUDFRONT_HOST) {
  14. attachmentHost = process.env.S3_ALIAS_HOST || process.env.S3_CLOUDFRONT_HOST;
  15. } else {
  16. attachmentHost = process.env.S3_HOSTNAME || `s3-${process.env.S3_REGION || 'us-east-1'}.amazonaws.com`;
  17. }
  18. } else if (process.env.SWIFT_ENABLED === 'true') {
  19. const { host } = new URL(process.env.SWIFT_OBJECT_URL);
  20. attachmentHost = host;
  21. } else {
  22. attachmentHost = null;
  23. }
  24. module.exports = merge(sharedConfig, {
  25. mode: 'production',
  26. devtool: 'source-map',
  27. stats: 'normal',
  28. bail: true,
  29. optimization: {
  30. minimize: true,
  31. minimizer: [
  32. new UglifyJsPlugin({
  33. cache: true,
  34. parallel: true,
  35. sourceMap: true,
  36. uglifyOptions: {
  37. compress: {
  38. warnings: false,
  39. },
  40. output: {
  41. comments: false,
  42. },
  43. },
  44. }),
  45. ],
  46. },
  47. plugins: [
  48. new CompressionPlugin({
  49. filename: '[path].gz[query]',
  50. cache: true,
  51. test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/,
  52. }),
  53. new BundleAnalyzerPlugin({ // generates report.html
  54. analyzerMode: 'static',
  55. openAnalyzer: false,
  56. logLevel: 'silent', // do not bother Webpacker, who runs with --json and parses stdout
  57. }),
  58. new OfflinePlugin({
  59. publicPath: output.publicPath, // sw.js must be served from the root to avoid scope issues
  60. caches: {
  61. main: [':rest:'],
  62. additional: [':externals:'],
  63. optional: [
  64. '**/locale_*.js', // don't fetch every locale; the user only needs one
  65. '**/*_polyfills-*.js', // the user may not need polyfills
  66. '**/*.woff2', // the user may have system-fonts enabled
  67. // images/audio can be cached on-demand
  68. '**/*.png',
  69. '**/*.jpg',
  70. '**/*.jpeg',
  71. '**/*.svg',
  72. '**/*.mp3',
  73. '**/*.ogg',
  74. ],
  75. },
  76. externals: [
  77. '/emoji/1f602.svg', // used for emoji picker dropdown
  78. '/emoji/sheet_10.png', // used in emoji-mart
  79. ],
  80. excludes: [
  81. '**/*.gz',
  82. '**/*.map',
  83. 'stats.json',
  84. 'report.html',
  85. // any browser that supports ServiceWorker will support woff2
  86. '**/*.eot',
  87. '**/*.ttf',
  88. '**/*-webfont-*.svg',
  89. '**/*.woff',
  90. ],
  91. ServiceWorker: {
  92. entry: `imports-loader?ATTACHMENT_HOST=>${encodeURIComponent(JSON.stringify(attachmentHost))}!${encodeURI(path.join(__dirname, '../../app/javascript/mastodon/service_worker/entry.js'))}`,
  93. cacheName: 'mastodon',
  94. output: '../assets/sw.js',
  95. publicPath: '/sw.js',
  96. minify: true,
  97. },
  98. }),
  99. ],
  100. });