Skip to content

Commit

Permalink
fix(cache): cache config value (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaobos authored Feb 17, 2023
1 parent 344a120 commit 6114023
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ AutoImport({
// Set `false` to disable.
dts: './auto-imports.d.ts',

// Cache the result of resolving, across multiple vite builds.
// A custom path is supported.
// When set to `true`, the cache will be stored in `node_modules/.cache/unplugin-auto-import.json`.
cache: false,

// Auto import inside Vue template
// see https://github.com/unjs/unimport/pull/15 and https://github.com/unjs/unimport/pull/72
vueTemplate: false,
Expand Down
9 changes: 5 additions & 4 deletions src/core/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, isAbsolute, posix, relative, resolve } from 'path'
import { dirname, isAbsolute, posix, relative, resolve, sep } from 'path'
import { promises as fs } from 'fs'
import { slash, throttle, toArray } from '@antfu/utils'
import { createFilter } from '@rollup/pluginutils'
Expand Down Expand Up @@ -30,7 +30,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {

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

const unimport = createUnimport({
imports: [],
Expand Down Expand Up @@ -159,7 +159,8 @@ ${dts}`.trim()}\n`
const cacheData = await getCacheData(cachePath)
await Promise.allSettled(Object.keys(cacheData).map(async (filePath) => {
try {
await fs.access(posix.resolve(root, filePath))
const normalizeRoot = root.replaceAll(sep, posix.sep)
await fs.access(posix.join(normalizeRoot, filePath))
}
catch {
Reflect.deleteProperty(cacheData, filePath)
Expand All @@ -184,7 +185,7 @@ ${dts}`.trim()}\n`
const cacheData = await getCacheData(cachePath)

if (id && importList) {
const filePath = posix.normalize(relative(root, id))
const filePath = posix.normalize(posix.relative(root, id))
importList = importList.filter(i => (i.name ?? i.as) && i.name !== 'default')
if (importList.length)
cacheData[filePath] = importList
Expand Down
1 change: 1 addition & 0 deletions src/core/unplugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default createUnplugin<Options>((options) => {
ctx.scanDirs(),
ctx.updateCacheImports(),
])
await ctx.writeConfigFiles()
},
async buildEnd() {
await ctx.writeConfigFiles()
Expand Down

0 comments on commit 6114023

Please sign in to comment.