DEV Community

Cover image for How i migrated my create-react-app legacy code to vite for faster build and compile time
Abhi Raj
Abhi Raj

Posted on

2

How i migrated my create-react-app legacy code to vite for faster build and compile time

This is just a small article, i will not write a long paras on why i did it, and what improvements i experience, i will just tell you how i did it.

About my previous create-react-app legacy code:
it is made in typescript and javascript

How i migrated:

first i installed these libraries
npm install vite @vitejs/plugin-react vite-tsconfig-paths

and create a vite.config.ts file

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsconfigPaths from 'vite-tsconfig-paths';

export default defineConfig({
    // depending on your application, base can also be "/"
    base: '',
    plugins: [react(), viteTsconfigPaths()],
    server: {
        // this ensures that the browser opens upon server start
        open: true,
        // this sets a default port to 3000
        port: 3000
    }
});

Enter fullscreen mode Exit fullscreen mode

Then i moved my index.html file to root directory, earlier it was on src folder

then i converted all my js file to jsx where i had some html inside

[note: only do this if you are getting error]
i used this script to convert all my js file to jsx file

it is a powershell script for windows, you can search for your os, just run this file in powershell

/.script.ps1

Get-ChildItem -Path "src" -Filter *.js -Recurse | ForEach-Object {
    $newName = $_.FullName -replace '.js$','.jsx'
    if (-Not (Test-Path $newName)) {
        Rename-Item -Path $_.FullName -NewName $newName
    } else {
        Write-Host "Skipping renaming of $($_.FullName) as $newName already exists."
    }
}

Enter fullscreen mode Exit fullscreen mode

then in my .env file i replaced all REACT_APP_ to VITE_REACT_APP_

and in my code i replaced all process.env to import.meta.env.VITE_REACT_APP

i also made a vite-env.d.ts file and with this and placed it inside src as well as root directory.

/// <reference types="vite/client" />
Enter fullscreen mode Exit fullscreen mode

and that's it. Thanks for reading.

so far my build time has reduced to more than half.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more