2022-02-26 09:41:14 +03:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2022-07-22 14:09:25 +03:00
|
|
|
use swc_atoms::Atom;
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_common::{sync::Lrc, FileName, Mark, SourceMap};
|
2021-08-30 08:21:38 +03:00
|
|
|
use swc_ecma_ast::*;
|
|
|
|
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
|
|
|
|
use swc_ecma_minifier::{
|
|
|
|
eval::{EvalResult, Evaluator},
|
|
|
|
marks::Marks,
|
|
|
|
};
|
2022-02-26 09:41:14 +03:00
|
|
|
use swc_ecma_parser::{parse_file_as_expr, parse_file_as_module, EsConfig, Syntax};
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::resolver;
|
2021-08-30 08:21:38 +03:00
|
|
|
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
|
|
|
|
use testing::{assert_eq, DebugUsingDisplay};
|
|
|
|
|
|
|
|
fn eval(module: &str, expr: &str) -> Option<String> {
|
|
|
|
testing::run_test2(false, |cm, _handler| {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, module.to_string());
|
|
|
|
let marks = Marks::new();
|
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let module_ast = parse_file_as_module(
|
|
|
|
&fm,
|
|
|
|
Default::default(),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-08-30 08:21:38 +03:00
|
|
|
|
|
|
|
let expr_ast = {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, expr.to_string());
|
2022-02-26 09:41:14 +03:00
|
|
|
parse_file_as_expr(
|
|
|
|
&fm,
|
2021-08-30 08:21:38 +03:00
|
|
|
Default::default(),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap()
|
2021-08-30 08:21:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut evaluator = Evaluator::new(module_ast, marks);
|
|
|
|
|
|
|
|
let res = evaluator.eval(&expr_ast);
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Some(res) => match res {
|
|
|
|
EvalResult::Lit(l) => match l {
|
|
|
|
swc_ecma_ast::Lit::Str(v) => Ok(Some(v.value.to_string())),
|
|
|
|
swc_ecma_ast::Lit::Bool(v) => Ok(Some(v.value.to_string())),
|
|
|
|
swc_ecma_ast::Lit::Num(v) => Ok(Some(v.value.to_string())),
|
|
|
|
swc_ecma_ast::Lit::Null(_) => Ok(Some("null".into())),
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
EvalResult::Undefined => Ok(Some("undefined".into())),
|
|
|
|
},
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn simple() {
|
|
|
|
assert_eq!(eval("const foo = 4", "foo").unwrap(), "4");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PartialInliner {
|
|
|
|
marks: Marks,
|
|
|
|
eval: Option<Evaluator>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialInliner {
|
|
|
|
fn run_test<F>(src: &str, op: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(Lrc<SourceMap>, Module, &mut PartialInliner),
|
|
|
|
{
|
|
|
|
testing::run_test2(false, |cm, _handler| {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, src.to_string());
|
|
|
|
let marks = Marks::new();
|
|
|
|
|
2022-02-26 09:41:14 +03:00
|
|
|
let mut module = parse_file_as_module(
|
|
|
|
&fm,
|
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-04-26 10:38:50 +03:00
|
|
|
module.visit_mut_with(&mut resolver(Mark::new(), Mark::new(), false));
|
2021-08-30 08:21:38 +03:00
|
|
|
|
|
|
|
let mut inliner = PartialInliner {
|
|
|
|
marks,
|
|
|
|
eval: Default::default(),
|
|
|
|
};
|
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
op(cm, module, &mut inliner);
|
2021-08-30 08:21:38 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expect(src: &str, expected: &str) {
|
|
|
|
PartialInliner::run_test(src, |cm, mut module, inliner| {
|
|
|
|
//
|
|
|
|
module.visit_mut_with(inliner);
|
|
|
|
|
|
|
|
let expected_module = {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, expected.to_string());
|
2022-02-26 09:41:14 +03:00
|
|
|
|
|
|
|
parse_file_as_module(
|
|
|
|
&fm,
|
2021-08-30 08:21:38 +03:00
|
|
|
Syntax::Es(EsConfig {
|
|
|
|
jsx: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
2022-02-26 09:41:14 +03:00
|
|
|
&mut vec![],
|
|
|
|
)
|
|
|
|
.unwrap()
|
2021-08-30 08:21:38 +03:00
|
|
|
};
|
|
|
|
let expected = {
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut emitter = Emitter {
|
|
|
|
cfg: Default::default(),
|
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
|
|
|
wr: Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None)),
|
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(&expected_module).unwrap();
|
|
|
|
}
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let actual = {
|
|
|
|
let mut buf = vec![];
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut emitter = Emitter {
|
|
|
|
cfg: Default::default(),
|
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
2022-01-13 17:06:11 +03:00
|
|
|
wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)),
|
2021-08-30 08:21:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
emitter.emit_module(&module).unwrap();
|
|
|
|
}
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(DebugUsingDisplay(&expected), DebugUsingDisplay(&actual));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VisitMut for PartialInliner {
|
|
|
|
noop_visit_mut_type!();
|
|
|
|
|
|
|
|
fn visit_mut_expr(&mut self, e: &mut Expr) {
|
|
|
|
e.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
if let Some(evaluator) = self.eval.as_mut() {
|
2022-01-28 07:43:29 +03:00
|
|
|
if let Expr::TaggedTpl(tt) = e {
|
2022-05-11 07:36:22 +03:00
|
|
|
if let Expr::Ident(ref tag) = &*tt.tag {
|
|
|
|
if &*tag.sym == "css" {
|
|
|
|
let res = evaluator.eval_tpl(&tt.tpl);
|
|
|
|
|
|
|
|
let res = match res {
|
|
|
|
Some(v) => v,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
EvalResult::Lit(Lit::Str(s)) => {
|
|
|
|
let el = TplElement {
|
|
|
|
span: s.span,
|
|
|
|
tail: true,
|
|
|
|
// TODO possible bug for quotes
|
2022-07-22 14:09:25 +03:00
|
|
|
raw: Atom::new(&*s.value),
|
|
|
|
cooked: Some(Atom::new(&*s.value)),
|
2022-05-11 07:36:22 +03:00
|
|
|
};
|
|
|
|
tt.tpl = Tpl {
|
|
|
|
span: el.span,
|
|
|
|
exprs: Default::default(),
|
|
|
|
quasis: vec![el],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2021-08-30 08:21:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mut_module(&mut self, module: &mut Module) {
|
|
|
|
self.eval = Some(Evaluator::new(module.clone(), self.marks));
|
|
|
|
module.visit_mut_children_with(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_1() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
const color = 'red'
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const foo = css`
|
|
|
|
div {
|
|
|
|
color: ${color};
|
|
|
|
}
|
|
|
|
`
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const color = 'red'
|
|
|
|
|
|
|
|
export const foo = css`
|
|
|
|
div {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_2() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
export default css`
|
|
|
|
div {
|
|
|
|
font-size: 3em;
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
color: ${props.color};
|
|
|
|
}
|
|
|
|
`
|
|
|
|
",
|
|
|
|
"
|
|
|
|
export default css`
|
|
|
|
div {
|
|
|
|
font-size: 3em;
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
color: ${props.color};
|
|
|
|
}
|
|
|
|
`
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_3() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s1 = css`
|
|
|
|
p.${color} {
|
|
|
|
color: ${otherColor};
|
|
|
|
display: ${obj.display};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s1 = css`
|
|
|
|
p.red {
|
|
|
|
color: green;
|
|
|
|
display: block;
|
|
|
|
}
|
2021-11-07 14:46:12 +03:00
|
|
|
`
|
2021-08-30 08:21:38 +03:00
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn partial_4() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s2 = css`
|
|
|
|
p.${color} {
|
|
|
|
color: ${darken(color)};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s2 = css`
|
|
|
|
p.red {
|
|
|
|
color: 'red';
|
|
|
|
}
|
|
|
|
`
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn partial_5() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s3 = css`
|
|
|
|
p.${color} {
|
|
|
|
color: ${darken(color) + 2};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s3 = css`
|
|
|
|
p.red {
|
|
|
|
color: 'red2';
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_6() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s4 = css`
|
|
|
|
@media (min-width: ${mediumScreen}) {
|
|
|
|
p {
|
|
|
|
color: green;
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
color: ${`red`};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export const s4 = css`
|
|
|
|
@media (min-width: 680px) {
|
|
|
|
p {
|
|
|
|
color: green;
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_7() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export default ({ display }) => (
|
|
|
|
css`
|
|
|
|
span {
|
|
|
|
display: ${display ? 'block' : 'none'};
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
2021-11-07 14:46:12 +03:00
|
|
|
|
2021-08-30 08:21:38 +03:00
|
|
|
export default ({ display }) => (
|
|
|
|
css`
|
|
|
|
span {
|
|
|
|
display: ${display ? 'block' : 'none'};
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
2022-01-28 07:43:29 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn partial_8() {
|
|
|
|
PartialInliner::expect(
|
|
|
|
"const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
|
|
|
|
|
|
|
export default ({ display }) => (
|
|
|
|
css`
|
|
|
|
p.${color} {
|
|
|
|
color: ${otherColor};
|
|
|
|
display: ${obj.display};
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
",
|
|
|
|
"
|
|
|
|
const darken = c => c
|
|
|
|
const color = 'red'
|
|
|
|
const otherColor = 'green'
|
|
|
|
const mediumScreen = '680px'
|
|
|
|
const animationDuration = '200ms'
|
|
|
|
const animationName = 'my-cool-animation'
|
|
|
|
const obj = { display: 'block' }
|
|
|
|
|
|
|
|
export default ({ display }) => (
|
|
|
|
css`
|
|
|
|
p.red {
|
|
|
|
color: green;
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
)
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|