mirror of
https://github.com/swc-project/swc.git
synced 2024-11-24 02:06:08 +03:00
fix(es/minifier): Preserve cooked
while compressing template literals (#7773)
**Related issue:** - Closes #7770
This commit is contained in:
parent
05200ad352
commit
05990a98fd
@ -240,7 +240,7 @@ impl Pure<'_> {
|
|||||||
let mut quasis = vec![];
|
let mut quasis = vec![];
|
||||||
let mut exprs = vec![];
|
let mut exprs = vec![];
|
||||||
let mut cur_raw = String::new();
|
let mut cur_raw = String::new();
|
||||||
let mut cur_cooked = String::new();
|
let mut cur_cooked = Some(String::new());
|
||||||
|
|
||||||
for i in 0..(tpl.exprs.len() + tpl.quasis.len()) {
|
for i in 0..(tpl.exprs.len() + tpl.quasis.len()) {
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
@ -249,8 +249,14 @@ impl Pure<'_> {
|
|||||||
|
|
||||||
cur_raw.push_str(&q.raw);
|
cur_raw.push_str(&q.raw);
|
||||||
if let Some(cooked) = q.cooked {
|
if let Some(cooked) = q.cooked {
|
||||||
|
if let Some(cur_cooked) = &mut cur_cooked {
|
||||||
cur_cooked.push_str(&cooked);
|
cur_cooked.push_str(&cooked);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// If cooked is None, it means that the template literal contains invalid escape
|
||||||
|
// sequences.
|
||||||
|
cur_cooked = None;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let i = i / 2;
|
let i = i / 2;
|
||||||
let e = tpl.exprs[i].take();
|
let e = tpl.exprs[i].take();
|
||||||
@ -258,15 +264,18 @@ impl Pure<'_> {
|
|||||||
match *e {
|
match *e {
|
||||||
Expr::Lit(Lit::Str(s)) => {
|
Expr::Lit(Lit::Str(s)) => {
|
||||||
cur_raw.push_str(&convert_str_value_to_tpl_raw(&s.value));
|
cur_raw.push_str(&convert_str_value_to_tpl_raw(&s.value));
|
||||||
|
if let Some(cur_cooked) = &mut cur_cooked {
|
||||||
cur_cooked.push_str(&convert_str_value_to_tpl_cooked(&s.value));
|
cur_cooked.push_str(&convert_str_value_to_tpl_cooked(&s.value));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
quasis.push(TplElement {
|
quasis.push(TplElement {
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
tail: true,
|
tail: true,
|
||||||
cooked: Some(Atom::from(&*cur_cooked)),
|
cooked: cur_cooked.take().map(From::from),
|
||||||
raw: take(&mut cur_raw).into(),
|
raw: take(&mut cur_raw).into(),
|
||||||
});
|
});
|
||||||
|
cur_cooked = Some(String::new());
|
||||||
|
|
||||||
exprs.push(e);
|
exprs.push(e);
|
||||||
}
|
}
|
||||||
@ -279,7 +288,7 @@ impl Pure<'_> {
|
|||||||
quasis.push(TplElement {
|
quasis.push(TplElement {
|
||||||
span: DUMMY_SP,
|
span: DUMMY_SP,
|
||||||
tail: true,
|
tail: true,
|
||||||
cooked: None,
|
cooked: cur_cooked.map(From::from),
|
||||||
raw: cur_raw.into(),
|
raw: cur_raw.into(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
exports.MainCSS1 = foo`
|
||||||
|
\x<-invalid, so no cooked
|
||||||
|
${'this breaks splits the quasis'}
|
||||||
|
this should have a cooked
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports.MainCSS2 = foo`
|
||||||
|
\x<-invalid, so no cooked
|
||||||
|
${'this breaks splits the quasis'}
|
||||||
|
this should have a cooked
|
||||||
|
${'this breaks splits the quasis'}
|
||||||
|
this should also have a cooked
|
||||||
|
`;
|
@ -0,0 +1,11 @@
|
|||||||
|
exports.MainCSS1 = foo`
|
||||||
|
\x<-invalid, so no cooked
|
||||||
|
${'this breaks splits the quasis'}
|
||||||
|
this should have a cooked
|
||||||
|
`, exports.MainCSS2 = foo`
|
||||||
|
\x<-invalid, so no cooked
|
||||||
|
${'this breaks splits the quasis'}
|
||||||
|
this should have a cooked
|
||||||
|
${'this breaks splits the quasis'}
|
||||||
|
this should also have a cooked
|
||||||
|
`;
|
@ -0,0 +1,20 @@
|
|||||||
|
const sWidth = "asdasd";
|
||||||
|
function absolute() {
|
||||||
|
return `
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
function flex() {
|
||||||
|
return `
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
exports.MainCSS = `
|
||||||
|
.ThisshouldOnlyBeonTop {
|
||||||
|
${absolute()}
|
||||||
|
}
|
||||||
|
.abcBlablaOne .asdsad {
|
||||||
|
${flex()}
|
||||||
|
}
|
||||||
|
.aasdasdasd .asdsada {
|
||||||
|
we: ${sWidth} !important;
|
||||||
|
}
|
||||||
|
`;
|
@ -0,0 +1,13 @@
|
|||||||
|
exports.MainCSS = `
|
||||||
|
.ThisshouldOnlyBeonTop {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
.abcBlablaOne .asdsad {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
.aasdasdasd .asdsada {
|
||||||
|
we: asdasd !important;
|
||||||
|
}
|
||||||
|
`;
|
Loading…
Reference in New Issue
Block a user