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

Docker exposes port 2375, causing server attacks and solutions

I believe that students who have learned about th...

A detailed discussion of components in Vue

Table of contents 1. Component Registration 2. Us...

Detailed process of NTP server configuration under Linux

Table of contents 1. Environment Configuration 1....

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

How to use axios request in Vue project

Table of contents 1. Installation 2. There is no ...

Linux kernel device driver kernel time management notes

/****************** * Linux kernel time managemen...

Query process and optimization method of (JOIN/ORDER BY) statement in MySQL

The EXPLAIN statement is introduced in MySQL quer...

Implementing carousel with native JavaScript

This article shares the specific code for impleme...

Docker generates images through containers and submits DockerCommit in detail

Table of contents After creating a container loca...

Detailed explanation of Linux dynamic library generation and usage guide

The file name of the dynamic library file under L...

Let’s talk in detail about how browsers view closures

Table of contents Preface Introduction to Closure...

Beginner's guide to building a website ⑥: Detailed usage of FlashFXP

Today I will introduce the most basic functions of...

How to check if the firewall is turned off in Linux

1. Service method Check the firewall status: [roo...

Detailed explanation of how to synchronize data from MySQL to Elasticsearch

Table of contents 1. Synchronization Principle 2....