diff --git a/.commitlintrc.json b/.commitlintrc.json new file mode 100644 index 0000000..c30e5a9 --- /dev/null +++ b/.commitlintrc.json @@ -0,0 +1,3 @@ +{ + "extends": ["@commitlint/config-conventional"] +} diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..37d38bb --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true \ No newline at end of file diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..03f97ad --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +node_modules +dist +coverage +.eslintcache diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..9854509 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,39 @@ +{ + "extends": [ + "eslint:recommended", + "plugin:import/recommended", + "plugin:jsx-a11y/recommended", + "plugin:react/recommended", + "plugin:react/jsx-runtime", + "plugin:react-hooks/recommended", + "prettier" + ], + "overrides": [ + { + // enable eslint-plugin-testing-library rules or preset only for matching files! + "files": ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"], + "extends": ["plugin:jest/recommended", "plugin:jest-dom/recommended", "plugin:testing-library/react"] + }, + { + "files": ["**/*.ts?(x)"], + "parser": "@typescript-eslint/parser", + "extends": ["plugin:@typescript-eslint/recommended", "plugin:import/typescript"] + } + ], + "rules": { + // for testing if eslint works properly, + // feel free to remove this if you do not need it + "import/no-unused-modules": [1, { "unusedExports": true }] + }, + "settings": { + "import/parsers": { + "@typescript-eslint/parser": [".ts", ".tsx"] + }, + "import/resolver": { + "typescript": {} + }, + "react": { + "version": "detect" + } + } +} diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..6a0efe0 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,70 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: 'CodeQL' + +on: + push: + branches: [main] + pull_request: + # The branches below must be a subset of the branches above + branches: [main] + schedule: + - cron: '19 13 * * 0' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: ['javascript'] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://git.io/codeql-language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏ī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8d3ef8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules +.DS_Store +dist +dist-ssr +*.local +coverage +.eslintcache +pnpm-lock.yaml +*.log diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100755 index 0000000..0c7dedb --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1,5 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +pnpm exec commitlint --edit +pnpm exec commitlint --edit $1 diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..f340c6d --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,5 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +pnpm test +pnpm exec lint-staged diff --git a/.lintstagedrc.json b/.lintstagedrc.json new file mode 100644 index 0000000..5c541db --- /dev/null +++ b/.lintstagedrc.json @@ -0,0 +1,3 @@ +{ + "*.{tsx,ts}": ["eslint --cache --max-warnings=0 ./src", "prettier --write --ignore-unknown ."] +} diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..3b3877b --- /dev/null +++ b/.prettierignore @@ -0,0 +1,5 @@ +node_modules +dist +coverage +.eslintcache +pnpm-lock.yaml diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..add3cbd --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,19 @@ +{ + "printWidth": 100, + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": true, + "quoteProps": "as-needed", + "jsxSingleQuote": false, + "trailingComma": "es5", + "bracketSpacing": true, + "bracketSameLine": false, + "arrowParens": "always", + "requirePragma": false, + "insertPragma": false, + "proseWrap": "preserve", + "htmlWhitespaceSensitivity": "css", + "endOfLine": "lf", + "embeddedLanguageFormatting": "auto" +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..73c38e9 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + "recommendations": [ + "dbaeumer.vscode-eslint", + "esbenp.prettier-vscode", + "csstools.postcss", + "bradlc.vscode-tailwindcss", + "orta.vscode-jest", + "editorconfig.editorconfig" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4085edc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "editor.tabSize": 2, + "editor.formatOnSaveMode": "modificationsIfAvailable", + "editor.formatOnSave": true, + "prettier.documentSelectors": ["*.{tsx,ts}", "*.{jsx,js}"], + "editor.defaultFormatter": "esbenp.prettier-vscode" +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..a4ca7f3 --- /dev/null +++ b/README.md @@ -0,0 +1,99 @@ +# Drop-in Replacement for [CRA](http://create-react-app.dev/) but powered by [Vite](https://vitejs.dev/) + +### Things in CRA, supported here: +- Import SVG's directly as React Component via SVGR +- Unit Testing via Jest & React Testing Library +- ESLint Rules & Prettier +- Tailwind +- Absolute imports within `src` directory + +--- + +### Extra Additions +- Conventional Commits using `Commitlint` +- Run Lint Checks, TS Checks, Formatting & Unit Tests in Pre-commit via `lintstaged` & `husky` + +--- + +### Usage (with [degit](https://github.com/Rich-Harris/degit)) +```bash +degit uchihamalolan/vite-react-ts your-app-name +``` + +--- + +### Tech Stack - Overview +- Vite +- React - Typscript + - react-error-boundary +- pnpm + +--- + +### Editor Config + +- vscode settings & extension recommendations +- `.editorconfig` file + +--- + +### Lint and Formatting +- Eslint & Prettier Configured +- Lints + - react + - react hooks + - typescript + - jsx-a11y + +--- + +### Testing +- Jest + React Testing Library (plus some plugins) + +--- + +### Styling +- TailwindCSS v3 + +--- + +# Other Recommended Libraries: + +### Forms +- React Hook Form +- Zod (for validations) + +### CSS-in-js +- Emotion +- Complie Time Atomic CSS-in-Js + - Stylex (Facebook, not Open Source yet) + - Linaria + - Compiled (still in beta) + +### Routing +- React-Router-Dom +- React Location + +### Date Manipulation +- Dayjs +- date-fns +- Luxon + +### HTTP Client +- Ky +- Axios + +### Global Store (full-blown / lite-version) +- Redux Toolkit / Zustand +- Recoil / Jotai +- Mobx / Valtio +- xstate / robot + +### Server State +- React Query +- SWR +- RTKQuery + + diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..27ef33a --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,14 @@ +# Security Policy + +## Supported Versions + +All versions are supported, since I did not do versioning anyways as this is just a template repository + +| Version | Supported | +| ------- | ------------------ | +| x.x.x | :white_check_mark: | + +## Reporting a Vulnerability + +This is just a Template Repository, So no possible vulnerabilities there to report. +If any such is found pertaining to Project Configurations, an issue can be created. diff --git a/env/.env.production b/env/.env.production new file mode 100644 index 0000000..e69de29 diff --git a/env/.env.staging b/env/.env.staging new file mode 100644 index 0000000..e69de29 diff --git a/env/.env.testing b/env/.env.testing new file mode 100644 index 0000000..e69de29 diff --git a/index.html b/index.html new file mode 100644 index 0000000..ff50042 --- /dev/null +++ b/index.html @@ -0,0 +1,14 @@ + + + + + + + + Vite App + + +
+ + + diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..75fe726 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,11 @@ +/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ +// eslint-disable-next-line no-undef +module.exports = { + preset: 'ts-jest', + testEnvironment: 'jsdom', + coverageDirectory: 'coverage', + coverageProvider: 'v8', + extensionsToTreatAsEsm: ['.ts', '.tsx'], + moduleDirectories: ['node_modules', './src'], + rootDir: './src', +}; diff --git a/license b/license new file mode 100644 index 0000000..0467544 --- /dev/null +++ b/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Malolan Balaji + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9d1b6c0 --- /dev/null +++ b/package.json @@ -0,0 +1,73 @@ +{ + "name": "vite-react-ts", + "version": "0.0.0", + "description": "React Typescript Starter Template with Vite", + "repository": { + "type": "git", + "url": "git+https://github.com/uchihamalolan/vite-react-ts.git" + }, + "keywords": [ + "vite", + "react", + "starter", + "template" + ], + "author": "Malolan B. (https://twitter.com/malolan12)", + "license": "MIT", + "homepage": "https://github.com/uchihamalolan/vite-react-ts", + "scripts": { + "dev": "vite", + "build": "tsc && vite build", + "test": "jest", + "serve": "vite preview", + "prepare": "husky install", + "commit": "commit", + "format": "prettier --write --ignore-unknown .", + "lint": "eslint --cache ./src" + }, + "dependencies": { + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-error-boundary": "^3.1.4", + "vite-plugin-svgr": "2.4.0" + }, + "devDependencies": { + "@commitlint/cli": "^17.4.0", + "@commitlint/config-conventional": "^17.4.0", + "@commitlint/prompt-cli": "^17.4.0", + "@testing-library/dom": "^8.19.1", + "@testing-library/jest-dom": "^5.16.5", + "@testing-library/react": "^13.4.0", + "@testing-library/user-event": "^14.4.3", + "@types/jest": "^29.2.5", + "@types/node": "^18.11.18", + "@types/react": "^18.0.26", + "@types/react-dom": "^18.0.10", + "@typescript-eslint/eslint-plugin": "^5.48.0", + "@typescript-eslint/parser": "^5.48.0", + "@vitejs/plugin-react": "^3.0.1", + "autoprefixer": "^10.4.13", + "eslint": "^8.31.0", + "eslint-config-prettier": "^8.6.0", + "eslint-import-resolver-typescript": "^3.5.2", + "eslint-plugin-import": "^2.26.0", + "eslint-plugin-jest": "^27.2.1", + "eslint-plugin-jest-dom": "^4.0.3", + "eslint-plugin-jsx-a11y": "^6.6.1", + "eslint-plugin-react": "^7.31.11", + "eslint-plugin-react-hooks": "^4.6.0", + "eslint-plugin-testing-library": "^5.9.1", + "husky": "^8.0.3", + "jest": "29.3.1", + "jest-environment-jsdom": "^29.3.1", + "lint-staged": "^13.1.0", + "postcss": "^8.4.21", + "prettier": "2.8.2", + "react-test-renderer": "^18.2.0", + "tailwindcss": "^3.2.4", + "ts-jest": "29.0.3", + "typescript": "^4.9.4", + "vite": "^4.0.4", + "vite-tsconfig-paths": "^4.0.3" + } +} diff --git a/postcss.config.js b/postcss.config.js new file mode 100644 index 0000000..9f23996 --- /dev/null +++ b/postcss.config.js @@ -0,0 +1,7 @@ +// eslint-disable-next-line no-undef +module.exports = { + plugins: { + tailwindcss: {}, + autoprefixer: {}, + }, +}; diff --git a/public/icons/favicon.svg b/public/icons/favicon.svg new file mode 100644 index 0000000..de4aedd --- /dev/null +++ b/public/icons/favicon.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/src/App.tsx b/src/App.tsx new file mode 100644 index 0000000..bb11147 --- /dev/null +++ b/src/App.tsx @@ -0,0 +1,15 @@ +import { Header } from 'components/Header'; +import { Button } from 'components/Button'; +import { ReactComponent as Logo } from 'assets/favicon.svg'; + +function App() { + return ( +
+
+ + +
+ ); +} + +export default App; diff --git a/src/assets/favicon.svg b/src/assets/favicon.svg new file mode 100644 index 0000000..de4aedd --- /dev/null +++ b/src/assets/favicon.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/src/components/Button.tsx b/src/components/Button.tsx new file mode 100644 index 0000000..5df5e88 --- /dev/null +++ b/src/components/Button.tsx @@ -0,0 +1,12 @@ +interface ButtonProps { + onClick: () => void; + children: React.ReactNode; +} + +export function Button({ onClick, children }: ButtonProps) { + return ( + + ); +} diff --git a/src/components/Header.tsx b/src/components/Header.tsx new file mode 100644 index 0000000..76ca468 --- /dev/null +++ b/src/components/Header.tsx @@ -0,0 +1,3 @@ +export function Header({ title }: { title: string }) { + return

{title}

; +} diff --git a/src/hooks/__tests__/use-counter.test.ts b/src/hooks/__tests__/use-counter.test.ts new file mode 100644 index 0000000..3ea4e30 --- /dev/null +++ b/src/hooks/__tests__/use-counter.test.ts @@ -0,0 +1,8 @@ +import { renderHook } from '@testing-library/react'; +import useCounter from '../use-counter'; + +test('should use counter', () => { + const { result } = renderHook(() => useCounter()); + expect(result.current.count).toBe(0); + expect(typeof result.current.increment).toBe('function'); +}); diff --git a/src/hooks/use-counter.ts b/src/hooks/use-counter.ts new file mode 100644 index 0000000..2b9d6be --- /dev/null +++ b/src/hooks/use-counter.ts @@ -0,0 +1,7 @@ +import { useState, useCallback } from 'react'; + +export default function useCounter() { + const [count, setCount] = useState(0); + const increment = useCallback(() => setCount((x) => x + 1), []); + return { count, increment }; +} diff --git a/src/index.css b/src/index.css new file mode 100644 index 0000000..88ed046 --- /dev/null +++ b/src/index.css @@ -0,0 +1,13 @@ +@import './styles/reset.css'; + +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', + 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace; +} diff --git a/src/main.tsx b/src/main.tsx new file mode 100644 index 0000000..5fcf901 --- /dev/null +++ b/src/main.tsx @@ -0,0 +1,17 @@ +import { createRoot } from 'react-dom/client'; +import { StrictMode } from 'react'; + +import './index.css'; +import 'tailwindcss/tailwind.css'; + +import App from './App'; + +const container = document.getElementById('root'); +if (container) { + const root = createRoot(container); + root.render( + + + + ); +} diff --git a/src/styles/reset.css b/src/styles/reset.css new file mode 100644 index 0000000..fcedf57 --- /dev/null +++ b/src/styles/reset.css @@ -0,0 +1,38 @@ +/*** The new CSS Reset - version 1.3.1 (last updated 28.10.2021) ***/ + +/* + Remove all the styles of the "User-Agent-Stylesheet", except for the 'display' property + - The "symbol *" part is to solve Firefox SVG sprite bug + */ +*:where(:not(iframe, canvas, img, svg, video):not(svg *, symbol *)) { + all: unset; + display: revert; +} + +/* Preferred box-sizing value */ +*, +*::before, +*::after { + box-sizing: border-box; +} + +/* Remove list styles (bullets/numbers) */ +ol, +ul { + list-style: none; +} + +/* For images to not be able to exceed their container */ +img { + max-width: 100%; +} + +/* removes spacing between cells in tables */ +table { + border-collapse: collapse; +} + +/* revert the 'white-space' property for textarea elements on Safari */ +textarea { + white-space: revert; +} diff --git a/src/utils/__tests__/sum.test.ts b/src/utils/__tests__/sum.test.ts new file mode 100644 index 0000000..570a509 --- /dev/null +++ b/src/utils/__tests__/sum.test.ts @@ -0,0 +1,5 @@ +import { sum } from 'utils/sum'; + +test('adds 1 + 2 to equal 3', () => { + expect(sum(1, 2)).toBe(3); +}); diff --git a/src/utils/sum.ts b/src/utils/sum.ts new file mode 100644 index 0000000..508f35f --- /dev/null +++ b/src/utils/sum.ts @@ -0,0 +1,3 @@ +export function sum(a: number, b: number) { + return a + b; +} diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/tailwind.config.js b/tailwind.config.js new file mode 100644 index 0000000..429e250 --- /dev/null +++ b/tailwind.config.js @@ -0,0 +1,8 @@ +// eslint-disable-next-line no-undef +module.exports = { + content: ['./src/**/*.html', './src/**/*.{js,jsx,ts,tsx}'], + theme: { + extend: {}, + }, + plugins: [], +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..5ee9f75 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ESNext", + "useDefineForClassFields": true, + "lib": ["DOM", "DOM.Iterable", "ESNext"], + "allowJs": false, + "skipLibCheck": false, + "esModuleInterop": false, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "module": "ESNext", + "moduleResolution": "Node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react-jsx", + "baseUrl": "src", + "types": ["vite-plugin-svgr/client", "jest"] + }, + "include": ["./src"] +} diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..b30fcac --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from 'vite'; +import react from '@vitejs/plugin-react'; +import tsconfigPaths from 'vite-tsconfig-paths'; +import svgrPlugin from 'vite-plugin-svgr'; + +// https://vitejs.dev/config/ +// eslint-disable-next-line import/no-unused-modules +export default defineConfig({ + envDir: './env', + plugins: [react(), tsconfigPaths(), svgrPlugin()], + /* If proxy is needed + server: { + proxy: { + "/api": "localhost:8080" + } + }, + */ + build: { + sourcemap: true, + }, +});