Skip to content

Commit

Permalink
frontend: Replace Create React App with Vite. Replace jest with vitest
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Dubenko <oldubenko@microsoft.com>
Co-authored-by: Mariusz Winnik <tazo90@gmail.com>
  • Loading branch information
sniok and tazo90 committed May 21, 2024
1 parent 17f6e7b commit 302294f
Show file tree
Hide file tree
Showing 175 changed files with 99,103 additions and 137,188 deletions.
10 changes: 7 additions & 3 deletions backend/cmd/headlamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ func baseURLReplace(staticDir string, baseURL string) {

copyReplace(indexBaseURL,
index,
[]byte("headlampBaseUrl = './'"),
[]byte("headlampBaseUrl = '"+replaceURL+"'"),
// Replace any resource that has "./" in it
[]byte("./"),
[]byte(baseURL+"/"),
[]byte("headlampBaseUrl=\".\""),
[]byte("headlampBaseUrl=\""+replaceURL+"\""))
[]byte(baseURL+"/"))

// Insert baseURL in css url() imports, they don't have "./" in them
copyReplace(index, index, []byte("url("), []byte("url("+baseURL+"/"), []byte(""), []byte(""))
}

func getOidcCallbackURL(r *http.Request, config *HeadlampConfig) string {
Expand Down
2 changes: 2 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ yarn-debug.log*
yarn-error.log*

.eslintcache

**/locales/**/*_old.json
20 changes: 8 additions & 12 deletions frontend/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
module.exports = {
export default {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],

addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
// '@storybook/addon-interactions',
'@storybook/preset-create-react-app',
],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],

core: {
disableTelemetry: true
builder: '@storybook/builder-vite',
disableTelemetry: true,
},

webpackFinal: config => {
Expand All @@ -24,18 +20,18 @@ module.exports = {
https: require.resolve('https-browserify'),
path: require.resolve('path-browserify'),
stream: require.resolve('stream-browserify'),
vm: require.resolve("vm-browserify"),
vm: require.resolve('vm-browserify'),
},
},
};
},

framework: {
name: '@storybook/react-webpack5',
options: {}
options: {},
},

docs: {
autodocs: true
}
autodocs: true,
},
};
26 changes: 15 additions & 11 deletions frontend/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import themesConf from '../src/lib/themes';
import { ThemeProvider, StyledEngineProvider } from '@mui/material/styles';
import { initialize, mswDecorator } from 'msw-storybook-addon';
import { rest } from 'msw'
import { rest } from 'msw';

// https://github.com/mswjs/msw-storybook-addon
initialize();
Expand All @@ -11,9 +11,7 @@ const darkTheme = themesConf['dark'];
const lightTheme = themesConf['light'];

const withThemeProvider = (Story, context) => {
const backgroundColor = context.globals.backgrounds
? context.globals.backgrounds.value
: 'light';
const backgroundColor = context.globals.backgrounds ? context.globals.backgrounds.value : 'light';
const theme = backgroundColor !== 'dark' ? lightTheme : darkTheme;

const ourThemeProvider = (
Expand Down Expand Up @@ -60,18 +58,24 @@ export const parameters = {
rest.get('http://localhost/api/v1/events', (_req, res, ctx) => {
return res(ctx.json({}));
}),
rest.post('http://localhost/apis/authorization.k8s.io/v1/selfsubjectaccessreviews', (_req, res, ctx) => {
return res(ctx.json({status: 200}));
}),
rest.post(
'http://localhost/apis/authorization.k8s.io/v1/selfsubjectaccessreviews',
(_req, res, ctx) => {
return res(ctx.json({ status: 200 }));
}
),
rest.get('http://localhost:4466/api/v1/namespaces', (_req, res, ctx) => {
return res(ctx.json({}));
}),
rest.get('http://localhost:4466/api/v1/events', (_req, res, ctx) => {
return res(ctx.json({}));
}),
rest.post('http://localhost:4466/apis/authorization.k8s.io/v1/selfsubjectaccessreviews', (_req, res, ctx) => {
return res(ctx.json({status: 200}));
}),
]
rest.post(
'http://localhost:4466/apis/authorization.k8s.io/v1/selfsubjectaccessreviews',
(_req, res, ctx) => {
return res(ctx.json({ status: 200 }));
}
),
],
},
};
82 changes: 32 additions & 50 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,50 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
# Headlamp Frontend

## Available Scripts
React frontend for the Headlamp. Used by the electron app and for in-cluster deployment.

In the project directory, you can run:
## Development

### `npm start`
To start the project run:

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
```sh
npm run start
```

The page will reload if you make edits.<br />
You will also see any lint errors in the console.
This will start a development server at the 3000 port.
Make sure the backend is running. See more in [Backend Readme](../backend/README.md).

### `npm test`
## Storybook

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
[Storybook documentation](https://storybook.js.org/docs/get-started)
We have stories for a lot of components. Stories are files that end in .stories.tsx. To open storybook run:

### `npm run build`
```sh
npm run storybook
```

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.
## Tests

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!
[Vitest documentation](https://vitest.dev/guide/)
Tests are run using vitest. To run all test use:

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
```sh
npm run test
```

### `npm run eject`
To run some particular test:

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
```sh
npm run test -- testFileName
```

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
If you're using VSCode consider installing [vitest extension](https://marketplace.visualstudio.com/items?itemName=vitest.explorer).

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
## Production build

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
To create a production build run

## Learn More
```sh
npm run build
```

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `npm run build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
This will create static production app in the `build/` folder.
49 changes: 24 additions & 25 deletions frontend/public/index.html → frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="apple-touch-icon" sizes="180x180" href="%PUBLIC_URL%/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="%PUBLIC_URL%/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="%PUBLIC_URL%/favicon-16x16.png">
<link rel="mask-icon" href="%PUBLIC_URL%/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#fff">
<meta
name="description"
content="Headlamp: Kubernetes Web UI"
/>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
<meta name="msapplication-TileColor" content="#fff" />
<meta name="description" content="Headlamp: Kubernetes Web UI" />
<!-- Custom font -->
<style type="text/css">
/* cyrillic-ext */
Expand All @@ -22,7 +19,7 @@
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(%PUBLIC_URL%/fonts/overpass/cyrillic-ext.woff2) format('woff2');
src: url(/fonts/overpass/cyrillic-ext.woff2) format('woff2');
unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
/* cyrillic */
Expand All @@ -31,7 +28,7 @@
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(%PUBLIC_URL%/fonts/overpass/cyrillic.woff2) format('woff2');
src: url(/fonts/overpass/cyrillic.woff2) format('woff2');
unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
/* vietnamese */
Expand All @@ -40,45 +37,46 @@
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(%PUBLIC_URL%/fonts/overpass/vietnamese.woff2) format('woff2');
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB;
src: url(/fonts/overpass/vietnamese.woff2) format('woff2');
unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0,
U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
font-family: 'Overpass';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(%PUBLIC_URL%/fonts/overpass/latin-ext.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
src: url(/fonts/overpass/latin-ext.woff2) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113,
U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Overpass';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(%PUBLIC_URL%/fonts/overpass/latin.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
src: url(/fonts/overpass/latin.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
</style>
<link rel="apple-touch-icon" href="logo192.png" />
<!--
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" />
<link rel="manifest" href="/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
Unlike "/favicon.ico" or "favicon.ico", "/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`.
-->
<title>Headlamp</title>
<script>headlampBaseUrl='%PUBLIC_URL%';</script>
<script>
headlampBaseUrl = '%BASE_URL%';
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand All @@ -93,5 +91,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>
12 changes: 5 additions & 7 deletions frontend/make-env.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

// Creates the .env file

const fs = require('fs');
const {execSync} = require('child_process');
const appInfo = require('../app/package.json');
import { execSync } from 'child_process';
import fs from 'fs';
const appInfo = JSON.parse(fs.readFileSync('../app/package.json', 'utf8'));

const gitVersion = execSync('git rev-parse HEAD').toString().trim();

Expand All @@ -17,8 +15,8 @@ const envContents = {
function createEnvText() {
let text = '';
Object.entries(envContents).forEach(([key, value]) => {
text += `${key}=${value}\n`
})
text += `${key}=${value}\n`;
});

return text;
}
Expand Down
Loading

0 comments on commit 302294f

Please sign in to comment.