-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
74 lines (66 loc) · 2.11 KB
/
index.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
import * as binding from "./binding";
export type MinifierType = "js-module" | "js-script" | "json" | "css" | "html";
export type Options = {
filename?: string;
iframeSrcdoc?: boolean;
scriptingEnabled?: boolean;
forceSetHtml5Doctype?: boolean;
collapseWhitespaces?:
| "none"
| "all"
| "smart"
| "conservative"
| "advanced-conservative"
| "only-metadata";
removeEmptyMetadataElements?: boolean;
removeComments?: boolean;
preserveComments: string[];
minifyConditionalComments?: boolean;
removeEmptyAttributes?: boolean;
removeRedundantAttributes?: boolean;
collapseBooleanAttributes?: boolean;
normalizeAttributes?: boolean;
minifyJson?: boolean | { pretty?: boolean };
// TODO improve me after typing `@swc/css`
minifyJs?: boolean | { parser?: any; minifier?: any; codegen?: any };
minifyCss?: boolean | { parser?: any; minifier?: any; codegen?: any };
minifyAdditionalScriptsContent?: [string, MinifierType][];
minifyAdditionalAttributes?: [string, MinifierType][];
sortSpaceSeparatedAttributeValues?: boolean;
sortAttributes?: boolean;
tagOmission?: boolean;
selfClosingVoidElements?: boolean;
quotes?: boolean;
};
export type FragmentOptions = Options & {
mode?: "no-quirks" | "limited-quirks" | "quirks";
context_element?: binding.Element;
form_element?: binding.Element;
};
export async function minify(
content: Buffer,
options?: Options
): Promise<binding.TransformOutput> {
return binding.minify(content, toBuffer(options ?? {}));
}
export async function minifyFragment(
content: Buffer,
options?: FragmentOptions
): Promise<binding.TransformOutput> {
return binding.minifyFragment(content, toBuffer(options ?? {}));
}
export function minifySync(
content: Buffer,
options?: Options
): binding.TransformOutput {
return binding.minifySync(content, toBuffer(options ?? {}));
}
export async function minifyFragmentSync(
content: Buffer,
options?: FragmentOptions
): Promise<binding.TransformOutput> {
return binding.minifyFragmentSync(content, toBuffer(options ?? {}));
}
function toBuffer(t: any): Buffer {
return Buffer.from(JSON.stringify(t));
}