Skip to content

Commit

Permalink
feat: dynamically load stacks auto imports
Browse files Browse the repository at this point in the history
chore: wip
  • Loading branch information
chrisbbreuer committed Jan 21, 2025
1 parent 448662d commit 28c4523
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
30 changes: 28 additions & 2 deletions src/configs/javascript.ts
Original file line number Diff line number Diff line change
@@ -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<string, true> = {}
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<TypedFlatConfigItem[]> {
Expand All @@ -12,6 +34,9 @@ export async function javascript(
overrides = {},
} = options

// Load auto-imports
const autoImportGlobals = await loadAutoImports()

return [
{
languageOptions: {
Expand All @@ -20,6 +45,7 @@ export async function javascript(
...globals.browser,
...globals.es2021,
...globals.node,
...autoImportGlobals,
document: 'readonly',
navigator: 'readonly',
window: 'readonly',
Expand Down
32 changes: 30 additions & 2 deletions src/configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, true> = {}
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<TypedFlatConfigItem[]> {
Expand All @@ -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',
Expand Down Expand Up @@ -76,6 +102,9 @@ export async function typescript(
files,
...ignores ? { ignores } : {},
languageOptions: {
globals: {
...autoImportGlobals,
},
parser: parserTs,
parserOptions: {
extraFileExtensions: componentExts.map(ext => `.${ext}`),
Expand Down Expand Up @@ -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', {
Expand Down

0 comments on commit 28c4523

Please sign in to comment.