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

[DR-3123] Migrate to vite #1475

Merged
merged 25 commits into from
Aug 30, 2023
Merged

[DR-3123] Migrate to vite #1475

merged 25 commits into from
Aug 30, 2023

Conversation

samanehsan
Copy link
Contributor

@samanehsan samanehsan commented Aug 9, 2023

This PR updates the UI to use Vite instead of create react app (react-scripts is one of the dependencies, which has not been maintained for over a year and contains a security vulnerability: https://broadworkbench.atlassian.net/browse/DR-3123).

Some things to note:

  • Vite has a built-in hot reloader
  • The route proxying is defined in vite.config.js (so setupProxy is no longer needed)
  • When running locally, the url needs to use "localhost" instead of "127.0.0.1" in order for log in to work

I also updated Cypress from version 9 -> 12, which supports the latest version of Vite. This involved:

  • Replacing cypress.json with cypress.config.ts
  • Removing the cypress/plugins/index.js file
  • Adding a cypress/support/component-index.html file (where the component test html gets rendered)
  • Updating the syntax in the e2e tests (e.g. cy.route is now cy.intercept) and some of the unit/component tests

@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch from d2883a4 to 4384ecb Compare August 15, 2023 15:05
@samanehsan samanehsan marked this pull request as ready for review August 15, 2023 16:29
@samanehsan
Copy link
Contributor Author

The test datasets/snapshots used by the e2e tests exist but for some reason are not accessible to the test user (I can't access them with my dev account either). I created new ones in int-6 as a temporary work-around so I could run/fix the e2e tests locally, so they should pass once we can sort out the issue with int-4 and int-5!

@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch from 9e599f1 to d5f8f36 Compare August 16, 2023 16:48
@samanehsan
Copy link
Contributor Author

The SonarCloud failure can be resolved by generating a hash for our fontawesome kit "https://kit.fontawesome.com/316fe749fb.js", but I get a 403 error when I try to do that (I used this website: https://www.srihash.org/). Does anyone have access? Or should I create a new custom kit?

@okotsopoulos
Copy link
Contributor

@samanehsan that FontAwesome question rings a bell, I'm not sure if this is possible :(

From @snf2ye in February '23:

Fontawesome supports the use an SRI hash with versions <5. We are using a fontawesome kit v6. Fontawesome made a product choice to no longer support SRI hashes (source).

https://broadworkbench.atlassian.net/browse/DR-2872

@samanehsan
Copy link
Contributor Author

Thanks @okotsopoulos! That's helpful context! I'll mark that script as excluded

@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch from d5f8f36 to 678f95c Compare August 17, 2023 18:04
@samanehsan
Copy link
Contributor Author

I think the e2e tests aren't running because it waits on localhost:3000 but the vite dev server is using 127.0.0.1:3000. I'll see if there's a way to change the server to use localhost.

Copy link
Contributor

@s-rubenstein s-rubenstein left a comment

Choose a reason for hiding this comment

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

I'm noticing a lot more casting than was there before - is this a result of the libraries changing? Casting like that can sometimes be a code smell, so I am curious if you know where its coming from.

src/components/common/TextContent.test.tsx Show resolved Hide resolved
cy.get('div#__cy_root > div > p').should('contain.text', 'A bad day');
cy.get('div#__cy_root > div > p').children().should('have.length', 0);
cy.root().should('have.length', 1);
cy.root().get('p').should('contain.text', 'A bad day');
Copy link
Contributor

Choose a reason for hiding this comment

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

Just as a note, do you think this sort of test could maybe be simplified by doing a findByText on the page? That is what Terra-UI does

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah oops I missed this comment! It turns out findByText requires another dependency so I'm inclined to leave it out for now if that's okay

@@ -1,5 +1,4 @@
import { CloudPlatform } from 'generated/tdr';
import _ from 'lodash';
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised you can remove the import from lodash without any other changes to the file. Do you have any linting for unused imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Our eslint rules don't include unused imports so I created a ticket for that: https://broadworkbench.atlassian.net/browse/DR-3176

// Don't reset state if the only change was query parameters
const path = action.payload?.location?.pathname;
if (path === state.lastPath) {
return immutable(state, {
lastPath: { $set: path },
});
lastPath: { $set: path as string },
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised you have to do the casts here. Does typescript not type interpolate correctly for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, weirdly there were a couple of places where the type couldn't be inferred and without making it explicit the server wouldn't run

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, in this particular case it might be that it thinks path is string | undefined given the ?s. Would you file a ticket to look into the specific casts? My guess is that they are resolvable using typescript but there may be some code structure changes needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch from ecb4cbe to fcbb24e Compare August 21, 2023 20:00
Work around wait-on 404 error

Originally from: vitejs/vite#9520 (comment)
@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch 2 times, most recently from 80fb251 to 7040f9f Compare August 22, 2023 14:52
@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch 4 times, most recently from 7d41e77 to 2d265f2 Compare August 22, 2023 16:10
@cypress
Copy link

cypress bot commented Aug 22, 2023

Passing run #2946 ↗︎

0 25 0 0 Flakiness 0

Details:

Merge b73807f into 7c334b7...
Project: jade-data-repo-ui Commit: cd31fd1ca1 ℹ️
Status: Passed Duration: 08:52 💡
Started: Aug 30, 2023 2:07 PM Ended: Aug 30, 2023 2:16 PM

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings.

@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch 5 times, most recently from de01eeb to 6b514bc Compare August 23, 2023 03:55
Fixes build issue with vite
@samanehsan samanehsan force-pushed the se/DR-3123-migrate-to-vite branch from 6b514bc to 5b487a0 Compare August 23, 2023 03:56
@@ -7,7 +7,7 @@ export const sagaMiddleware = createSagaMiddleware();
const middleware = [sagaMiddleware];

/* istanbul ignore next */
if (process.env.NODE_ENV === 'development') {
if (import.meta.env.DEV) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This env variable is provided by Vite by default: https://vitejs.dev/guide/env-and-mode.html#env-variables

vite builds production by default
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
8.5% 8.5% Duplication

@samanehsan
Copy link
Contributor Author

The "process not defined" issue causing the e2e tests to fail was due to to the version of jsondiffpatch we were using. Updating to the latest version fixed the issue! 🎉

Copy link
Contributor

@s-rubenstein s-rubenstein left a comment

Choose a reason for hiding this comment

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

LGTM

# Copy the local code
COPY . /
# Copy the generated code
COPY --from=codegen /local /
# Run the build
RUN export DISABLE_ESLINT_PLUGIN=true && \
npm ci && \
npm run build-no-code-gen --production
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this flag removed?

Copy link
Contributor Author

@samanehsan samanehsan Aug 29, 2023

Choose a reason for hiding this comment

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

The --production flag isn't needed because vite build uses production by default

package.json Outdated
@@ -24,6 +24,10 @@
"dependencies": {
"@babel/runtime": "^7.21.5",
"@codemirror/lang-javascript": "^6.1.5",
"@cypress/vite-dev-server": "^5.0.5",
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe there are some deps that can be moved to devDependencies. This isn't a big deal, but more for package.json hygiene

Feel free to do this in a follow up PR

package.json Outdated
"classlist-polyfill": "^1.2.0",
"clsx": "^1.1.0",
"connected-react-router": "^6.9.3",
"cypress": "^9.7.0",
"cypress": "^12.17.3",
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be moved to devDependencies

package.json Outdated
@@ -97,25 +98,27 @@
"tss-react": "^4.8.4",
"typescript": "^5.0.4",
"validator": "^13.9.0",
"vite": "^4.4.9",
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe vite can be moved to devDepenendcies

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
5.7% 5.7% Duplication

@samanehsan samanehsan merged commit 5157015 into develop Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants