@haltcase/style v6.1.0
@haltcase/style
·
Style guide and configurations for tools in the web ecosystem.
This package originated from Vercel's style guide.
installation
Install @haltcase/style
with your preferred package manager:
# npm
npm i --save-dev @haltcase/style
# pnpm
pnpm i --save-dev @haltcase/style
# yarn
yarn add --dev @haltcase/style
Some ESLint scenarios require additional peer dependencies. See the ESLint section.
usage
Prettier
Note Prettier is a peer dependency you'll need to install at the root of your project.
Add the following in package.json
:
{
"prettier": "@haltcase/style/prettier"
}
ESLint
Note ESLint is a peer dependency you'll need to install at the root of your project.
See: https://eslint.org/docs/user-guide/getting-started#installation-and-usage
Several ESLint configs are available which can be combined.
Start with at least one of these base configs, which should always be first in extends
:
@haltcase/style/eslint/browser
@haltcase/style/eslint/node
Note You can scope configs so they only target specific files.
The following additional configs are available:
@haltcase/style/eslint/jest
@haltcase/style/eslint/next
(requires@next/eslint-plugin-next
to be installed at the same version asnext
)@haltcase/style/eslint/playwright-test
@haltcase/style/eslint/react
@haltcase/style/eslint/typescript
(requirestypescript
to be installed and additional configuration)
Be sure to use require.resolve
to reference these configs — ESLint requires absolute paths (eslint/eslint#9188).
For example, to use the shared ESLint config(s) in a Next.js project, set the
following in .eslintrc.js
.
module.exports = {
extends: [
require.resolve("@haltcase/style/eslint/browser"),
require.resolve("@haltcase/style/eslint/react"),
require.resolve("@haltcase/style/eslint/next")
]
};
Configuring ESLint for TypeScript
Some of the rules enabled in the TypeScript config require additional type
information, you'll need to provide the path to your tsconfig.json
.
For more information, see: https://typescript-eslint.io/docs/linting/type-linting
const { resolve } = require("path");
const project = resolve(__dirname, "tsconfig.json");
module.exports = {
root: true,
extends: [
require.resolve("@haltcase/style/eslint/node"),
require.resolve("@haltcase/style/eslint/typescript")
],
parserOptions: {
project
},
settings: {
"import/resolver": {
typescript: {
project
}
}
}
};
Configuring custom components for jsx-a11y
It's common practice for React apps to have shared components like Button
that wrap native elements. You can pass this information along to jsx-a11y
via the components
setting.
The below list is not exhaustive.
module.exports = {
root: true,
extends: [require.resolve("@haltcase/style/eslint/react")],
settings: {
"jsx-a11y": {
components: {
Article: "article",
Button: "button",
Image: "img",
Input: "input",
Link: "a",
Video: "video"
}
}
}
};
Scoped configuration with overrides
ESLint configs can be scoped to include/exclude specific paths. This ensures that rules don't "leak" into places where those rules don't apply.
In this example, Jest rules are only being applied to files matching Jest's default test match pattern.
module.exports = {
extends: [require.resolve("@haltcase/style/eslint/node")],
overrides: [
{
files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
extends: [require.resolve("@haltcase/style/eslint/jest")]
}
]
};
A note on file extensions
By default, all TypeScript rules are scoped to files ending with .ts
and
.tsx
.
However, when using overrides, file extensions must be included or ESLint will
only include .js
files.
module.exports = {
overrides: [
{
files: ["directory/**/*.[jt]s?(x)"],
rules: {
"my-rule": "off"
}
}
]
};
TypeScript
Several Typescript configs are available to cover various scenarios:
Name | Description |
---|---|
@haltcase/style/typescript/base | Baseline config, intended to be extended from. |
@haltcase/style/typescript/bundler | For use in bundled projects, most commonly Vite + React. |
@haltcase/style/typescript/next | For use in Next.js projects. |
@haltcase/style/typescript/node | Default Node config, currently targeting Node 20. |
@haltcase/style/typescript/node18 | For projects targeting Node 18. |
@haltcase/style/typescript/node20 | For projects targeting Node 20. |
@haltcase/style/typescript/web | For use in web projects. |
Typically, you'll only need to extend from one of these:
{
"extends": "@haltcase/style/typescript/next"
}
You could also combine them with an extends
array in Typescript 5+:
{
"extends": [
"@haltcase/style/typescript/node",
"@haltcase/style/typescript/web"
]
}
Note Run
tsc --showConfig
to see the result of the combined configs.
contributing
Please read our contributing guide before creating a pull request.
1 month ago
1 month ago
1 month ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago
9 months ago
9 months ago
9 months ago
6 months ago
6 months ago
9 months ago
6 months ago
6 months ago
6 months ago
6 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago