Skip to content

Commit

Permalink
feat(bindings/html): Accept Buffer|string instead of Buffer (#9625)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Oct 8, 2024
1 parent 6a3b0fc commit 62edb36
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
30 changes: 22 additions & 8 deletions bindings/binding_html_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,21 @@ fn minify_inner(
})
}

fn to_string(code: Either<Buffer, String>) -> String {
match code {
Either::A(code) => String::from_utf8_lossy(code.as_ref()).to_string(),
Either::B(code) => code,
}
}

#[allow(unused)]
#[napi]
fn minify(code: Buffer, opts: Buffer, signal: Option<AbortSignal>) -> AsyncTask<MinifyTask> {
let code = String::from_utf8_lossy(code.as_ref()).to_string();
fn minify(
code: Either<Buffer, String>,
opts: Buffer,
signal: Option<AbortSignal>,
) -> AsyncTask<MinifyTask> {
let code = to_string(code);
let options = String::from_utf8_lossy(opts.as_ref()).to_string();

let task = MinifyTask {
Expand All @@ -712,11 +723,11 @@ fn minify(code: Buffer, opts: Buffer, signal: Option<AbortSignal>) -> AsyncTask<
#[allow(unused)]
#[napi]
fn minify_fragment(
code: Buffer,
code: Either<Buffer, String>,
opts: Buffer,
signal: Option<AbortSignal>,
) -> AsyncTask<MinifyTask> {
let code = String::from_utf8_lossy(code.as_ref()).to_string();
let code = to_string(code);
let options = String::from_utf8_lossy(opts.as_ref()).to_string();

let task = MinifyTask {
Expand All @@ -730,17 +741,20 @@ fn minify_fragment(

#[allow(unused)]
#[napi]
pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result<TransformOutput> {
let code = String::from_utf8_lossy(code.as_ref());
pub fn minify_sync(code: Either<Buffer, String>, opts: Buffer) -> napi::Result<TransformOutput> {
let code = to_string(code);
let options = get_deserialized(opts)?;

minify_inner(&code, options, false).convert_err()
}

#[allow(unused)]
#[napi]
pub fn minify_fragment_sync(code: Buffer, opts: Buffer) -> napi::Result<TransformOutput> {
let code = String::from_utf8_lossy(code.as_ref());
pub fn minify_fragment_sync(
code: Either<Buffer, String>,
opts: Buffer,
) -> napi::Result<TransformOutput> {
let code = to_string(code);
let options = get_deserialized(opts)?;

minify_inner(&code, options, true).convert_err()
Expand Down
11 changes: 7 additions & 4 deletions packages/html/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ export interface Element {
}

export declare function minify(
code: Buffer,
code: Buffer | string,
opts: Buffer,
signal?: AbortSignal | undefined | null
): Promise<TransformOutput>;

export declare function minifyFragment(
code: Buffer,
code: Buffer | string,
opts: Buffer,
signal?: AbortSignal | undefined | null
): Promise<TransformOutput>;

export declare function minifyFragmentSync(
code: Buffer,
code: Buffer | string,
opts: Buffer
): TransformOutput;

export declare function minifySync(code: Buffer, opts: Buffer): TransformOutput;
export declare function minifySync(
code: Buffer | string,
opts: Buffer
): TransformOutput;

export interface TransformOutput {
code: string;
Expand Down
13 changes: 5 additions & 8 deletions packages/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export type Options = {
preserveComments?: string[];
minifyConditionalComments?: boolean;
removeEmptyAttributes?: boolean;
removeRedundantAttributes?:
| "none"
| "all"
| "smart";
removeRedundantAttributes?: "none" | "all" | "smart";
collapseBooleanAttributes?: boolean;
normalizeAttributes?: boolean;
minifyJson?: boolean | { pretty?: boolean };
Expand All @@ -48,28 +45,28 @@ export type FragmentOptions = Options & {
};

export async function minify(
content: Buffer,
content: string | Buffer,
options?: Options
): Promise<binding.TransformOutput> {
return binding.minify(content, toBuffer(options ?? {}));
}

export async function minifyFragment(
content: Buffer,
content: string | Buffer,
options?: FragmentOptions
): Promise<binding.TransformOutput> {
return binding.minifyFragment(content, toBuffer(options ?? {}));
}

export function minifySync(
content: Buffer,
content: string | Buffer,
options?: Options
): binding.TransformOutput {
return binding.minifySync(content, toBuffer(options ?? {}));
}

export async function minifyFragmentSync(
content: Buffer,
content: string | Buffer,
options?: FragmentOptions
): Promise<binding.TransformOutput> {
return binding.minifyFragmentSync(content, toBuffer(options ?? {}));
Expand Down
4 changes: 2 additions & 2 deletions packages/html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"prepack": "tsc -d && napi prepublish -p scripts/npm --tag-style npm",
"prepublishOnly": "tsc -d && napi prepublish -p scripts/npm --tagstyle npm",
"build:ts": "tsc -d",
"build": "tsc -d && napi build --manifest-path ../../bindings/Cargo.toml --platform -p binding_html_node --js ./binding.js --dts ./binding.d.ts --release -o .",
"build:dev": "tsc -d && napi build --manifest-path ../../bindings/Cargo.toml --platform -p binding_html_node --js ./binding.js --dts ./binding.d.ts -o .",
"build": "(tsc -d || true) && napi build --manifest-path ../../bindings/Cargo.toml --platform -p binding_html_node --js ./binding.js --dts ./binding.d.ts --release -o .",
"build:dev": "(tsc -d || true) && napi build --manifest-path ../../bindings/Cargo.toml --platform -p binding_html_node --js ./binding.js --dts ./binding.d.ts -o .",
"test": "echo 'done!'",
"version": "napi version --npm-dir scripts/npm"
},
Expand Down

0 comments on commit 62edb36

Please sign in to comment.