Skip to content

Commit

Permalink
feat(html/minifier): Add remove_redundant_attributes (#5023)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 22, 2022
1 parent c7c1f63 commit 03d8a3a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
37 changes: 21 additions & 16 deletions crates/swc_html_minifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ struct Minifier {
collapse_whitespaces: Option<CollapseWhitespaces>,

remove_empty_attributes: bool,
remove_redundant_attributes: bool,
collapse_boolean_attributes: bool,
minify_json: bool,
minify_js: bool,
Expand Down Expand Up @@ -909,6 +910,7 @@ impl Minifier {
force_set_html5_doctype: self.force_set_html5_doctype,
collapse_whitespaces: self.collapse_whitespaces.clone(),
remove_empty_attributes: self.remove_empty_attributes,
remove_redundant_attributes: self.remove_empty_attributes,
collapse_boolean_attributes: self.collapse_boolean_attributes,
minify_js: self.minify_js,
minify_json: self.minify_json,
Expand Down Expand Up @@ -1005,23 +1007,25 @@ impl VisitMut for Minifier {
return true;
}

if self.is_default_attribute_value(
n.namespace,
&n.tag_name,
&attribute.name,
match &*n.tag_name {
"script" if matches!(n.namespace, Namespace::HTML | Namespace::SVG) => {
let original_value = attribute.value.as_ref().unwrap();

if let Some(next) = original_value.split(';').next() {
next
} else {
original_value
if self.remove_redundant_attributes
&& self.is_default_attribute_value(
n.namespace,
&n.tag_name,
&attribute.name,
match &*n.tag_name {
"script" if matches!(n.namespace, Namespace::HTML | Namespace::SVG) => {
let original_value = attribute.value.as_ref().unwrap();

if let Some(next) = original_value.split(';').next() {
next
} else {
original_value
}
}
}
_ => attribute.value.as_ref().unwrap(),
},
) {
_ => attribute.value.as_ref().unwrap(),
},
)
{
return false;
}

Expand Down Expand Up @@ -1321,6 +1325,7 @@ fn create_minifier(context_element: Option<&Element>, options: &MinifyOptions) -
collapse_whitespaces: options.collapse_whitespaces.clone(),

remove_empty_attributes: options.remove_empty_attributes,
remove_redundant_attributes: options.remove_redundant_attributes,
collapse_boolean_attributes: options.collapse_boolean_attributes,

minify_js: options.minify_js,
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_html_minifier/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct MinifyOptions {
#[serde(default = "true_by_default")]
pub remove_empty_attributes: bool,
#[serde(default = "true_by_default")]
pub remove_redundant_attributes: bool,
#[serde(default = "true_by_default")]
pub collapse_boolean_attributes: bool,
#[serde(default = "true_by_default")]
pub minify_js: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"removeRedundantAttributes": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
<link rel="stylesheet" href="a.css">
<link rel="stylesheet" href="b.css" type="text/css">
<link rel="stylesheet" href="b.css" type="TEXT/CSS">
<link rel="stylesheet" href="c.css" type=" text/css ">
<link rel="stylesheet" href="d.css" type="">
<link rel="stylesheet" href="d.css" type="unknown/unknown">
</head>
<body>
<div>test</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!doctype html><html lang=en><title>Document</title><link rel=stylesheet href=a.css><link rel=stylesheet href=b.css type=text/css><link rel=stylesheet href=b.css type=TEXT/CSS><link rel=stylesheet href=c.css type=" text/css "><link rel=stylesheet href=d.css type=""><link rel=stylesheet href=d.css type=unknown/unknown><body>
<div>test</div>

1 comment on commit 03d8a3a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 03d8a3a Previous: 17c51fa Ratio
es/full/minify/libraries/antd 1618146525 ns/iter (± 7702654) 1731841746 ns/iter (± 45495392) 0.93
es/full/minify/libraries/d3 406834710 ns/iter (± 5405047) 443157766 ns/iter (± 7411315) 0.92
es/full/minify/libraries/echarts 1598170809 ns/iter (± 14541989) 1732131313 ns/iter (± 37519704) 0.92
es/full/minify/libraries/jquery 89103878 ns/iter (± 1302569) 115781881 ns/iter (± 7588106) 0.77
es/full/minify/libraries/lodash 117746226 ns/iter (± 2346764) 145730217 ns/iter (± 4393504) 0.81
es/full/minify/libraries/moment 51564244 ns/iter (± 442836) 65768042 ns/iter (± 3557495) 0.78
es/full/minify/libraries/react 17305172 ns/iter (± 822995) 19936016 ns/iter (± 359616) 0.87
es/full/minify/libraries/terser 595203232 ns/iter (± 12893118) 633708367 ns/iter (± 15582042) 0.94
es/full/minify/libraries/three 540731385 ns/iter (± 6533171) 574230452 ns/iter (± 8732932) 0.94
es/full/minify/libraries/typescript 3457193911 ns/iter (± 34914285) 3682627601 ns/iter (± 54453633) 0.94
es/full/minify/libraries/victory 727574988 ns/iter (± 14087175) 779569075 ns/iter (± 14570329) 0.93
es/full/minify/libraries/vue 144490205 ns/iter (± 1806348) 170539456 ns/iter (± 9833634) 0.85
es/full/codegen/es3 32588 ns/iter (± 779) 33161 ns/iter (± 2009) 0.98
es/full/codegen/es5 32642 ns/iter (± 817) 33423 ns/iter (± 1946) 0.98
es/full/codegen/es2015 32932 ns/iter (± 794) 33013 ns/iter (± 2267) 1.00
es/full/codegen/es2016 32807 ns/iter (± 1466) 33225 ns/iter (± 2309) 0.99
es/full/codegen/es2017 32936 ns/iter (± 444) 33027 ns/iter (± 1114) 1.00
es/full/codegen/es2018 32910 ns/iter (± 622) 32579 ns/iter (± 797) 1.01
es/full/codegen/es2019 33254 ns/iter (± 792) 33386 ns/iter (± 3306) 1.00
es/full/codegen/es2020 32859 ns/iter (± 858) 33126 ns/iter (± 1758) 0.99
es/full/all/es3 183492790 ns/iter (± 8578683) 186893916 ns/iter (± 13577607) 0.98
es/full/all/es5 175076108 ns/iter (± 5277015) 200373672 ns/iter (± 17765138) 0.87
es/full/all/es2015 139171315 ns/iter (± 3765043) 146090867 ns/iter (± 15504210) 0.95
es/full/all/es2016 137326056 ns/iter (± 6764531) 140405064 ns/iter (± 4947429) 0.98
es/full/all/es2017 136490061 ns/iter (± 2282006) 140360398 ns/iter (± 4328503) 0.97
es/full/all/es2018 138268333 ns/iter (± 2977881) 141148328 ns/iter (± 6755038) 0.98
es/full/all/es2019 140918798 ns/iter (± 3433998) 137526347 ns/iter (± 3289707) 1.02
es/full/all/es2020 134618863 ns/iter (± 3283732) 132345338 ns/iter (± 4685046) 1.02
es/full/parser 713521 ns/iter (± 13013) 730611 ns/iter (± 31528) 0.98
es/full/base/fixer 29384 ns/iter (± 558) 30152 ns/iter (± 1444) 0.97
es/full/base/resolver_and_hygiene 87686 ns/iter (± 3628) 90687 ns/iter (± 9704) 0.97
serialization of ast node 207 ns/iter (± 4) 212 ns/iter (± 5) 0.98
serialization of serde 219 ns/iter (± 1) 223 ns/iter (± 13) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.