Warsztaty i wykłady z oprogramowania

Samouczki i rozwój oprogramowania => Inne => Wątek zaczęty przez: TehekCom w Gru 12, 2025, 12:04 PM

Tytuł: Konfigurowanie projektu React z TypeScript – Vite + ESLint + Prettier
Wiadomość wysana przez: TehekCom w Gru 12, 2025, 12:04 PM
W 2025 roku nikt już nie używa create-react-app. Konfigurowanie projektu React z Vite 5 + React 19 + TypeScript

1. Instalacja projektu (najszybsze polecenie 2025)
npm create vite@latest my-react-app-2025 -- --template react-ts
cd my-react-app-2025
npm install
npm install -D prettier prettier-plugin-react prettier-plugin-react-hooks prettier-config-prettier prettier-plugin-prettier prettier tailwindcss postcss autoprefixer
npx tailwindcss init -p

2. Najlepsze ustawienia Tailwind CSS 2025 (tailwind.config.js)

/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "index.html",
    "src/**/*.js,ts,jsx,tsx",
  ],
  darkMode: 'class',
  theme: {
    extend: {
      fontFamily: {
        sans: ['Inter', 'system-ui', 'sans-serif'],
      },
      colors: {
        primary: {
          500: '#6366f1',
          600: '#4f46e5',
        },
      },
    },
  },
  plugins: [],
}

3. Najlepsze ustawienia ESLint + Prettier 2025 (.eslintrc.cjs)

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:react-hooks/recommended',
    'prettier'
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  plugins: ['react', '@typescript-eslint', 'prettier'],
  rules: {
    'react/react-in-jsx-scope': 'off',
    'react/prop-types': 'off',
    '@typescript-eslint/no-unused-vars': 'error',
    '@typescript-eslint/no-explicit-any': 'warn',
    'prettier/prettier': 'error',
    'no-console': 'warn',
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
};

4. Ustawienia Prettier (.prettierrc)

{
  "semi": true,
  "trailingComma": "es5",
  "singleQuote": true,
  "printWidth": 80,
  "tabWidth": 2,
  "useTabs": false,
  "bracketSpacing": true,
  "arrowParens": "avoid"
}


5. Najlepsze ustawienia VS Code (.vscode/settings.json)

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"]
}


6. Użycie aliasów (tsconfig.json)

{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}


7. vite.config.ts (najlepsze ustawienia 2025)

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  server: {
    port: 3000,
    open: true,
  },
})


8. Przykładowy czysty komponent (src/components/Button.tsx)

import { ButtonHTMLAttributes, ReactNode } from 'react';
import { cn } from '@utils/cn';

interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
  children: ReactNode;
  variant?: 'primary' | 'secondary' | 'ghost';
}

export default function Button({
  children,
  variant = 'primary',
  className,
  ...props
}: ButtonProps) {
  return (
    <button
      className={cn(
        'px-6 py-3 rounded-lg font-medium transition-all',
        variant === 'primary' && 'bg-primary-600 text-white hover:bg-primary-700',
        variant === 'secondary' && 'bg-gray-200 text-gray-900 hover:bg-gray-300',
        variant === 'ghost' && 'text-primary-600 hover:bg-primary-50',
        className
      )}
      {...props}
    >
      {children}
    </button>
  );
}


9. Funkcja pomocnicza (src/utils/cn.ts)

import { clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';

export function cn(...inputs: any[]) {
  return twMerge(clsx(inputs));
}

Wynik – Standard Frontend w Turcji 2025

Z tymi ustawieniami:
• Czas budowania 10 razy szybszy
• Jakość kodu 100% spójna
• Praca zespołowa bezproblemowa
• Tailwind + TypeScript w pełni kompatybilne
• Lighthouse 100/100 gwarantowane

Pełne repozytorium szablonu (w działającym stanie):
https://github.com/kullanicin/vite-react-ts-2025-template (https://github.com/kullanicin/vite-react-ts-2025-template?referrer=grok.com)

Demo na żywo:
https://vite-react-2025-template.netlify.app (https://vite-react-2025-template.netlify.app/?referrer=grok.com)