vite.config.ts
This file
Path
vite.config.ts
365 lines 路 11.1 KiB 路 TypeScript
Last changed by
chore: unified oxfmt and oxlint config (#5518)Rahul Mishrabefore adoptionf78a245
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366import { RolldownMagicString } from 'rolldown'import { parse, Visitor } from 'rolldown/utils'import { defineConfig } from 'vite-plus'import type { TsdownPluginOption } from 'vite-plus/pack'import { appendExportEmptyToDts } from './build/dts-plugins'
const entry = ['src/**/*.ts', '!src/**/*.test.ts', '!src/**/*.test.tsx']const common = { entry, tsconfig: 'tsconfig.build.json', unbundle: true, clean: false,}
const removeDtsPrivateFieldsPlugin: TsdownPluginOption = { name: 'hono:remove-dts-private-fields', async renderChunk(code, id) { if (!id.fileName.endsWith('.d.ts')) { return }
const ast = await parse(id.fileName, code) const ms = new RolldownMagicString(code, { filename: id.fileName })
new Visitor({ ClassDeclaration: (node) => { node.body.body.forEach((elem) => { if (elem.type === 'PropertyDefinition' && elem.key.type === 'PrivateIdentifier') { this.info(`Removing private field from ${id.fileName}`) ms.remove(elem.start, elem.end) } }) }, }).visit(ast.program)
return ms.toString() },}
const validatePackageExportsPlugin: TsdownPluginOption = { name: 'hono:validate-package-exports', async buildStart() { const validateExports = ( source: Record<string, unknown>, target: Record<string, unknown>, fileName: string ) => { const isEntryInTarget = (entry: string): boolean => { if (entry in target) { return true }
// e.g., "./utils/*" -> "./utils" const wildcardPrefix = entry.replace(/\/\*$/, '') if (entry.endsWith('/*')) { return Object.keys(target).some( (targetEntry) => targetEntry.startsWith(wildcardPrefix + '/') && targetEntry !== wildcardPrefix ) }
const separatedEntry = entry.split('/') while (separatedEntry.length > 0) { const pattern = `${separatedEntry.join('/')}/*` if (pattern in target) { return true } separatedEntry.pop() }
return false }
Object.keys(source).forEach((sourceEntry) => { if (!isEntryInTarget(sourceEntry)) { throw new Error(`Missing "${sourceEntry}" in '${fileName}'`) } }) }
const pkgJson = await import('./package.json', { with: { type: 'json' } }) const jsrJson = await import('./jsr.json', { with: { type: 'json' } })
validateExports(pkgJson.exports, jsrJson.exports, 'jsr.json') validateExports(jsrJson.exports, pkgJson.exports, 'package.json') },}
export default defineConfig({ fmt: { printWidth: 100, trailingComma: 'es5', tabWidth: 2, semi: false, singleQuote: true, jsxSingleQuote: true, endOfLine: 'lf', sortImports: { newlinesBetween: false, }, sortPackageJson: false, ignorePatterns: ['dist/', 'coverage/', '.wrangler/', 'pnpm-lock.yaml', '**/*.json', '**/*.mts'], }, lint: { plugins: ['node', 'typescript', 'import', 'unicorn'], categories: { correctness: 'off', }, env: { builtin: true, es2024: true, }, globals: { __dirname: 'off', __filename: 'off', AbortController: 'readonly', AbortSignal: 'readonly', atob: 'readonly', Blob: 'readonly', BroadcastChannel: 'readonly', btoa: 'readonly', Buffer: 'readonly', ByteLengthQueuingStrategy: 'readonly', clearImmediate: 'readonly', clearInterval: 'readonly', clearTimeout: 'readonly', CloseEvent: 'readonly', CompressionStream: 'readonly', console: 'readonly', CountQueuingStrategy: 'readonly', crypto: 'readonly', Crypto: 'readonly', CryptoKey: 'readonly', CustomEvent: 'readonly', DecompressionStream: 'readonly', DOMException: 'readonly', Event: 'readonly', EventTarget: 'readonly', exports: 'off', fetch: 'readonly', File: 'readonly', FormData: 'readonly', global: 'readonly', Headers: 'readonly', MessageChannel: 'readonly', MessageEvent: 'readonly', MessagePort: 'readonly', module: 'off', navigator: 'readonly', Navigator: 'readonly', performance: 'readonly', Performance: 'readonly', PerformanceEntry: 'readonly', PerformanceMark: 'readonly', PerformanceMeasure: 'readonly', PerformanceObserver: 'readonly', PerformanceObserverEntryList: 'readonly', PerformanceResourceTiming: 'readonly', process: 'readonly', queueMicrotask: 'readonly', ReadableByteStreamController: 'readonly', ReadableStream: 'readonly', ReadableStreamBYOBReader: 'readonly', ReadableStreamBYOBRequest: 'readonly', ReadableStreamDefaultController: 'readonly', ReadableStreamDefaultReader: 'readonly', Request: 'readonly', require: 'off', Response: 'readonly', setImmediate: 'readonly', setInterval: 'readonly', setTimeout: 'readonly', structuredClone: 'readonly', SubtleCrypto: 'readonly', TextDecoder: 'readonly', TextDecoderStream: 'readonly', TextEncoder: 'readonly', TextEncoderStream: 'readonly', TransformStream: 'readonly', TransformStreamDefaultController: 'readonly', URL: 'readonly', URLSearchParams: 'readonly', WebAssembly: 'readonly', WebSocket: 'readonly', WritableStream: 'readonly', WritableStreamDefaultController: 'readonly', WritableStreamDefaultWriter: 'readonly', addEventListener: 'readonly', }, ignorePatterns: ['.wrangler', '**/coverage', '**/dist'], rules: { 'constructor-super': 'error', 'for-direction': 'error', 'getter-return': 'error', 'no-async-promise-executor': 'error', 'no-case-declarations': 'error', 'no-class-assign': 'error', 'no-compare-neg-zero': 'error', 'no-cond-assign': 'error', 'no-const-assign': 'error', 'no-constant-binary-expression': 'error', 'no-constant-condition': 'error', 'no-control-regex': 'error', 'no-debugger': ['error'], 'no-delete-var': 'error', 'no-dupe-class-members': 'error', 'no-dupe-else-if': 'error', 'no-dupe-keys': 'error', 'no-duplicate-case': 'error', 'no-empty': [ 'warn', { allowEmptyCatch: true, }, ], 'no-empty-character-class': 'error', 'no-empty-pattern': 'error', 'no-empty-static-block': 'error', 'no-ex-assign': 'error', 'no-extra-boolean-cast': 'error', 'no-fallthrough': 'error', 'no-func-assign': 'error', 'no-global-assign': 'error', 'no-import-assign': 'error', 'no-invalid-regexp': 'error', 'no-irregular-whitespace': 'error', 'no-loss-of-precision': 'error', 'no-misleading-character-class': 'error', 'no-new-native-nonconstructor': 'error', 'no-nonoctal-decimal-escape': 'error', 'no-obj-calls': 'error', 'no-prototype-builtins': 'error', 'no-redeclare': 'error', 'no-regex-spaces': 'error', 'no-self-assign': 'error', 'no-setter-return': 'error', 'no-shadow-restricted-names': 'error', 'no-sparse-arrays': 'error', 'no-this-before-super': 'error', 'no-unreachable': 'error', 'no-unsafe-finally': 'error', 'no-unsafe-negation': 'error', 'no-unsafe-optional-chaining': 'error', 'no-unused-labels': 'error', 'no-unused-private-class-members': 'error', 'no-unused-vars': [ 'warn', { args: 'all', argsIgnorePattern: '^_', caughtErrors: 'all', caughtErrorsIgnorePattern: '^_', destructuredArrayIgnorePattern: '^_', varsIgnorePattern: '^_', ignoreRestSiblings: true, }, ], 'no-useless-backreference': 'error', 'no-useless-catch': 'error', 'no-with': 'error', 'require-yield': 'error', 'use-isnan': 'error', 'valid-typeof': 'error', 'no-array-constructor': 'error', 'no-empty-function': [ 'error', { allow: ['arrowFunctions'], }, ], 'prefer-const': [ 'warn', { destructuring: 'all', }, ], curly: ['error', 'all'], 'import/consistent-type-specifier-style': ['error', 'prefer-top-level'], 'import/no-duplicates': 'error', 'node/no-exports-assign': 'error', 'typescript/ban-ts-comment': [ 'error', { minimumDescriptionLength: 10, }, ], 'typescript/no-duplicate-enum-values': 'error', 'typescript/no-explicit-any': 'warn', 'typescript/no-extra-non-null-assertion': 'error', 'typescript/no-misused-new': 'error', 'typescript/no-namespace': 'error', 'typescript/no-non-null-asserted-nullish-coalescing': 'error', 'typescript/no-non-null-asserted-optional-chain': 'error', 'typescript/no-this-alias': 'error', 'typescript/no-unnecessary-type-constraint': 'error', 'typescript/no-unsafe-declaration-merging': 'error', 'typescript/no-wrapper-object-types': 'error', 'typescript/prefer-as-const': 'error', 'typescript/prefer-namespace-keyword': 'error', 'typescript/triple-slash-reference': 'error', 'typescript/adjacent-overload-signatures': 'error', 'typescript/ban-tslint-comment': 'error', 'typescript/class-literal-property-style': 'error', 'typescript/consistent-type-assertions': 'error', 'typescript/consistent-type-imports': [ 'error', { prefer: 'type-imports', }, ], }, overrides: [ { files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'], rules: { 'constructor-super': 'off', 'getter-return': 'off', 'no-class-assign': 'off', 'no-const-assign': 'off', 'no-dupe-class-members': 'off', 'no-dupe-keys': 'off', 'no-func-assign': 'off', 'no-import-assign': 'off', 'no-new-native-nonconstructor': 'off', 'no-obj-calls': 'off', 'no-redeclare': 'off', 'no-setter-return': 'off', 'no-this-before-super': 'off', 'no-unreachable': 'off', 'no-unsafe-negation': 'off', 'no-var': 'error', 'no-with': 'off', 'prefer-rest-params': 'error', 'prefer-spread': 'error', }, }, ], }, pack: [ { ...common, format: ['esm'], outDir: 'dist', dts: false, outExtensions: () => ({ js: '.js' }), plugins: [validatePackageExportsPlugin], }, { ...common, format: ['cjs'], outDir: 'dist/cjs', dts: false, outExtensions: () => ({ js: '.js' }), // No need to validate package exports for CJS build, as it is already validated in ESM build. }, { ...common, format: ['esm'], outDir: 'dist/types', dts: { emitDtsOnly: true }, plugins: [removeDtsPrivateFieldsPlugin, appendExportEmptyToDts], outExtensions: () => ({ dts: '.d.ts' }), }, ],})