Skip to content

Commit 62edb36

Browse files
authoredOct 8, 2024··
feat(bindings/html): Accept Buffer|string instead of Buffer (#9625)
1 parent 6a3b0fc commit 62edb36

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed
 

‎bindings/binding_html_node/src/lib.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,21 @@ fn minify_inner(
694694
})
695695
}
696696

697+
fn to_string(code: Either<Buffer, String>) -> String {
698+
match code {
699+
Either::A(code) => String::from_utf8_lossy(code.as_ref()).to_string(),
700+
Either::B(code) => code,
701+
}
702+
}
703+
697704
#[allow(unused)]
698705
#[napi]
699-
fn minify(code: Buffer, opts: Buffer, signal: Option<AbortSignal>) -> AsyncTask<MinifyTask> {
700-
let code = String::from_utf8_lossy(code.as_ref()).to_string();
706+
fn minify(
707+
code: Either<Buffer, String>,
708+
opts: Buffer,
709+
signal: Option<AbortSignal>,
710+
) -> AsyncTask<MinifyTask> {
711+
let code = to_string(code);
701712
let options = String::from_utf8_lossy(opts.as_ref()).to_string();
702713

703714
let task = MinifyTask {
@@ -712,11 +723,11 @@ fn minify(code: Buffer, opts: Buffer, signal: Option<AbortSignal>) -> AsyncTask<
712723
#[allow(unused)]
713724
#[napi]
714725
fn minify_fragment(
715-
code: Buffer,
726+
code: Either<Buffer, String>,
716727
opts: Buffer,
717728
signal: Option<AbortSignal>,
718729
) -> AsyncTask<MinifyTask> {
719-
let code = String::from_utf8_lossy(code.as_ref()).to_string();
730+
let code = to_string(code);
720731
let options = String::from_utf8_lossy(opts.as_ref()).to_string();
721732

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

731742
#[allow(unused)]
732743
#[napi]
733-
pub fn minify_sync(code: Buffer, opts: Buffer) -> napi::Result<TransformOutput> {
734-
let code = String::from_utf8_lossy(code.as_ref());
744+
pub fn minify_sync(code: Either<Buffer, String>, opts: Buffer) -> napi::Result<TransformOutput> {
745+
let code = to_string(code);
735746
let options = get_deserialized(opts)?;
736747

737748
minify_inner(&code, options, false).convert_err()
738749
}
739750

740751
#[allow(unused)]
741752
#[napi]
742-
pub fn minify_fragment_sync(code: Buffer, opts: Buffer) -> napi::Result<TransformOutput> {
743-
let code = String::from_utf8_lossy(code.as_ref());
753+
pub fn minify_fragment_sync(
754+
code: Either<Buffer, String>,
755+
opts: Buffer,
756+
) -> napi::Result<TransformOutput> {
757+
let code = to_string(code);
744758
let options = get_deserialized(opts)?;
745759

746760
minify_inner(&code, options, true).convert_err()

‎packages/html/binding.d.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,26 @@ export interface Element {
2222
}
2323

2424
export declare function minify(
25-
code: Buffer,
25+
code: Buffer | string,
2626
opts: Buffer,
2727
signal?: AbortSignal | undefined | null
2828
): Promise<TransformOutput>;
2929

3030
export declare function minifyFragment(
31-
code: Buffer,
31+
code: Buffer | string,
3232
opts: Buffer,
3333
signal?: AbortSignal | undefined | null
3434
): Promise<TransformOutput>;
3535

3636
export declare function minifyFragmentSync(
37-
code: Buffer,
37+
code: Buffer | string,
3838
opts: Buffer
3939
): TransformOutput;
4040

41-
export declare function minifySync(code: Buffer, opts: Buffer): TransformOutput;
41+
export declare function minifySync(
42+
code: Buffer | string,
43+
opts: Buffer
44+
): TransformOutput;
4245

4346
export interface TransformOutput {
4447
code: string;

‎packages/html/index.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export type Options = {
1919
preserveComments?: string[];
2020
minifyConditionalComments?: boolean;
2121
removeEmptyAttributes?: boolean;
22-
removeRedundantAttributes?:
23-
| "none"
24-
| "all"
25-
| "smart";
22+
removeRedundantAttributes?: "none" | "all" | "smart";
2623
collapseBooleanAttributes?: boolean;
2724
normalizeAttributes?: boolean;
2825
minifyJson?: boolean | { pretty?: boolean };
@@ -48,28 +45,28 @@ export type FragmentOptions = Options & {
4845
};
4946

5047
export async function minify(
51-
content: Buffer,
48+
content: string | Buffer,
5249
options?: Options
5350
): Promise<binding.TransformOutput> {
5451
return binding.minify(content, toBuffer(options ?? {}));
5552
}
5653

5754
export async function minifyFragment(
58-
content: Buffer,
55+
content: string | Buffer,
5956
options?: FragmentOptions
6057
): Promise<binding.TransformOutput> {
6158
return binding.minifyFragment(content, toBuffer(options ?? {}));
6259
}
6360

6461
export function minifySync(
65-
content: Buffer,
62+
content: string | Buffer,
6663
options?: Options
6764
): binding.TransformOutput {
6865
return binding.minifySync(content, toBuffer(options ?? {}));
6966
}
7067

7168
export async function minifyFragmentSync(
72-
content: Buffer,
69+
content: string | Buffer,
7370
options?: FragmentOptions
7471
): Promise<binding.TransformOutput> {
7572
return binding.minifyFragmentSync(content, toBuffer(options ?? {}));

‎packages/html/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"prepack": "tsc -d && napi prepublish -p scripts/npm --tag-style npm",
4646
"prepublishOnly": "tsc -d && napi prepublish -p scripts/npm --tagstyle npm",
4747
"build:ts": "tsc -d",
48-
"build": "tsc -d && napi build --manifest-path ../../bindings/Cargo.toml --platform -p binding_html_node --js ./binding.js --dts ./binding.d.ts --release -o .",
49-
"build:dev": "tsc -d && napi build --manifest-path ../../bindings/Cargo.toml --platform -p binding_html_node --js ./binding.js --dts ./binding.d.ts -o .",
48+
"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 .",
49+
"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 .",
5050
"test": "echo 'done!'",
5151
"version": "napi version --npm-dir scripts/npm"
5252
},

0 commit comments

Comments
 (0)
Please sign in to comment.