Skip to content

Commit 569049f

Browse files
committed
fix: parentDir should not join again if it's absolute
1 parent c1e77d7 commit 569049f

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export default defineConfig({
3434
})
3535
```
3636

37+
`cacheDir` and `parentDir` can be absolute paths if you want to share cache across packages (for example `const sharedCacheDir = resolve(join(import.meta.dirname, '..', '..', '.cache'))`). Absolute values are used as-is; relative values are resolved against `config.root`. When `parentDir: false`, assets are copied to `config.root/<destination>` instead of being skipped.
38+
3739
## Other side projects born from Project AIRI
3840

3941
- [Awesome AI VTuber](https://github.com/proj-airi/awesome-ai-vtuber): A curated list of AI VTubers and related projects

src/vite/index.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Plugin } from 'vite'
22

33
import { Buffer } from 'node:buffer'
44
import { copyFile, mkdir, writeFile } from 'node:fs/promises'
5-
import { join, resolve } from 'node:path'
5+
import { isAbsolute, join, resolve } from 'node:path'
66

77
import { ofetch } from 'ofetch'
88
import { createLogger } from 'vite'
@@ -27,18 +27,17 @@ export function Download(
2727
return {
2828
name: `unplugin-fetch-${filename}`,
2929
async configResolved(config) {
30-
let { cacheDir, parentDir } = options || { cacheDir: '.cache', parentDir: config.publicDir }
31-
32-
if (parentDir === false) {
33-
parentDir = undefined
34-
}
35-
3630
const logger = createLogger()
3731

38-
cacheDir = resolve(join(config.root, cacheDir))
39-
if (parentDir !== false) {
40-
parentDir = resolve(join(config.root, parentDir))
41-
}
32+
const cacheDirOption = options?.cacheDir ?? '.cache'
33+
const parentDirOption = options?.parentDir ?? config.publicDir ?? config.root
34+
35+
const cacheDir = isAbsolute(cacheDirOption) ? cacheDirOption : resolve(config.root, cacheDirOption)
36+
const parentDir = parentDirOption === false
37+
? config.root
38+
: isAbsolute(parentDirOption)
39+
? parentDirOption
40+
: resolve(config.root, parentDirOption)
4241

4342
try {
4443
// cache
@@ -53,12 +52,12 @@ export function Download(
5352

5453
logger.info(`${filename} downloaded.`)
5554

56-
if (await exists(resolve(join(parentDir as string, destination, filename)))) {
55+
if (await exists(resolve(join(parentDir, destination, filename)))) {
5756
return
5857
}
5958

60-
await mkdir(join(parentDir as string, destination), { recursive: true }).catch(() => { })
61-
await copyFile(join(cacheDir, destination, filename), join(parentDir as string, destination, filename))
59+
await mkdir(join(parentDir, destination), { recursive: true }).catch(() => { })
60+
await copyFile(join(cacheDir, destination, filename), join(parentDir, destination, filename))
6261
}
6362
catch (err) {
6463
console.error(err)

0 commit comments

Comments
 (0)