webpack.prod.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Webpack production configuration (merged with common config).
  3. * it overrides webpack.common.js configuration and:
  4. * - Set mode to production (allow to minify css, js etc...)
  5. * - Add content hash to files name -> This way, each time bundled files content changed, file name will be different.
  6. * This allow browsers to keep cached files which did not change
  7. * - Minify/Uglify JS and CSS files
  8. * - Split js into two bundles: vendors (js comming from node_modules) and bundle (our own js)
  9. * This way, when we change our js, only our bundle is changed so browsers can keep vendors js in cache
  10. * - Allow Load css files (import './myCssFile.css') -> Css rules will be automatically added to index.html into a <style></style> tag.
  11. * - Allow to load fonts and images (import './myFont.eot'; import './someImage.jpg')
  12. * - Allow to load html angularjs partials (i.e all html files under src folder) as url ->
  13. *
  14. */
  15. const {merge} = require('webpack-merge');
  16. const common = require('./webpack.common');
  17. const path = require('path');
  18. const webpack = require('webpack');
  19. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  20. const TerserPlugin = require('terser-webpack-plugin');
  21. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  22. module.exports = merge(common, {
  23. mode: 'production',
  24. devtool: 'source-map',
  25. output: {
  26. // Add a chunkhash to file name so it will not be cached by browsers when content changed
  27. filename: '[name].[hash].bundle.js',
  28. // Path where bundled files will be output
  29. path: path.resolve(__dirname, './build'),
  30. // Path at which output assets will be served
  31. publicPath: '',
  32. },
  33. resolve: {
  34. symlinks: true,
  35. },
  36. plugins: [
  37. // Extract CSS into separated css files
  38. new MiniCssExtractPlugin({
  39. // Add a chunkhash to file name so it will not be cached by browsers when content changed
  40. filename: '[name].[hash].bundle.css',
  41. }),
  42. // see https://webpack.js.org/guides/caching#module-identifiers
  43. new webpack.HashedModuleIdsPlugin(),
  44. ],
  45. optimization: {
  46. // See https://webpack.js.org/guides/caching
  47. runtimeChunk: 'single',
  48. splitChunks: {
  49. cacheGroups: {
  50. // Bundle all initial chunks from node_modules into a "vendors" js file
  51. vendor: {
  52. name: 'vendors',
  53. test: /node_modules/,
  54. chunks: 'initial',
  55. },
  56. },
  57. },
  58. minimizer: [
  59. // JS minifier/uglifier
  60. new TerserPlugin({
  61. parallel: true,
  62. sourceMap: true,
  63. // Remove comments as well
  64. terserOptions: {output: {comments: false}},
  65. }),
  66. // CSS minifier
  67. new OptimizeCSSAssetsPlugin({
  68. cssProcessorPluginOptions: {
  69. preset: ['default', {discardComments: {removeAll: true}}],
  70. },
  71. }),
  72. ],
  73. },
  74. module: {
  75. rules: [
  76. // CSS files are bundled togethers
  77. {
  78. test: /\.css$/,
  79. use: [
  80. 'style-loader',
  81. {
  82. loader: MiniCssExtractPlugin.loader,
  83. options: {publicPath: ''},
  84. },
  85. 'css-loader',
  86. ],
  87. },
  88. {
  89. test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
  90. use: {
  91. loader: 'url-loader',
  92. },
  93. },
  94. // Load images as URLs
  95. {
  96. test: /\.(png|svg|jpg|gif)$/,
  97. use: {
  98. loader: 'url-loader',
  99. },
  100. },
  101. // Load locales files
  102. {
  103. test: /\.json$/,
  104. type: 'javascript/auto',
  105. include: path.resolve(__dirname, 'src/assets/locales'),
  106. use: [
  107. {
  108. loader: 'file-loader',
  109. options: {
  110. name: '[name].[contenthash].[ext]',
  111. outputPath: 'locales',
  112. },
  113. },
  114. ],
  115. },
  116. // AngularJS templates are cached using cache template
  117. {
  118. test: /\.html$/,
  119. exclude: path.resolve(__dirname, './src/index.html'),
  120. use: [
  121. {
  122. loader: 'html-loader',
  123. options: {minimize: true, caseSensitive: true},
  124. },
  125. ],
  126. },
  127. ],
  128. },
  129. });