vue打包之后体积过大的问题处理----vue.config.js


基于vue-cli4.0     老样子   话不多说  直接上代码:

  1 // Vue.config.js 配置选项
  2 
  3 const webpack = require('webpack')
  4 
  5 // 判断开发环境
  6 
  7 const isProduction = process.env.NODE_ENV === 'production';
  8 
  9 module.exports = {
 10 
 11   // publicPath: process.env.NODE_ENV === 'production'
 12 
 13   // ? './'
 14 
 15   // : '/',
 16 
 17   publicPath: "./",
 18 
 19   configureWebpack: {
 20 
 21     plugins: [
 22 
 23       new webpack.ProvidePlugin({
 24 
 25         $: "jquery",
 26 
 27         jQuery: "jquery",
 28 
 29         "windows.jQuery": "jquery"
 30 
 31       })
 32 
 33     ]
 34 
 35   },
 36 
 37   //webpack配置
 38 
 39   configureWebpack: config => {
 40 
 41     // 开启gzip压缩
 42 
 43     if (isProduction) {
 44 
 45       // config.plugins.push(new CompressionWebpackPlugin({
 46 
 47       //   algorithm: 'gzip',
 48 
 49       //   test: /\.js$|\.html$|\.json$|\.css/,
 50 
 51       //   threshold: 10240,
 52 
 53       //   minRatio: 0.8
 54 
 55       // }));
 56 
 57       // 开启分离js
 58 
 59       config.optimization = {
 60 
 61         runtimeChunk: 'single',
 62 
 63         splitChunks: {
 64 
 65           chunks: 'all',
 66 
 67           maxInitialRequests: Infinity,
 68 
 69           minSize: 20000,
 70 
 71           cacheGroups: {
 72 
 73             vendor: {
 74 
 75               test: /[\\/]node_modules[\\/]/,
 76 
 77               name(module) {
 78 
 79                 // get the name. E.g. node_modules/packageName/not/this/part.js
 80 
 81                 // or node_modules/packageName
 82 
 83                 const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
 84 
 85                 // npm package names are URL-safe, but some servers don't like @ symbols
 86 
 87                 return `npm.${packageName.replace('@', '')}`
 88 
 89               }
 90 
 91             }
 92 
 93           }
 94 
 95         }
 96 
 97       };
 98 
 99       // 取消webpack警告的性能提示
100 
101       config.performance = {
102 
103         hints: 'warning',
104 
105         //入口起点的最大体积
106 
107         maxEntrypointSize: 50000000,
108 
109         //生成文件的最大体积
110 
111         maxAssetSize: 30000000,
112 
113         //只给出 js 文件的性能提示
114 
115         assetFilter: function (assetFilename) {
116 
117           return assetFilename.endsWith('.js');
118 
119         }
120 
121       }
122 
123     }
124 
125   },
126 
127   chainWebpack(config) {
128 
129     // it can improve the speed of the first screen, it is recommended to turn on preload
130 
131     config.plugin('preload').tap(() => [{
132 
133       rel: 'preload',
134 
135       // to ignore runtime.js
136 
137       // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
138 
139       fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
140 
141       include: 'initial'
142 
143     }])
144 
145     // when there are many pages, it will cause too many meaningless requests
146 
147     config.plugins.delete('prefetch')
148 
149     // 其他配置
150 
151     // config.entry('main').add('babel-polyfill') // main是入口js文件
152 
153     config
154 
155       .when(process.env.NODE_ENV !== 'development',
156 
157         config => {
158 
159           config
160 
161             .optimization.splitChunks({
162 
163               chunks: 'all',
164 
165               cacheGroups: {
166 
167                 libs: {
168 
169                   name: 'chunk-libs',
170 
171                   test: /[\\/]node_modules[\\/]/,
172 
173                   priority: 10,
174 
175                   chunks: 'initial' // only package third parties that are initially dependent
176 
177                 },
178 
179                 elementUI: {
180 
181                   name: 'chunk-elementUI', // split elementUI into a single package
182 
183                   priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
184 
185                   test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
186 
187                 }
188 
189               }
190 
191             })
192 
193           // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
194 
195           config.optimization.runtimeChunk('single')
196 
197         }
198 
199       )
200 
201   },
202 
203   css: {
204 
205     loaderOptions: {
206 
207       less: {
208 
209         javascriptEnabled: true
210 
211       }
212 
213     },
214 
215     extract: true, // 是否使用css分离插件 ExtractTextPlugin
216 
217     sourceMap: false, // 开启 CSS source maps
218 
219     modules: false // 启用 CSS modules for all css / pre-processor files.
220 
221   },
222 
223   // 打包时不生成.map文件
224 
225   productionSourceMap: false
226 
227   // 配置 webpack-dev-server 行为。
228 
229   // devServer: {
230 
231   //   open: process.platform === 'darwin',
232 
233   //   host: '0.0.0.0',
234 
235   //   port: 8080,
236 
237   //   https: false,
238 
239   //   hotOnly: false,
240 
241   //   // 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/cli-service.md#配置代理
242 
243   //   proxy: {
244 
245   //     '/api': {
246 
247   //       target: "http://qsdx.hh.g.cn:96", //正式环境测试版本
248 
249   //       // target: "http://192.168.2.228:1102", //luzongyu
250 
251   //       changeOrigin: true,
252 
253   //       pathRewrite: {
254 
255   //         "^/api": "/"
256 
257   //       }
258 
259   //     }
260 
261   //   }, // string | Object
262 
263   //   before: app => {}
264 
265   // }
266 
267 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM