Skip to content

Commit

Permalink
Config: Migrate from CRA to Vite (wait for release 1.9.0 to merge) (#688
Browse files Browse the repository at this point in the history
)

* config: Add Vite configuration and update environment variables

* chore: Update meta tags and link references in index.html

* config: Update import paths for Timer module

* fix: Update favicon link in index.html and fix path in tsconfig.json

* config: Use Vite as core builder for Storybook

* refactor: Migrate to vitest

* fix: Add eslint-plugin-chai-friendly and update package.json

* fix(test): Fix fn() call in Consent test

* fix(test): Convert most tests to be vitest/chai based testing

* fix(tests): Fix FloatingActionButton tests

* config(env): Migrate frontend environment variables from REACT_APP_ prefix to VITE_

* config: Update frontend ci test command

* fix(lint): Add correct imports and remove unused imports

* fix(cov): Fix frontend coverage generation

* fix(story): Fix storybook component imports after vite migration

* config: Fix test commands after vite migration

* fix(test): Fix frontend tests by binding vitest configuration to container

* revert: onTick call

* fix: Fix timer imports

* chore: Resolve yarn after rebase onto develop

* config: Configure frontend to be a module, according to [Vite's recommendation](https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated)

* fix: Add bind mount for index.html file

* fix: Expose vite dev server to the outside

* chore: Migrate index.tsx to use import.meta.env

* chore: Remove typescript step from build

* fix: Change build directory from dist/ to build/

* refactor: Update frontend tsconfig.json and add tsconfig.node.json

* feat: Add build-front script

* fix: Migrate tests to vitest

* fix: Update storybook deps & config to support react vite with jsx

* fix: Fix frontend linting

* feat: Add linting script for front-end code

* config: Start using FRONTEND_ instead of REACT_APP_ / VITE_

* fix: Fix tests & imports after rebase onto develop

* refactor/fix: Remove unused dependencies and update test assertions after rebase onto develop

* fix(lint): Add vi import for vitest
  • Loading branch information
drikusroor authored Mar 13, 2024
1 parent 1a04fc9 commit 2c303a0
Show file tree
Hide file tree
Showing 62 changed files with 3,056 additions and 9,042 deletions.
19 changes: 10 additions & 9 deletions .env-github-actions
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ AML_SECRET_KEY=amlsecretkey
AML_LOCATION_PROVIDER=http://ip2country:5000/{}
AML_DEBUG=True
DJANGO_SETTINGS_MODULE=aml.development_settings
REACT_APP_API_ROOT=http://localhost:8000
REACT_APP_EXPERIMENT_SLUG=gold-msi
REACT_APP_AML_HOME=https://www.amsterdammusiclab.nl
REACT_APP_HTML_PAGE_TITLE=Amsterdam Music Lab Experiment
REACT_APP_HTML_OG_DESCRIPTION=Listening experiments from the Amsterdam Music Lab. Test your musical knowledge and skills in engaging citizen-science experiments.
REACT_APP_HTML_OG_IMAGE=https://app.amsterdammusiclab.nl/images/og-hooked.jpg
REACT_APP_HTML_OG_TITLE=Amsterdam Music Lab Experiment
REACT_APP_HTML_OG_URL=https://app.amsterdammusiclab.nl
REACT_APP_HTML_BODY_CLASS=

FRONTEND_API_ROOT=http://localhost:8000
FRONTEND_EXPERIMENT_SLUG=gold-msi
FRONTEND_AML_HOME=https://www.amsterdammusiclab.nl
FRONTEND_HTML_PAGE_TITLE=Amsterdam Music Lab Experiment
FRONTEND_HTML_OG_DESCRIPTION=Listening experiments from the Amsterdam Music Lab. Test your musical knowledge and skills in engaging citizen-science experiments.
FRONTEND_HTML_OG_IMAGE=https://app.amsterdammusiclab.nl/images/og-hooked.jpg
FRONTEND_HTML_OG_TITLE=Amsterdam Music Lab Experiment
FRONTEND_HTML_OG_URL=https://app.amsterdammusiclab.nl
FRONTEND_HTML_BODY_CLASS=
26 changes: 13 additions & 13 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ DJANGO_SUPERUSER_EMAIL=mail@example.com # do not use in production!
AML_LOCATION_PROVIDER=http://ip2country:5000/{} # address of the ip2country container, don't change
AML_ALLOWED_HOSTS="localhost" # needs to be changed when running in production

REACT_APP_API_ROOT=http://localhost:8000 # address of the server, don't change
REACT_APP_EXPERIMENT_SLUG=gold-msi # experiment slug that the frontend redirects to
REACT_APP_AML_HOME=https://www.amsterdammusiclab.nl # website you will be redirected to if you do not agree with an informed consent form
REACT_APP_LOGO_URL= # optional: link to logo
REACT_APP_HTML_PAGE_TITLE=Amsterdam Music Lab Experiment # optional: information for web crawlers
REACT_APP_HTML_FAVICON=/favicon.ico # optional: which image to use as favicon
REACT_APP_HTML_OG_DESCRIPTION=Listening experiments from the Amsterdam Music Lab. Test your musical knowledge and skills in engaging citizen-science experiments. # optional: information for web crawlers
REACT_APP_HTML_OG_IMAGE= # optional: information for web crawlers
REACT_APP_HTML_OG_TITLE=Amsterdam Music Lab Experiment # optional: information for web crawlers
REACT_APP_HTML_OG_URL= # optional: information for web crawlers
REACT_APP_HTML_BODY_CLASS= # optional: assign a css class to the <body> tag in index.html, for custom styling
REACT_APP_SENTRY_DSN= # optional: link to sentry instance, e.g. https://xxx@xxx.ingest.sentry.io/xxx
REACT_APP_STRICT= # optional: use StrictMode for development
FRONTEND_API_ROOT=http://localhost:8000 # address of the server, don't change
FRONTEND_EXPERIMENT_SLUG=gold-msi # experiment slug that the frontend redirects to
FRONTEND_AML_HOME=https://www.amsterdammusiclab.nl # website you will be redirected to if you do not agree with an informed consent form
FRONTEND_LOGO_URL= # optional: link to logo
FRONTEND_HTML_PAGE_TITLE=Amsterdam Music Lab Experiment # optional: information for web crawlers
FRONTEND_HTML_FAVICON=/favicon.ico # optional: which image to use as favicon
FRONTEND_HTML_OG_DESCRIPTION=Listening experiments from the Amsterdam Music Lab. Test your musical knowledge and skills in engaging citizen-science experiments. # optional: information for web crawlers
FRONTEND_HTML_OG_IMAGE= # optional: information for web crawlers
FRONTEND_HTML_OG_TITLE=Amsterdam Music Lab Experiment # optional: information for web crawlers
FRONTEND_HTML_OG_URL= # optional: information for web crawlers
FRONTEND_HTML_BODY_CLASS= # optional: assign a css class to the <body> tag in index.html, for custom styling
FRONTEND_SENTRY_DSN= # optional: link to sentry instance, e.g. https://xxx@xxx.ingest.sentry.io/xxx
FRONTEND_STRICT= # optional: use StrictMode for development
18 changes: 9 additions & 9 deletions README_TOONTJEHOGER.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ backend/templates/info/toontjehoger/experiment6.html
Use the following .env variables to configure your ToontjeHoger instance:

```
REACT_APP_EXPERIMENT_SLUG=toontjehoger
REACT_APP_AML_HOME=/toontjehoger
REACT_APP_LOGO_URL=/images/experiments/toontjehoger/logo-white.svg
REACT_APP_HTML_PAGE_TITLE=ToontjeHoger
REACT_APP_HTML_OG_DESCRIPTION=ToontjeHoger is een website met spelletjes die de luisteraar laat inzien dat zij muzikaler is dan je zou denken.
REACT_APP_HTML_OG_IMAGE=https://toontjehoger.changeme.example.com/images/og-hooked.jpg
REACT_APP_HTML_OG_TITLE=ToontjeHoger
REACT_APP_HTML_OG_URL=https://toontjehoger.changeme.example.com/
REACT_APP_HTML_BODY_CLASS=toontjehoger
FRONTEND_EXPERIMENT_SLUG=toontjehoger
FRONTEND_AML_HOME=/toontjehoger
FRONTEND_LOGO_URL=/images/experiments/toontjehoger/logo-white.svg
FRONTEND_HTML_PAGE_TITLE=ToontjeHoger
FRONTEND_HTML_OG_DESCRIPTION=ToontjeHoger is een website met spelletjes die de luisteraar laat inzien dat zij muzikaler is dan je zou denken.
FRONTEND_HTML_OG_IMAGE=https://toontjehoger.changeme.example.com/images/og-hooked.jpg
FRONTEND_HTML_OG_TITLE=ToontjeHoger
FRONTEND_HTML_OG_URL=https://toontjehoger.changeme.example.com/
FRONTEND_HTML_BODY_CLASS=toontjehoger
```

## Playlists
Expand Down
26 changes: 13 additions & 13 deletions docker-compose-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ services:
source: ./frontend/.storybook
target: /client/.storybook
environment:
- REACT_APP_API_ROOT=${REACT_APP_API_ROOT}
- REACT_APP_EXPERIMENT_SLUG=${REACT_APP_EXPERIMENT_SLUG}
- REACT_APP_AML_HOME=${REACT_APP_AML_HOME}
- REACT_APP_LOGO_URL=${REACT_APP_LOGO_URL}
- REACT_APP_HTML_FAVICON=${REACT_APP_HTML_FAVICON}
- REACT_APP_HTML_PAGE_TITLE=${REACT_APP_HTML_PAGE_TITLE}
- REACT_APP_HTML_OG_DESCRIPTION=${REACT_APP_HTML_OG_DESCRIPTION}
- REACT_APP_HTML_OG_IMAGE=${REACT_APP_HTML_OG_IMAGE}
- REACT_APP_HTML_OG_TITLE=${REACT_APP_HTML_OG_TITLE}
- REACT_APP_HTML_OG_URL=${REACT_APP_HTML_OG_URL}
- REACT_APP_HTML_BODY_CLASS=${REACT_APP_HTML_BODY_CLASS}
- REACT_APP_SENTRY_DSN=${REACT_APP_SENTRY_DSN}
- REACT_APP_STRICT=${REACT_APP_STRICT}
- REACT_APP_API_ROOT=${FRONTEND_API_ROOT}
- REACT_APP_EXPERIMENT_SLUG=${FRONTEND_EXPERIMENT_SLUG}
- REACT_APP_AML_HOME=${FRONTEND_AML_HOME}
- REACT_APP_LOGO_URL=${FRONTEND_LOGO_URL}
- REACT_APP_HTML_FAVICON=${FRONTEND_HTML_FAVICON}
- REACT_APP_HTML_PAGE_TITLE=${FRONTEND_HTML_PAGE_TITLE}
- REACT_APP_HTML_OG_DESCRIPTION=${FRONTEND_HTML_OG_DESCRIPTION}
- REACT_APP_HTML_OG_IMAGE=${FRONTEND_HTML_OG_IMAGE}
- REACT_APP_HTML_OG_TITLE=${FRONTEND_HTML_OG_TITLE}
- REACT_APP_HTML_OG_URL=${FRONTEND_HTML_OG_URL}
- REACT_APP_HTML_BODY_CLASS=${FRONTEND_HTML_BODY_CLASS}
- REACT_APP_SENTRY_DSN=${FRONTEND_SENTRY_DSN}
- REACT_APP_STRICT=${FRONTEND_STRICT}
restart: "no"

# This service is responsible for serving
Expand Down
41 changes: 28 additions & 13 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,35 @@ services:
- type: bind
source: ./frontend/.storybook
target: /client/.storybook
- type: bind
source: ./frontend/tsconfig.json
target: /client/tsconfig.json
- type: bind
source: ./frontend/vite-env.d.ts
target: /client/vite-env.d.ts
- type: bind
source: ./frontend/vite.config.ts
target: /client/vite.config.ts
- type: bind
source: ./frontend/vitest.config.ts
target: /client/vitest.config.ts
- type: bind
source: ./frontend/index.html
target: /client/index.html
environment:
- REACT_APP_API_ROOT=${REACT_APP_API_ROOT}
- REACT_APP_EXPERIMENT_SLUG=${REACT_APP_EXPERIMENT_SLUG}
- REACT_APP_AML_HOME=${REACT_APP_AML_HOME}
- REACT_APP_LOGO_URL=${REACT_APP_LOGO_URL}
- REACT_APP_HTML_FAVICON=${REACT_APP_HTML_FAVICON}
- REACT_APP_HTML_PAGE_TITLE=${REACT_APP_HTML_PAGE_TITLE}
- REACT_APP_HTML_OG_DESCRIPTION=${REACT_APP_HTML_OG_DESCRIPTION}
- REACT_APP_HTML_OG_IMAGE=${REACT_APP_HTML_OG_IMAGE}
- REACT_APP_HTML_OG_TITLE=${REACT_APP_HTML_OG_TITLE}
- REACT_APP_HTML_OG_URL=${REACT_APP_HTML_OG_URL}
- REACT_APP_HTML_BODY_CLASS=${REACT_APP_HTML_BODY_CLASS}
- REACT_APP_SENTRY_DSN=${REACT_APP_SENTRY_DSN}
- REACT_APP_STRICT=${REACT_APP_STRICT}
- VITE_API_ROOT=${FRONTEND_API_ROOT}
- VITE_EXPERIMENT_SLUG=${FRONTEND_EXPERIMENT_SLUG}
- VITE_AML_HOME=${FRONTEND_AML_HOME}
- VITE_LOGO_URL=${FRONTEND_LOGO_URL}
- VITE_HTML_FAVICON=${FRONTEND_HTML_FAVICON}
- VITE_HTML_PAGE_TITLE=${FRONTEND_HTML_PAGE_TITLE}
- VITE_HTML_OG_DESCRIPTION=${FRONTEND_HTML_OG_DESCRIPTION}
- VITE_HTML_OG_IMAGE=${FRONTEND_HTML_OG_IMAGE}
- VITE_HTML_OG_TITLE=${FRONTEND_HTML_OG_TITLE}
- VITE_HTML_OG_URL=${FRONTEND_HTML_OG_URL}
- VITE_HTML_BODY_CLASS=${FRONTEND_HTML_BODY_CLASS}
- VITE_SENTRY_DSN=${FRONTEND_SENTRY_DSN}
- VITE_STRICT=${FRONTEND_STRICT}
ports:
- 3000:3000
command: sh -c "yarn scss && yarn start"
Expand Down
24 changes: 0 additions & 24 deletions frontend/.storybook/main.js

This file was deleted.

40 changes: 40 additions & 0 deletions frontend/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { StorybookConfig } from '@storybook/react-vite';
import { mergeConfig } from 'vite';

const config: StorybookConfig = {
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-onboarding",
"@storybook/addon-interactions",
],
core: {
builder: '@storybook/builder-vite',
},
docs: {
autodocs: "tag",
},
viteFinal: (config) => {
return mergeConfig(config, {
esbuild: {
loader: 'jsx',
},
resolve: {
alias: {
'@/': '/src/',
},
},
});
},
framework: {
name: "@storybook/react-vite",
options: { },
},
staticDirs: ["../public"],
env: {
VITE_API_ROOT: "http://localhost:8000",
},
};

