This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
132 lines (114 loc) · 3.67 KB
/
types.ts
File metadata and controls
132 lines (114 loc) · 3.67 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
import glob from 'fast-glob';
import rio from '@resolute/rio';
import { Asset, Variant } from './variant.js';
import marko from './marko.js';
import { JsOptions } from './js.js';
// export type RioOptions = NonNullable<Parameters<typeof rio>[0]>;
// export type ImageOptions = NonNullable<Parameters<ReturnType<typeof rio>>[1]>;
// export type ImageTypes = NonNullable<ImageOptions['type']>;
// TODO
// For now, just copying-and-pasting from @resolute/rio and
// @resolute/std...authoring Node packages is a nightmare.
// -- begin paste --
export type MimeTypes = keyof typeof mimeDatabase;
export type MimeExtensions = keyof typeof extDatabase;
const mimeDatabase = {
'text/html': ['html'],
'text/plain': ['txt'],
'text/css': ['css'],
'application/javascript': ['js'],
'application/pdf': ['pdf'],
'font/woff': ['woff'],
'font/woff2': ['woff2'],
'video/mp4': ['mp4'],
'image/avif': ['avif', 'heif'], // libvips reports "avif" as "heif"
'image/webp': ['webp'],
'image/png': ['png'],
'image/jpeg': ['jpg', 'jpeg'],
'image/svg+xml': ['svg'],
'image/vnd.microsoft.icon': ['ico'],
} as const;
const extDatabase = Object.fromEntries(
[...Object.entries(mimeDatabase)]
.map(([mimetype, extensions]) =>
extensions
.map((extension) => [extension, mimetype as MimeTypes] as const)).flat(),
) as { [K in MimeTypes as (typeof mimeDatabase)[K][number]]: K };
export interface RioOptions {
cacheDirectory: string;
cachePrefix: string;
remote?: string;
}
export interface ImageOptions { // extends sharp.ResizeOptions {
type: MimeExtensions;
width: number;
height: number;
fit: 'contain' | 'cover' | 'fill' | 'inside' | 'outside';
position: typeof Positions[keyof typeof Positions];
quality: number;
}
export type ImageTypes = MimeExtensions;
export const Positions = {
center: 'center', // 0
centre: 'center', // 0
top: 'top', // 1
north: 'top', // 1
right: 'right', // 2
east: 'right', // 2
bottom: 'bottom', // 3
south: 'bottom', // 3
left: 'left', // 4
west: 'left', // 4
'right top': 'right top', // 5
northeast: 'right top', // 5
'right bottom': 'right bottom', // 6
southeast: 'right bottom', // 6
'left bottom': 'left bottom', // 7
southwest: 'left bottom', // 7
'left top': 'left top', // 8
northwest: 'left top', // 8
entropy: 'entropy', // 16
attention: 'attention', // 17
} as const;
// -- end paste --
export interface Database {
[slug: string]: Promise<(Asset | Variant)>;
}
export interface WatchOptions {
ignore: (string | RegExp)[];
}
export type RepackTypes = 'js' | 'ts' | 'scss' | 'svg' | 'css' | 'mp4' | 'woff2' | 'woff' | 'pdf' | ImageTypes;
export interface Repack {
(source: string, variantOptions?: any): Promise<Variant>;
run: () => Promise<ReturnType<typeof rio>['stats']>;
all: () => Database;
delete: (filename: string) => Promise<void>;
watch: () => void;
// eslint-disable-next-line no-use-before-define
options: RepackOptions;
}
export interface Handler {
(repack: Repack, handlerOptions?: any): (asset: Asset, variantOptions: any) => Promise<Buffer | (Pick<Variant, 'data'> & Partial<Pick<Variant, 'type' | 'hash' | 'width' | 'height'>>)>;
}
export type HandlerList = [RepackTypes[], Handler][];
export interface RunOptions {
svg: ReturnType<Handler>;
js: ReturnType<Handler>;
css: ReturnType<Handler>;
img: ReturnType<Handler>;
marko: ReturnType<typeof marko>;
glob: typeof glob;
repack: Repack;
}
export interface RepackOptions {
jsonFile: string;
handlers: HandlerList;
src: string | string[];
destDir: string;
baseUri: string;
dev: boolean;
watch: WatchOptions;
rio?: Partial<RioOptions>;
js?: Partial<JsOptions>;
run: (runtime: RunOptions) => Promise<void>;
}