Skip to content

Commit

Permalink
chore: DX improvements (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusrbrown authored Nov 5, 2024
1 parent 292e4fb commit 31940e1
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 117 deletions.
6 changes: 6 additions & 0 deletions .changeset/three-mice-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/eslint-config": patch
---

Export missing type definitions.

6 changes: 6 additions & 0 deletions .changeset/young-peaches-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bfra.me/prettier-config": minor
---

Ship a bundled config as the default; simplify everything.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
auto-install-peers = false
loglevel = warn
resolution-mode = time-based
save-prefix = ''
shamefully-hoist = true
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
packages/api-core/test-utils/sdks/
packages/eslint-config/.eslint-config-inspector
packages/eslint-config/src/*.d.ts
packages/prettier-config/prettier.config.js
2 changes: 1 addition & 1 deletion eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defineConfig} from './packages/eslint-config'
import {defineConfig} from './packages/eslint-config/src'

export const config = defineConfig({
name: '@bfra.me/works',
Expand Down
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@
"author": "Marcus R. Brown <contact@bfra.me>",
"type": "module",
"scripts": {
"bootstrap": "pnpm install-quickly && pnpm build-essential",
"build": "pnpm -r --stream run build",
"build-essential": "pnpm -r --filter=\"@bfra.me/*-config...\" --stream run build",
"check-format": "prettier --check .",
"clean": "pnpx rimraf --glob \"**/node_modules\" \"packages/*/lib\" \"packages/api-core/dist\" \"packages/eslint-config/src/types?(.d).ts\" \"**/*.tsbuildinfo\"",
"bootstrap": "pnpm install --prefer-frozen-lockfile --ignore-scripts --prefer-offline --loglevel error",
"build": "pnpm -r --stream run build && pnpm lint-packages",
"check-format": "prettier --config ./package.json --check .",
"clean": "pnpx rimraf --glob \"**/node_modules/**/!(.pnpm)\" \"packages/*/lib\" \"packages/api-core/dist\" \"packages/eslint-config/src/types?(.d).ts\" \"**/*.tsbuildinfo\"",
"dev": "pnpm --recursive --parallel --stream run dev",
"fix": "manypkg fix && pnpm lint-source --fix",
"format": "prettier --write .",
"format": "prettier --config ./package.json --write .",
"inspect-eslint-config": "eslint-config-inspector --config eslint.config.ts --open false",
"install-quickly": "pnpm install --frozen-lockfile --ignore-scripts --prefer-offline --silent",
"lint": "manypkg check && pnpm lint-packages && pnpm lint-source",
"prelint-packages": "pnpm build",
"lint": "manypkg check && pnpm lint-source",
"lint-packages": "pnpm -r --include-workspace-root --parallel --filter=!@bfra.me/prettier-config exec publint",
"lint-source": "eslint --flag unstable_config_lookup_from_file --flag unstable_ts_config .",
"publish-changesets": "changeset publish",
"test": "pnpm -r run test",
"test": "pnpm -r --parallel --aggregate-output run test",
"version-changesets": "changeset version",
"watch": "pnpm run build --watch"
},
Expand Down
43 changes: 22 additions & 21 deletions packages/api-core/eslint.config.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import {defineConfig} from '@bfra.me/eslint-config'
import {composeConfig} from '../eslint-config/src'
import {config} from '../../eslint.config'

