6.1.0 • Published 1 month ago

@haltcase/style v6.1.0

Weekly downloads
-
License
MPL-2.0
Repository
github
Last release
1 month ago

@haltcase/style · Version License GitHub Actions

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.

See: https://prettier.io/docs/en/install.html

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.

See: Scoped configuration with overrides.

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 as next)
  • @haltcase/style/eslint/playwright-test
  • @haltcase/style/eslint/react
  • @haltcase/style/eslint/typescript (requires typescript 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:

NameDescription
@haltcase/style/typescript/baseBaseline config, intended to be extended from.
@haltcase/style/typescript/bundlerFor use in bundled projects, most commonly Vite + React.
@haltcase/style/typescript/nextFor use in Next.js projects.
@haltcase/style/typescript/nodeDefault Node config, currently targeting Node 20.
@haltcase/style/typescript/node18For projects targeting Node 18.
@haltcase/style/typescript/node20For projects targeting Node 20.
@haltcase/style/typescript/webFor 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.

6.2.0-canary.3

1 month ago

6.2.0-canary.1

1 month ago

6.2.0-canary.2

1 month ago

6.1.0

4 months ago

6.1.0-canary.1

4 months ago

6.0.0

5 months ago

5.1.0-canary.1

5 months ago

6.0.0-canary.1

5 months ago

6.0.0-canary.2

5 months ago

4.1.0-canary.1

6 months ago

5.0.0

6 months ago

3.0.0-canary.1

9 months ago

3.0.1

9 months ago

3.0.1-canary.1

9 months ago

4.1.1-canary.1

6 months ago

5.0.0-canary.1

6 months ago

3.0.0

9 months ago

4.0.0-canary.1

6 months ago

4.1.0

6 months ago

4.0.0

6 months ago

4.1.1

6 months ago

2.1.0

11 months ago

2.1.0-canary.1

11 months ago

2.0.2-canary.1

11 months ago

2.0.1

11 months ago

2.0.1-canary.1

11 months ago

2.0.0

11 months ago

2.0.0-canary.1

11 months ago

1.1.0

11 months ago

1.1.0-canary.1

11 months ago

1.0.0-canary.7

11 months ago

1.0.0-canary.6

11 months ago

1.0.0-canary.5

11 months ago

1.0.0-canary.4

11 months ago

1.0.0-canary.3

11 months ago

1.0.0-canary.2

11 months ago

1.0.0-canary.1

11 months ago

1.0.0

11 months ago