diff --git a/src/configs/javascript.ts b/src/configs/javascript.ts index cf853b0cfa..804a17249e 100644 --- a/src/configs/javascript.ts +++ b/src/configs/javascript.ts @@ -1,9 +1,31 @@ import type { OptionsIsInEditor, OptionsOverrides, TypedFlatConfigItem } from '../types' - +import { existsSync } from 'node:fs' +import { join } from 'node:path' import globals from 'globals' - import { pluginAntfu, pluginUnusedImports } from '../plugins' +async function loadAutoImports() { + const globals: Record = {} + const paths = [ + './storage/framework/browser-auto-imports.json', + './storage/framework/server-auto-imports.json', + ] + + for (const path of paths) { + if (existsSync(join(process.cwd(), path))) { + try { + const { globals: fileGlobals } = await import(path) + Object.assign(globals, fileGlobals) + } + catch (error) { + console.warn(`Failed to load auto-imports from ${path}:`, error) + } + } + } + + return globals +} + export async function javascript( options: OptionsIsInEditor & OptionsOverrides = {}, ): Promise { @@ -12,6 +34,9 @@ export async function javascript( overrides = {}, } = options + // Load auto-imports + const autoImportGlobals = await loadAutoImports() + return [ { languageOptions: { @@ -20,6 +45,7 @@ export async function javascript( ...globals.browser, ...globals.es2021, ...globals.node, + ...autoImportGlobals, document: 'readonly', navigator: 'readonly', window: 'readonly', diff --git a/src/configs/typescript.ts b/src/configs/typescript.ts index 98e3bbf4f7..e33a1384a8 100644 --- a/src/configs/typescript.ts +++ b/src/configs/typescript.ts @@ -7,12 +7,35 @@ import type { OptionsTypeScriptWithTypes, TypedFlatConfigItem, } from '../types' - +import { existsSync } from 'node:fs' +import { join } from 'node:path' import process from 'node:process' import { GLOB_MARKDOWN, GLOB_TS, GLOB_TSX } from '../globs' import { pluginAntfu } from '../plugins' import { interopDefault, renameRules } from '../utils' +async function loadAutoImports() { + const globals: Record = {} + const paths = [ + './storage/framework/browser-auto-imports.json', + './storage/framework/server-auto-imports.json', + ] + + for (const path of paths) { + if (existsSync(join(process.cwd(), path))) { + try { + const { globals: fileGlobals } = await import(path) + Object.assign(globals, fileGlobals) + } + catch (error) { + console.warn(`Failed to load auto-imports from ${path}:`, error) + } + } + } + + return globals +} + export async function typescript( options: OptionsFiles & OptionsComponentExts & OptionsOverrides & OptionsTypeScriptWithTypes & OptionsTypeScriptParserOptions & OptionsProjectType = {}, ): Promise { @@ -39,6 +62,9 @@ export async function typescript( : undefined const isTypeAware = !!tsconfigPath + // Load auto-imports dynamically + const autoImportGlobals = await loadAutoImports() + const typeAwareRules: TypedFlatConfigItem['rules'] = { 'dot-notation': 'off', 'no-implied-eval': 'off', @@ -76,6 +102,9 @@ export async function typescript( files, ...ignores ? { ignores } : {}, languageOptions: { + globals: { + ...autoImportGlobals, + }, parser: parserTs, parserOptions: { extraFileExtensions: componentExts.map(ext => `.${ext}`), @@ -159,7 +188,6 @@ export async function typescript( 'ts/no-wrapper-object-types': 'error', 'ts/triple-slash-reference': 'off', 'ts/unified-signatures': 'off', - ...(type === 'lib' ? { 'ts/explicit-function-return-type': ['error', {