build(frontend): replace create-react-app with vite (#272)

This commit is contained in:
Julian Tölle 2023-05-07 04:05:11 +02:00 committed by GitHub
parent 8721fd101d
commit eb8fe11be6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 4700 additions and 22943 deletions

View file

@ -17,5 +17,7 @@
!frontend/postcss.config.js
!frontend/tailwind.config.js
!frontend/tsconfig.json
!frontend/vite.config.js
!frontend/index.html
!frontend/src/**/*
!frontend/public/**/*

View file

@ -54,14 +54,16 @@ services:
build:
context: .
target: build-frontend
command: ["npm", "run", "start"]
command: ["npm", "run", "start", "--", "--host"]
volumes:
- ./frontend/vite.config.js:/app/frontend/vite.config.js:ro
- ./frontend/postcss.config.js:/app/frontend/postcss.config.js:ro
- ./frontend/tailwind.config.js:/app/frontend/tailwind.config.js:ro
- ./frontend/index.html:/app/frontend/index.html:ro
- ./frontend/src:/app/frontend/src:ro
- ./frontend/public:/app/frontend/public:ro
ports:
- 3000
- 5173
labels:
- "traefik.enable=true" # Enable reverse-proxy for this service
- "traefik.http.routers.frontend.rule=PathPrefix(`/`)"

View file

@ -1 +1 @@
REACT_APP_VERSION=$VERSION
VITE_VERSION=$VERSION

View file

@ -2,15 +2,16 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.png" />
<link rel="icon" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#48bb78" />
<meta name="description" content="Listory - Track your Spotify Listens" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="manifest" href="/manifest.json" />
<title>Listory - Spotify Listen History</title>
</head>
<body>
<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>

27411
frontend/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -17,26 +17,30 @@
"@types/react-dom": "18.2.4",
"@types/react-router-dom": "5.3.3",
"@types/recharts": "1.8.24",
"@vitejs/plugin-react": "4.0.0",
"autoprefixer": "10.4.14",
"axios": "0.27.2",
"date-fns": "2.30.0",
"eslint-config-react-app": "7.0.1",
"jsdom": "22.0.0",
"npm-run-all": "4.1.5",
"postcss": "8.4.23",
"prettier": "2.8.8",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-router-dom": "6.11.1",
"react-scripts": "5.0.1",
"recharts": "2.5.0",
"tailwindcss": "3.3.2",
"typescript": "4.9.5"
"typescript": "4.9.5",
"vite": "4.3.5",
"vitest": "0.31.0"
},
"scripts": {
"format": "prettier --write \"./*.js\" \"src/**/*.(tsx|ts|css)\"",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"start": "vite",
"build": "vite build",
"serve": "vite preview",
"test": "vitest --passWithNoTests"
},
"eslintConfig": {
"extends": [

View file

@ -3,7 +3,7 @@ import React from "react";
const REPO_URL = "https://github.com/apricote/Listory";
const CHANGELOG_URL = `${REPO_URL}/blob/main/CHANGELOG.md`;
const VERSION = process.env.REACT_APP_VERSION || "Unknown";
const VERSION = import.meta.env.VITE_VERSION || "Unknown";
export const Footer: React.FC = () => {
return (

View file

@ -4,7 +4,6 @@ import { createRoot } from "react-dom/client";
import { App } from "./App";
import { ProvideApiClient } from "./hooks/use-api-client";
import { ProvideAuth } from "./hooks/use-auth";
import * as serviceWorker from "./serviceWorker";
import "./index.css";
const root = createRoot(document.getElementById("root")!);
@ -20,5 +19,3 @@ root.render(
</ProvideAuth>
</React.StrictMode>
);
serviceWorker.unregister();

View file

@ -1,147 +0,0 @@
/* eslint-disable no-console */
// This optional code is used to register a service worker.
// register() is not called by default.
// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.
// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA
const isLocalhost = Boolean(
window.location.hostname === "localhost" ||
// [::1] is the IPv6 localhost address.
window.location.hostname === "[::1]" ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);
type Config = {
onSuccess?: (registration: ServiceWorkerRegistration) => void;
onUpdate?: (registration: ServiceWorkerRegistration) => void;
};
export function register(config?: Config) {
if (process.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);
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
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}
window.addEventListener("load", () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);
// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
"This web app is being served cache-first by a service " +
"worker. To learn more, visit https://bit.ly/CRA-PWA"
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}
function registerValidSW(swUrl: string, config?: Config) {
navigator.serviceWorker
.register(swUrl)
.then((registration) => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === "installed") {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
"New content is available and will be used when all " +
"tabs for this page are closed. See https://bit.ly/CRA-PWA."
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log("Content is cached for offline use.");
// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch((error) => {
console.error("Error during service worker registration:", error);
});
}
function checkValidServiceWorker(swUrl: string, config?: Config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { "Service-Worker": "script" },
})
.then((response) => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get("content-type");
if (
response.status === 404 ||
(contentType != null && contentType.indexOf("javascript") === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
"No internet connection found. App is running in offline mode."
);
});
}
export function unregister() {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.ready
.then((registration) => {
registration.unregister();
})
.catch((error) => {
console.error(error.message);
});
}
}

11
frontend/tests/setup.js Normal file
View file

@ -0,0 +1,11 @@
import { expect, afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
import matchers from "@testing-library/jest-dom/matchers";
// extends Vitest's expect method with methods from react-testing-library
expect.extend(matchers);
// runs a cleanup after each test case (e.g. clearing jsdom)
afterEach(() => {
cleanup();
});

16
frontend/vite.config.js Normal file
View file

@ -0,0 +1,16 @@
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
export default defineConfig(() => {
return {
build: {
outDir: "build",
},
plugins: [react()],
test: {
globals: true,
environment: "jsdom",
setupFiles: "./tests/setup.js",
},
};
});