webpack.prod.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. options: {
  100. esModule: false,
  101. },
  102. },
  103. },
  104. // Load locales files
  105. {
  106. test: /\.json$/,
  107. type: 'javascript/auto',
  108. include: path.resolve(__dirname, 'src/assets/locales'),
  109. use: [
  110. {
  111. loader: 'file-loader',
  112. options: {
  113. name: '[name].[contenthash].[ext]',
  114. outputPath: 'locales',
  115. },
  116. },
  117. ],
  118. },
  119. // AngularJS templates are cached using cache template
  120. {
  121. test: /\.html$/,
  122. exclude: path.resolve(__dirname, './src/index.html'),
  123. use: [
  124. 'ng-cache-loader?prefix=[dir]/[dir]',
  125. 'extract-loader',
  126. {
  127. loader: 'html-loader',
  128. options: {minimize: true},
  129. },
  130. ],
  131. },
  132. ],
  133. },
  134. });