export default config;
12 changes: 6 additions & 6 deletions frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Tip: Use SongBool or SongSync as an example when creating a new view with audio
Use the following variables to configure your index.html file:

```
REACT_APP_HTML_PAGE_TITLE=Amsterdam Music Lab Experiment
REACT_APP_HTML_OG_DESCRIPTION=Listening experiments from the Amsterdam Music Lab. Test your musical knowledge and skills in engaging citizen-science experiments.
REACT_APP_HTML_OG_IMAGE=https://app.amsterdammusiclab.nl/images/og-hooked.jpg
REACT_APP_HTML_OG_TITLE=Amsterdam Music Lab Experiment
REACT_APP_HTML_OG_URL=https://app.amsterdammusiclab.nl
REACT_APP_HTML_BODY_CLASS=
FRONTEND_HTML_PAGE_TITLE=Amsterdam Music Lab Experiment
FRONTEND_HTML_OG_DESCRIPTION=Listening experiments from the Amsterdam Music Lab. Test your musical knowledge and skills in engaging citizen-science experiments.
FRONTEND_HTML_OG_IMAGE=https://app.amsterdammusiclab.nl/images/og-hooked.jpg
FRONTEND_HTML_OG_TITLE=Amsterdam Music Lab Experiment
FRONTEND_HTML_OG_URL=https://app.amsterdammusiclab.nl
FRONTEND_HTML_BODY_CLASS=
```
40 changes: 40 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%VITE_HTML_FAVICON%" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
/>
<meta name="mobile-web-app-capable" content="yes" />
<meta name="theme-color" content="#212529" />
<meta name="description" content="%VITE_HTML_OG_DESCRIPTION%" />
<meta property="og:title" content="%VITE_HTML_OG_TITLE%" />
<meta property="og:image" content="%VITE_HTML_OG_IMAGE%" />
<meta property="og:image:url" content="%VITE_HTML_OG_IMAGE%" />
<meta property="og:image:height" content="628" />
<meta property="og:image:width" content="1200"/>
<meta property="og:image:type" content="image/png"/>
<meta property="twitter:image" content="%VITE_HTML_OG_IMAGE%" />
<meta name="twitter:card" content="summary_large_image" />
<meta
property="og:description"
content="%VITE_HTML_OG_DESCRIPTION%"
/>
<meta property="og:url" content="%VITE_HTML_OG_URL%" />

