-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
options.ts
137 lines (128 loc) · 2.96 KB
/
options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import type { WatchOptions } from 'chokidar'
type MaybePromise<T> = T | Promise<T>
export type RenameFunc = (
fileName: string,
fileExtension: string,
fullPath: string
) => MaybePromise<string>
/**
* @param content content of file
* @param filename absolute path to the file
* @returns the transformed content. when `null` is returned, the file won't be created.
*/
export type TransformFunc<T extends string | Buffer> = (
content: T,
filename: string
) => MaybePromise<T | null>
export type TransformOptionObject =
| {
encoding: Exclude<BufferEncoding, 'binary'>
handler: TransformFunc<string>
}
| {
encoding: 'buffer'
handler: TransformFunc<Buffer>
}
export type TransformOption = TransformFunc<string> | TransformOptionObject
export type Target = {
/**
* path or glob
*/
src: string | string[]
/**
* destination
*/
dest: string
/**
* rename
*/
rename?: string | RenameFunc
/**
* transform
*
* `src` should only include files when this option is used
*/
transform?: TransformOption
/**
* Should timestamps on copied files be preserved?
*
* When false, timestamp behavior is OS-dependent.
* Ignored for transformed files.
* @default false
*/
preserveTimestamps?: boolean
/**
* Whether to dereference symlinks.
*
* When true, symlinks will be dereferenced.
* When false, symlinks will not be dereferenced.
* @default true
*/
dereference?: boolean
/**
* Whether to overwrite existing file or directory.
*
* When true, it will overwrite existing file or directory.
* When false, it will skip those files/directories.
* When 'error', it will throw an error.
*
* @default true
*/
overwrite?: boolean | 'error'
}
export type ViteStaticCopyOptions = {
/**
* Array of targets to copy.
*/
targets: Target[]
/**
* Preserve the directory structure.
*
* Similar to `flatten: false` in rollup-plugin-copy
* @default false
*/
structured?: boolean
/**
* Suppress console output and ignore validation errors.
* @default false
*/
silent?: boolean
watch?: {
/**
* Watch options
*/
options?: WatchOptions
/**
* Reloads page on file change when true
* @default false
*/
reloadPageOnChange?: boolean
}
/**
* Rollup hook the plugin should use during build.
* @default 'writeBundle'
*/
hook?: string
}
export type ResolvedViteStaticCopyOptions = {
targets: Target[]
structured: boolean
silent: boolean
watch: {
options: WatchOptions
reloadPageOnChange: boolean
}
hook: string
}
export const resolveOptions = (
options: ViteStaticCopyOptions
): ResolvedViteStaticCopyOptions => ({
targets: options.targets,
structured: options.structured ?? false,
silent: options.silent ?? false,
watch: {
options: options.watch?.options ?? {},
reloadPageOnChange: options.watch?.reloadPageOnChange ?? false
},
hook: options.hook ?? 'writeBundle'
})