Skip to content

Commit

Permalink
feat: ASAP-249 Migrate frontend to Vite (#3989)
Browse files Browse the repository at this point in the history
* convert to vite
* fix packages and jest config
* add babelrc and tweak jest config
* fix linting and package errors
* fix service worker env var refs
* fix unsupported browser test for CRN
* update bundlwatch dir
* change s3 destination dir
* add prod sourcemap generation
* fix api missing url
* revert auth config file
* convert gp2 to vite
* add gp2 frontend to bundlewatch
* fix lint service worker
* update and fix release for sentry
* update flags env vars
* update readme's
* fix the config to use import.meta instead of process
* fix tests
* remove remaining react-scripts bits
* remove comments
* update pnp cjs
* support browserslist

---------

Co-authored-by: Quentin Leonetti <[email protected]>
Co-authored-by: Gabriela Ueno <[email protected]>
  • Loading branch information
3 people authored Dec 6, 2023
1 parent 057057c commit 0c1a942
Show file tree
Hide file tree
Showing 50 changed files with 2,316 additions and 2,096 deletions.
1 change: 1 addition & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ not dead
not ie 11 # CSS would be all broken anyway
not ios_saf 12.2 # there seems to be no reason it wouldn't work, but we're getting error reports about e.g. Object.fromEntries
not op_mini all
not safari tp # had to add that in otherwise esbuild fails

[development]
last 2 chrome versions
Expand Down
38 changes: 17 additions & 21 deletions .github/workflows/reusable-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
run: |
yarn build:babel
env:
REACT_APP_ENVIRONMENT: ${{ steps.setup.outputs.react-app-environment }}
VITE_APP_ENVIRONMENT: ${{ steps.setup.outputs.react-app-environment }}
- name: Cache transpile output
uses: ./.github/actions/cache-build
with:
Expand Down Expand Up @@ -176,28 +176,24 @@ jobs:
env:
CRN_AUTH_FRONTEND_BASE_URL: '${{ steps.setup.outputs.crn-auth-url}}'
GP2_AUTH_FRONTEND_BASE_URL: '${{ steps.setup.outputs.gp2-auth-url}}'
REACT_APP_CRN_ALGOLIA_APP_ID: ${{ steps.crn-algolia-keys.outputs.algolia-app-id }}
REACT_APP_CRN_ALGOLIA_INDEX: ${{ steps.setup.outputs.crn-algolia-index }}
REACT_APP_GP2_ALGOLIA_APP_ID: ${{ steps.gp2-algolia-keys.outputs.algolia-app-id }}
REACT_APP_GP2_ALGOLIA_INDEX: ${{ steps.setup.outputs.gp2-algolia-index }}
REACT_APP_API_BASE_URL: ${{ steps.setup.outputs.crn-api-url }}
REACT_APP_CRN_AUTH0_AUDIENCE: ${{ steps.setup.outputs.crn-auth0-audience }}
REACT_APP_CRN_AUTH0_CLIENT_ID: ${{ steps.setup.outputs.crn-auth0-client-id }}
VITE_APP_CRN_AUTH0_CLIENT_ID: ${{ steps.setup.outputs.crn-auth0-client-id }}
REACT_APP_CRN_AUTH0_DOMAIN: ${{ steps.setup.outputs.crn-auth0-domain }}
VITE_APP_CRN_AUTH0_DOMAIN: ${{ steps.setup.outputs.crn-auth0-domain }}
REACT_APP_CRN_SENTRY_DSN: ${{ steps.setup.outputs.react-app-crn-sentry-dsn }}
REACT_APP_ENVIRONMENT: ${{ steps.setup.outputs.react-app-environment }}
REACT_APP_GP2_API_BASE_URL: ${{ steps.setup.outputs.gp2-api-url }}
REACT_APP_GP2_AUTH0_AUDIENCE: ${{ steps.setup.outputs.gp2-auth0-audience }}
REACT_APP_GP2_AUTH0_CLIENT_ID: ${{ steps.setup.outputs.gp2-auth0-client-id }}
VITE_APP_API_BASE_URL: ${{ steps.setup.outputs.crn-api-url }}
VITE_APP_CRN_GTM_CONTAINER_ID: ${{ steps.setup.outputs.react-app-crn-gtm-container-id }}
VITE_APP_ENVIRONMENT: ${{ steps.setup.outputs.react-app-environment }}
VITE_APP_GP2_ALGOLIA_APP_ID: ${{ steps.gp2-algolia-keys.outputs.algolia-app-id }}
VITE_APP_GP2_ALGOLIA_INDEX: ${{ steps.setup.outputs.gp2-algolia-index }}
VITE_APP_GP2_API_BASE_URL: ${{ steps.setup.outputs.gp2-api-url }}
VITE_APP_GP2_AUTH0_AUDIENCE: ${{ steps.setup.outputs.gp2-auth0-audience }}
VITE_APP_GP2_AUTH0_CLIENT_ID: ${{ steps.setup.outputs.gp2-auth0-client-id }}
REACT_APP_GP2_AUTH0_DOMAIN: ${{ steps.setup.outputs.gp2-auth0-domain }}
VITE_APP_GP2_AUTH0_DOMAIN: ${{ steps.setup.outputs.gp2-auth0-domain }}
REACT_APP_GP2_SENTRY_DSN: ${{ steps.setup.outputs.react-app-gp2-sentry-dsn }}
REACT_APP_CRN_GTM_CONTAINER_ID: ${{ steps.setup.outputs.react-app-crn-gtm-container-id }}
REACT_APP_GP2_GTM_CONTAINER_ID: ${{ steps.setup.outputs.react-app-gp2-gtm-container-id }}
REACT_APP_RELEASE: ${{ steps.setup.outputs.app-release }}
VITE_APP_GP2_GTM_CONTAINER_ID: ${{ steps.setup.outputs.react-app-gp2-gtm-container-id }}
VITE_APP_GP2_SENTRY_DSN: ${{ steps.setup.outputs.react-app-gp2-sentry-dsn }}
VITE_APP_RELEASE: ${{ steps.setup.outputs.app-release }}
VITE_APP_CRN_ALGOLIA_APP_ID: ${{ steps.crn-algolia-keys.outputs.algolia-app-id }}
VITE_APP_CRN_ALGOLIA_INDEX: ${{ steps.setup.outputs.crn-algolia-index }}
VITE_APP_CRN_AUTH0_AUDIENCE: ${{ steps.setup.outputs.crn-auth0-audience }}
VITE_APP_CRN_AUTH0_CLIENT_ID: ${{ steps.setup.outputs.crn-auth0-client-id }}
VITE_APP_CRN_AUTH0_DOMAIN: ${{ steps.setup.outputs.crn-auth0-domain }}
VITE_APP_CRN_SENTRY_DSN: ${{ steps.setup.outputs.react-app-crn-sentry-dsn }}
run: |
yarn build:frontend
- name: Cache build output
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,4 @@ jobs:
yarn run release:frontend
env:
FRONTEND_SENTRY_RELEASE_AUTH_TOKEN: ${{ secrets.FRONTEND_SENTRY_RELEASE_AUTH_TOKEN }}
REACT_APP_RELEASE: ${{ steps.setup.outputs.app-release }}
FRONTEND_RELEASE: ${{ steps.setup.outputs.app-release }}
3,998 changes: 2,063 additions & 1,935 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions apps/crn-auth-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"type": "module",
"scripts": {
"start": "cross-env PORT=3002 TSC_COMPILE_ON_ERROR=true react-scripts start",
"start": "cross-env PORT=3002 TSC_COMPILE_ON_ERROR=true vite",
"build": "vite build",
"typecheck": "tsc",
"test": "echo Run the workspace root script instead"
Expand All @@ -25,7 +25,7 @@
"react-app-polyfill": "3.0.0",
"react-dom": "17.0.2",
"react-router-dom": "5.3.4",
"vite": "^5.0.2"
"vite": "5.0.4"
},
"devDependencies": {
"@asap-hub/auth-frontend-scripts": "workspace:*",
Expand Down
6 changes: 6 additions & 0 deletions apps/crn-frontend/.babelrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
[require.resolve('@babel/preset-react'), { runtime: 'automatic' }],
require.resolve('babel-preset-vite'),
],
};
4 changes: 2 additions & 2 deletions apps/crn-frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ You will also see any lint errors in the console.

