Shahryar
Shahryar
SCSWC Community
Created by Shahryar on 2/21/2025 in #questions
How can keep css comments after codegen?
Hello friends, in rust I can not be able to keep comments for a css file or content for example as you see I return the comments in lexer Some(&comments), but it deletes the comments
pub fn parse(
file_content: &str,
) -> Result<(Stylesheet, SingleThreadedComments, Lrc<SourceMap>), String> {
let cm: Lrc<SourceMap> = Default::default();
let fm = cm.new_source_file(FileName::Anon.into(), file_content.into());

let comments = SingleThreadedComments::default();

let lexer = Lexer::new(
SourceFileInput::from(&*fm),
Some(&comments),
ParserConfig::default(),
);

let mut parser = Parser::new(lexer, ParserConfig::default());

let stylesheet = match parser.parse_all() {
Ok(s) => s,
Err(_) => {
return Err("Failed to parse CSS".to_string());
}
};

Ok((stylesheet, comments, cm))
}
pub fn parse(
file_content: &str,
) -> Result<(Stylesheet, SingleThreadedComments, Lrc<SourceMap>), String> {
let cm: Lrc<SourceMap> = Default::default();
let fm = cm.new_source_file(FileName::Anon.into(), file_content.into());

let comments = SingleThreadedComments::default();

let lexer = Lexer::new(
SourceFileInput::from(&*fm),
Some(&comments),
ParserConfig::default(),
);

let mut parser = Parser::new(lexer, ParserConfig::default());

let stylesheet = match parser.parse_all() {
Ok(s) => s,
Err(_) => {
return Err("Failed to parse CSS".to_string());
}
};

Ok((stylesheet, comments, cm))
}
my css codegen:
let mut output = String::new();
{
let mut writer = BasicCssWriter::new(&mut output, None, Default::default());
let mut gen = CodeGenerator::new(&mut writer, CodegenConfig { minify: false });
gen.emit(&stylesheet).expect("Failed to generate CSS");
}

Ok(output)
let mut output = String::new();
{
let mut writer = BasicCssWriter::new(&mut output, None, Default::default());
let mut gen = CodeGenerator::new(&mut writer, CodegenConfig { minify: false });
gen.emit(&stylesheet).expect("Failed to generate CSS");
}

Ok(output)
Thank you in advance --- Example:
let css_code = r#"
@import "reset.css";
/* This file is for your main application CSS */
body { color: black; }
"#;

let new_imports = r#"
@import "theme.css";
@import "reset.css";
@import "custom.css";
"#;

let result = insert_import_to_ast(css_code, new_imports).unwrap();
println!("{}", result);
let css_code = r#"
@import "reset.css";
/* This file is for your main application CSS */
body { color: black; }
"#;

let new_imports = r#"
@import "theme.css";
@import "reset.css";
@import "custom.css";
"#;

let result = insert_import_to_ast(css_code, new_imports).unwrap();
println!("{}", result);
#### Returns
@import "theme.css";
@import "custom.css";
@import "reset.css";
body {
color: black;
}
@import "theme.css";
@import "custom.css";
@import "reset.css";
body {
color: black;
}
3 replies
SCSWC Community
Created by Shahryar on 1/12/2025 in #questions
Emitter in SWC does not add expected space after '=>' in Arrow Functions
Hello friends, I am new to rust and SWC, I hope I am not wasting your time 🥹🙏🏻 I edited with extra space.
// This is first test we need to have
console.log("We are here");
// This is another comment
const add = () => {
// This is inner content
return "Test" + "Test";
};


const min = () => {return "Test" + "Test"};
// This is first test we need to have
console.log("We are here");
// This is another comment
const add = () => {
// This is inner content
return "Test" + "Test";
};


const min = () => {return "Test" + "Test"};
Now I want to edit the add to adds, I is more than 2000 character and discord dose not let me send the code, so I put it in gist: https://gist.github.com/shahryarjb/6f314fa7fab85fb09751bb8146c919f3 My problem is it returns
// This is first test we need to have
console.log("We are here");
// This is another comment
const adds = ()=>{
// This is inner content
return "Shahryar" + "Tavakkoli";
};
const min = ()=>{
return "Shahryar" + "Tavakkoli";
};
// This is first test we need to have
console.log("We are here");
// This is another comment
const adds = ()=>{
// This is inner content
return "Shahryar" + "Tavakkoli";
};
const min = ()=>{
return "Shahryar" + "Tavakkoli";
};
but I need to have an space after => and before{} like this
const adds = () => {
// This is inner content
return "Test" + "Test";
};
const adds = () => {
// This is inner content
return "Test" + "Test";
};
how can do it? Thank you in advance
8 replies
SCSWC Community
Created by Shahryar on 1/10/2025 in #questions
Does SWC remove comments in the code when transforming a JS file's AST and converting it back to a c
Hello friends, I'm quite new to Rust. Recently, I used OXC in my Elixir project with NIF to modify JS files in my CLI. Unfortunately, after making changes to the JS file, all the comments above functions or within the code are removed, which is a major issue for me. Before diving into SWC, I wanted to ask if this library exhibits the same behavior. My next question is whether SWC supports converting the entire AST to JSON and vice versa, particularly from JSON back to AST. My current requirement is focused on JS files. Thanks in advance!
6 replies