The CDN of Vue3 series introduces dependent packages to optimize the packaging rate

be careful!!! be careful!!! be careful!!! This article is based on Webpack packaging

I summary

  • Benefits of using CDN
    • Relieve the pressure on the server and allocate the requests when the first screen is loaded to other servers
    • Optimize the packaged verdor JS too big problem
    • Speed up the first screen loading
    • Speed up packaging
  • In particular, Vue3's new tree shaking technology only packs the module s that need to be loaded, which will be even stronger with CDN!

II CDN website sharing

Switch relevant CDN s as needed
Not all dependent references are available. Some js will be incompatible. Try to reference after they are available!

III Comparison before and after packaging

It can be seen from the comparison that the package size is significantly smaller after the dependency package is introduced through CDN

  • Before using CDN
    • This figure shows the package size after the complete introduction of element plus
    • Even by introducing element plus on demand, the packaged size is more than 260KB
  • After using CDN

IV Example code

1.index.html page

No need to say more, just do it

<!DOCTYPE html>
<html lang="">
    <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>
    <!--css Loading of style files-->
     <% for (var i in htmlWebpackPlugin.options.cdn&&htmlWebpackPlugin.options.cdn.css) { %>
    <link href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" rel="preload" as="style" />
    <link href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" rel="stylesheet" />
    <% } %>
	<!--js Loading of files-->
    <% for (let i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js) { %>
      <script  src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
    <% } %>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    <div id="app"></div>



  • The loading of vuejs must be put in the first place, otherwise the subsequent js files will report that Vue cannot be found
  • The corresponding dependency name must be based on the exposed name of cdn dependency package, for example, Vue corresponds to 'Vue'
  • IS_PROD is a judgment item to judge whether it is in packaging mode

2.1 cdn preset value

  • After cdn is introduced, be sure to test it, because individual js types are not suitable for your development environment
  • At present, these js I introduced are available in vue3 environment
const cdn = {
  // development environment 
  dev: {
    css: [],
    js: []
  // production environment 
  build: {
    css: [''],
    js: [

let objExternals = {};
function externalsSet() {
  if (IS_PROD) {
    objExternals = {
      vue: 'Vue',// Take Vue as an example
      axios: 'axios', 
      vuex: 'Vuex',
      'vue-router': 'VueRouter',
      'element-plus': 'ElementPlus'
externalsSet(); //This method is used to judge whether the current mode is packaging mode

2.2 define externals under configureWebpack,

  • External dependencies to be ignored when packaging
	 // Custom webpack configuration
  configureWebpack: {
    externals: objExternals

2.3 send the cdn value defined on the current page to the main page under chainWebpack

chainWebpack: config => {
    config.plugin('html').tap(args => {
      if (IS_PROD) {
        args[0].cdn =;
      return args;

3. main.js

  • It is the same as the original use method, the only difference is that it directly calls the exposed object of the external dependency package, and there is no need to define it in advance
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

const app = createApp(App);


V Wordy

  • At present, there are so many researches on the introduction of Cdn, which is basically enough. After testing, the production environment is available!
  • If you are an intranet development user (mostly in a network free environment), it is not recommended that you use CDN or the original node_module, wait until the development is completed and packaged, and then change the relevant configuration to CDN mode!
  • If there are better suggestions or incorrect descriptions, you are welcome to point out and will actively correct them!

Keywords: Javascript Front-end Vue Webpack cdn

Added by nitromaster on Thu, 03 Feb 2022 04:24:51 +0200