#### Environment Variables

The frontend application uses the production API by default, to change this behavior you can set the `REACT_APP_API_BASE_URL` environment variable.
The frontend application uses the production API by default, to change this behavior you can set the `VITE_APP_API_BASE_URL` environment variable.

- `REACT_APP_API_BASE_URL` - Set the api endpoint, default value is `http://localhost:3333`.
- `VITE_APP_API_BASE_URL` - Set the api endpoint, default value is `http://localhost:3333`.

### `yarn test`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
name="description"
content="The Hub application by ASAP: Aligning Science Across Parkinson's"
/>
<meta build-version="%REACT_APP_RELEASE%" />
<link rel="icon" href="%PUBLIC_URL%/favicon.png" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<meta build-version="%VITE_APP_RELEASE%" />
<link rel="icon" href="/favicon.png" />
<link rel="apple-touch-icon" href="/logo192.png" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
Expand All @@ -24,22 +24,13 @@
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link rel="manifest" href="/manifest.json" />
<title>ASAP Hub</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script src="%PUBLIC_URL%/detect-unsupported-browser.js"></script>
<script src="/detect-unsupported-browser.js"></script>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
Expand All @@ -50,5 +41,6 @@
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
10 changes: 3 additions & 7 deletions apps/crn-frontend/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
setupFilesAfterEnv,
moduleNameMapper,
transform,
...baseConfig
} = require('../../jest/jest-base.config.js');