<link rel="apple-touch-icon" href="/logo192.png" />
<link rel="manifest" href="/manifest.json" />
<link
rel="stylesheet"
href="/vendor/bootstrap/bootstrap.min.css"
/>
<title>%VITE_HTML_PAGE_TITLE%</title>
</head>
<body class="root %VITE_HTML_BODY_CLASS%">
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
65 changes: 34 additions & 31 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "aml-frontend",
"description": "The React Frontend for the MUSCLE platform",
"type": "module",
"license": "MIT",
"dependencies": {
"@sentry/react": "^7.85.0",
"@types/react-router-dom": "^5.3.3",
"@vitejs/plugin-react": "^4.2.1",
"axios": ">=1.6.0",
"classnames": "^2.2.6",
"email-validator": "^2.0.4",
Expand All @@ -15,46 +18,45 @@
"react-rangeslider": "^2.2.0",
"react-router": "5.2.0",
"react-router-dom": "5.2.0",
"react-scripts": "5.0.0",
"react-select": "^5.4.0",
"react-transition-group": "^4.4.5",
"sass": "^1.69.5",
"typescript": "^5.3.3",
"vite": "^5.0.10",
"vite-tsconfig-paths": "^4.2.2",
"zustand": "^4.4.7"
},
"scripts": {
"start": "react-scripts start",
"build": "sass src/index.scss src/index.css; react-scripts build",
"test": "react-scripts test",
"test:ci": "react-scripts test --coverage --watchAll=false",
"start": "vite --host",
"build": "sass src/index.scss src/index.css && vite build",
"preview": "vite preview",
"test": "vitest",
"test:ci": "vitest --watch=false --coverage",
"generate-badge": "coverage-badges -s coverage/coverage-summary.json -o coverage-frontend-badge-new.svg --label 'Frontend Code Coverage'",
"eject": "react-scripts eject",
"scss": "sass src/index.scss src/index.css",
"scss:watch": "sass src/index.scss src/index.css; sass --watch src/index.scss src/index.css",
"storybook": "REACT_APP_API_ROOT=http://localhost:8000 && storybook dev -p 6006",
"storybook": "VITE_API_ROOT=http://localhost:8000 && storybook dev -p 6006",
"storybook:build": "storybook build",
"lint:ts": "eslint . --ext .js,.jsx,.ts,.tsx",
"lint": "eslint src/**/*.js",
"lint": "eslint src/**/*.{js,jsx,ts,tsx}",
"lint:fix": "eslint --fix src/**/*.js"
},
"jest": {
"moduleNameMapper": {
"^components/(.*)$": "<rootDir>/src/components/$1",
"^config/(.*)$": "<rootDir>/src/config/$1",
"^hooks/(.*)$": "<rootDir>/src/hooks/$1",
"^scss/(.*)$": "<rootDir>/src/scss/$1",
"^stories/(.*)$": "<rootDir>/src/stories/$1",
"^util/(.*)$": "<rootDir>/src/util/$1"
},
"coverageReporters": [
"lcov",
"json-summary"
]
},
"eslintConfig": {
"plugins": [
"chai-friendly"
],
"extends": [
"react-app",
"plugin:storybook/recommended"
]
],
"rules": {
"no-unused-expressions": 0,
"chai-friendly/no-unused-expressions": [
"error",
{
"allowShortCircuit": true
}
]
}
},
"browserslist": {
"production": [
Expand All @@ -74,22 +76,23 @@
"@storybook/addon-links": "7.6.6",
"@storybook/addon-onboarding": "1.0.10",
"@storybook/blocks": "7.6.6",
"@storybook/preset-create-react-app": "7.6.6",
"@storybook/react": "7.6.6",
"@storybook/react-webpack5": "7.6.6",
"@storybook/builder-vite": "^7.6.17",
"@storybook/react-vite": "^7.6.17",
"@storybook/testing-library": "0.2.2",
"@testing-library/jest-dom": "^6.1.5",
"@testing-library/react": "^14.2.1",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.1",
"@vitest/coverage-v8": "^1.2.1",
"axios-mock-adapter": "^1.22.0",
"babel-plugin-named-exports-order": "0.0.2",
"coverage-badges-cli": "^1.2.5",
"eslint": "^8.54.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-chai-friendly": "^0.7.2",
"eslint-plugin-storybook": "^0.6.15",
"happy-dom": "^12.10.3",
"history": "^5.3.0",
"jest-mock-axios": "^4.7.3",
"prop-types": "15.8.1",
"storybook": "7.6.6",
"webpack": "5.89.0"
"vitest": "^1.1.0"
}
}
Loading

0 comments on commit 2c303a0

Please sign in to comment.