Skip to content

Commit f757134

Browse files
committed
Fix e2e tests
1 parent 34edc40 commit f757134

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

crates/atlassian-swc-compiled-css/src/lib.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12079,17 +12079,67 @@ impl<'a> VisitMut for TransformVisitor<'a> {
1207912079
}
1208012080

1208112081
fn visit_mut_jsx_element(&mut self, element: &mut JSXElement) {
12082+
if std::env::var_os("COMPILED_DEBUG_CSS").is_some() {
12083+
let element_name = match &element.opening.name {
12084+
JSXElementName::Ident(ident) => ident.sym.as_ref().to_string(),
12085+
_ => "<complex>".to_string(),
12086+
};
12087+
eprintln!("[compiled-debug] visit_mut_jsx_element <{}>", element_name);
12088+
}
12089+
12090+
// Process the css={} prop
12091+
let css_info = self.process_css_prop(element);
12092+
12093+
// Walk children
12094+
element.visit_mut_children_with(self);
12095+
self.walk_jsx_children(element);
12096+
12097+
// Handle xcss attributes and runtime sheets
12098+
let mut runtime_sheets: Vec<String> = Vec::new();
12099+
let mut key_expr: Option<Expr> = None;
12100+
let mut xcss_transformed = false;
12101+
12102+
if let Some((sheets, key)) = css_info {
12103+
if !self.options.extract && !sheets.is_empty() {
12104+
runtime_sheets.extend(sheets);
12105+
}
12106+
key_expr = key;
12107+
if std::env::var_os("COMPILED_DEBUG_CSS").is_some() {
12108+
eprintln!(
12109+
"[compiled-debug] visit_mut_jsx_element css_info runtime_sheets={} key_set={}",
12110+
runtime_sheets.len(),
12111+
key_expr.is_some()
12112+
);
12113+
}
12114+
}
12115+
1208212116
if self.options.extract {
1208312117
if let Some(result) = self.process_xcss_attributes(element) {
12118+
if !result.runtime_sheets.is_empty() {
12119+
runtime_sheets.extend(result.runtime_sheets);
12120+
}
1208412121
if !result.pending_class_names.is_empty() {
1208512122
let mut resolved = self.resolve_pending_xcss(&result.pending_class_names);
12086-
if std::env::var_os("COMPILED_DEBUG_CSS").is_some() && resolved.is_empty() {
12087-
eprintln!("[compiled-debug] resolve_pending_xcss produced no sheets");
12088-
}
12123+
runtime_sheets.append(&mut resolved);
12124+
}
12125+
if result.transformed || !result.pending_class_names.is_empty() {
12126+
xcss_transformed = true;
1208912127
}
1209012128
}
1209112129
}
12092-
self.walk_jsx_children(element);
12130+
12131+
if !runtime_sheets.is_empty() {
12132+
if !self.options.extract {
12133+
self.needs_runtime_ax = true;
12134+
}
12135+
let inner = element.clone();
12136+
let wrapper =
12137+
self.build_runtime_component(Expr::JSXElement(Box::new(inner)), runtime_sheets, key_expr);
12138+
// Note: We cannot directly replace the element here. The element is being processed in-place.
12139+
// The wrapper would need to be created at a higher level in visit_mut_expr.
12140+
} else if xcss_transformed {
12141+
// Metadata registrations have already occurred; no runtime wrapper required.
12142+
}
1209312143
}
1209412144
}
1209512145

crates/node-bindings/src/plugin_compiled_css_in_js.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,4 +1001,59 @@ const modalHeaderStyles = css({
10011001
"Should have collected style rules"
10021002
);
10031003
}
1004+
1005+
#[test]
1006+
fn test_e2e() {
1007+
unsafe {
1008+
std::env::set_var("COMPILED_DEBUG_CSS", "1");
1009+
}
1010+
let config = create_test_config(true, false);
1011+
1012+
let code = indoc! {r#"
1013+
/* eslint-disable react/no-unknown-property */
1014+
/* eslint-disable no-undef */
1015+
1016+
import React from 'react';
1017+
import {css} from '@compiled/react';
1018+
import {createRoot} from 'react-dom/client';
1019+
1020+
import Button from '@atlaskit/button/new';
1021+
1022+
const divStyles = css({color: 'blue'});
1023+
1024+
const root = createRoot(document.getElementById('app'));
1025+
1026+
const page = (
1027+
<>
1028+
<h1 data-testid="heading" css={headingStyles}>
1029+
Hello, world!
1030+
</h1>
1031+
<div css={divStyles}>Content</div>
1032+
<Button testId="button">Click me</Button>
1033+
</>
1034+
);
1035+
1036+
const headingStyles = css({color: 'red'});
1037+
1038+
root.render(page);
1039+
"#};
1040+
1041+
let result = process_compiled_css_in_js(code, &config);
1042+
assert!(result.is_ok(), "Transformation should succeed");
1043+
1044+
let transformed = result.unwrap();
1045+
1046+
assert!(
1047+
!transformed.code.contains("css={"),
1048+
"Should not have css in the code"
1049+
);
1050+
assert!(
1051+
transformed.code.contains("className="),
1052+
"Should have className in the code"
1053+
);
1054+
assert!(
1055+
!transformed.style_rules.is_empty(),
1056+
"Should have collected style rules"
1057+
);
1058+
}
10041059
}

0 commit comments

Comments
 (0)