vue中打包分析代码大小

admin2023-12-29 09:30:02
npm run preview -- --report

vue中打包分析代码大小

将几个比较大的多在打包时排除,这样可以缩小整体打包的大小,保证js的加载速度,排除的包采用cdn的方式用外链去引入,cdn本名为分发服务器,意为更近的访问区间更快的访问速度将所需要的文件返回给客户端

  • webpack排除打包-代码位置(vue.config.js)

configureWebpack: {
   // provide the app's title in webpack's name field, so that
   // it can be accessed in index.html to inject the correct title.
   name: name,
   resolve: {
     alias: {
       '@': resolve('src')
     }
   },
   // 配置需要排出的包
   externals: {
     'vue': 'Vue',//前面是包名,后面是替代的变量名称
     'element-ui': 'ELEMENT',
   }
 },
  • 在html中采用外链引入排除的文件-代码位置(public/index.html)

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
   <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
   <link rel="icon" href="<%= BASE_URL %>favicon.ico">
   <title><%= webpackConfig.name %></title>
   <link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.13/theme-chalk/index.min.css" rel="stylesheet">
 </head>
 <body>
   <noscript>
     <strong>We're sorry but <%= webpackConfig.name %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
   </noscript>
   <div id="app"></div>
   <!-- built files will be auto injected -->
   <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.min.js"></script>
   <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.13/index.min.js"></script>
 </body>
</html>