|
| 1 | +// @ts-check |
| 2 | +import { builtinModules, createRequire } from 'node:module' |
| 3 | +import eslint from '@eslint/js' |
| 4 | +import pluginN from 'eslint-plugin-n' |
| 5 | +import * as pluginI from 'eslint-plugin-i' |
| 6 | +import pluginRegExp from 'eslint-plugin-regexp' |
| 7 | +import tsParser from '@typescript-eslint/parser' |
| 8 | +import tseslint from 'typescript-eslint' |
| 9 | +import globals from 'globals' |
| 10 | + |
| 11 | +const require = createRequire(import.meta.url) |
| 12 | +const pkg = require('./package.json') |
| 13 | +const pkgVite = require('./packages/vite/package.json') |
| 14 | + |
| 15 | +export default tseslint.config( |
| 16 | + { |
| 17 | + ignores: [ |
| 18 | + 'packages/create-vite/template-*', |
| 19 | + '**/dist/**', |
| 20 | + '**/fixtures/**', |
| 21 | + '**/playground-temp/**', |
| 22 | + '**/temp/**', |
| 23 | + '**/.vitepress/cache/**', |
| 24 | + '**/*.snap', |
| 25 | + ], |
| 26 | + }, |
| 27 | + eslint.configs.recommended, |
| 28 | + ...tseslint.configs.recommended, |
| 29 | + ...tseslint.configs.stylistic, |
| 30 | + /** @type {any} */ (pluginRegExp.configs['flat/recommended']), |
| 31 | + { |
| 32 | + name: 'main', |
| 33 | + languageOptions: { |
| 34 | + parser: tsParser, |
| 35 | + parserOptions: { |
| 36 | + sourceType: 'module', |
| 37 | + ecmaVersion: 2022, |
| 38 | + }, |
| 39 | + globals: { |
| 40 | + ...globals.es2021, |
| 41 | + ...globals.node, |
| 42 | + }, |
| 43 | + }, |
| 44 | + plugins: { |
| 45 | + n: pluginN, |
| 46 | + i: pluginI, |
| 47 | + }, |
| 48 | + rules: { |
| 49 | + 'n/no-exports-assign': 'error', |
| 50 | + 'n/no-unpublished-bin': 'error', |
| 51 | + 'n/no-unsupported-features/es-builtins': 'error', |
| 52 | + 'n/no-unsupported-features/node-builtins': 'error', |
| 53 | + 'n/process-exit-as-throw': 'error', |
| 54 | + 'n/hashbang': 'error', |
| 55 | + |
| 56 | + eqeqeq: ['warn', 'always', { null: 'never' }], |
| 57 | + 'no-debugger': ['error'], |
| 58 | + 'no-empty': ['warn', { allowEmptyCatch: true }], |
| 59 | + 'no-process-exit': 'off', |
| 60 | + 'no-useless-escape': 'off', |
| 61 | + 'prefer-const': [ |
| 62 | + 'warn', |
| 63 | + { |
| 64 | + destructuring: 'all', |
| 65 | + }, |
| 66 | + ], |
| 67 | + |
| 68 | + 'n/no-missing-require': [ |
| 69 | + 'error', |
| 70 | + { |
| 71 | + // for try-catching yarn pnp |
| 72 | + allowModules: ['pnpapi', 'vite'], |
| 73 | + tryExtensions: ['.ts', '.js', '.jsx', '.tsx', '.d.ts'], |
| 74 | + }, |
| 75 | + ], |
| 76 | + 'n/no-extraneous-import': [ |
| 77 | + 'error', |
| 78 | + { |
| 79 | + allowModules: ['vite', 'less', 'sass', 'vitest', 'unbuild'], |
| 80 | + }, |
| 81 | + ], |
| 82 | + 'n/no-extraneous-require': [ |
| 83 | + 'error', |
| 84 | + { |
| 85 | + allowModules: ['vite'], |
| 86 | + }, |
| 87 | + ], |
| 88 | + |
| 89 | + '@typescript-eslint/ban-ts-comment': 'error', |
| 90 | + '@typescript-eslint/ban-types': 'off', // TODO: we should turn this on in a new PR |
| 91 | + '@typescript-eslint/explicit-module-boundary-types': [ |
| 92 | + 'error', |
| 93 | + { allowArgumentsExplicitlyTypedAsAny: true }, |
| 94 | + ], |
| 95 | + '@typescript-eslint/no-empty-function': [ |
| 96 | + 'error', |
| 97 | + { allow: ['arrowFunctions'] }, |
| 98 | + ], |
| 99 | + '@typescript-eslint/no-empty-interface': 'off', |
| 100 | + '@typescript-eslint/no-explicit-any': 'off', // maybe we should turn this on in a new PR |
| 101 | + 'no-extra-semi': 'off', |
| 102 | + '@typescript-eslint/no-extra-semi': 'off', // conflicts with prettier |
| 103 | + '@typescript-eslint/no-inferrable-types': 'off', |
| 104 | + '@typescript-eslint/no-unused-vars': 'off', // maybe we should turn this on in a new PR |
| 105 | + '@typescript-eslint/no-var-requires': 'off', |
| 106 | + '@typescript-eslint/consistent-type-imports': [ |
| 107 | + 'error', |
| 108 | + { prefer: 'type-imports', disallowTypeAnnotations: false }, |
| 109 | + ], |
| 110 | + // disable rules set in @typescript-eslint/stylistic v6 that wasn't set in @typescript-eslint/recommended v5 and which conflict with current code |
| 111 | + // maybe we should turn them on in a new PR |
| 112 | + '@typescript-eslint/array-type': 'off', |
| 113 | + '@typescript-eslint/ban-tslint-comment': 'off', |
| 114 | + '@typescript-eslint/consistent-generic-constructors': 'off', |
| 115 | + '@typescript-eslint/consistent-indexed-object-style': 'off', |
| 116 | + '@typescript-eslint/consistent-type-definitions': 'off', |
| 117 | + '@typescript-eslint/prefer-for-of': 'off', |
| 118 | + '@typescript-eslint/prefer-function-type': 'off', |
| 119 | + |
| 120 | + 'i/no-nodejs-modules': [ |
| 121 | + 'error', |
| 122 | + { allow: builtinModules.map((mod) => `node:${mod}`) }, |
| 123 | + ], |
| 124 | + 'i/no-duplicates': 'error', |
| 125 | + 'i/order': 'error', |
| 126 | + 'sort-imports': [ |
| 127 | + 'error', |
| 128 | + { |
| 129 | + ignoreCase: false, |
| 130 | + ignoreDeclarationSort: true, |
| 131 | + ignoreMemberSort: false, |
| 132 | + memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'], |
| 133 | + allowSeparatedGroups: false, |
| 134 | + }, |
| 135 | + ], |
| 136 | + |
| 137 | + 'regexp/no-contradiction-with-assertion': 'error', |
| 138 | + // in some cases using explicit letter-casing is more performant than the `i` flag |
| 139 | + 'regexp/use-ignore-case': 'off', |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: 'vite/globals', |
| 144 | + files: ['packages/**/*.?([cm])[jt]s?(x)'], |
| 145 | + ignores: ['**/__tests__/**'], |
| 146 | + rules: { |
| 147 | + 'no-restricted-globals': ['error', 'require', '__dirname', '__filename'], |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: 'vite/node', |
| 152 | + files: ['packages/vite/src/node/**/*.?([cm])[jt]s?(x)'], |
| 153 | + rules: { |
| 154 | + 'no-console': ['error'], |
| 155 | + 'n/no-restricted-require': [ |
| 156 | + 'error', |
| 157 | + Object.keys(pkgVite.devDependencies).map((d) => ({ |
| 158 | + name: d, |
| 159 | + message: |
| 160 | + `devDependencies can only be imported using ESM syntax so ` + |
| 161 | + `that they are included in the rollup bundle. If you are trying to ` + |
| 162 | + `lazy load a dependency, use (await import('dependency')).default instead.`, |
| 163 | + })), |
| 164 | + ], |
| 165 | + }, |
| 166 | + }, |
| 167 | + { |
| 168 | + name: 'playground/enforce-esm', |
| 169 | + files: ['playground/**/*.?([cm])[jt]s?(x)'], |
| 170 | + ignores: [ |
| 171 | + 'playground/ssr-resolve/**', |
| 172 | + 'playground/**/*{commonjs,cjs}*/**', |
| 173 | + 'playground/**/*{commonjs,cjs}*', |
| 174 | + 'playground/**/*dep*/**', |
| 175 | + 'playground/resolve/browser-module-field2/index.web.js', |
| 176 | + 'playground/resolve/browser-field/**', |
| 177 | + 'playground/tailwind/**', // blocked by https://github.com/postcss/postcss-load-config/issues/239 |
| 178 | + ], |
| 179 | + rules: { |
| 180 | + 'i/no-commonjs': 'error', |
| 181 | + }, |
| 182 | + }, |
| 183 | + { |
| 184 | + name: 'playground/test', |
| 185 | + files: ['playground/**/__tests__/**/*.?([cm])[jt]s?(x)'], |
| 186 | + rules: { |
| 187 | + // engine field doesn't exist in playgrounds |
| 188 | + 'n/no-unsupported-features/es-builtins': [ |
| 189 | + 'error', |
| 190 | + { |
| 191 | + version: pkg.engines.node, |
| 192 | + }, |
| 193 | + ], |
| 194 | + 'n/no-unsupported-features/node-builtins': [ |
| 195 | + 'error', |
| 196 | + { |
| 197 | + version: pkg.engines.node, |
| 198 | + // ideally we would like to allow all experimental features |
| 199 | + // https://github.com/eslint-community/eslint-plugin-n/issues/199 |
| 200 | + ignores: ['fetch'], |
| 201 | + }, |
| 202 | + ], |
| 203 | + }, |
| 204 | + }, |
| 205 | + |
| 206 | + { |
| 207 | + name: 'disables/vite/client', |
| 208 | + files: ['packages/vite/src/client/**/*.?([cm])[jt]s?(x)'], |
| 209 | + ignores: ['**/__tests__/**'], |
| 210 | + rules: { |
| 211 | + 'n/no-unsupported-features/node-builtins': 'off', |
| 212 | + }, |
| 213 | + }, |
| 214 | + { |
| 215 | + name: 'disables/vite/types', |
| 216 | + files: [ |
| 217 | + 'packages/vite/src/types/**/*.?([cm])[jt]s?(x)', |
| 218 | + 'packages/vite/scripts/**/*.?([cm])[jt]s?(x)', |
| 219 | + '**/*.spec.ts', |
| 220 | + ], |
| 221 | + rules: { |
| 222 | + 'n/no-extraneous-import': 'off', |
| 223 | + }, |
| 224 | + }, |
| 225 | + { |
| 226 | + name: 'disables/create-vite/templates', |
| 227 | + files: [ |
| 228 | + 'packages/create-vite/template-*/**/*.?([cm])[jt]s?(x)', |
| 229 | + '**/build.config.ts', |
| 230 | + ], |
| 231 | + rules: { |
| 232 | + 'no-undef': 'off', |
| 233 | + 'n/no-missing-import': 'off', |
| 234 | + 'n/no-extraneous-import': 'off', |
| 235 | + 'n/no-extraneous-require': 'off', |
| 236 | + '@typescript-eslint/explicit-module-boundary-types': 'off', |
| 237 | + }, |
| 238 | + }, |
| 239 | + { |
| 240 | + name: 'disables/playground', |
| 241 | + files: ['playground/**/*.?([cm])[jt]s?(x)', 'docs/**/*.?([cm])[jt]s?(x)'], |
| 242 | + rules: { |
| 243 | + 'n/no-extraneous-import': 'off', |
| 244 | + 'n/no-extraneous-require': 'off', |
| 245 | + 'n/no-missing-import': 'off', |
| 246 | + 'n/no-missing-require': 'off', |
| 247 | + 'n/no-unsupported-features/es-builtins': 'off', |
| 248 | + 'n/no-unsupported-features/node-builtins': 'off', |
| 249 | + '@typescript-eslint/explicit-module-boundary-types': 'off', |
| 250 | + 'no-undef': 'off', |
| 251 | + 'no-empty': 'off', |
| 252 | + 'no-constant-condition': 'off', |
| 253 | + '@typescript-eslint/no-empty-function': 'off', |
| 254 | + }, |
| 255 | + }, |
| 256 | + { |
| 257 | + name: 'disables/playground/tsconfig-json', |
| 258 | + files: [ |
| 259 | + 'playground/tsconfig-json/**/*.?([cm])[jt]s?(x)', |
| 260 | + 'playground/tsconfig-json-load-error/**/*.?([cm])[jt]s?(x)', |
| 261 | + ], |
| 262 | + ignores: ['**/__tests__/**'], |
| 263 | + rules: { |
| 264 | + '@typescript-eslint/ban-ts-comment': 'off', |
| 265 | + }, |
| 266 | + }, |
| 267 | + { |
| 268 | + name: 'disables/js', |
| 269 | + files: ['**/*.js', '**/*.mjs', '**/*.cjs'], |
| 270 | + rules: { |
| 271 | + '@typescript-eslint/explicit-module-boundary-types': 'off', |
| 272 | + }, |
| 273 | + }, |
| 274 | + { |
| 275 | + name: 'disables/dts', |
| 276 | + files: ['**/*.d.ts'], |
| 277 | + rules: { |
| 278 | + '@typescript-eslint/triple-slash-reference': 'off', |
| 279 | + }, |
| 280 | + }, |
| 281 | + { |
| 282 | + name: 'disables/test', |
| 283 | + files: ['**/__tests__/**/*.?([cm])[jt]s?(x)'], |
| 284 | + rules: { |
| 285 | + 'no-console': 'off', |
| 286 | + '@typescript-eslint/ban-ts-comment': 'off', |
| 287 | + }, |
| 288 | + }, |
| 289 | +) |
0 commit comments