Monorepo Setup for Create React App and Components (2/4)
This is the second part of a serie which provides answers on setting up a fullstack monorepo. If need help on how to initialize a monorepo, have a look into part 1.
- Setup a Monorepo for Javascript backend services
- Add a Create React App frontend with a component libary (current)
- Support Typescript in packages
- Deploy to private Github NPM registry
Goals
- Website which lists events
- Backend service with Login to create new events
Create the Website
we will use Create React App to create the website:
cd services
yarn create react-app web
change the default port to 3001:
echo "PORT=3001" > web/.env
Create the Backend Web Application
we will use Create React App again:
cd services
yarn create react-app admin
change the default port to 3002:
echo "PORT=3002" > admin/.env
Create the shared React Comonents Libary
mkdir -p packages/ekzemplo-components/src
cd packages/ekzemplo-components
yarn init -y
yarn add react --peer
**Hint: ** --peer will add as a requirement but does not add it to the libary
Modify the new package.json:
- set
versionto `0.0.0“ - change
mainto./dist/index.js - add
moduleandsource - add
scripts
{
"name": "ekzemplo-components",
"version": "0.0.0",
"main": "./dist/index.js",
"module": "./dist/index.js",
"source": "./src/index.js",
"license": "MIT",
"scripts": {
"start": "microbundle watch --jsx React.createElement",
"build": "microbundle build --jsx React.createElement"
},
"peerDependencies": {
"react": "^17.0.1"
}
}
Important: the extentions for "module": "./dist/index.js" has to be .js - using .mjs will break create-react-app
add microbundle to transform and bundle the components:
yarn add --dev microbundle
create the src/Event.jsx component to show a event:
import React from "react"
export default function Event({ name, date }) {
return (
<div>
{date} - {name}
</div>
)
}
create the src/EventList.jsx component to show a event:
import React from "react"
import { useQuery } from "react-query"
import Event from "./Event"
2
export default function EventList() {
const { isLoading, error, data } = useQuery("repoData", () =>
fetch("http://localhost:3010/events").then(res => res.json()),
)
if (isLoading) return "Loading..."
if (error) return "An error has occurred: " + error.message
return (
<ul>
{data.map(e => (
<li key={e.id}>
<Event name={e.name} date={e.date} />
</li>
))}
</ul>
)
}
add the react-query to peerDependencies:
yarn add react-query --peer
add the src/index.js
import Event from "./Event"
import EventList from "./EventList"
export { Event, EventList }
transform all components
yarn build
switch back to the frontend:
cd ../../services/web
First, install http-proxy-middleware:
yarn add http-proxy-middleware
Next, create src/setupProxy.js to route all call to /events to the events microservice:
const { createProxyMiddleware } = require("http-proxy-middleware")
module.exports = function (app) {
app.use(
"/events",
createProxyMiddleware({
target: "http://localhost:3010",
changeOrigin: true,
}),
)
}
**next part: ** Support Typescript in packages