# Initialize React Project with Vite

[![documentation](https://img.shields.io/badge/documentation-brightgreen?style=flat-square)](https://react.dev/learn)
[![documentation](https://img.shields.io/badge/facebook/react-gray?style=flat-square&logo=github)](https://github.com/facebook/react)

Create a New Project
--------------------

[![documentation](https://img.shields.io/badge/documentation-brightgreen?style=flat-square)](https://vite.dev/guide/)
[![documentation](https://img.shields.io/badge/vitejs/vite-gray?style=flat-square&logo=github)](https://github.com/vitejs/vite)

```shell
export VITE_PROJECT="vite-project"
```

```shell
yarn create vite -t react --no-interactive "$VITE_PROJECT"
```

### Cleanup Vite Project Template

```shell
yarn install --immutable --immutable-cache
yarn remove @types/react @types/react-dom
yarn remove oxlint
rm .oxlintrc.json
sed -i '/"lint":/d' package.json
sed -i 's@/src/main.jsx@/app/main.jsx@' index.html
mv src app
```

### Setup Custom Import Aliases

[![documentation](https://img.shields.io/badge/documentation-brightgreen?style=flat-square)](https://nodejs.org/api/packages.html#subpath-imports)

```json package.json
{
  "imports": {
    "#app/": "./app/",
    "#assets/": "./assets/",
    "#lib/": "./lib/",
    "#routes/": "./routes/"
  }
}
```

Use Latest Version of Yarn
--------------------------

[![documentation](https://img.shields.io/badge/documentation-brightgreen?style=flat-square)](https://yarnpkg.com/getting-started/install)
[![documentation](https://img.shields.io/badge/yarnpkg/berry-gray?style=flat-square&logo=github)](https://github.com/yarnpkg/berry)

```shell
yarn set version stable
yarn install
```

### Enable Node Modules Linking with Local Cache

```yml .yarnrc.yml
# SPDX-FileCopyrightText: None
#
# SPDX-License-Identifier: 0BSD

enableGlobalCache: false

# [!] NPM Supply Chain Attack Mitigation.
enableScripts: false

enableTelemetry: false

nodeLinker: node-modules
```

```gitignore
# SPDX-FileCopyrightText: None
#
# SPDX-License-Identifier: 0BSD

.yarn/*
!.yarn/cache/
!.yarn/releases/
yarn-debug.log*
yarn-error.log*
/node_modules/
```

Unify Chores via Makefile
-------------------------

```Makefile
# SPDX-FileCopyrightText: None
# SPDX-License-Identifier: 0BSD

JQ := $(shell command -v jq 2>/dev/null)
ifeq ($(JQ),)
$(error jq is required but not installed)
endif

DISTNAME := $(shell jq -r .name package.json)
VERSION := $(shell jq -r .version package.json)
DISTDIR := dist

HOST := 127.27.0.1
PORT := 5173

.PHONY: dev
dev:
	yarn dev --host="$(HOST)" --port="$(PORT)"

.PHONY: install
install:
	yarn install --immutable --immutable-cache

.PHONY: update
update:
	yarn up

.PHONY: build
build:
	yarn build --outDir="$(DISTDIR)"

.PHONY: dist
dist: build
	tar --directory="$(DISTDIR)" --create --xz --file="$(DISTNAME)-$(VERSION).tar.xz" .

.PHONY: test
test:
	yarn test

.PHONY: clean
clean:
	-rm --recursive "$(DISTDIR)"
	-rm "$(DISTNAME)-$(VERSION).tar.xz"

.PHONY: distclean
distclean: clean
	git clean --force -Xd
```