|
| 1 | +// 引入path 解决路径 |
| 2 | +const path = require('path') |
| 3 | +// 引入eslint插件 |
| 4 | +const EslintWebpackPlugin = require('eslint-webpack-plugin') |
| 5 | +// 引入 html 插件 |
| 6 | +const HtmlWebpackPlugin = require('html-webpack-plugin') |
| 7 | +// 提取 CSS 为单独文件 |
| 8 | +const MinniCssExtractPlugin = require('mini-css-extract-plugin') |
| 9 | +// 压缩Css |
| 10 | +const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin') |
| 11 | +// 压缩js |
| 12 | +const TerserWebpackPlugin = require('terser-webpack-plugin') |
| 13 | +// 压缩 图片 |
| 14 | +const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin') |
| 15 | +// 复制静态文件 |
| 16 | +const CopyPlugin = require('copy-webpack-plugin') |
| 17 | +// 开启热更新 |
| 18 | +const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin') |
| 19 | +console.log(process.env.NODE_ENV) |
| 20 | +// 判断是否是生产模式 |
| 21 | +const isProd = process.env.NODE_ENV === 'production' |
| 22 | +const getStyleLoaders = (loader) => { |
| 23 | + return [ |
| 24 | + isProd ? MinniCssExtractPlugin.loader : 'style-loader', |
| 25 | + 'css-loader', |
| 26 | + { |
| 27 | + // 处理css兼容性问题 |
| 28 | + // 配合 package.json 中browserslist 来指定兼容性 |
| 29 | + loader: 'postcss-loader', |
| 30 | + options: { |
| 31 | + postcssOptions: { |
| 32 | + plugins: ['postcss-preset-env'], |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + loader && { |
| 37 | + loader: loader, |
| 38 | + options: |
| 39 | + loader === 'less-loader' |
| 40 | + ? { |
| 41 | + // antd 主题配置 |
| 42 | + // https://github.com/ant-design/ant-design/blob/master/components/style/themes/default.less |
| 43 | + lessOptions: { |
| 44 | + modifyVars: { '@primary-color': '#1DA57A' }, |
| 45 | + javascriptEnabled: true, |
| 46 | + }, |
| 47 | + } |
| 48 | + : {}, |
| 49 | + }, |
| 50 | + ].filter(Boolean) |
| 51 | +} |
| 52 | + |
| 53 | +module.exports = { |
| 54 | + entry: './src/main.tsx', |
| 55 | + output: { |
| 56 | + path: isProd ? path.resolve(__dirname, '../dist') : undefined, |
| 57 | + // 入口文件名 |
| 58 | + filename: isProd |
| 59 | + ? 'static/js/[name].[contenthash:10].js' |
| 60 | + : 'static/js/[name].js', |
| 61 | + // chunk 文件路径 |
| 62 | + chunkFilename: isProd |
| 63 | + ? 'static/js/[name].[contenthash:10].chunk.js' |
| 64 | + : 'static/js/[name].chunk.js', |
| 65 | + // asset 处理文件路径 |
| 66 | + assetModuleFilename: 'static/media/[hash:10][ext][query]', |
| 67 | + // 清除上次 dist文件 |
| 68 | + clean: isProd, |
| 69 | + }, |
| 70 | + module: { |
| 71 | + rules: [ |
| 72 | + // 处理css |
| 73 | + { |
| 74 | + test: /\.css$/, |
| 75 | + use: getStyleLoaders(), |
| 76 | + }, |
| 77 | + { |
| 78 | + test: /\.less$/, |
| 79 | + use: getStyleLoaders('less-loader'), |
| 80 | + }, |
| 81 | + { |
| 82 | + test: /\.s[ac]ss$/, |
| 83 | + use: getStyleLoaders('sass-loader'), |
| 84 | + }, |
| 85 | + { |
| 86 | + test: /\.styl$/, |
| 87 | + use: getStyleLoaders('stylus-loader'), |
| 88 | + }, |
| 89 | + // 处理图片 |
| 90 | + { |
| 91 | + test: /\.(jpe?g|png|gif|svg|webp)$/, |
| 92 | + type: 'asset', |
| 93 | + parser: { |
| 94 | + dataUrlCondition: { |
| 95 | + // 如果没有超出maxSize 将会被转化成base64 格式输出 |
| 96 | + maxSize: 10 * 1024, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + // 其他资源 |
| 101 | + { |
| 102 | + test: /\.(woff2?|ttf)$/, |
| 103 | + type: 'asset/resource', |
| 104 | + }, |
| 105 | + // 处理js |
| 106 | + { |
| 107 | + test: /\.(ts|tsx|js|jsx)$/, |
| 108 | + // 只处理src文件下的 jsx |
| 109 | + include: path.resolve(__dirname, '../src'), |
| 110 | + loader: 'babel-loader', |
| 111 | + options: { |
| 112 | + cacheDirectory: true, // 开启 js 缓存 |
| 113 | + cacheCompression: false, // 关闭 js 压缩 |
| 114 | + plugins: [!isProd && 'react-refresh/babel'].filter(Boolean), // 开启HMR |
| 115 | + }, |
| 116 | + }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + //处理 html |
| 120 | + plugins: [ |
| 121 | + new EslintWebpackPlugin({ |
| 122 | + // 处理文件路径 |
| 123 | + context: path.resolve(__dirname, '../src'), |
| 124 | + //不包含哪些文件 |
| 125 | + exclude: 'node_modules', |
| 126 | + cache: true, |
| 127 | + // 开启eslint 缓存 |
| 128 | + cacheLocation: path.resolve( |
| 129 | + __dirname, |
| 130 | + '../node_modules/.cache/.eslintcache' |
| 131 | + ), |
| 132 | + }), |
| 133 | + new HtmlWebpackPlugin({ |
| 134 | + template: path.resolve(__dirname, '../public/index.html'), |
| 135 | + }), |
| 136 | + isProd && |
| 137 | + new MinniCssExtractPlugin({ |
| 138 | + filename: 'static/css/[name].[contenthash:10].css', |
| 139 | + chunkFilename: 'static/css/[name].[contenthash:10].chunk.css', |
| 140 | + }), |
| 141 | + // 复制 public 下的文件 到 dist目录中 |
| 142 | + isProd && |
| 143 | + new CopyPlugin({ |
| 144 | + patterns: [ |
| 145 | + { |
| 146 | + from: path.resolve(__dirname, '../public'), // 来自 |
| 147 | + to: path.resolve(__dirname, '../dist'), // 去处 |
| 148 | + globOptions: { |
| 149 | + // 因为已经做过 html 处理 所以要忽略掉 index.html |
| 150 | + ignore: ['**/index.html'], |
| 151 | + }, |
| 152 | + }, |
| 153 | + ], |
| 154 | + }), |
| 155 | + !isProd && new ReactRefreshWebpackPlugin(), |
| 156 | + ].filter(Boolean), |
| 157 | + // 开启测试环境 |
| 158 | + mode: isProd ? 'production' : 'development', |
| 159 | + // 开启错误提示 |
| 160 | + devtool: isProd ? 'source-map' : 'cheap-module-source-map', |
| 161 | + // 压缩操作 |
| 162 | + optimization: { |
| 163 | + // 代码分割 |
| 164 | + splitChunks: { |
| 165 | + chunks: 'all', |
| 166 | + cacheGroups: { |
| 167 | + // react react-dom react-router-dom 一起打包成一个js文件 |
| 168 | + react: { |
| 169 | + test: /[\\/]node_modules[\\/]react(.*)?[\\/]/, |
| 170 | + name: 'chunk-react', |
| 171 | + priority: 40, |
| 172 | + }, |
| 173 | + // antd 打包在一起 |
| 174 | + antd: { |
| 175 | + test: /[\\/]node_modules[\\/]antd[\\/]/, |
| 176 | + name: 'chunk-antd', |
| 177 | + priority: 30, |
| 178 | + }, |
| 179 | + // 剩下的 node_modules 打包一起 |
| 180 | + libs: { |
| 181 | + test: /[\\/]node_modules[\\/]/, |
| 182 | + name: 'chunk-libs', |
| 183 | + priority: 20, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + runtimeChunk: { |
| 188 | + name: (entrypoint) => `runtime~${entrypoint.name}.js`, |
| 189 | + }, |
| 190 | + minimize: isProd, |
| 191 | + minimizer: [ |
| 192 | + new CssMinimizerWebpackPlugin(), |
| 193 | + new TerserWebpackPlugin(), |
| 194 | + new ImageMinimizerPlugin({ |
| 195 | + minimizer: { |
| 196 | + implementation: ImageMinimizerPlugin.imageminMinify, |
| 197 | + options: { |
| 198 | + // Lossless optimization with custom option |
| 199 | + // Feel free to experiment with options for better result for you |
| 200 | + plugins: [ |
| 201 | + ['gifsicle', { interlaced: true }], |
| 202 | + ['jpegtran', { progressive: true }], |
| 203 | + ['optipng', { optimizationLevel: 5 }], |
| 204 | + // Svgo configuration here https://github.com/svg/svgo#configuration |
| 205 | + [ |
| 206 | + 'svgo', |
| 207 | + { |
| 208 | + plugins: [ |
| 209 | + 'preset-default', |
| 210 | + 'prefixIds', |
| 211 | + { |
| 212 | + name: 'sortAttrs', |
| 213 | + params: { |
| 214 | + xmlnsOrder: 'alphabetical', |
| 215 | + }, |
| 216 | + }, |
| 217 | + ], |
| 218 | + }, |
| 219 | + ], |
| 220 | + ], |
| 221 | + }, |
| 222 | + }, |
| 223 | + }), |
| 224 | + ], |
| 225 | + }, |
| 226 | + // webpack 解析模块加载选项 |
| 227 | + resolve: { |
| 228 | + // 自动补全文件扩展名 |
| 229 | + extensions: ['.ts', '.tsx', '.jsx', '.js', '.json'], |
| 230 | + }, |
| 231 | + devServer: { |
| 232 | + host: '0.0.0.0', |
| 233 | + port: '8080', |
| 234 | + open: true, |
| 235 | + hot: true, |
| 236 | + historyApiFallback: true, // 解决前端路由刷新404问题 |
| 237 | + }, |
| 238 | + performance: false, // 关闭性能分析, 提升打包速度 |
| 239 | +} |
0 commit comments