Skip to content

Commit

Permalink
feat: include nitro unimport to globals as well, close #461
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 27, 2024
1 parent 77bc797 commit 533fa40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions packages/module/src/modules/config/addons/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@ import type { Linter } from 'eslint'
import type { ESLintConfigGenAddon } from '../../../types'

export function createAddonGlobals(nuxt: Nuxt): ESLintConfigGenAddon {
let unimport: Unimport
let unimport: Unimport | undefined
let nitroUnimport: Unimport | undefined

nuxt.hook('imports:context', (context) => {
unimport = context
})

nuxt.hook('nitro:init', (nitro) => {
nitroUnimport = nitro.unimport
})

return {
name: 'nuxt:eslint:import-globals',
async getConfigs() {
const imports = [
...await unimport?.getImports() || [],
...await nitroUnimport?.getImports() || [],
]

return {
configs: [
'// Set globals from imports registry\n'
+ JSON.stringify(<Linter.Config>{
'// Set globals from imports registry',
JSON.stringify(<Linter.Config>{
name: 'nuxt/import-globals',
languageOptions: {
globals: Object.fromEntries((await unimport.getImports()).map(i => [i.as || i.name, 'readonly'])),
globals: Object.fromEntries(imports.map(i => [i.as || i.name, 'readonly'])),
},
}),
],
Expand Down
2 changes: 1 addition & 1 deletion packages/module/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface ESLintConfigGenAddonResult {
*/
imports?: Import[]
/**
* Flat config items, should be stringified
* Flat config items, should be stringified lines
*/
configs?: string[]
}
Expand Down

0 comments on commit 533fa40

Please sign in to comment.