Skip to content

Commit

Permalink
feat: introduce dumpUnimportItems option
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 19, 2024
1 parent 13851ba commit a4e4332
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ dist
.idea
auto-imports.d.ts
.eslintrc-auto-import.json

.vscode
.unimport-items.json
.vscode
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ AutoImport({
enabled: false, // Default `false`
filepath: './.biomelintrc-auto-import.json', // Default `./.biomelintrc-auto-import.json`
},

// Save unimport items into a JSON file for other tools to consume
dumpUnimportItems: './auto-imports.json', // Default `false`
})
```

Expand Down
1 change: 1 addition & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineConfig({
return normalizeImportFrom.includes('/directives/')
},
},
dumpUnimportItems: true,
}),
],
})
19 changes: 19 additions & 0 deletions src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export function createContext(options: Options = {}, root = process.cwd()) {
biomelintrc.enabled = biomelintrc.enabled !== undefined
biomelintrc.filepath = biomelintrc.filepath || './.biomelintrc-auto-import.json'

const dumpUnimportItems = options.dumpUnimportItems === true
? './.unimport-items.json'
: options.dumpUnimportItems ?? false

const resolvers = options.resolvers ? [options.resolvers].flat(2) : []

// When "options.injectAtEnd" is undefined or true, it's true.
Expand Down Expand Up @@ -201,6 +205,7 @@ ${dts}`.trim()}\n`
let lastDTS: string | undefined
let lastESLint: string | undefined
let lastBiomeLint: string | undefined
let lastUnimportItems: string | undefined

async function writeConfigFiles() {
const promises: any[] = []
Expand Down Expand Up @@ -243,6 +248,20 @@ ${dts}`.trim()}\n`
)
}

if (dumpUnimportItems) {
promises.push(
unimport.getImports().then((items) => {
if (!dumpUnimportItems)
return
const content = JSON.stringify(items, null, 2)
if (content !== lastUnimportItems) {
lastUnimportItems = content
return writeFile(dumpUnimportItems, content)
}
}),
)
}

return Promise.all(promises)
}

Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ export interface Options {
*/
biomelintrc?: BiomeLintrc

/**
* Save unimport items into a JSON file for other tools to consume.
* Provide a filepath to save the JSON file.
*
* When set to `true`, it will save to `./.unimport-items.json`
*
* @default false
*/
dumpUnimportItems?: boolean | string

/**
* Include auto-imported packages in Vite's `optimizeDeps` option
*
Expand Down

0 comments on commit a4e4332

Please sign in to comment.