Open2

[備忘録]vite React 環境構築

knmtknmt

viteでReactアプリを作成

React + Typescript + SWCを選択

npm create vite@latest

必要なパッケージをいれる

test

npm i -D jest @types/jest ts-jest @testing-library/react @testing-library/jest-dom @testing-library/user-event jest-environment-jsdom

formatter

npm i -D prettier

設定ファイルの作成

.prettierrc.json
{
  "tabWidth": 2,
  "singleQuote": true,
  "trailingComma": "none",
  "semi": false,
  "useTabs": false
}
jest.setup.ts
import '@testing-library/jest-dom'
jest.config.json
{
  "roots": ["<rootDir>/"],
  "testMatch": [
    "**/__tests__/**/*.+(ts|tsx|js)",
    "**/?(*.)+(spec|test).+(ts|tsx|js)"
  ],
  "transform": {
    "^.+\\.(ts|tsx)$": "ts-jest"
  },
  "testEnvironment": "jest-environment-jsdom",
  "setupFilesAfterEnv": ["<rootDir>/jest.setup.ts"],
  "moduleNameMapper": {
    "@src/(.*)$": ["<rootDir>/src/$1"]
  }
}
ts.config.json
{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": ".",
    "paths": {
      "@src/*": ["src/*"]
    }
  },
  "include": ["src", "jest.setup.ts"],
  "references": [{ "path": "./tsconfig.node.json" }]
}
knmtknmt

tailwindcssも入れる

npm i -D tailwindcss autoprefixer postcss clsx prettier-plugin-tailwindcss tailwind-merge
tailwind.config.cjs
const { fontFamily } = require('tailwindcss/defaultTheme')

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
  theme: {
    extend: {}
  }
}
postcss.config.cjs
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {}
  }
}
index.css
@tailwind base;
@tailwind components;
@tailwind utilities;
prettier.config.cjs
module.exports = {
  ...
  // 追加
  plugins: [require('prettier-plugin-tailwindcss')]
}