vue打包cdn优化

vue打包cdn优化

这里的例子是对vue,vue-router,element-ui的cdn引入

  1. 安装webpack-bundle-analyzer,参考地址:https://github.com/webpack-contrib/webpack-bundle-analyzer

  2. 设置vue.config.js,参考地址:https://cli.vuejs.org/zh/config/#configurewebpack

    main.js

    import Vue from "vue";
    import App from "./App.vue";
    import router from "./router";
    import { store } from "./store/index";
    import ElementUI from "element-ui";
    import Vuex from "vuex";
    
    Vue.use(ElementUI);
    Vue.use(Vuex);
    
    Vue.config.productionTip = false;
    
    new Vue({
      store,
      router,
      render: (h) => h(App),
    }).$mount("#app");
    

    vue.config.js

    const BundleAnalyzerPlugin =
      require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
    
    module.exports = {
      //运行打包后的html必须要写
      //参考网址:https://cli.vuejs.org/zh/config/#publicpath
      publicPath: "./",
      configureWebpack: {
        externals: {
          //左边是import右边的值,右边是你定义的全局变量
          "vue": "Vue",
          //element-ui比较特殊,右边固定ELEMENT,与你定义的全局变量无关
          "element-ui": "ELEMENT",
          "vue-router": "VueRouter",
        },
        plugins: [new BundleAnalyzerPlugin()],
      },
    };
    
  3. 在public/index.html 注入cdn

    <!DOCTYPE html>
    <html lang="">
      <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />
        <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
        <title><%= htmlWebpackPlugin.options.title %></title>
      </head>
      <body>
        <noscript>
          <strong
            >We're sorry but <%= htmlWebpackPlugin.options.title %> 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://cdn.jsdelivr.net/npm/vue@2"></script>
        <!-- 引入样式 -->
        <link
          rel="stylesheet"
          href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
        />
        <!-- 引入组件库 -->
        <script src="https://unpkg.com/element-ui/lib/index.js"></script>
        <script src="https://unpkg.com/vue-router@3.2.0/dist/vue-router.js"></script>
      </body>
    </html>
    

注意

  1. 使用cdn时,要注意cdn引入的版本和你npm i 的版本是否一致
  2. cdn的资源可以参考bootCDN 地址:https://www.bootcdn.cn/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue项目的打包速度优化,可以尝试以下几种方法: 1. 使用CDN:将一些常用的第三方库(如VueVuex、Vue Router等)从本地打包中移除,改为通过CDN引入。这样可以减少打包体积和加快打包速度。 2. 按需引入组件:在使用UI组件库时,可以考虑按需引入组件,而不是全部引入。这可以通过babel-plugin-component等工具来实现,减少不必要的打包和编译时间。 3. 代码拆分:将大型的代码块拆分成更小的模块,利用Webpack的代码分割功能(如异步加载、按需加载)来实现懒加载。这样可以减少初始加载时间,提升用户体验。 4. 优化图片:对于项目中的图片资源,可以使用压缩工具来减小图片大小,如使用imagemin-webpack-plugin等插件进行图片压缩。 5. 缓存和持久化:合理利用浏览器缓存和服务端缓存,减少不必要的请求和加载时间。另外,可以考虑使用localStorage或IndexedDB等技术进行数据持久化,减少数据加载时间。 6. 使用Tree Shaking:通过配置Webpack的tree shaking功能,可以剔除项目中未使用的代码,减少打包体积和提升加载速度。 7. 配置合理的Webpack:根据项目需求,合理配置Webpack的各项参数,如使用cache-loader、thread-loader等插件来提升构建速度。 需要注意的是,优化策略要根据具体项目情况进行选择,不同的项目可能有不同的瓶颈和优化空间。可以通过Webpack Bundle Analyzer等工具来分析打包结果,找出优化的重点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值