Are you configuring your project for scalability and avoiding coding challenges?

Hey there! Are you prepared to enhance your project setup and take it to the next level? Let's begin and simplify your development process to make it more scalable and maintainable. Trust me, these quick configurations will save you a lot of time and prevent headaches in the future.

Why Bother? What's in it for You?

Before we begin, you may be wondering why you should invest your time in this., Well, my friend, think of it this way: just two minutes now can save you hours of frustration later.
By standardizing your project setup, you'll ensure smoother collaboration across your team and maintain clean, readable code effortlessly.

Getting Started: Essential Tools

Let's dive into the tools that will help you achieve code clarity and maintainability:

  1. Prettier: This magic tool automatically formats your code according to a set of rules, ensuring consistent style and readability.

  2. ESLint: Think of ESLint as your code coach. It analyzes your code for potential errors and stylistic issues, helping you write clean and maintainable code.

  3. Husky: This tool acts as a pre-commit guardian, automatically running Prettier and ESLint before you commit changes. This ensures your code adheres to the project's standards before it gets merged.

  4. VS Code Settings: By customizing your VS Code settings, you can enforce consistent formatting and linting across your entire development environment.

Let's Get Technical

1. Setting Up Prettier

Start by adding these dev dependencies to your project:

// You may use npm | yarn as well
pnpm add -D prettier-plugin-tailwindcss @ianvs/prettier-plugin-sort-imports eslint-plugin-unicorn @typescript-eslint/eslint-plugin eslint-config-prettier husky

This command will add some dev dependencies to your project.

  1. prettier-plugin-tailwindcss: Prettier plugin that helps format your Tailwind CSS classes.

  2. @ianvs/prettier-plugin-sort-imports: This Prettier plugin automatically sorts your import statements.

  3. eslint-plugin-unicorn: ESLint plugin that provides a set of rules that enforce good practices in your code, aiming to improve code readability and maintainability.

  4. @typescript-eslint/eslint-plugin: This is a plugin that contains sets of ESLint rules specifically for TypeScript code.

  5. eslint-config-prettier: This is a config that disables ESLint rules that conflict with Prettier. It helps to avoid situations where ESLint and Prettier fight over code formatting.

  6. husky: Husky is a tool that can prevent bad git commit, git push and more by running linters on your changes. It supports all Git hooks.

Create a .prettierrc file in your project root and paste the following configuration:

{
  "endOfLine": "lf",
  "semi": false,
  "singleQuote": false,
  "tabWidth": 2,
  "printWidth": 135,
  "trailingComma": "es5",
  "importOrder": [
    "^(react/(.*)$)|^(react$)",
    "^(next/(.*)$)|^(next$)",
    "<THIRD_PARTY_MODULES>",
    "",
    "^types$",
    "^@/env(.*)$",
    "^@/types/(.*)$",
    "^@/config/(.*)$",
    "^@/lib/(.*)$",
    "^@/hooks/(.*)$",
    "^@/components/ui/(.*)$",
    "^@/components/(.*)$",
    "^@/styles/(.*)$",
    "^@/app/(.*)$",
    "",
    "^[./]"
  ],
  "importOrderSeparation": false,
  "importOrderSortSpecifiers": true,
  "importOrderBuiltinModulesToTop": true,
  "importOrderParserPlugins": ["typescript", "jsx", "decorators-legacy"],
  "importOrderMergeDuplicateImports": true,
  "importOrderCombineTypeAndValueImports": true,
  "plugins": ["@ianvs/prettier-plugin-sort-imports", "prettier-plugin-tailwindcss"]
}

Now, let's break down what each property will do.

Here I am using two plugins that help in sorting and ordering tailwind classes and module imports.

Now add this script to your package.json file. This will make it prettier to format your project.

"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,css,scss,md}\""

2. Configuring ESLint

Create a .eslintrc file in your project root and paste the following configuration:

{
  "$schema": "https://json.schemastore.org/eslintrc",
  "env": {
    "browser": true,
    "node": true
  },
  "root": true,
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "settings": {
    "next": {
      "rootDir": true
    }
  },
  "plugins": ["react", "@typescript-eslint/eslint-plugin", "unicorn"],
  "extends": ["next/core-web-vitals", "prettier", "plugin:@typescript-eslint/recommended"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx"],
      "parser": "@typescript-eslint/parser"
    }
  ],
  "rules": {
    "camelcase": ["warn", { "properties": "always" }],
    "@typescript-eslint/no-unused-vars": "warn",
    "@typescript-eslint/no-explicit-any": "off",
    "@typescript-eslint/no-empty-interface": "off",
    "@typescript-eslint/no-this-alias": "off",
    "@typescript-eslint/ban-ts-comment": [
      "error",
      {
        "ts-ignore": "allow-with-description",
        "ts-nocheck": "allow-with-description"
      }
    ],
    "unicorn/filename-case": [
      "error",
      {
        "case": "kebabCase"
      }
    ]
  }
}

Now, let's break down the configuration.

3. Automating Checks with Husky

This will initialize husky in your project and you may now see a .husky folder.

pnpm dlx husky-init

Create a .husky/pre-commit file and add the following script:

pnpm format 
pnpm lint

git add .

4. Standardizing VS Code Settings

{
  "typescript.enablePromptUseWorkspaceTsdk": true,
  "editor.quickSuggestions": {
    "strings": true
  },
  "editor.formatOnSave": true,
  "editor.formatOnType": true
}

Now, you are good to go with your project.

Just check that everything fits well in your environment.

Remember: This is just the beginning! As your project grows, you can customize these tools and rules to fit your specific needs.

Conclusion

By investing a little time upfront to configure these tools, you'll reap the benefits of clean, maintainable code for years to come. Your future self (and your fellow developers) will thank you! Think of it as future-proofing your project – a well-planned foundation makes it easier to adapt and grow as your needs evolve.

Spread the Knowledge!

Building a strong developer community relies on sharing valuable insights. If you found this guide helpful, consider sharing it on social media or within your network. It could be the key to unlocking cleaner, more maintainable code for someone else!

Need Help?

Don't be afraid to ask questions! If you encounter any errors during setup, feel free to reach out to me on Twitter / LinkedIn. I'm here to help you navigate the process and ensure your project thrives.

Happy coding!

MORE NOTES