Skip to content

Commit 6114023

Browse files
authoredFeb 17, 2023
fix(cache): cache config value (#324)
1 parent 344a120 commit 6114023

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed
 

‎README.md

+5
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ AutoImport({
242242
// Set `false` to disable.
243243
dts: './auto-imports.d.ts',
244244

245+
// Cache the result of resolving, across multiple vite builds.
246+
// A custom path is supported.
247+
// When set to `true`, the cache will be stored in `node_modules/.cache/unplugin-auto-import.json`.
248+
cache: false,
249+
245250
// Auto import inside Vue template
246251
// see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
247252
vueTemplate: false,

‎src/core/ctx.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, isAbsolute, posix, relative, resolve } from 'path'
1+
import { dirname, isAbsolute, posix, relative, resolve, sep } from 'path'
22
import { promises as fs } from 'fs'
33
import { slash, throttle, toArray } from '@antfu/utils'
44
import { createFilter } from '@rollup/pluginutils'
@@ -30,7 +30,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
3030

3131
const cachePath = isCache === false
3232
? false
33-
: resolve(root, typeof isCache === 'string' ? 'string' : 'node_modules/.cache/unplugin-auto-import.json')
33+
: resolve(root, typeof isCache === 'string' ? isCache : 'node_modules/.cache/unplugin-auto-import.json')
3434

3535
const unimport = createUnimport({
3636
imports: [],
@@ -159,7 +159,8 @@ ${dts}`.trim()}\n`
159159
const cacheData = await getCacheData(cachePath)
160160
await Promise.allSettled(Object.keys(cacheData).map(async (filePath) => {
161161
try {
162-
await fs.access(posix.resolve(root, filePath))
162+
const normalizeRoot = root.replaceAll(sep, posix.sep)
163+
await fs.access(posix.join(normalizeRoot, filePath))
163164
}
164165
catch {
165166
Reflect.deleteProperty(cacheData, filePath)
@@ -184,7 +185,7 @@ ${dts}`.trim()}\n`
184185
const cacheData = await getCacheData(cachePath)
185186

186187
if (id && importList) {
187-
const filePath = posix.normalize(relative(root, id))
188+
const filePath = posix.normalize(posix.relative(root, id))
188189
importList = importList.filter(i => (i.name ?? i.as) && i.name !== 'default')
189190
if (importList.length)
190191
cacheData[filePath] = importList

‎src/core/unplugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default createUnplugin<Options>((options) => {
1818
ctx.scanDirs(),
1919
ctx.updateCacheImports(),
2020
])
21+
await ctx.writeConfigFiles()
2122
},
2223
async buildEnd() {
2324
await ctx.writeConfigFiles()

0 commit comments

Comments
 (0)
Please sign in to comment.