Giter Site home page Giter Site logo

kuubson / react-vite-trpc Goto Github PK

View Code? Open in Web Editor NEW
81.0 1.0 5.0 219 KB

template for monorepo (turborepo): client (React + Vite) + server (Express + tRPC) + vitest + cypress + pnpm, emphasizing the use of absolute paths.

License: MIT License

JavaScript 6.62% HTML 2.01% TypeScript 90.75% SCSS 0.62%
node react trpc vite pnpm typescript vitest cypress

react-vite-trpc's Introduction

๐Ÿ› ๏ธ Monorepo template

A template that provides a straightforward and flexible way to use the benefits of tRPC in your React projects. It emphasizes the use of absolute paths and a monorepo approach that significantly improves the developer experience. If you're looking for a clean setup with pure React and modularization, this template is an excellent place to start!

Stack Highlights Quick start Challenge Scripts Env Ports License

๐Ÿ”ง Stack

TypeScript React Vite Styled Components Cypress

NodeJS Express tRPC Vitest Turborepo

Render GitHub Actions

๐ŸŒŸ Highlights

Global Server Web
  • โœ… Envs validation with envalid
  • โœจ prettier + eslint
  • โžก๏ธ Auto sorting imports with @trivago/prettier-plugin-sort-imports
  • ๐Ÿš€ Simple Github Actions workflow for ts/lint check + running tests
  • ๐Ÿ“ฆ Bundling local packages with tsup
  • โš™๏ธ pm2 for running server as a background process (i.e. for test coverage)
  • ๐Ÿ–ง Express + tRPC
  • โœ… vitest (unit / integration / e2e tests) + test coverage (istanbul)
  • ๐Ÿ”‘ separated .env files for development and testing
  • ๐Ÿ› ๏ธ Absolute paths set up with module-alias
  • โšก Server compilation with swc
  • ๐Ÿ”„ nodemon autoreload based on source code and local packages
  • โš›๏ธ React + Vite
  • ๐Ÿ—๏ธ Feature-Driven Development architecture
  • ๐Ÿ’… styled-components + stylelint
  • ๐Ÿ›ค๏ธ Routing with @tanstack/router
  • โœ… vitest (unit / integration) + test coverage (istanbul)
  • ๐ŸŒ cypress (e2e)

๐Ÿš€ Quick start

Preview the deployment: https://react-vite-trpc.onrender.com

For local execution, use pnpm install and pnpm dev.

Customization & tips:

  • To tailor the template to your needs, replace of @react-vite-trpc with @your-idea (you can use CMD + SHIFT + H in VS Code to do this globally)

  • When deploying, make sure to update the host property in the HttpService class (@react-vite-trpc\config\src\services\httpService.ts)

  • Adjust the preferred order of imports in .prettierrc files using the importOrder property

  • When adding a new local package (i.e. /@react-vite-trpc/new-package), remember to update the watch array in nodemon.json and provide appropriate TypeScript references in tsconfig.json files for an enhanced DX

  • The test coverage .lcov files (generated with pnpm test:coverage), can be easily leveraged with VS Code extension Coverage Gutters

๐Ÿงฉ The challenge

This setup faced a challenge while importing the AppRouter from the server folder to the client folder, which resulted in Typescript server errors related to imports from the 'trpc' path on the server side.

The solution leverages Typescript references to allow importing the AppRouter on the client side while using absolute paths on the server side.

// apps/web/tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "dist",
  },
  "references": [{ "path": "../server" }] <~ fixes the /server references on the /web
}

// apps/web/package.json
{
  "scripts": {
    "ts:check": "tsc -b", <~ the -b flag is crucial when building an app that has references in its tsconfig.json
    "build": "pnpm ts:check && vite build"
  }
}

// apps/server/tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "dist", <~ required, sets the build destination folder
    "composite": true <~ required to make TS references work
  },
  "ts-node": { "swc": true }
}

โŒจ Scripts

command description
pnpm start Runs the production build of the server (/server)
pnpm pm2:start Runs the server production build as a background process, using pm2 (/server)
pnpm pm2:delete Deletes all pm2 processes (/server)
pnpm pm2:logs Shows logs for pm2 (/server)
pnpm dev Launches apps and bundles all packages in watch mode
pnpm lint Performs an eslint check through all workspaces
pnpm lint:fix Performs an eslint fix through all workspaces
pnpm ts:check Performs a TypeScript check through all workspaces
pnpm ts:references Syncs TypeScript references in all tsconfig.json files + updates nodemon.json watch array
pnpm stylelint Performs an stylelint check through all workspaces
pnpm check Performs eslint, TypeScript, and stylelint checks through all workspaces
pnpm build Builds all apps
pnpm build:lib Bundles all packages
pnpm test:unit Runs unit tests in watch mode
pnpm test:unit:run Runs unit tests once
pnpm test:integration Runs integration tests in watch mode
pnpm test:integration:run Runs integration tests once
pnpm test:e2e Runs e2e tests in watch mode
pnpm test:e2e:run Runs e2e tests once
pnpm test:coverage Generates test coverage reports
pnpm test:coverage:preview Generates test coverage reports and opens preview
pnpm cypress Opens the Cypress UI (/web)
pnpm cypress:install Installs the Cypress (/web)
pnpm postinstall Ensures that local or CI environment is ready after installing packages

๐Ÿ”’ Envs

Envs are validated with the package envalid. Check out .env-example & .env.test-example files

If the pnpm dev script is executed without the required environment variables, the application will output similar details in the console:

================================
Missing environment variables:
PORT: Port the Express server is running on (eg. "3001"). See https://expressjs.com/en/starter/hello-world.html
================================

๐ŸŒ Ports

  • ๐ŸŒ :3000 - Web
  • ๐Ÿ–ฅ๏ธ :3001 - Server

๐Ÿ“œ License

The MIT License (MIT)

react-vite-trpc's People

Contributors

kuubson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-vite-trpc's Issues

RFC: Some proposed changes

Hi, I have some ideas for improving this repo and I'd like to know what you think.

Depending on your response I'd happily open a PR / some PRs addressing them.

Server Hotreload

We can add a simple nodemon setup so that when you change some code in the server, it will automatically reload the changes that you made. This is a great DX.

Basic Github Actions

I know every project has some peculiarities and since this is a template we shouldn't add too much, but having some small github actions in place can be very useful and, honestly, pretty easy to delete if you don't like them.
This amazing Template repo called Create-T3-Turbo is a great reference of template repo in my opinion. They implemented some simple github actions and I could implement something very similar for this project.

What it does is: everytime you open a PR, Github automatically runs the ESLint checks in your changes to see if something went wrong with your types and code formatting. Great stuff!

License

It is pretty nice to have a License file in the project so it's more obvious how people can use your repo. Projects usually go for is the MIT License, would you be up for that one? That is a very standard license that allows people to take your code to any direction they want... which is perfect for a template repo. That's the same license they are using in Create-T3-Turbo

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.