Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use flat config #250

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .eslintrc.json

This file was deleted.

3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.experimental.useFlatConfig": true
}
137 changes: 108 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
- Lint also for json, yaml, markdown
- Sorted imports, dangling commas
- Reasonable defaults, best practices, only one-line of config
- [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
- **Style principle**: Minimal for reading, stable for diff

> Configs uses [ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)
> Configs uses [🌈 ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)

## Usage

Expand All @@ -20,12 +21,20 @@
pnpm add -D eslint @antfu/eslint-config
```

### Config `.eslintrc`
### Create config file

```json
{
"extends": "@antfu"
}
```js
// eslint.config.js
import antfu from '@antfu/eslint-config'

export default [
...antfu,
{
rules: {
// your overrides
},
},
]
```

> You don't need `.eslintignore` normally as it has been provided by the preset.
Expand All @@ -43,14 +52,18 @@ For example:
}
```

### VS Code support (auto fix)
## VS Code support (auto fix)

Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)

Add the following settings to your `settings.json`:

```jsonc
{
// Enable the flat config support
"eslint.experimental.useFlatConfig": true,

// Disable the default formatter
"prettier.enable": false,
"editor.formatOnSave": false,

Expand Down Expand Up @@ -87,19 +100,98 @@ Add the following settings to your `settings.json`:
}
```

### TypeScript Aware Rules
## Flat Config

Since v0.44.0, we migrated to [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), provides a much better organization and composition.

Type aware rules are enabled when a `tsconfig.eslint.json` is found in the project root, which will introduce some stricter rules into your project. If you want to enable it while have no `tsconfig.eslint.json` in the project root, you can change tsconfig name by modifying `ESLINT_TSCONFIG` env.
You can now compose your own config easily:

```js
// .eslintrc.js
const process = require('node:process')
// eslint.config.js
import {
presetAuto,
presetJavaScriptCore,
presetLangsExtensions,
presetTypeScript,
} from '@antfu/eslint-config'

export default [
// javascript, node, unicorn, jsdoc, imports, etc.
...presetJavaScriptCore,
// typescript support
...presetTypeScript,
// yaml, markdown, json, support
...presetLangsExtensions,
]
```

process.env.ESLINT_TSCONFIG = 'tsconfig.json'
Or even more granular:

module.exports = {
extends: '@antfu'
}
```js
// eslint.config.js
import {
comments,
ignores,
imports,
javascript,
javascriptStylistic,
jsdoc,
jsonc,
markdown,
node,
sortPackageJson,
sortTsconfig,
typescript,
typescriptStylistic,
unicorn,
vue,
yml,
} from '@antfu/eslint-config'

export default [
...ignores,
...javascript,
...comments,
...node,
...jsdoc,
...imports,
...unicorn,
...javascriptStylistic,

...typescript,
...typescriptStylistic,

...vue,

...jsonc,
...yml,
...markdown,
]
```

Check out the [presets](https://github.com/antfu/eslint-config/blob/main/packages/eslint-config/src/presets.ts) and [configs](https://github.com/antfu/eslint-config/blob/main/packages/eslint-config/src/configs) for more details.

> Thanks to [sxzz/eslint-config](https://github.com/sxzz/eslint-config) for the inspiration and reference.

### Type Aware Rules

You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by importing `typescriptWithLanguageServer` config:

```js
// eslint.config.js
import { presetAuto, typescriptWithLanguageServer } from '@antfu/eslint-config'

export default [
...presetAuto,
...typescriptWithLanguageServer({
tsconfig: 'tsconfig.json', // path to your tsconfig
}),
{
rules: {
// your overrides
},
},
]
```

### Lint Staged
Expand Down Expand Up @@ -145,20 +237,7 @@ This config does NOT lint CSS. I personally use [UnoCSS](https://github.com/unoc

### I prefer XXX...

Sure, you can override the rules in your `.eslintrc` file.

<!-- eslint-skip -->

```jsonc
{
"extends": "@antfu",
"rules": {
// your rules...
}
}
```

Or you can always fork this repo and make your own.
Sure, you can override rules locally in your project to fit your needs. Or you can always fork this repo and make your own.

## Check Also

Expand Down
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import antfu from '@antfu/eslint-config'

export default [
...antfu,
]
2 changes: 2 additions & 0 deletions example/vitesse/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
File renamed without changes.
1 change: 1 addition & 0 deletions example/vitesse/.github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: antfu
96 changes: 96 additions & 0 deletions example/vitesse/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: CI

on:
push:
branches:
- main

pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: pnpm

- name: Install
run: pnpm install

- name: Lint
run: pnpm run lint

typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: pnpm

- name: Install
run: pnpm install

- name: Typecheck
run: pnpm run typecheck

test:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [18.x, 20.x]
os: [ubuntu-latest]
fail-fast: false

steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: https://registry.npmjs.org/
cache: pnpm

- run: pnpm install
- run: pnpm run test:unit

test-e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache
key: cypress-cache-${{ runner.os }}-${{ hashFiles('pnpm-lock.yaml') }}

- uses: pnpm/action-setup@v2

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: lts/*
registry-url: https://registry.npmjs.org/
cache: pnpm

- run: pnpm install

- name: Cypress PNPM Patch
run: cp pnpm-lock.yaml package-lock.json

- name: Cypress
uses: cypress-io/github-action@v4
with:
install-command: echo
build: pnpm run build
start: npx vite preview --port 3333
1 change: 1 addition & 0 deletions fixtures/vitesse/.gitignore → example/vitesse/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist-ssr
node_modules
.idea/
*.log
cypress/downloads
2 changes: 2 additions & 0 deletions example/vitesse/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
shamefully-hoist=true
strict-peer-dependencies=false
13 changes: 13 additions & 0 deletions example/vitesse/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"recommendations": [
"antfu.iconify",
"antfu.unocss",
"antfu.vite",
"antfu.goto-alias",
"csstools.postcss",
"dbaeumer.vscode-eslint",
"vue.volar",
"lokalise.i18n-ally",
"streetsidesoftware.code-spell-checker"
]
}
15 changes: 15 additions & 0 deletions example/vitesse/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"cSpell.words": ["Vitesse", "Vite", "unocss", "vitest", "vueuse", "pinia", "demi", "antfu", "iconify", "intlify", "vitejs", "unplugin", "pnpm"],
"i18n-ally.sourceLanguage": "en",
"i18n-ally.keystyle": "nested",
"i18n-ally.localesPaths": "locales",
"i18n-ally.sortKeys": true,
"prettier.enable": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"files.associations": {
"*.css": "postcss"
},
"editor.formatOnSave": false
}
18 changes: 18 additions & 0 deletions example/vitesse/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:16-alpine as build-stage

WORKDIR /app
RUN corepack enable

COPY .npmrc package.json pnpm-lock.yaml ./
RUN --mount=type=cache,id=pnpm-store,target=/root/.pnpm-store \
pnpm install --frozen-lockfile

COPY . .
RUN pnpm build

FROM nginx:stable-alpine as production-stage

COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
2 changes: 1 addition & 1 deletion fixtures/vitesse/LICENSE → example/vitesse/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2021 Anthony Fu
Copyright (c) 2020-PRESENT Anthony Fu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading