Vue3 + element plus configuration cdn

Posted by midge1970 on Tue, 18 Jan 2022 08:49:40 +0100

1. Project configuration

Recently, I was working on a project. The project configuration version is as follows:

  • vue: 3.2.6
  • vue-router: 4.0.11
  • vuex: 4.0.2
  • axios: 0.21.4
  • element-plus: 1.2.0-beta.6
  • typescript: 4.1.5
  • sass: 1.26.5

Let's share how to configure cdn acceleration for the vue3 project configured above

Quietly, there are so many element plus pits_ T

2. CDN introduction

Introduce the common free cdn websites

2.1 common cdn websites

Here, Tucao make complaints about BootCDN and Staticfile CDN, which can only be found in element-plus version. beta. 71. I'm dizzy. People are out to 1.3.0-beta Four!

2.2. Find the required cdn resources through UNPKG

The simple and clear search methods of BootCDN and Staticfile CDN are not mentioned. Here is the way UNPKG searches cdn resources.

cdn file path format:

The path resolution is shown in the following figure:

Here are some examples of cdn paths:

// element-plus

// axios

Directly use the resource name followed by /, you can view the folder directory, such as: /, copy it into the browser address bar, and you can see the following directory:

After opening what files you need, you can find them yourself. It's very convenient. More black technologies Baidu by themselves.

3. Actual combat code

According to the following configuration, the operation mode is basically the same, and the cdn version of the file is replaced by itself.


const CDN = {
  css: [
  js: [

let objExternals = {
  vue: 'Vue',
  axios: 'axios',
  vuex: 'Vuex',
  'vue-router': 'VueRouter',
  'element-plus': 'ElementPlus'

module.exports = {
  publicPath: '/',
  assetsDir: './assets',
  chainWebpack: config => {
  	// Configure and transfer the cdn value defined on the current page to the main page (index.html)
    config.plugin('html').tap(args => {
    // Except for the local environment, I use CDN here. You can choose whether to configure it or not
      args[0].cdn = process.env.VUE_APP_STAGE === 'LOCAL' ? {} : CDN
      return args;
  configureWebpack: {
    devServer: {
    	//... Not related to this article
    resolve: {
    	//... Not related to this article
    plugins: [
    	//... Not related to this article
    // Define webpack packaging configuration
    externals: process.env.VUE_APP_STAGE === 'LOCAL' ? {} : objExternals 


Add the following in the head tag:

<% 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" />
  <% } %>

Add the following to the body tag:

<% for (let i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js) { %>
    <script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
  <% } %>


import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import App from '@/App.vue'
import router from '@/router'
import store from '@/store'

const app = createApp(App)
app.use(ElementPlus, { locale: zhCn }) // Configure Chinese

4. Postscript

It is true that the package file is much smaller after cdn is equipped, but if cdn hangs, your project will hang.

If you find it helpful, i'm @ pengduoduo i, welcome to like and pay attention to comments;

official account

Previous articles

Personal home page

Topics: Front-end Vue.js cdn