swc/packages/html/index.ts
Sébastien Lorber 7b98bb5e93
Some checks failed
CI / Cargo fmt (push) Has been cancelled
CI / Cargo clippy (push) Has been cancelled
CI / Check license of dependencies (push) Has been cancelled
CI / Check (macos-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CI / Test wasm (binding_core_wasm) (push) Has been cancelled
CI / Test wasm (binding_minifier_wasm) (push) Has been cancelled
CI / Test wasm (binding_typescript_wasm) (push) Has been cancelled
CI / List crates (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Has been cancelled
CI / Test with @swc/cli (push) Has been cancelled
CI / Miri (better_scoped_tls) (push) Has been cancelled
CI / Miri (string_enum) (push) Has been cancelled
CI / Miri (swc) (push) Has been cancelled
CI / Miri (swc_bundler) (push) Has been cancelled
CI / Miri (swc_ecma_codegen) (push) Has been cancelled
CI / Miri (swc_ecma_minifier) (push) Has been cancelled
Benchmark / Bench everything (push) Has been cancelled
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Has been cancelled
CI / Done (push) Has been cancelled
fix(html/minifier): Fix HTML minifier TS types (#9615)
**Description:**

 - `preserveComments` is optional but required in the TS type

 - `removeRedundantAttributes` is an enum and passing a boolean leads to errors

See https://github.com/swc-project/swc/blob/main/crates/swc_html_minifier/src/option.rs#L54

**Related issue:**

 - https://github.com/facebook/docusaurus/pull/10554
2024-10-06 18:05:16 +09:00

81 lines
2.3 KiB
TypeScript

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?:
| "none"
| "all"
| "smart";
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
| { lib: "lightningcss" }
| { lib: "swc"; 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));
}