|
| 1 | +const webpack = require('webpack'); |
| 2 | +const path = require('path'); |
| 3 | +const HtmlWebpackPlugin = require('html-webpack-plugin'); |
| 4 | +const CompressionWebpackPlugin = require('compression-webpack-plugin'); |
| 5 | +const TerserPlugin = require('terser-webpack-plugin'); |
| 6 | +const workboxPlugin = require('workbox-webpack-plugin'); |
| 7 | +const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
| 8 | +const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); |
| 9 | +const ModernizrWebpackPlugin = require('modernizr-webpack-plugin'); |
| 10 | +const BundleAnalyzerPlugin = require('webpack-bundle-analyzer') |
| 11 | + .BundleAnalyzerPlugin; |
| 12 | + |
| 13 | +// #region constants |
| 14 | +const nodeModulesDir = path.join(__dirname, 'node_modules'); |
| 15 | +const indexFile = path.join(__dirname, 'src/index.tsx'); |
| 16 | +// #endregion |
| 17 | + |
| 18 | +const config = { |
| 19 | + target: 'web', |
| 20 | + mode: 'production', |
| 21 | + entry: { app: indexFile }, |
| 22 | + output: { |
| 23 | + path: path.join(__dirname, '/../docs/assets'), |
| 24 | + publicPath: '/assets/', |
| 25 | + filename: '[name].[hash].js', |
| 26 | + chunkFilename: '[name].[hash].js', |
| 27 | + }, |
| 28 | + resolve: { |
| 29 | + modules: ['src', 'node_modules'], |
| 30 | + extensions: ['.css', '.json', '.js', '.jsx', '.ts', '.tsx'], |
| 31 | + }, |
| 32 | + module: { |
| 33 | + rules: [ |
| 34 | + { |
| 35 | + test: /\.ts(x)?$/, |
| 36 | + use: ['awesome-typescript-loader'], |
| 37 | + exclude: [nodeModulesDir], |
| 38 | + }, |
| 39 | + { |
| 40 | + test: /\.css$/, |
| 41 | + use: ['style-loader', 'css-loader'], |
| 42 | + }, |
| 43 | + { |
| 44 | + test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif)(\?\S*)?$/, |
| 45 | + use: [ |
| 46 | + { |
| 47 | + loader: 'url-loader', |
| 48 | + options: { |
| 49 | + limit: 100000, |
| 50 | + name: '[name].[ext]', |
| 51 | + }, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + optimization: { |
| 58 | + runtimeChunk: false, |
| 59 | + splitChunks: { |
| 60 | + cacheGroups: { |
| 61 | + commons: { |
| 62 | + test: /[\\/]node_modules[\\/]/, |
| 63 | + name: 'vendors', |
| 64 | + chunks: 'all', |
| 65 | + }, |
| 66 | + styles: { |
| 67 | + name: 'styles', |
| 68 | + test: /\.css$/, |
| 69 | + chunks: 'all', |
| 70 | + enforce: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + minimizer: [ |
| 75 | + new MiniCssExtractPlugin({ |
| 76 | + filename: '[name].[hash].css', |
| 77 | + chunkFilename: '[id].[hash].css', |
| 78 | + }), |
| 79 | + new TerserPlugin({ |
| 80 | + cache: true, |
| 81 | + parallel: true, |
| 82 | + sourceMap: true, |
| 83 | + }), |
| 84 | + new OptimizeCSSAssetsPlugin({}), |
| 85 | + ], |
| 86 | + }, |
| 87 | + plugins: [ |
| 88 | + new HtmlWebpackPlugin({ |
| 89 | + template: 'src/index.html', |
| 90 | + filename: '../index.html', // hack since outPut path would place in '/dist/assets/' in place of '/dist/' |
| 91 | + }), |
| 92 | + new webpack.DefinePlugin({ |
| 93 | + 'process.env': { |
| 94 | + NODE_ENV: JSON.stringify('production'), |
| 95 | + }, |
| 96 | + }), |
| 97 | + new ModernizrWebpackPlugin({ |
| 98 | + htmlWebpackPlugin: true, |
| 99 | + }), |
| 100 | + new MiniCssExtractPlugin({ |
| 101 | + filename: '[name].[hash].css', |
| 102 | + chunkFilename: '[id].[hash].css', |
| 103 | + }), |
| 104 | + new CompressionWebpackPlugin({ |
| 105 | + filename: '[path].gz[query]', |
| 106 | + algorithm: 'gzip', |
| 107 | + test: new RegExp('\\.(js|css)$'), |
| 108 | + threshold: 10240, |
| 109 | + minRatio: 0.8, |
| 110 | + }), |
| 111 | + new workboxPlugin.GenerateSW({ |
| 112 | + swDest: 'sw.js', |
| 113 | + clientsClaim: true, |
| 114 | + skipWaiting: true, |
| 115 | + }), |
| 116 | + new BundleAnalyzerPlugin(), |
| 117 | + ], |
| 118 | +}; |
| 119 | + |
| 120 | +module.exports = config; |
0 commit comments