-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathoptions.ts
More file actions
182 lines (172 loc) · 5.53 KB
/
options.ts
File metadata and controls
182 lines (172 loc) · 5.53 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import type { WatchOptions } from 'chokidar'
type MaybePromise<T> = T | Promise<T>
type WithRequiredSingleKey<T, K extends keyof T> = {
[P in K]-?: Required<T[P]>
} & T
type OptionalRenameObject = { stripBase?: number | true; name?: string }
export type RenameObject =
| WithRequiredSingleKey<OptionalRenameObject, 'stripBase'>
| WithRequiredSingleKey<OptionalRenameObject, 'name'>
export type RenameFunc = (
fileName: string,
fileExtension: string,
fullPath: string,
) => MaybePromise<string | RenameObject>
/**
* @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 path
*
* If a relative path is passed, it will be resolved from `build.outDir`.
*/
dest: string
/**
* Rename the output file.
*
* When a string is provided, it is a shorthand for `{ name: string }`.
*
* When an object is provided:
* - `name`: replaces the file's basename (filename + extension).
* - `stripBase`: the given number of leading directory segments from the matched
* path are stripped from the destination. When `true`, all directory segments
* are stripped (equivalent to flat copy).
* - When both are provided, `stripBase` is applied first, then `name` replaces
* the basename.
*
* When a function is provided, it receives `(fileName, fileExtension, fullPath)`
* and should return the new file name or a `RenameObject`.
* When a string is returned, it is joined with the resolved `dest` directory
* using `path.join`, so it can include path segments (e.g. `subdir/file.txt`)
* or `../` traversals to restructure the output.
* When a `RenameObject` is returned, it is applied the same way as the object
* form above.
*
* @example
* ```js
* // Copies src/pages/events/test.html to dist/events/test.html
* { src: 'src/pages/**\/*.html', dest: 'dist/', rename: { stripBase: 2 } }
* // Copies ../../src/pages/events/test.html to dist/events/test.html
* { src: '../../src/pages/**\/*.html', dest: 'dist/', rename: { stripBase: 2 } }
* // Copies src/pages/events/test.html to dist/test.html
* { src: 'src/pages/**\/*.html', dest: 'dist/', rename: { stripBase: true } }
* // Copies ../../src/pages/events/test.html to dist/test.html
* { src: '../../src/pages/**\/*.html', dest: 'dist/', rename: { stripBase: true } }
* // Copies foo.txt to dist/newname.txt (name only)
* { src: 'foo.txt', dest: 'dist/', rename: { name: 'newname.txt' } }
* // Copies src/pages/events/test.html to dist/events/newname.txt (stripBase + name)
* { src: 'src/pages/**\/*.html', dest: 'dist/', rename: { stripBase: 2, name: 'newname.txt' } }
*
* // Copies src/pages/events/test.html to dist/src/pages/events/test2.html
* { src: 'src/pages/**\/*.html', dest: 'dist/', rename: (name, ext) => `${name}2.${ext}` }
* // Copies src/pages/events/test.html to dist/pages/events/test.html
* { src: 'src/pages/**\/*.html', dest: 'dist/', rename: (name, ext, fullPath) => `../${name}.${ext}` }
* ```
*/
rename?: string | RenameObject | RenameFunc
/**
* Transform file contents before writing.
*/
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[]
/**
* 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
/**
* The environment to run this plugin in.
* @default 'client'
*/
environment?: string
}
export type ResolvedViteStaticCopyOptions = {
targets: Target[]
silent: boolean
watch: {
options: WatchOptions
reloadPageOnChange: boolean
}
hook: string
environment: string
}
export const resolveOptions = (
options: ViteStaticCopyOptions,
): ResolvedViteStaticCopyOptions => ({
targets: options.targets,
silent: options.silent ?? false,
watch: {
options: options.watch?.options ?? {},
reloadPageOnChange: options.watch?.reloadPageOnChange ?? false,
},
hook: options.hook ?? 'writeBundle',
environment: options.environment ?? 'client',
})