Expand All @@ -19,13 +20,8 @@ module.exports = {
],

transform: {
'^.+\\.(js|jsx|ts|tsx)$': require.resolve(
'react-scripts/config/jest/babelTransform.js',
),
'^.+\\.css$': require.resolve('react-scripts/config/jest/cssTransform.js'),
'^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': require.resolve(
'react-scripts/config/jest/fileTransform.js',
),
'^.+\\.css$': [require.resolve('jest-transform-css'), { modules: true }],
...transform,
},
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$',
Expand Down
14 changes: 9 additions & 5 deletions apps/crn-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
"node": "18.x"
},
"scripts": {
"start": "cross-env TSC_COMPILE_ON_ERROR=true react-scripts start",
"build": "node ./scripts/build-unsupported-browser-page.js && DISABLE_ESLINT_PLUGIN=true react-scripts build",
"start": "cross-env TSC_COMPILE_ON_ERROR=true vite",
"build": "node ./scripts/build-unsupported-browser-page.js && vite build",
"typecheck": "tsc",
"test": "echo Run the workspace root script instead",
"eject": "react-scripts eject",
"release:frontend": "node ./scripts/sentry-release.js"
},
"dependencies": {
Expand All @@ -26,8 +25,10 @@
"@asap-hub/unsupported-browser-page": "workspace:*",
"@asap-hub/validation": "workspace:*",
"@auth0/auth0-spa-js": "1.22.6",
"@babel/preset-react": "7.23.3",
"@sentry/react": "6.19.7",
"@sentry/tracing": "6.19.7",
"@vitejs/plugin-react": "^4.2.0",
"browser-image-compression": "1.0.17",
"csv-stringify": "6.4.4",
"date-fns": "2.30.0",
Expand All @@ -45,7 +46,8 @@
"stream": "npm:[email protected]",
"use-debounce": "8.0.4",
"use-deep-compare-effect": "1.6.1",
"uuid": "8.3.2"
"uuid": "8.3.2",
"vite": "5.0.4"
},
"devDependencies": {
"@asap-hub/dom-test-utils": "workspace:*",
Expand All @@ -71,13 +73,15 @@
"@types/testing-library__react": "10.0.1",
"@types/testing-library__react-hooks": "3.4.1",
"@types/uuid": "8.3.4",
"babel-preset-vite": "^1.1.0",
"browserslist-to-esbuild": "^1.2.0",
"browserslist-useragent-regexp": "3.0.2",
"cross-env": "7.0.3",
"identity-obj-proxy": "3.0.0",
"jest-transform-css": "^6.0.1",
"jose": "2.0.6",
"nock": "13.4.0",
"pirates": "4.0.6",
"react-scripts": "5.0.1",
"react-test-renderer": "17.0.2",
"typescript": "4.9.5",
"webpack": "5.76.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('the detection script', () => {
const detectionScriptCode = readFileSync(
resolve(
require.resolve('@asap-hub/crn-frontend/package.json'),
'../build/detect-unsupported-browser.js',
'../dist/detect-unsupported-browser.js',
),
).toString();

Expand Down
2 changes: 1 addition & 1 deletion apps/crn-frontend/scripts/sentry-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function createReleaseAndUpload() {
project: 'asap-hub-frontend',
});
try {
const release = process.env.REACT_APP_RELEASE;
const release = process.env.FRONTEND_RELEASE;
console.log('Creating sentry release ' + release);
await cli.releases.new(release);
console.log('Uploading source maps');
Expand Down
21 changes: 11 additions & 10 deletions apps/crn-frontend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export const GTM_CONTAINER_ID = process.env.REACT_APP_CRN_GTM_CONTAINER_ID;
export const GTM_CONTAINER_ID = import.meta.env.VITE_APP_CRN_GTM_CONTAINER_ID;
export const API_BASE_URL =
process.env.REACT_APP_API_BASE_URL || 'http://localhost:3333';
import.meta.env.VITE_APP_API_BASE_URL || 'http://localhost:3333';
export const ALGOLIA_APP_ID =
process.env.REACT_APP_CRN_ALGOLIA_APP_ID || 'LVYWOPQ0A9';
import.meta.env.VITE_APP_CRN_ALGOLIA_APP_ID || 'LVYWOPQ0A9';
export const ALGOLIA_INDEX =
process.env.REACT_APP_CRN_ALGOLIA_INDEX || 'asap-hub_dev';
export const SENTRY_DSN = process.env.REACT_APP_CRN_SENTRY_DSN;
export const ENVIRONMENT = process.env.REACT_APP_ENVIRONMENT || 'local';
export const RELEASE = process.env.REACT_APP_RELEASE;
export const AUTH0_AUDIENCE = process.env.REACT_APP_CRN_AUTH0_AUDIENCE || '';
export const AUTH0_CLIENT_ID = process.env.REACT_APP_CRN_AUTH0_CLIENT_ID || '';
export const AUTH0_DOMAIN = process.env.REACT_APP_CRN_AUTH0_DOMAIN || '';
import.meta.env.VITE_APP_CRN_ALGOLIA_INDEX || 'asap-hub_dev';
export const SENTRY_DSN = import.meta.env.VITE_APP_CRN_SENTRY_DSN;
export const ENVIRONMENT = import.meta.env.VITE_APP_ENVIRONMENT || 'local';
export const RELEASE = import.meta.env.VITE_APP_RELEASE;
export const AUTH0_AUDIENCE = import.meta.env.VITE_APP_CRN_AUTH0_AUDIENCE || '';
export const AUTH0_CLIENT_ID =
import.meta.env.VITE_APP_CRN_AUTH0_CLIENT_ID || '';
export const AUTH0_DOMAIN = import.meta.env.VITE_APP_CRN_AUTH0_DOMAIN || '';
1 change: 0 additions & 1 deletion apps/crn-frontend/src/react-app-env.d.ts

This file was deleted.

12 changes: 9 additions & 3 deletions apps/crn-frontend/src/serviceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ type Config = {
};

export function register(config?: Config): void {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
if (
import.meta.env.NODE_ENV === 'production' &&
'serviceWorker' in navigator
) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
const publicUrl = new URL(
import.meta.env.VITE_PUBLIC_URL,
window.location.href,
);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
Expand All @@ -41,7 +47,7 @@ export function register(config?: Config): void {
}

window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
const swUrl = `${import.meta.env.VITE_PUBLIC_URL}/service-worker.js`;

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
Expand Down
6 changes: 4 additions & 2 deletions apps/crn-frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

"emitDeclarationOnly": false,
"noEmit": true,

"target": "ESNext",
"lib": ["esnext", "dom", "dom.iterable"],
"jsx": "react-jsx"
"jsx": "react-jsx",
"types": ["vite/client", "@testing-library/jest-dom"],
"isolatedModules": true
},
"include": ["src"],
"references": [
Expand Down
1 change: 1 addition & 0 deletions apps/crn-frontend/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
18 changes: 18 additions & 0 deletions apps/crn-frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import browserslistToEsbuild from 'browserslist-to-esbuild';

export default defineConfig({
plugins: [react()],
build: {
sourcemap: true,
target: browserslistToEsbuild(),
},
server: {
open: true,
port: 3000,
},
define: {
global: 'globalThis',
},
});
2 changes: 1 addition & 1 deletion apps/crn-server/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const serverlessConfig: AWS = {
{
bucketName: '${self:service}-${self:provider.stage}-frontend',
deleteRemoved: false,
localDir: '../crn-frontend/build',
localDir: '../crn-frontend/dist',
},
{
bucketName: '${self:service}-${self:provider.stage}-auth-frontend',
Expand Down
4 changes: 2 additions & 2 deletions apps/gp2-auth-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"node": "18.x"
},
"scripts": {
"start": "cross-env PORT=3002 TSC_COMPILE_ON_ERROR=true react-scripts start",
"start": "cross-env PORT=3002 TSC_COMPILE_ON_ERROR=true vite",
"build": "vite build",
"typecheck": "tsc",
"test": "echo Run the workspace root script instead"
Expand All @@ -26,7 +26,7 @@
"react-dom": "17.0.2",
"react-router-dom": "5.3.4",
"util": "0.12.5",
"vite": "^5.0.2"
"vite": "5.0.4"
},
"devDependencies": {
"@asap-hub/auth-frontend-scripts": "workspace:*",
Expand Down
6 changes: 6 additions & 0 deletions apps/gp2-frontend/.babelrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
[require.resolve('@babel/preset-react'), { runtime: 'automatic' }],
require.resolve('babel-preset-vite'),
],
};
4 changes: 2 additions & 2 deletions apps/gp2-frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ You will also see any lint errors in the console.

#### Environment Variables

The frontend application uses the production API by default, to change this behavior you can set the `REACT_APP_API_BASE_URL` environment variable.
The frontend application uses the production API by default, to change this behavior you can set the `VITE_APP_API_BASE_URL` environment variable.

- `REACT_APP_API_BASE_URL` - Set the api endpoint, default value is `http://localhost:3333`.
- `VITE_APP_API_BASE_URL` - Set the api endpoint, default value is `http://localhost:3333`.

### `yarn test`

Expand Down
Loading

0 comments on commit 0c1a942

Please sign in to comment.