Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: Replace create-react-app with vite and vitest #1981

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

sniok
Copy link
Contributor

@sniok sniok commented May 15, 2024

This PR continues work started in #1696
Thanks @tazo90!

Replaced cra with vite
Remoted storybook storyshot addon because it is deprecated and doesn't work with vite
Updated all tests to use vitest instead of jest

Two tests don't work because of the bug in vitest, it stuggles with cyclic imports sometimes.

headlamp-plugin doesn't work yet, looking into it

changes:

  • Migrated frontend/ from CRA to vite
  • Migrated from jest to vitest
  • Migrated from storybook/storyshots
  • See if I can keep the REACT_ prefix for env variables
  • Create readme file for the frontend/
  • Fix baseUrl replacement in backend
  • Update all env variable access to import.meta.env
  • Sync dependencies

testing:

  • Tested building headlamp-plugin
  • Tested compiling, bundling and running plugin in a frontend/ built by vite
  • Tested app/
  • Tested -base-url and PUBLIC_URL options

@sniok sniok added frontend Issues related to the frontend dx labels May 15, 2024
@sniok sniok force-pushed the vite branch 24 times, most recently from d09655a to c4b8de6 Compare May 21, 2024 10:50
@@ -2,18 +2,15 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<link rel="icon" href="/favicon.ico" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for PUBLIC_URL here as vite handles that nicely
https://vitejs.dev/guide/assets.html
https://vitejs.dev/guide/build.html#public-base-path

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try with a different baseURL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, it works

frontend/package.json Outdated Show resolved Hide resolved
@sniok sniok marked this pull request as ready for review May 21, 2024 12:48
@sniok sniok requested review from illume and joaquimrocha May 21, 2024 12:48
@sniok sniok changed the title frontend: Replace create-react-app with vite frontend: Replace create-react-app with vite and vitest May 21, 2024
@sniok
Copy link
Contributor Author

sniok commented May 23, 2024

this is ready for review @joaquimrocha @illume

@illume
Copy link
Contributor

illume commented May 24, 2024

Are you going to get storyshots working?

@illume
Copy link
Contributor

illume commented May 24, 2024

There should be a thankyou/co-author added from the other vite PR.

@illume
Copy link
Contributor

illume commented May 24, 2024

It could be a good idea to do preparation PR(s) of changes. Changes that can be done to prepare for this one that don't break what's already there. This way the size of the PR could be reduced.

@@ -37,7 +37,7 @@ export function runCommand(
throw new Error('runCommand only works in Headlamp app mode.');
}

if (process.env.REACT_APP_ENABLE_RUN_CMD !== 'true') {
if (import.meta.env.REACT_APP_ENABLE_RUN_CMD !== 'true') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can support for process.env be kept somehow?

Mostly interested for a future move of the plugins to vite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, added vite-plugin-env-compatible that makes process.env work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revisited this topic. I removed the process env plugin because app itself is using import.meta.env and plugins don't have access to this since all the env variables are statically replaced during build time and process.env is not available at all.
you can check it by printing process in the dev console in the current version of the app

@sniok sniok force-pushed the vite branch 5 times, most recently from f1a8899 to c20bd76 Compare May 27, 2024 14:05
@sniok
Copy link
Contributor Author

sniok commented May 27, 2024

Improved storyshots test, now it generates separate files for each story, instead of putting it in one big file.
Initially I thought there was something wrong with the formatter but then I figured out that toMatchFilesnapshot would override the same file when there are more than 1 story per file, so now it generates it in separate files.

Moved the i18next-parser version bump into separate commit. Everything else seems to require vite so keeping it in the other (big) commit.

Added env plugin to make process.env work

@sniok
Copy link
Contributor Author

sniok commented May 31, 2024

Some performance comparison:

npm run start

measured time from running the command to page finishing loading in the browser

CRA cold start: ~23s
CRA hot start: ~17s

Vite hot/cold start: ~6s
vite server starts in <200ms but initial page load takes some time

Build

CRA build: 41s
vite build: 41s

same time, no surprises here. patiently waiting for rolldown

hot reload

CRA reload: 2s
vite reload: <100ms, too fast to measure

now this is one of the best quality of life improvement, having quick feedback loop while developing frontend is great

@sniok
Copy link
Contributor Author

sniok commented May 31, 2024

Build output comparison is here: https://gist.github.com/sniok/95165478c77f1bf86e752c7d6f1f9741

It's mostly similar, I don't anything concerning.

Unrelated to vite, there is a lot we can do about what's in each chunk. We can improve headlamp startup performance by using more lazy components and improving code splitting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dx frontend Issues related to the frontend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants