Skip to content

Frontend Development Environment

Karl Tarvas edited this page Feb 8, 2022 · 26 revisions

Prerequisites

Node and npm have to be installed:

zypper in nodejs8 # nodejs10 works too

http://download.opensuse.org/repositories/devel:/languages:/nodejs/openSUSE_Leap_15.1/

npm install -g yarn
yarn global add webpack
yarn global add flow-bin@0.107.0
cd web/html/src
yarn install

Frontend Server - faster development cycle - frontend proxy server - yarn proxy

Frontend proxy server that will serve all the local javascript files and proxy the rest for a chosen backend server.

cd web/html/src
# Don't forget about the https://
yarn proxy https://server.tf.local

Frontend Server - React hot reload

React hot reload allows us to see our react changes without refreshing the whole page. For that to work we need to have our "yarn proxy" running and make sure the entry point of the react page we are working on has the hot reloading enabled. You can find an example here: https://github.com/uyuni-project/uyuni/blob/master/web/html/src/manager/content-management/create-project/create-project.js#L59

Just be aware that this mode doesn't work for all the changes as the components aren't remounted again. If you have new code inside constructor/lifecycle methods, you might need to reload the full page.

React style guide of reusable components

We use storybooks to document our React reusable components. This can be useful either to find which components can be reused while implementing a new feature or to understand the impact of changing a component. The development of the react reusable components ideally should be styleguide first.

Instructions to run the styleguide (this assumes you have the frontend proxy server running):

cd web/html/src
yarn run storybook

After this all the files "<component_name<.stories.js" will be loaded as documentation inside the styleguide. You can consult the official docs storybooks for more information.

New dependency

All the frontend dependencies are tracked inside the module "susemanager-frontend/susemanager-nodejs-sdk-devel". Inside OBS these are the only dependencies that will be available.

To add a new dependency run the command

yarn add <depency_name>
or
yarn add --dev <depency_name> (if it's only a development/build dependency)

After running this command run "yarn build" on web/html/src, add a new entry to the file "susemanager-frontend/susemanager-nodejs-sdk-devel/susemanager-nodejs-sdk-devel.changes" and commit all the generated files

How to add a new react page

All the routes exported on the files 'manager/<folder_name>/index.js' will be automatically registered as react pages entries. Check the file content-management/index.js for an example. Each page there will have their own bundle automatically created and asynchronously fetched by webpack.

Folders Structure

For the sake of organization, each new react page should be inside their own new folder. A good structure for a new React page would be:

src/manager/dashboard/dashboard.js
src/manager/dashboard/dashboard.renderer.js

The file dashboard.renderer.js will provide a renderer function to render the page. This function can be obtained from anywhere using the global function: spaImportReactPage('dashboard/dashboard').then(function(module) {module.renderer('id')}

For an example take a look at the content-management feature.

Bear in mind if the new react page is expected to grow a lot, instead of creating multiple react child pages, you could consider the creation of a Mini SPA react app adding client routing with "react-router"

Adding a new Component

All shared components are living in the components folder. Ideally for each component the following files should be created withing a sub-folder:

  • <ComponentName>.js: contains the ComponentName code
  • <ComponentName>.stories.js: holds some demo code to showcase the component in storybook. New story files should be written using the Component Story Format (CSF).
  • <ComponentName>.test.js: holds the Jest tests for the component.

When writing a component, ensure that every property flow type and component has a JSDoc comment: those will be rendered by the storybook in the Docs tab. The downside of this is that all the property types of the component need to be defined in the component's file. Default property values are also displayed by Storybook.

yarn.lock conflicts - What should I do

Conflicts on this file can be hard to solve manually, luckily Yarn has an internal tool to figure out conflicts.

  • run Yarn with no arguments: yarn

If this doesn't resolve all of the conflicts, you can try the following:

  • solve conflicts with theirs
  • run yarn add again with the new dependencies you added

Linting

cd $PROJECT_ROOT/web/html/src
yarn lint

Blogpost

Blogpost about frontend work on UYUNI until October 2019. In the comments, there might be some useful ideas for the future. https://medium.com/better-programming/dont-be-afraid-of-legacy-how-to-gradually-introduce-react-js-in-a-really-old-application-79876c0dfa42?source=friends_link&sk=5499d4c809b8ccd6af121f61fd7bacd4

susemanager-nodejs-sdk-devel

Only the libraries present in this module will be upload to OBS. Yarn install will download all the needed libraries in jenkins and uploaded to OBS as a zip to be used to build our rpms.

Clone this wiki locally