diff --git a/eslint.config.js b/eslint.config.js index b0126a964df467..e75ac25fc68f18 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -11,6 +11,11 @@ const require = createRequire(import.meta.url) const pkg = require('./package.json') const pkgVite = require('./packages/vite/package.json') +// Some rules work better with typechecking enabled, but as enabling it is slow, +// we only do so when linting in IDEs for now. If you want to lint with typechecking +// explicitly, set this to `true` manually. +const shouldTypeCheck = typeof process.env.VSCODE_PID === 'string' + export default tseslint.config( { ignores: [ @@ -34,6 +39,12 @@ export default tseslint.config( parserOptions: { sourceType: 'module', ecmaVersion: 2022, + project: shouldTypeCheck + ? [ + './packages/*/tsconfig.json', + './packages/vite/src/*/tsconfig.json', + ] + : undefined, }, globals: { ...globals.es2021, @@ -294,4 +305,25 @@ export default tseslint.config( '@typescript-eslint/ban-ts-comment': 'off', }, }, + { + name: 'disables/typechecking', + files: [ + '**/*.js', + '**/*.mjs', + '**/*.cjs', + '**/*.d.ts', + '**/*.d.cts', + '**/__tests__/**', + 'docs/**', + 'playground/**', + 'scripts/**', + 'vitest.config.ts', + 'vitest.config.e2e.ts', + ], + languageOptions: { + parserOptions: { + project: false, + }, + }, + }, ) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 2fabb042a3cefc..d05d8323389dfa 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -120,7 +120,7 @@ const legacyEnvVarMarker = `__VITE_IS_LEGACY__` const _require = createRequire(import.meta.url) const nonLeadingHashInFileNameRE = /[^/]+\[hash(?::\d+)?\]/ -const prefixedHashInFileNameRE = /\W?\[hash(:\d+)?\]/ +const prefixedHashInFileNameRE = /\W?\[hash(?::\d+)?\]/ function viteLegacyPlugin(options: Options = {}): Plugin[] { let config: ResolvedConfig diff --git a/packages/plugin-legacy/tsconfig.json b/packages/plugin-legacy/tsconfig.json index bd94458fe2dc28..95d499371d8a94 100644 --- a/packages/plugin-legacy/tsconfig.json +++ b/packages/plugin-legacy/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src"], + "include": ["build.config.ts", "src"], "exclude": ["**/*.spec.ts"], "compilerOptions": { "outDir": "dist", diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index 5cd07e5663b12c..9c8d773759ea83 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -275,7 +275,7 @@ const __require = require; name: 'cjs-chunk-patch', renderChunk(code, chunk) { if (!chunk.fileName.includes('chunks/dep-')) return - const match = code.match(/^(?:import[\s\S]*?;\s*)+/) + const match = /^(?:import[\s\S]*?;\s*)+/.exec(code) const index = match ? match.index! + match[0].length : 0 const s = new MagicString(code) // inject after the last `import` diff --git a/packages/vite/rollupLicensePlugin.ts b/packages/vite/rollupLicensePlugin.ts index aa08c64f2903ff..f285ca377047ef 100644 --- a/packages/vite/rollupLicensePlugin.ts +++ b/packages/vite/rollupLicensePlugin.ts @@ -68,7 +68,7 @@ export default function licensePlugin( '\n' + licenseText .trim() - .replace(/(\r\n|\r)/g, '\n') + .replace(/\r\n|\r/g, '\n') .split('\n') .map((line) => `> ${line}`) .join('\n') + diff --git a/packages/vite/src/node/optimizer/resolve.ts b/packages/vite/src/node/optimizer/resolve.ts index b76634dd8ae8cf..822b19e1898bad 100644 --- a/packages/vite/src/node/optimizer/resolve.ts +++ b/packages/vite/src/node/optimizer/resolve.ts @@ -109,7 +109,7 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] { // `filePath`: "./dist/glob/foo-browser/foo.js" // we need to revert the file path back to the export key by // matching value regex and replacing the capture groups to the key - const matched = slash(filePath).match(exportsValueGlobRe) + const matched = exportsValueGlobRe.exec(slash(filePath)) // `matched`: [..., 'foo', 'foo'] if (matched) { let allGlobSame = matched.length === 2 diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 1cdef6c339c103..c47f38e18a8b32 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -413,10 +413,10 @@ function esbuildScanPlugin( let scriptId = 0 const matches = raw.matchAll(scriptRE) for (const [, openTag, content] of matches) { - const typeMatch = openTag.match(typeRE) + const typeMatch = typeRE.exec(openTag) const type = typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3]) - const langMatch = openTag.match(langRE) + const langMatch = langRE.exec(openTag) const lang = langMatch && (langMatch[1] || langMatch[2] || langMatch[3]) // skip non type module script @@ -440,7 +440,7 @@ function esbuildScanPlugin( } else if (p.endsWith('.astro')) { loader = 'ts' } - const srcMatch = openTag.match(srcRE) + const srcMatch = srcRE.exec(openTag) if (srcMatch) { const src = srcMatch[1] || srcMatch[2] || srcMatch[3] js += `import ${JSON.stringify(src)}\n` @@ -480,7 +480,7 @@ function esbuildScanPlugin( const virtualModulePath = JSON.stringify(virtualModulePrefix + key) - const contextMatch = openTag.match(contextRE) + const contextMatch = contextRE.exec(openTag) const context = contextMatch && (contextMatch[1] || contextMatch[2] || contextMatch[3]) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 200262ffdd0427..3218ccf5321ee6 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -195,7 +195,7 @@ const inlineCSSRE = /[?&]inline-css\b/ const styleAttrRE = /[?&]style-attr\b/ const functionCallRE = /^[A-Z_][\w-]*\(/i const transformOnlyRE = /[?&]transform-only\b/ -const nonEscapedDoubleQuoteRe = /(? { - const match = e.match(esRE) + const match = esRE.exec(e) if (!match) return e const year = Number(match[1]) if (!esMap[year]) throw new Error(`Unsupported target "${e}"`) diff --git a/packages/vite/src/node/plugins/dataUri.ts b/packages/vite/src/node/plugins/dataUri.ts index 36c5c0b28b731c..c2a71a88ca14d1 100644 --- a/packages/vite/src/node/plugins/dataUri.ts +++ b/packages/vite/src/node/plugins/dataUri.ts @@ -31,7 +31,7 @@ export function dataURIPlugin(): Plugin { return } - const match = uri.pathname.match(dataUriRE) + const match = dataUriRE.exec(uri.pathname) if (!match) { return } diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 54f1796afd6381..fda6ca02a8baa7 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -28,7 +28,7 @@ const debug = createDebugger('vite:esbuild') // IIFE content looks like `var MyLib = function() {`. // Spaces are removed and parameters are mangled when minified const IIFE_BEGIN_RE = - /(const|var)\s+\S+\s*=\s*function\([^()]*\)\s*\{\s*"use strict";/ + /(?:const|var)\s+\S+\s*=\s*function\([^()]*\)\s*\{\s*"use strict";/ const validExtensionRE = /\.\w+$/ const jsxExtensionsRE = /\.(?:j|t)sx\b/ diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 04fd6268e95a35..b7109debc3863a 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -47,7 +47,7 @@ interface ScriptAssetsUrl { } const htmlProxyRE = - /\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(js|css)$/ + /\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(?:js|css)$/ const isHtmlProxyRE = /\?html-proxy\b/ const inlineCSSRE = /__VITE_INLINE_CSS__([a-z\d]{8}_\d+)__/g @@ -99,7 +99,7 @@ export function htmlInlineProxyPlugin(config: ResolvedConfig): Plugin { }, load(id) { - const proxyMatch = id.match(htmlProxyRE) + const proxyMatch = htmlProxyRE.exec(id) if (proxyMatch) { const index = Number(proxyMatch[1]) const file = cleanUrl(id) @@ -237,7 +237,7 @@ export function overwriteAttrValue( sourceCodeLocation.startOffset, sourceCodeLocation.endOffset, ) - const valueStart = srcString.match(attrValueStartRE) + const valueStart = attrValueStartRE.exec(srcString) if (!valueStart) { // overwrite attr value can only be called for a well-defined value throw new Error( @@ -1354,7 +1354,7 @@ export async function applyHtmlTransforms( return html } -const importRE = /\bimport\s*("[^"]*[^\\]"|'[^']*[^\\]');*/g +const importRE = /\bimport\s*(?:"[^"]*[^\\]"|'[^']*[^\\]');*/g const commentRE = /\/\*[\s\S]*?\*\/|\/\/.*$/gm function isEntirelyImport(code: string) { // only consider "side-effect" imports, which match