Ahmad Safari
Ahmad Safari
خواندن ۵ دقیقه·۸ ماه پیش

ایجاد Nx monorepo با ویژگی های TypeScript,storybook, Prettier, ESLint, Husky به همراه پروژه های ری اکت و نکست جی اس


create app as user and admin with next-js and react

  • چزور کانفیک های تیم ورک را تنظم کنیم؟
  • چطور ui-shared برای اشتراک در پروژه ها ایجاد کنیم؟
  • در Nx چطور هوک بسازیم؟
  • هاسکی را در Nx چطور کانفیگ کنیم ؟
  • چطور کتابخانه های مجزا روی پروزه ها نصب کنیم؟
  • و...

1- ساخت Nx Monorepo:

شما با دستور زیر به صورت عمومی میتوانید ان ایکس را ایجاد نمایید:

npm install -g nx

2- ساخت یک ورک اسپیس به همرا یک پروژه نکست:

npx create-nx-workspace@latest --preset=next # In add new next app by running the following command: nx g @nx/next:app my-next-app

✔ Where would you like to create your workspace? · my-workspace

✔ Application name · nextjs-user

✔ Would you like to use the App Router (recommended)? · No

✔ Would you like to use the src/ directory? · Yes

✔ Test runner to use for end to end (E2E) tests · cypress

✔ Default stylesheet format · @emotion/styled

✔ Do you want Nx Cloud to make your CI fast? · skip
after install next js ap

3- اضافه کردن یک پروژه جدید:

nx g @nx/react:app my-react-app

Replace my-react-app with your application name.
✔ Which stylesheet format would you like to use? · @emotion/styled
✔ Would you like to add React Router to this application? (y/N) · true
✔ Which E2E test runner would you like to use? · cypress
✔ Which bundler do you want to use to build the application? · webpack
✔ What should be the project name and where should it be generated? · react-admin @ apps/react-admin

4- نصب متریال یو آی یا هر کتابخانه ی دیگر به صورت مجزا:

به دایرکتوری پروژه خود میرویم:

cd apps/my-react-app npm install @mui/material @emotion/react @emotion/styled

نصب tailwind

cd apps/my-next-app npm install tailwindcss postcss autoprefixer npx tailwindcss init -p

تنظیمات tailwind.config.js

/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./src/**/*.{js,jsx,ts,tsx}'],
darkMode: 'class',
theme: {
extend: {},
},
plugins: [require('autoprefixer')],
};

اضافه کردن کلاس ها به فایل global.css

دستورا زیر رو به فایل اصلی استایل خود که درون پروژه ادد شده است اضافه کنید

@tailwind base;
@tailwind components;
@tailwind utilities;

5- ساخت یک کتابخانه به عنوان ورک اسپیس:

مورادی که ممکنه به شما در این بخش کمک میکند:

  • Share code between applications
  • Publish a package to be used outside the monorepo
  • Better visualize the architecture using nx graph
nx g @nx/next:lib my-next-lib # for remove workspace nx g @nrwl/workspace:remove --project=my-next-lib

6- ایجاد ui-shared lib و shared-component

nx g @nrwl/react:lib ui-shared nx g @nrwl/react:component ui-shared/src/my-shared-component --project=ui-shared --export usage all react base project: import { MySharedComponent } from '@myorg/ui-shared';

7- نصب storybook:

nx add @nx/storybook

برای ست کردن تنظیمات هاسکی روی هر پروژه به صورت زیر انجام میشود:

nx g @nx/storybook:configuration my-next-app --uiFramework=@storybook/nextjs nx g @nx/storybook:configuration my-react-app --uiFramework=@storybook/react nx g @nx/storybook:configuration my-next-lib --uiFramework=@storybook/react nx g @nrwl/react:storybook-configuration ui-shared

نصب story book روی ui-shared

nx g @nrwl/react:storybook-configuration ui-shared run: nx run ui-shared:storybook

✔ Do you want to set up Storybook interaction tests? (Y/n) · false

✔ Automatically generate *.stories.ts files for components declared in this project? (Y/n) · true

✔ Configure a static file server for the storybook instance? (Y/n) · true

راه اندازی Storybook

Serve :

nx run project-name:storybook

or:

nx storybook my-next-app

ساخت نهایی Storybook

Build Storybook using this command:

nx run my-next-app:build-storybook

or

nx build-storybook my-next-app

تست Storybook

With the Storybook server running, you can test Storybook (run all the interaction tests) using this command:

nx run my-next-app:test-storybook

or

nx test-storybook my-next-app

8- نصب husky و lint-staged

We install and init Husky with:

npx husky-init && npm install

This will also add a prepare script in the package.json:

&quotprepare&quot: &quothusky install&quot

Setting up lint-staged

We first need to set up lint-staged:

npm install --save-dev lint-staged

Let’s go ahead and use lint-staged to run lint and formatting before committing.

Setting up the hook

We create a .lintstagedrc to run commands on staged files:

{ &quot*.ts&quot: [&quotnx affected:lint --fix --files&quot],
&quot*&quot: [&quotnpx nx format:write --files&quot],
&quot*.scss&quot: [&quotnpx stylelint --fix&quot] }

We make a pre-commit hook that triggers stage-lint:

npx husky add .husky/pre-commit 'npx lint-staged –relative'

This gives us:

#!/usr/bin/env sh
. &quot$(dirname -- &quot$0&quot)/_/husky.sh&quot
npx lint-staged --relative

We use --relative so lint-staged will apply the paths of the staged files as relative paths which we need for nx format:write.

So now we will have eslint, formatting and stylelint running when we commit.

pre-push

Running tests can be a rather lengthy job especially if you both are running unit and component tests (which is what I recommend). So we want to only run this before we push a branch so we still get fast feedback if a test is failing and can fix it with a git amend commit in case.

npx husky add .husky/pre-push 'npx nx affected -t test && npx nx affected -t component-test'

This will both run test and component-test for any affected project on pre-push.

9- ایجاد Pages و Components

Nx also provides commands to quickly generate new pages and components for your application.

nx g @nx/next:page my-next-page --project=my-next-app nx g @nx/next:component my-next-component --project=my-next-app

Above commands will add a new page my-next-page and a component my-next-component to my-next-app project respectively.

Nx generates components with tests by default. For pages, you can pass the --withTests option to generate tests under the specs folder.

10- ساخت Hooks

If you want to add a new hook, run the following

nx g @nx/react:hook my-react-hook --project=my-react-app

Replace my-react-lib with the name of your project.

11- چگونگی استفاده پروژه ها

Run as Deve Mode:

nx dev my-next-app
nx serve my-react-app

Testing Projects

You can run unit tests with:

nx test my-next-app nx test my-next-lib

Replace my-next-app with the name or your project. This command works for both applications and libraries.

You can also run E2E tests for applications:

nx e2e my-next-app-e2e

Replace my-next-app-e2e with the name or your project with -e2e appended.

12-خروجی نهایی React Projects

React applications can be build with:

nx build my-react-app

And if you generated a library with --bundler specified, then you can build a library as well:

nx build my-next-lib

The output is in the dist folder. You can customize the output folder by setting outputPath in the project's project.json file.

The application in dist is deployable, and you can try it out locally with:

npx http-server dist/apps/my-react-app

The library in dist is publishable to npm or a private registry.

13-خروجی نهایی Next-js Projects

React applications can be build with:

nx build my-next-app

To serve a Next.js application for production:

nx start my-next-app


لینک پروژه در گیت هاب:
nx repository


nx monoreporeactnextjstailwindhusky
شاید از این پست‌ها خوشتان بیاید