Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions crates/swc_ecma_minifier/src/compress/optimize/conditionals.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem::swap;

use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP};
use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::ExprRefExt;
use swc_ecma_transforms_optimization::debug_assert_valid;
Expand Down Expand Up @@ -112,19 +112,24 @@ impl Optimizer<'_> {
return;
}

// we must inline first to avoid https://github.com/swc-project/swc/issues/11517
stmts
.iter_mut()
.filter_map(|s| s.as_stmt_mut().and_then(|s| s.as_mut_if_stmt()))
.for_each(|s| {
self.changed |= self.vars.inline_with_multi_replacer(s);
});

let has_work =
stmts
.windows(2)
.any(|stmts| match (&stmts[0].as_stmt(), &stmts[1].as_stmt()) {
(
Some(Stmt::If(l @ IfStmt { alt: None, .. })),
Some(Stmt::If(r @ IfStmt { alt: None, .. })),
) => {
// We should NOT ignore syntax context here because the cons blocks
// may contain references to local variables with the same name but
// different values. See https://github.com/swc-project/swc/issues/11517
) => SyntaxContext::within_ignored_ctxt(|| {
l.cons.eq_ignore_span(&r.cons) && l.cons.terminates()
}
}),
_ => false,
});
if !has_work {
Expand All @@ -145,12 +150,9 @@ impl Optimizer<'_> {

match &mut cur {
Some(cur_if) => {
// If cons is same, we merge conditions.
// We should NOT ignore syntax context here because the cons
// blocks may contain references to local variables with the
// same name but different values.
// See https://github.com/swc-project/swc/issues/11517
if cur_if.cons.eq_ignore_span(&stmt.cons) {
if SyntaxContext::within_ignored_ctxt(|| {
cur_if.cons.eq_ignore_span(&stmt.cons)
}) {
cur_if.test = BinExpr {
span: DUMMY_SP,
left: cur_if.test.take(),
Expand Down
24 changes: 7 additions & 17 deletions crates/swc_ecma_minifier/tests/benches-full/echarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2383,11 +2383,7 @@
var keyframes = this.keyframes, len = keyframes.length;
if (this.interpolable) if (isArrayLike(value)) {
var value1, arrayDim = isArrayLike((value1 = value) && value1[0]) ? 2 : 1;
if (len > 0 && this.arrDim !== arrayDim) {
this.interpolable = !1;
return;
}
if (1 === arrayDim && 'number' != typeof value[0] || 2 === arrayDim && 'number' != typeof value[0][0]) {
if (len > 0 && this.arrDim !== arrayDim || 1 === arrayDim && 'number' != typeof value[0] || 2 === arrayDim && 'number' != typeof value[0][0]) {
this.interpolable = !1;
return;
}
Expand Down Expand Up @@ -13076,18 +13072,12 @@
globalRect.copy(labelItem.rect), globalRect.width -= 0.1, globalRect.height -= 0.1, globalRect.x += 0.05, globalRect.y += 0.05;
for(var obb = labelItem.obb, overlapped = !1, j = 0; j < displayedLabels.length; j++){
var existsTextCfg = displayedLabels[j]; // Fast rejection.
if (globalRect.intersect(existsTextCfg.rect)) {
if (isAxisAligned && existsTextCfg.axisAligned) {
// Is overlapped
overlapped = !0;
break;
}
if (existsTextCfg.obb || // If self is not axis aligned. But other is.
(existsTextCfg.obb = new OrientedBoundingRect(existsTextCfg.localRect, existsTextCfg.transform)), obb || // If self is axis aligned. But other is not.
(obb = new OrientedBoundingRect(localRect, transform)), obb.intersect(existsTextCfg.obb)) {
overlapped = !0;
break;
}
if (globalRect.intersect(existsTextCfg.rect) && (isAxisAligned && existsTextCfg.axisAligned || (existsTextCfg.obb || // If self is not axis aligned. But other is.
(existsTextCfg.obb = new OrientedBoundingRect(existsTextCfg.localRect, existsTextCfg.transform)), obb || // If self is axis aligned. But other is not.
(obb = new OrientedBoundingRect(localRect, transform)), obb.intersect(existsTextCfg.obb)))) {
// Is overlapped
overlapped = !0;
break;
}
} // TODO Callback to determine if this overlap should be handled?
overlapped ? (hideEl(label), labelLine && hideEl(labelLine)) : (label.attr('ignore', labelItem.defaultAttr.ignore), labelLine && labelLine.attr('ignore', labelItem.defaultAttr.labelGuideIgnore), displayedLabels.push(labelItem));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16479,9 +16479,7 @@
var type = (0, _videojs_vhs_utils_es_containers__WEBPACK_IMPORTED_MODULE_12__ /* .detectContainerForBytes */ .Xm)(bytes); // if this looks like a ts segment but we don't have enough data
return(// to see the second sync byte, wait until we have enough data
// before declaring it ts
"ts" === type && bytes.length < 188 ? callbackOnCompleted(request, function() {
return endRequestAndCallback(error, request, "", bytes);
}) : !type && bytes.length < 376 ? callbackOnCompleted(request, function() {
"ts" === type && bytes.length < 188 || !type && bytes.length < 376 ? callbackOnCompleted(request, function() {
return endRequestAndCallback(error, request, "", bytes);
}) : endRequestAndCallback(null, request, type, bytes) // this may be an unsynced ts segment
);
Expand Down Expand Up @@ -20855,12 +20853,7 @@
case "timed-metadata":
// Exit early because we don't have enough to parse
// the ID3 tag header
if (bytes.length - byteIndex < 10) {
endLoop = !0;
break;
}
// to emit a full packet
if ((frameSize = probe.aac.parseId3TagSize(bytes, byteIndex)) > bytes.length) {
if (bytes.length - byteIndex < 10 || (frameSize = probe.aac.parseId3TagSize(bytes, byteIndex)) > bytes.length) {
endLoop = !0;
break;
}
Expand All @@ -20869,12 +20862,7 @@
case "audio":
// Exit early because we don't have enough to parse
// the ADTS frame header
if (bytes.length - byteIndex < 7) {
endLoop = !0;
break;
}
// to emit a full packet
if ((frameSize = probe.aac.parseAdtsSize(bytes, byteIndex)) > bytes.length) {
if (bytes.length - byteIndex < 7 || (frameSize = probe.aac.parseAdtsSize(bytes, byteIndex)) > bytes.length) {
endLoop = !0;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6889,16 +6889,11 @@
// last char was a lead
if (!leadSurrogate) {
// no lead yet
if (codePoint > 0xdbff) {
if (codePoint > 0xdbff || i + 1 === length) {
// unexpected trail
(units -= 3) > -1 && bytes.push(0xef, 0xbf, 0xbd);
continue;
}
if (i + 1 === length) {
// unpaired lead
(units -= 3) > -1 && bytes.push(0xef, 0xbf, 0xbd);
continue;
}
// valid lead
leadSurrogate = codePoint;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,7 @@
for(var t, f = e.length, n = null, i = [], o = 0; o < f; ++o){
if ((t = e.charCodeAt(o)) > 55295 && t < 57344) {
if (!n) {
if (t > 56319) {
(r -= 3) > -1 && i.push(239, 191, 189);
continue;
}
if (o + 1 === f) {
if (t > 56319 || o + 1 === f) {
(r -= 3) > -1 && i.push(239, 191, 189);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20898,11 +20898,7 @@
for(var r, n = t.length, i = null, o = [], a = 0; a < n; ++a){
if ((r = t.charCodeAt(a)) > 55295 && r < 57344) {
if (!i) {
if (r > 56319) {
(e -= 3) > -1 && o.push(239, 191, 189);
continue;
}
if (a + 1 === n) {
if (r > 56319 || a + 1 === n) {
(e -= 3) > -1 && o.push(239, 191, 189);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11462,16 +11462,11 @@
// last char was a lead
if (!leadSurrogate) {
// no lead yet
if (codePoint > 0xDBFF) {
if (codePoint > 0xDBFF || i + 1 === length) {
// unexpected trail
(units -= 3) > -1 && bytes.push(0xEF, 0xBF, 0xBD);
continue;
}
if (i + 1 === length) {
// unpaired lead
(units -= 3) > -1 && bytes.push(0xEF, 0xBF, 0xBD);
continue;
}
// valid lead
leadSurrogate = codePoint;
continue;
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/tests/libs-size.snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
| --- | --- | --- | --- |
| antd.js | 6.38 MiB | 2.06 MiB | 445.45 KiB |
| d3.js | 542.74 KiB | 261.43 KiB | 85.34 KiB |
| echarts.js | 3.41 MiB | 977.09 KiB | 314.11 KiB |
| echarts.js | 3.41 MiB | 977.04 KiB | 314.11 KiB |
| jquery.js | 280.89 KiB | 87.79 KiB | 30.21 KiB |
| lodash.js | 531.35 KiB | 68.92 KiB | 24.60 KiB |
| moment.js | 169.83 KiB | 57.34 KiB | 18.26 KiB |
| react.js | 70.45 KiB | 22.45 KiB | 8.04 KiB |
| terser.js | 1.08 MiB | 446.62 KiB | 120.49 KiB |
| three.js | 1.19 MiB | 630.55 KiB | 154.77 KiB |
| typescript.js | 10.45 MiB | 3.17 MiB | 840.60 KiB |
| typescript.js | 10.45 MiB | 3.17 MiB | 840.61 KiB |
| victory.js | 2.30 MiB | 694.04 KiB | 154.19 KiB |
| vue.js | 334.13 KiB | 113.56 KiB | 41.81 KiB |
Loading