How to add Vite support to old Vue projects

How to add Vite support to old Vue projects

1. Introduction

I have taken over a project of the company for two years. Now it takes nearly 1 minute to start the project every time, and HMR takes several seconds. But after the release of Vite2, I saw the light, but I never upgraded it. Yesterday, I finally couldn't help it, and it was completed in seconds after the upgrade.

vite is a web development tool developed by Vue author Yuxi You. It has the following features:

Fast cold start

Instant module hot update

True on-demand compilation

2. Start the upgrade

Note: I just upgraded the development environment, and webpack is still used for packaging (I also tried to use vite for packaging, but after packaging, I found that there was a problem with the font icon of iview. Preliminary verification showed that it was a problem with static resources. The static resources packaged by vite are placed under assets by default. If anyone has a solution, please let me know the solution)

2.1 Install the vuecli plugin vite

vue add vit # Add vite plugin

After the plugin is installed, it will be added to the script in package.json:

{
    "script": {
        "vite": "node ./bin/vite"
    }
}

For students who use pnpm, if there is no .npmrc file in the project root directory, please add it yourself and add shamefully-hoist=true in the file; otherwise, the installation of the vite plugin may fail.

2.2. Run the project and troubleshoot errors

2.2.1、TypeError: Cannot read property 'alias' of undefined

This error is because configureWebpack in vue.config.js can only use the object configuration method (vue cli supports both objects and functions)

2.2.2 Unrestricted file system access to "/src/components/editPwd

The reason for this problem is that the extensions in the default configuration of vite do not contain .vue; Solution:

1. Add extensions in vue.config

// vue.config.js
module.exports = {
    configureWebpack:{
        resolve:{
            extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
        }
    }
}

2. Add the suffix .vue to all vue components when importing.

2.2.3. The startup port is not 8080

The default startup port of vite is 3000, so you need to add port:8080 to devServer in vue.config.js

// vue.config.js
module.exports = {
    devServer:{
        port: 8080
    }
}

2.2.4. Proxy failure during development

The reason for the proxy failure: the rewrite configuration in vuecli is pathRewrite, while in vite it is rewrite.

Solution:

module.exports = {
    devServer: {
        port: 8080,
        proxy: {
            "/api/cost/": {
                target: "http://localhost:9331",
                changeOrigin: true,
                pathRewrite: {
                    "^/api/cost/": "/",
                },
                rewrite: path => path.replace(/^\/api\/cost\//, "/"), // Adapt to vite
            },
            "/api/import/": {
                target: "http://localhost:9332",
                changeOrigin: true,
                pathRewrite: {
                    "^/api/import/": "/",
                },
                rewrite: path => path.replace(/^\/api\/import\//, "/"), // Adapt to vite
            },
            "/api/": {
                target: "http://localhost:9333",
                ws: true,
                changeOrigin: true,
                pathRewrite: {
                    "^/api/": "/",
                },
                rewrite: path => path.replace(/^\/api\//, "/"), // Adapt to vite
            },
        },
    },
}

3. Upgrade completed

The entire upgrade process is now over. Overall, it was relatively smooth without too many pitfalls, and most of them were problems that were relatively easy to solve.

This is the end of this article on how to add vite support to old vue projects. For more relevant content on adding vite to old vue projects, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vite+Electron to quickly build VUE3 desktop applications
  • Implementation of Vue 3.x project based on Vite2.x
  • Detailed explanation of vite2.0+vue3 mobile project
  • How to use vite to build vue3 application
  • What we have to say about Vue3 and Vite

<<:  Detailed configuration of Nginx supporting both Http and Https

>>:  Detailed explanation of MySQL 8.0 dictionary table enhancement

Recommend

Detailed explanation of Nginx access restriction configuration

What is Nginx access restriction configuration Ng...

Analysis of the principle of Nginx using Lua module to implement WAF

Table of contents 1. Background of WAF 2. What is...

HTML is something that web page creators must learn and master.

What are the benefits of learning HTML? 1: Easily...

NodeJs high memory usage troubleshooting actual combat record

Preface This is an investigation caused by the ex...

Ways to improve MongoDB performance

MongoDB is a high-performance database, but in th...

MySQL slow query and query reconstruction method record

Preface What is a slow query and how to optimize ...

my.cnf (my.ini) important parameter optimization configuration instructions

MyISAM storage engine The MyISAM storage engine i...

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...

Detailed explanation of three methods of JS interception string

JS provides three methods for intercepting string...

Navicat for MySQL 11 Registration Code\Activation Code Summary

Recommended reading: Navicat12.1 series cracking ...

Linux Check the installation location of the software simple method

1. Check the software installation path: There is...

Windows Server 2008 Tutorial on Monitoring Server Performance

Next, we will learn how to monitor server perform...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...

Detailed explanation of the construction and use of Docker private warehouse

The image can be saved on hub.docker.com, but the...