Giter Club home page Giter Club logo

monorepo's Introduction

Create your own Monorepo

Code for the Youtube video on how to create your own Monorepo using PNPM workspace, React, Vue, Node, Eslint, Prettier and Typescript

The commands and code used throughout the video:

npm install -g pnpm
pnpm init
git init

Setup Prettier

pnpm add -D prettier

Create .prettierignore with the below contents:

coverage
public
dist
pnpm-lock.yaml
pnpm-workspace.yaml

Create .prettierrc with the below contents:

{
  "semi": true,
  "singleQuote": true
}

Setup VSCODE

Create workspace settings:

mkdir .vscode && touch .vscode/settings.json

Add below contents inside settings.json

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode"
}

Setup ESLint

pnpm create @eslint/config

Create .eslintignore with the below contents:

coverage
public
dist
pnpm-lock.yaml
pnpm-workspace.yaml

Integrating ESLint with Prettier

Add below plugins so that both Prettier and Eslint can do both of their jobs without getting in ecah other ways

pnpm add -D eslint-config-prettier eslint-plugin-prettier
module.exports = {
  extends: [..., 'plugin:prettier/recommended'],
}
npm pkg set scripts.lint="eslint ."
npm pkg set scripts.format="prettier --write ."

Pre-commit hooks

pnpm add --save-dev husky lint-staged
pnpm exec husky install
npm pkg set scripts.prepare="husky install"
pnpm exec husky add .husky/pre-commit "pnpm exec lint-staged"
"lint-staged": {
    "**/*.{js,ts,tsx}": [
      "eslint --fix"
    ],
    "**/*": "prettier --write --ignore-unknown"
  }

Setup workspace

touch pnpm-workspace.yaml
packages:
  - 'apps/*'
  - 'libs/*'
mkdir apps libs

Setup common package - utils

pnpm create vite utils --template vanilla-ts
cd utils/
pnpm install
npm pkg set scripts.dev="pnpm run build --watch"

Remove unnecessary files

And add below contents in main.ts file

export const isEmpty = (data: unknown) => {
if (data === null || data === undefined) {
return 'It is Empty';
}
return 'It is not Empty';
};

Setup Vite Library Mode

Add below package to create type definitions from the library

pnpm add -D vite-plugin-dts

Create vite.config.ts file and add below contents in it

import { defineConfig } from 'vite';
import { resolve } from 'path';
import dts from 'vite-plugin-dts';

export default defineConfig({
  build: { lib: { entry: resolve(__dirname, 'src/main.ts'), name: 'utils' } },
  plugins: [dts()],
});

Update utils package.json to have

{...,
  "main": "./dist/utils.umd.cjs",
  "module": "./dist/utils.js",
  "types": "./dist/main.d.ts",
}

Setup Backend

Create a backend package using below commands

cd apps/
mkdir backend
cd backend
pnpm init
npm pkg set type="module"

Create tsconfig.json with below contents in it

{
  "compilerOptions": {
    "module": "ES2020",
    "moduleResolution": "Node",
    "target": "ES2020",
    "sourceMap": true,
    "outDir": "dist",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true
  },
  "include": ["src/**/*"]
}

Create nodemon.json with below contents in it

{
  "execMap": {
    "ts": "ts-node-esm"
  }
}

Since our backend will use the utils library, hence update package.json dependencies with

"utils": "workspace:*"

and run

pnpm install

so that our backend can symlink the utils package present in the workspace

Create an app.ts file in src directory and add below contents in it

import express from 'express';
import cors from 'cors';
import utils from 'utils';

const PORT = process.env.PORT || 5000;

const app = express();

app.use(cors());

app.get('/', (req, res) => {
  res.json({ message: utils.isEmpty({}) });
});

app.listen(PORT, () => {
  console.log(`Server running on PORT ${PORT}`);
});

Create vue-frontend and react-frontend using below commands and repeat the process to add utils as a dependency to both of them

pnpm create vite vue-frontend --template vue-ts
cd vue-frontend
pnpm install
pnpm create vite react-frontend --template react-ts
cd react-frontend
pnpm install

Now to use the utils library in React, update src/App.ts with below contents

import { isEmpty } from 'utils';
...
...
useEffect(() => {
  fetch('http://localhost:5000')
    .then((res) => res.json())
    .then((msg) => console.log({ msg }));
}, []);
....
....
return(
...
<pre>{isEmpty('abc')}</pre>
<pre>{isEmpty(null)}</pre>
</>
)

And similarly for Vue, update src/App.vue with below contents

import { onMounted } from 'vue';
import { isEmpty } from 'utils';
...
...
onMounted(() => {
  fetch('http://localhost:5000')
    .then((data) => data.json())
    .then((data) => console.log(data));
});
....
....
<template>
...
...
<pre>{isEmpty({})}</pre>
<pre>{isEmpty(null)}</pre>
</template>

If you are facing any issue related to eslint prettier integration while committing code then run below command

pnpm add -D eslint-plugin-prettier -w

monorepo's People

Contributors

mohitkumartoshniwal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.