export default defineConfig({
name: '@bfra.me/api-core',
ignores: ['test-utils'],
rules: {
'no-restricted-imports': [
'error',
{
name: 'fs/promises',
message: "Please use `fs` instead as some client frameworks don't polyfill `fs/promises`.",
},
],

'@typescript-eslint/explicit-function-return-type': 'off',
export default composeConfig(
config,
{
name: '@bfra.me/api-core',
ignores: ['test-utils'],
},
typescript: {
parserOptions: {
project: ['./tsconfig.json', './tsconfig.eslint.json'],
projectService: false,
{
name: '@bfra.me/eslint-config',
files: ['src/**/*.ts'],
rules: {
'no-restricted-imports': [
'error',
{
name: 'fs/promises',
message:
"Please use `fs` instead as some client frameworks don't polyfill `fs/promises`.",
},
],

'@typescript-eslint/explicit-function-return-type': 'off',
},
tsconfigPath: './tsconfig.json',
},
vitest: true,
})
)
3 changes: 1 addition & 2 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"build-inspector": "eslint-config-inspector build --config ./eslint.config.ts",
"dev": "eslint-config-inspector --config ./eslint.config.ts --open false",
"generate-types": "tsx ./scripts/generate-types.ts",
"test": "vitest"
"test": "pnpm --filter-prod=\"{.}...\" --loglevel error run build && vitest"
},
"prettier": "@bfra.me/prettier-config",
"dependencies": {
Expand All @@ -64,7 +64,6 @@
"@bfra.me/tsconfig": "workspace:*",
"@eslint/config-inspector": "0.5.6",
"@eslint/js": "9.14.0",
"@types/eslint__js": "8.42.3",
"@types/fs-extra": "11.0.4",
"@types/node": "22.8.6",
"@typescript-eslint/types": "8.12.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config/scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const rulesDts = await flatConfigsToRulesDTS(configs, {
const configNames = configs.map(config => config.name).filter(Boolean) as string[]
const configDts = `import type {Linter} from 'eslint'
import type {FlatConfigComposer, ResolvableFlatConfig} from 'eslint-flat-config-utils'
import type {Rules} from './rules'
import type {${rulesTypeName}} from './rules'
export type {FlatConfigComposer, ResolvableFlatConfig, Rules}
export type {FlatConfigComposer, ResolvableFlatConfig, ${rulesTypeName}}
/**
* Represents a value that resolves to one or more ESLint flat configurations.
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {defineConfig} from './define-config'

export * from './compose-config'
export * from './config.d'
export * from './configs'
export * from './define-config'
export * from './env'
export * from './globs'
export * from './options'
export * from './rules.d'

export const config = defineConfig()
export default config
1 change: 1 addition & 0 deletions packages/prettier-config/.prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/fixtures/
prettier.config.js
7 changes: 7 additions & 0 deletions packages/prettier-config/eslint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {composeConfig} from '../eslint-config/src'
import config from '../../eslint.config'

export default composeConfig(config, {
name: '@bfra.me/prettier-config/bundled',
ignores: ['prettier.config.js'],
})
1 change: 1 addition & 0 deletions packages/prettier-config/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"./index.js"
18 changes: 13 additions & 5 deletions packages/prettier-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
"type": "module",
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js"
"types": "./lib/prettier.config.d.ts",
"source": "./src/prettier.config.ts",
"import": "./prettier.config.js"
},
"./*": {
"types": "./lib/*.d.ts",
"source": "./src/*.ts",
"import": "./lib/*.js"
},
"./100-proof": "./lib/prettier.config.js",
"./120-proof": "./lib/120-proof.js",
Expand All @@ -32,16 +38,18 @@
"./*/semi": "./lib/*.js?semi=true",
"./package.json": "./package.json"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"main": "./prettier.config.js",
"types": "./lib/prettier.config.d.ts",
"files": [
"lib",
"src",
"prettier.config.js",
"!**/*.map"
],
"scripts": {
"build": "tsup",
"test": "vitest"
"postbuild": "prettier --config ./prettier.config.js --ignore-path .prettierignore --log-level warn --write .",
"test": "pnpm --filter-prod=\"{.}...\" --loglevel error run build && vitest"
},
"dependencies": {
"@bfra.me/prettier-plugins": "workspace:*"
Expand Down
9 changes: 9 additions & 0 deletions packages/prettier-config/prettier.config.js

Large diffs are not rendered by default.

19 changes: 8 additions & 11 deletions packages/prettier-config/src/120-proof.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import defaultConfig, {type DefaultConfig} from './prettier.config.js'

/**
* Shared Prettier configuration for bfra.me projects with `printWidth` set to 120 characters.
*/
export interface $120ProofConfig extends DefaultConfig {
/** @default 120 */
printWidth: 120 | DefaultConfig['printWidth']
}
import defaultConfig, {type Writable} from './prettier.config.js'

const $120ProofConfig = {
...defaultConfig,
printWidth: 120,
semi: new URL(import.meta.url).searchParams.has('semi', 'true') || defaultConfig.semi,
} as $120ProofConfig
} as const

/**
* Shared Prettier configuration for bfra.me projects with `printWidth` set to 120 characters.
*/
type $120ProofConfig = Writable<typeof $120ProofConfig>

export default $120ProofConfig
export default $120ProofConfig as $120ProofConfig
5 changes: 0 additions & 5 deletions packages/prettier-config/src/index.ts

This file was deleted.

21 changes: 16 additions & 5 deletions packages/prettier-config/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ const resolvedPlugins: Record<string, Plugin> = {
'@bfra.me/prettier-plugins/package-json': PluginPackageJson,
}

export const resolve = (resolver: (id: string) => string, plugin: string): string | Plugin => {
export async function resolve(
resolver: (id: string) => string,
plugin: string,
): Promise<string | Plugin<any>> {
try {
if (resolvedPlugins[plugin]) {
return resolvedPlugins[plugin]
if (!resolvedPlugins[plugin]) {
const resolved = resolver(plugin)
const resolvedPlugin = await interopDefault(import(resolved))
resolvedPlugins[plugin] = resolvedPlugin
}
return resolver(plugin)
} finally {
return resolvedPlugins[plugin] ?? plugin
} catch {
return plugin
}
}

async function interopDefault<T>(
m: T | PromiseLike<T>,
): Promise<T extends {default: infer U} ? U : T> {
return ((await m) as any).default || (await m)
}
41 changes: 16 additions & 25 deletions packages/prettier-config/src/prettier.config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import {createRequire} from 'node:module'
import type {Config} from 'prettier'
import type * as prettier from 'prettier'
import {resolve} from './plugins.js'

export type Writable<T> = {
-readonly [K in keyof T]: T[K] extends object ? {[J in keyof T[K]]: Writable<T[K][J]>} : T[K]
} & {}

const require = createRequire(import.meta.url)
const resolvePlugin = resolve.bind(null, require.resolve)
const {searchParams} = new URL(import.meta.url)

type RequiredConfig = Required<Config>

/**
* Shared Prettier configuration for bfra.me projects.
*/
export interface DefaultConfig extends Config {
/** @default avoid */
arrowParens: 'avoid' | RequiredConfig['arrowParens']
/** @default false */
bracketSpacing: false | boolean
/** @default auto */
endOfLine: 'auto' | RequiredConfig['endOfLine']
/** @default 100 */
printWidth: 100 | number
/** @default false */
semi: false | boolean
/** @default true */
singleQuote: true | boolean
}

const config = {
arrowParens: 'avoid',
bracketSpacing: false,
Expand Down Expand Up @@ -93,9 +77,16 @@ const config = {
singleQuote: false,
},
},
],
] as prettier.Config['overrides'],

plugins: (await Promise.all(
['@bfra.me/prettier-plugins/package-json'].map(resolvePlugin),
)) as prettier.Config['plugins'],
} as const

plugins: ['@bfra.me/prettier-plugins/package-json'].map(resolvePlugin),
} as DefaultConfig
/**
* Shared Prettier configuration for bfra.me projects.
*/
export type Config = Writable<typeof config>

export default config
export default config as Config
2 changes: 1 addition & 1 deletion packages/prettier-config/tsconfig.eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"compilerOptions": {
"noEmit": true
},
"include": ["./test/**/*.ts", "tsup.config.ts"]
"include": ["./test/**/*.ts", "*.config.ts"]
}
37 changes: 26 additions & 11 deletions packages/prettier-config/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import {defineConfig} from 'tsup'

export default defineConfig({
bundle: false,
clean: true,
dts: 'src/index.ts',
entry: ['./src/*.ts'],
format: ['esm'],
outDir: 'lib',
sourcemap: true,
// Splitting can place code using `import.meta` into a separate chunk, which breaks the preset export mappings.
splitting: false,
})
export default defineConfig([
{
bundle: false,
clean: true,
dts: true,
entry: ['./src/*.ts'],
format: ['esm'],
minify: true,
outDir: 'lib',
sourcemap: true,
// Splitting can place code using `import.meta` into a separate chunk, which breaks the preset export mappings.
splitting: false,
target: 'es2022',
},
{
entry: ['./src/prettier.config.ts'],
format: ['esm'],
minify: true,
// Bundle the config and dependencies into a single file.
noExternal: [/^@bfra.me\//],
outDir: '.',
sourcemap: true,
target: 'es2022',
treeshake: 'smallest',
},
])
Loading

0 comments on commit 31940e1

Please sign in to comment.