2
0

webpack.prod.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* eslint-disable */
  2. // Merges webpack.common config with this production config
  3. const merge = require('webpack-merge');
  4. const common = require('./webpack.common.js');
  5. const webpack = require('webpack');
  6. const CleanWebpackPlugin = require('clean-webpack-plugin');
  7. // Optimisations and Compression
  8. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  9. const TerserPlugin = require('terser-webpack-plugin');
  10. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  11. const CompressionPlugin = require('compression-webpack-plugin');
  12. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  13. const imageminMozjpeg = require('imagemin-mozjpeg');
  14. const fs = require('fs');
  15. const glob = require('glob');
  16. var WebpackOnBuildPlugin = require('on-build-webpack');
  17. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  18. const path = require('path')
  19. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  20. const PurgecssPlugin = require('purgecss-webpack-plugin')
  21. // Optional
  22. const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
  23. const PATHS = {
  24. src: path.join(__dirname, 'src')
  25. }
  26. module.exports = merge(common, {
  27. mode: 'production',
  28. stats: 'errors-only',
  29. optimization: {
  30. minimizer: [
  31. new TerserPlugin({
  32. test: /\.js(\?.*)?$/i,
  33. exclude: /node_modules/,
  34. cache: true,
  35. parallel: 4,
  36. sourceMap: true,
  37. }),
  38. new OptimizeCSSAssetsPlugin({})
  39. ],
  40. runtimeChunk: 'single',
  41. splitChunks: {
  42. chunks: 'all',
  43. // maxInitialRequests: Infinity,
  44. // minSize: 0,
  45. cacheGroups: {
  46. vendor: {
  47. test: /node_modules/, // you may add "vendor.js" here if you want to
  48. name: "node-modules",
  49. chunks: "initial",
  50. enforce: true
  51. },
  52. }
  53. },
  54. },
  55. plugins: [
  56. new MiniCssExtractPlugin({
  57. filename: 'css/[name].[hash:6].css',
  58. chunkFilename: 'css/[name].[contenthash].css',
  59. }),
  60. new ExtractTextPlugin('[name].css?[hash]'),
  61. new PurgecssPlugin({
  62. paths: glob.sync(`${PATHS.src}/*`),
  63. whitelist: ['whitelisted']
  64. }),
  65. new CleanWebpackPlugin(),
  66. new CompressionPlugin({
  67. test: /\.(js|css|html|svg)$/,
  68. filename: '[path].br[query]',
  69. algorithm: 'brotliCompress',
  70. compressionOptions: { level: 11 },
  71. threshold: 100,
  72. minRatio: 0.8,
  73. deleteOriginalAssets: false
  74. }),
  75. new CompressionPlugin({
  76. filename: '[path].gz[query]',
  77. algorithm: 'gzip',
  78. test: /\.js$|\.css$|\.html$/,
  79. threshold: 100,
  80. minRatio: 0.8,
  81. }),
  82. new ImageminPlugin({
  83. test: /\.(jpe?g|png|gif|svg)$/i,
  84. // lossLess gif compressor
  85. gifsicle: {
  86. optimizationLevel: 9
  87. },
  88. // lossy png compressor, remove for default lossLess
  89. pngquant: ({
  90. quality: '75'
  91. }),
  92. // lossy jpg compressor
  93. plugins: [imageminMozjpeg({
  94. quality: '75'
  95. })]
  96. }),
  97. // new FaviconsWebpackPlugin({
  98. // // Your source logo
  99. // logo: './src/assets/images/200px-ControllerAppIcon.png',
  100. // // // The prefix for all image files (might be a folder or a name)
  101. // //prefix: 'assets/icons_[hash:6]/',
  102. // prefix: 'icons_[hash:6]/',
  103. // // // Emit all stats of the generated icons
  104. // //emitStats: false,
  105. // // // The name of the json containing all favicon information
  106. // // statsFilename: 'iconstats-[hash].json',
  107. // // // Generate a cache file with control hashes and
  108. // // // don't rebuild the favicons until those hashes change
  109. // persistentCache: true,
  110. // // // Inject the html into the html-webpack-plugin
  111. // inject: true,
  112. // // // favicon background color (see https://github.com/haydenbleasel/favicons#usage)
  113. // background: '#fff',
  114. // // // which icons should be generated (see https://github.com/haydenbleasel/favicons#usage)
  115. // icons: {
  116. // // android: false,
  117. // // appleIcon: false,
  118. // favicons: true
  119. // // firefox: true,
  120. // // windows: false
  121. // }
  122. // }),
  123. new WebpackOnBuildPlugin(function(stats) {
  124. var getDirectories = function (src, callback) {
  125. glob(`${src}/**/*(*.gz|favicon-32x32.png)`, callback);
  126. };
  127. getDirectories('./webpack/dist', function (err, list) {
  128. if (err) {
  129. console.log('Error', err);
  130. } else {
  131. const regex = /^(.*\/)([^\/]*)$/
  132. const relativeRegex = /(\w+\/[^\/]*)$/
  133. const makePathRegex = /([^\.].*)$/
  134. let exportDefHead=
  135. '/***********************************\n'+
  136. 'webpack_headers\n'+
  137. stats+'\n'+
  138. '***********************************/\n'+
  139. '#pragma once\n'+
  140. '#include <inttypes.h>\n'+
  141. 'extern const char * resource_lookups[];\n'+
  142. 'extern const uint8_t * resource_map_start[];\n'+
  143. 'extern const uint8_t * resource_map_end[];\n';
  144. let exportDef= '// Automatically generated. Do not edit manually!.\n'+
  145. '#include <inttypes.h>\n';
  146. let lookupDef='const char * resource_lookups[] = {\n';
  147. let lookupMapStart='const uint8_t * resource_map_start[] = {\n';
  148. let lookupMapEnd='const uint8_t * resource_map_end[] = {\n';
  149. let cMake='';
  150. list.forEach(fileName=>{
  151. let exportName=fileName.match(regex)[2].replace(/[\. \-]/gm,'_');
  152. let relativeName=fileName.match(relativeRegex)[1];
  153. exportDef+= 'extern const uint8_t _'+exportName+'_start[] asm("_binary_'+exportName+'_start");\n'+
  154. 'extern const uint8_t _'+exportName+'_end[] asm("_binary_'+exportName+'_end");\n';
  155. lookupDef+='\t"/'+relativeName+'",\n';
  156. lookupMapStart+='\t_'+ exportName+'_start,\n';
  157. lookupMapEnd+= '\t_'+ exportName+'_end,\n';
  158. cMake+='target_add_binary_data( __idf_wifi-manager ./webapp'+fileName.match(makePathRegex)[1]+' BINARY)\n';
  159. });
  160. lookupDef+='""\n};\n';
  161. lookupMapStart=lookupMapStart.substring(0,lookupMapStart.length-2)+'\n};\n';
  162. lookupMapEnd=lookupMapEnd.substring(0,lookupMapEnd.length-2)+'\n};\n';
  163. try {
  164. fs.writeFileSync('webapp.cmake', cMake);
  165. fs.writeFileSync('webpack.c', exportDef+lookupDef+lookupMapStart+lookupMapEnd);
  166. fs.writeFileSync('webpack.h', exportDefHead);
  167. //file written successfully
  168. } catch (e) {
  169. console.error(e);
  170. }
  171. }
  172. });
  173. }),
  174. new BundleAnalyzerPlugin()
  175. ]
  176. });