Skip to content

Commit 8aaec26

Browse files
committedJul 11, 2024
feat: introduce viteOptimizeDeps
1 parent 11b29cf commit 8aaec26

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ AutoImport({
305305
/* ... */
306306
],
307307

308+
// Include auto-imported packages in Vite's `optimizeDeps` options
309+
// Recommend to enable
310+
viteOptimizeDeps: false,
311+
308312
// Inject the imports at the end of other imports
309313
injectAtEnd: true,
310314

‎src/core/ctx.ts

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ ${dts}`.trim()}\n`
254254
transform,
255255
generateDTS,
256256
generateESLint,
257+
unimport,
257258
}
258259
}
259260

‎src/core/unplugin.ts

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ export default createUnplugin<Options>((options) => {
2222
await ctx.writeConfigFiles()
2323
},
2424
vite: {
25+
async config(config) {
26+
if (!options.viteOptimizeDeps)
27+
return
28+
29+
const exclude = config.optimizeDeps?.exclude || []
30+
31+
const imports = new Set((await ctx.unimport.getImports()).map(i => i.from)
32+
.filter(i => i.match(/^[a-z@]/) && !exclude.includes(i)))
33+
34+
if (!imports.size)
35+
return
36+
37+
return {
38+
optimizeDeps: {
39+
include: [...imports],
40+
},
41+
}
42+
},
2543
async handleHotUpdate({ file }) {
2644
if (ctx.dirs?.some(glob => minimatch(slash(file), slash(glob))))
2745
await ctx.scanDirs()

‎src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ export interface Options {
168168
* Generate corresponding .eslintrc-auto-import.json file.
169169
*/
170170
eslintrc?: ESLintrc
171+
172+
/**
173+
* Include auto-imported packages in Vite's `optimizeDeps` option
174+
*
175+
* @default false
176+
*/
177+
viteOptimizeDeps?: boolean
171178
}
172179

173180
export { PresetName }

‎tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2017",
3+
"target": "es2018",
44
"jsx": "preserve",
55
"lib": ["esnext"],
66
"module": "esnext",

0 commit comments

Comments
 (0)
Please sign in to comment.