Welcome to the React-TS-Template documentation! This template is designed to streamline your React projects' setup process while incorporating useful features and tools for a smooth and efficient development experience. By utilizing the Remote Development extension pack in Visual Studio Code (VS Code) and configuring a custom devcontainer.json, I provide a consistent development environment that you can easily share with your team.
- Fork or clone this template repository to start your new project.
- Customize the
devcontainer.json
,init.sh
, andDockerfile
files to suit your project's specific requirements. - Open the cloned repository in VS Code.
- When prompted, click on "Reopen in Container" to start your development environment in the container.
- That's it! You're now set up with a powerful and flexible development environment for your React projects.
Happy coding! ππ»
The devcontainer.json
file serves as the configuration file for the development container. It defines various aspects of the development environment. Let's explore the key configurations and how you can change them:
"name": "project-name",
Change "project-name" to the desired name for your container.
{
"build": {
"dockerfile": "./Dockerfile",
"target": "dev",
"context": "../"
}
}
Specify the path to your Dockerfile, and adjust the "context" if needed.
{
"customizations": {
"vscode": {
"extensions": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"streetsidesoftware.code-spell-checker",
"tombonnike.vscode-status-bar-format-toggle"
]
}
}
}
You can specify additional VS Code extensions as needed to enhance your development experience.
{
"forwardPorts": [5173]
}
Add any required ports (e.g., for development servers) that should be available locally from the container.
{
"workspaceMount": "source=${localWorkspaceFolder},target=/home/node/project-name,type=bind"
}
Adjust the path to your project folder (/home/node/project-name
) to mount your source code into the container.
{
"workspaceFolder": "/home/node/project-name"
}
Set the path to your project folder within the container.
{
"postCreateCommand": ". .devcontainer/init.sh"
}
Specify any commands to run after the container is created. In this template, I execute init.sh
to set up additional configurations.
The init.sh
script, executed as a post-create command, sets up additional tools and configurations in the development container. It installs dependencies, clones the img-optimize
project repository, creates an alias for the image optimization tool, and sources the .bashrc
file to enable the alias.
The Dockerfile defines the development environment for your project. It sets the default shell to Bash, installs essential utilities and tools (e.g., git
, jpegoptim
, optipng
, webp
, and imagemagick
), and specifies the development environment variables.
Dependabot is a powerful tool that automatically checks for outdated dependencies in your project and creates pull requests to update them to the latest versions. It helps ensure that your project stays up-to-date with the latest security patches and features.
To get started with Dependabot version updates, you'll find a file named .github/dependabot.yml
in your React-TS-Template repository. This file contains the configuration for Dependabot, specifying which package ecosystems to update and where the package manifests are located.
# Enable version updates for npm
- package-ecosystem: "npm"
# Look for `package.json` and `lock` files in the `root` directory
directory: "/"
# Check the npm registry for updates every month
schedule:
interval: "monthly"
timezone: "Europe/Belgrade"
# Raise pull requests for npm dependencies version updates
# against the `develop` branch.
target-branch: "develop"
pull-request-branch-name:
# Separate sections of the branch name with a hyphen
separator: "-"
commit-message:
# Prefix all commit messages with...
prefix: "npm prod"
prefix-development: "npm dev"
include: "scope"
# Raise all npm pull requests with custom labels
labels:
- "npm dependencies"
# Enable version updates for Docker
- package-ecosystem: "docker"
# Look for a `Dockerfile` in the `.devcontainer` directory found in `root` of the project.
directory: "/.devcontainer"
# Check for updates once a month
schedule:
interval: "monthly"
timezone: "Europe/Bel
grade"
# Raise pull requests for Docker version updates
# against the `develop` branch.
target-branch: "develop"
pull-request-branch-name:
# Separate sections of the branch name with a hyphen
separator: "-"
commit-message:
# Prefix all commit messages with "[docker] " (no colon, but a trailing whitespace)
prefix: "[docker] "
# Use custom labels on pull requests for Docker version updates
labels:
- "Docker dependencies"
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/" # Check for updates once a month
schedule:
interval: "monthly"
timezone: "Europe/Belgrade" # Raise pull requests for Github Actions dep. version updates # against the `develop` branch.
target-branch: "develop"
pull-request-branch-name: # Separate sections of the branch name with a hyphen
separator: "-"
commit-message: # Prefix all commit messages with "GitHub Actions"
prefix: "GitHub Actions" # Use custom labels on pull requests for GitHub Actions dep. version updates
labels: - "GitHub Actions dependencies"
You can customize the Dependabot configuration to better suit your project's needs:
- Package Ecosystems: If your project uses other package ecosystems, you can add more entries to the
updates
list for each ecosystem. Refer to the documentation for the complete list of supported ecosystems. - Update Schedule: You can adjust the
interval
in theschedule
section to control how often Dependabot checks for updates. For example, setinterval
to "daily"
for more frequent checks. - Branch Names and Labels: In the
target-branch
andpull-request-branch-name
sections, customize the branch names for target branches and pull request branches as per your branch naming conventions. Similarly, modify thelabels
to your preferred ones in the labels section. - Commit Message Prefix: Change the
prefix
value in thecommit-message
section to specify a custom prefix for commit messages. Theprefix-development
option allows different prefixes for development dependency updates. - Package Manifest Location: Update the
directory
value to point to the correct location of package manifests for each package ecosystem.
Once you've saved the .github/dependabot.yml
file in your repository, Dependabot will automatically start monitoring for outdated dependencies according to the specified schedule. It will create pull requests on the develop branch whenever updates are available. Review the pull requests, test the updates, and merge them to keep your project up-to-date with the latest dependencies.
Ensure that you have a separate develop branch and keep it up-to-date with the latest changes from your main branch (master or main). Dependabot will create pull requests for dependency updates against the develop branch. Once you have validated the updates and tested your application, you can merge these pull requests into your main branch to keep your project updated.
That's it! You now have Dependabot configured to manage version updates for npm, Docker, and GitHub Actions dependencies in your React-TS-Template project.
I've added support for React projects using Vite, a fast and lightweight development tool for building modern web applications. Vite's blazing fast development server and instant hot module replacement make it a perfect fit for your React projects.
With Vite support in this template, you can leverage the following benefits:
- Lightning-fast development: Vite's server starts up quickly, and hot module replacement ensures instant updates without a full reload, resulting in an incredibly smooth development experience.
- Out-of-the-box ES6 module support: Vite natively supports ES6 modules, enhancing code modularity and performance.
- Optimized builds: Vite's build process generates highly optimized production builds for your React application, ensuring minimal bundle sizes and faster load times.
- Plugin ecosystem: Vite offers an extensive plugin ecosystem to extend its functionality and customize your development environment.
To get started with Vite in your React project, simply install it using npm or yarn and run the development server using the vite
command. I've already set up the necessary configuration and scripts in this template, so you can start building your React app with Vite right away.
I've integrated ESLint into the React-TS-Template to help you maintain a consistent code style and catch potential errors and issues early in the development process. ESLint is a widely used and powerful JavaScript linter that analyzes your code and enforces a set of rules to ensure code quality and readability.
The ESLint configuration is located in the .eslintrc.cjs
file in the root directory of your project. Here's an overview of the key aspects of the configuration:
-
Environment: The ESLint
env
option specifies the runtime environments in which your code will run. In this template, I've setbrowser
andes2020
as the environments. -
Extends: The
extends
option allows us to extend existing ESLint configurations. I've extended the following configurations in this template:-
eslint:recommended
: Includes the recommended ESLint rules. -
plugin:react-hooks/recommended
: Includes recommended rules for React hooks. -
plugin:@typescript-eslint/recommended
: Includes recommended TypeScript-specific rules. -
plugin:@typescript-eslint/recommended-requiring-type-checking
: Includes additional TypeScript rules that require type-checking.
-
-
Parser and Parser Options: The
parser
option specifies the parser to use for analyzing the code. In this template, I use@typescript-eslint/parser
to enable TypeScript support. TheparserOptions
provide additional configuration for the parser, such asecmaVersion
,sourceType
, andproject
. I use"latest"
forecmaVersion
,"module"
forsourceType
, and specify the paths to the TypeScript configuration files (tsconfig.json
andtsconfig.node.json
) using theproject
option. -
Plugins: I've added the
react-refresh
and@typescript-eslint
plugins to the configuration. Thereact-refresh
plugin is used to enable React Fast Refresh, providing instantaneous updates during development for components. The@typescript-eslint
plugin adds support for ESLint rules specific to TypeScript. -
Rules: I've included specific ESLint rules to enhance the development experience and code quality. For example, the
"react-refresh/only-export-components"
rule warns you if you're not using React Fast Refresh with only component exports. -
Ignore Patterns: The
ignorePatterns
option specifies the patterns of files and directories that ESLint should ignore when linting. In this template, I've excluded thedist
,.eslintrc.cjs
,.devcontainer
,.github
, and.husky
directories from linting.
The React-TS-Template comes with two TypeScript configuration files to support different scenarios:
The tsconfig.json
file in the root directory is the main TypeScript configuration. Here are the key options:
-
Compiler Options: This section includes various compiler options to control how TypeScript handles your code. For example,
target
specifies the ECMAScript version to target (ES2020 in this template). I've enabledstrict
mode to catch more potential issues at compile time, and other options such asnoImplicitAny
,noUnusedLocals
, andnoUnusedParameters
are set to true to enforce stricter type checking. -
Module Resolution: I've set
moduleResolution
to"bundler"
to support module resolution during bundling (e.g., with Vite).
The tsconfig.node.json
file is a supplementary TypeScript configuration used for Node.js specific scenarios. Here are the key options:
- Compiler Options: I've set
composite
to true to enable composite projects, which allows us to build and reference TypeScript projects as a group. This enables better build performance and faster incremental compilation. - Module Resolution: The
moduleResolution
is set to"bundler"
to use the bundler during module resolution.
With ESLint and TypeScript integrated into the template, you can take advantage of their features to maintain code quality and ensure robustness in your React projects. As you develop your application, ESLint will provide real-time feedback and suggestions based on the rules defined in the configuration. TypeScript will also check the types in your code and ensure type safety.
Feel free to customize the ESLint and TypeScript configurations further to fit your specific project requirements. You can adjust the ESLint rules, enable additional plugins, or modify the TypeScript compiler options according to your needs.
Git hooks are powerful automation tools that can enhance your development workflow by automating tasks such as linting, formatting, and testing before commits and pushes. I've integrated Git hooks into the React-TS-Template using the popular Husky library, allowing you to configure pre-commit and pre-push hooks easily.
The configuration for Git hooks and Husky is managed through the package.json
file in the root directory of your project. Here's an overview of the key scripts and settings related to Git hooks and Husky:
-
prepare
Script: The prepare script ensures that Husky is properly installed and set up. It runshusky install
, which sets up the hooks to be triggered when certain Git actions (e.g., commit or push) are performed. -
lint-staged
Configuration: Thelint-staged
section inpackage.json
defines the tasks to be executed on staged files (i.e., files that are about to be committed). In this template, I have configuredlint-staged
to runprettier
andeslint
on TypeScript and TSX files. Specifically:- For all files (
**/*
), I runprettier
with the--write
,--cache
, and--ignore-unknown
options to automatically format the code. - For TypeScript and TSX files (
*.{ts,tsx}
), I runeslint
with the--cache
,--ext ts,tsx
,--report-unused-disable-directives
, and--max-warnings 0
options to perform linting with zero warnings.
- For all files (
-
Husky Configuration: Husky is configured using the
npx husky
command. I've set up one hook:- pre-commit: This hook is triggered before every commit. It runs the
lint-staged
task, ensuring that the staged files meet the defined code formatting and linting standards. If any issues are found, the commit will be prevented, prompting you to fix the issues before proceeding.
- pre-commit: This hook is triggered before every commit. It runs the
By integrating Git hooks with Husky, you ensure that your codebase adheres to the defined code formatting and linting rules before committing and pushing changes. This helps maintain code consistency and reduces the chances of introducing bugs and issues into the repository.
As you make changes to your code and attempt to commit or push, Husky will automatically trigger the specified hooks. If any formatting or linting errors are detected, Husky will prevent the commit or push, prompting you to fix the issues. Once the code meets the defined standards, you can proceed with the commit or push.
Feel free to modify the lint-staged
configuration or add additional hooks to the husky
section according to your project's needs. You can include additional checks, tests, or even pre-commit messages to enhance the automation and maintain the codebase's quality.