fix(es/minifier): Preserve cooked while compressing template literals (#7773)

**Related issue:**

 - Closes #7770
This commit is contained in:
Donny/강동윤 2023-08-17 13:35:07 +09:00 committed by GitHub
parent 05200ad352
commit 05990a98fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 5 deletions

View File

@ -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(),
}); });

View File

@ -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
`;

View File

@ -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
`;

View File

@ -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;
}
`;

View File

@ -0,0 +1,13 @@
exports.MainCSS = `
.ThisshouldOnlyBeonTop {
}
.abcBlablaOne .asdsad {
}
.aasdasdasd .asdsada {
we: asdasd !important;
}
`;