Skip to content

Commit

Permalink
feat: introduce viteOptimizeDeps
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 11, 2024
1 parent 11b29cf commit 8aaec26
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ AutoImport({
/* ... */
],

// Include auto-imported packages in Vite's `optimizeDeps` options
// Recommend to enable
viteOptimizeDeps: false,

// Inject the imports at the end of other imports
injectAtEnd: true,

Expand Down
1 change: 1 addition & 0 deletions src/core/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ ${dts}`.trim()}\n`
transform,
generateDTS,
generateESLint,
unimport,
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/core/unplugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ export default createUnplugin<Options>((options) => {
await ctx.writeConfigFiles()
},
vite: {
async config(config) {
if (!options.viteOptimizeDeps)
return

const exclude = config.optimizeDeps?.exclude || []

const imports = new Set((await ctx.unimport.getImports()).map(i => i.from)
.filter(i => i.match(/^[a-z@]/) && !exclude.includes(i)))

if (!imports.size)
return

return {
optimizeDeps: {
include: [...imports],
},
}
},
async handleHotUpdate({ file }) {
if (ctx.dirs?.some(glob => minimatch(slash(file), slash(glob))))
await ctx.scanDirs()
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ export interface Options {
* Generate corresponding .eslintrc-auto-import.json file.
*/
eslintrc?: ESLintrc

/**
* Include auto-imported packages in Vite's `optimizeDeps` option
*
* @default false
*/
viteOptimizeDeps?: boolean
}

export { PresetName }
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"target": "es2018",
"jsx": "preserve",
"lib": ["esnext"],
"module": "esnext",
Expand Down

0 comments on commit 8aaec26

Please sign in to comment.