Skip to content

Commit 52edc66

Browse files
committed
feat: supported to specify cacheDir and parentDir
1 parent bcf7d1a commit 52edc66

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/vite/index.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,32 @@ export function Download(
1313
url: string,
1414
filename: string,
1515
destination: string,
16+
options?: {
17+
/**
18+
* @default '.cache'
19+
*/
20+
cacheDir?: string
21+
/**
22+
* @default 'public' or `config.publicDir`
23+
*/
24+
parentDir?: false | string
25+
},
1626
): Plugin {
1727
return {
18-
name: `download-${filename}`,
28+
name: `unplugin-fetch-${filename}`,
1929
async configResolved(config) {
30+
let { cacheDir, parentDir } = options || { cacheDir: '.cache', parentDir: config.publicDir }
31+
32+
if (parentDir === false) {
33+
parentDir = undefined
34+
}
35+
2036
const logger = createLogger()
2137

22-
const cacheDir = resolve(join(config.root, '.cache'))
23-
const publicDir = resolve(join(config.root, 'public'))
38+
cacheDir = resolve(join(config.root, cacheDir))
39+
if (parentDir !== false) {
40+
parentDir = resolve(join(config.root, parentDir))
41+
}
2442

2543
try {
2644
// cache
@@ -35,14 +53,12 @@ export function Download(
3553

3654
logger.info(`${filename} downloaded.`)
3755

38-
if (await exists(resolve(join(publicDir, destination, filename)))) {
56+
if (await exists(resolve(join(parentDir as string, destination, filename)))) {
3957
return
4058
}
4159

42-
await mkdir(join(publicDir, destination), { recursive: true }).catch(() => { })
43-
await copyFile(join(cacheDir, destination, filename), join(publicDir, destination, filename))
44-
45-
// TODO: use motion editor to remap emotions
60+
await mkdir(join(parentDir as string, destination), { recursive: true }).catch(() => { })
61+
await copyFile(join(cacheDir, destination, filename), join(parentDir as string, destination, filename))
4662
}
4763
catch (err) {
4864
console.error(err)

0 commit comments

Comments
 (0)