Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
passthrough the extension to the Extractor itself
  • Loading branch information
RobinMalfait committed Mar 28, 2025
commit 7dc7878c037fc3909feb5666638c816606745263
12 changes: 7 additions & 5 deletions crates/oxide/src/extractor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ impl fmt::Display for Extracted<'_> {
#[derive(Debug)]
pub struct Extractor<'a> {
cursor: cursor::Cursor<'a>,
extension: Option<&'a str>,

css_variable_machine: CssVariableMachine,
candidate_machine: CandidateMachine,
}

impl<'a> Extractor<'a> {
pub fn new(input: &'a [u8]) -> Self {
pub fn new(input: &'a [u8], extension: Option<&'a str>) -> Self {
Self {
cursor: cursor::Cursor::new(input),
extension,

css_variable_machine: Default::default(),
candidate_machine: Default::default(),
Expand Down Expand Up @@ -208,7 +210,7 @@ mod tests {
}

fn extract_sorted_candidates(input: &str) -> Vec<&str> {
let mut machine = Extractor::new(input.as_bytes());
let mut machine = Extractor::new(input.as_bytes(), None);
let mut actual = machine
.extract()
.iter()
Expand All @@ -222,7 +224,7 @@ mod tests {
}

fn extract_sorted_css_variables(input: &str) -> Vec<&str> {
let mut machine = Extractor::new(input.as_bytes());
let mut machine = Extractor::new(input.as_bytes(), None);
let mut actual = machine
.extract()
.iter()
Expand Down Expand Up @@ -287,12 +289,12 @@ mod tests {
let input = include_bytes!("../fixtures/example.html");

let throughput = Throughput::compute(iterations, input.len(), || {
let mut extractor = Extractor::new(input);
let mut extractor = Extractor::new(input, None);
_ = black_box(extractor.extract());
});
eprintln!("Extractor throughput: {:}", throughput);

let mut extractor = Extractor::new(input);
let mut extractor = Extractor::new(input, None);
let start = std::time::Instant::now();
_ = black_box(extractor.extract().len());
let end = start.elapsed();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxide/src/extractor/pre_processors/pre_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait PreProcessor: Sized + Default {
let processor = Self::default();
let transformed = processor.process(input);

let extracted = Extractor::new(&transformed).extract();
let extracted = Extractor::new(&transformed, None).extract();

// Extract all candidates and css variables.
let candidates = extracted
Expand Down
2 changes: 1 addition & 1 deletion crates/oxide/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tailwindcss_oxide::extractor::{Extracted, Extractor};
use tailwindcss_oxide::throughput::Throughput;

fn run_full_extractor(input: &[u8]) -> Vec<&[u8]> {
Extractor::new(input)
Extractor::new(input, None)
.extract()
.into_iter()
.map(|x| match x {
Expand Down
18 changes: 9 additions & 9 deletions crates/oxide/src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl Scanner {
&mut self,
changed_content: ChangedContent,
) -> Vec<(String, usize)> {
let content = read_changed_content(changed_content).unwrap_or_default();
let (content, extension) = read_changed_content(changed_content).unwrap_or_default();
let original_content = &content;

// Workaround for legacy upgrades:
Expand All @@ -328,7 +328,7 @@ impl Scanner {
let content = content.replace("-[]", "XYZ");
let offset = content.as_ptr() as usize;

let mut extractor = Extractor::new(&content[..]);
let mut extractor = Extractor::new(&content[..], Some(&extension));

extractor
.extract()
Expand All @@ -355,7 +355,7 @@ impl Scanner {
}
}

fn read_changed_content(c: ChangedContent) -> Option<Vec<u8>> {
fn read_changed_content(c: ChangedContent) -> Option<(Vec<u8>, String)> {
let (content, extension) = match c {
ChangedContent::File(file, extension) => match std::fs::read(&file) {
Ok(content) => (content, extension),
Expand All @@ -368,7 +368,7 @@ fn read_changed_content(c: ChangedContent) -> Option<Vec<u8>> {
ChangedContent::Content(contents, extension) => (contents.into_bytes(), extension),
};

Some(pre_process_input(&content, &extension))
Some((pre_process_input(&content, &extension), extension))
}

pub fn pre_process_input(content: &[u8], extension: &str) -> Vec<u8> {
Expand All @@ -389,7 +389,7 @@ pub fn pre_process_input(content: &[u8], extension: &str) -> Vec<u8> {
}

#[tracing::instrument(skip_all)]
fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<Vec<u8>> {
fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<(Vec<u8>, String)> {
event!(
tracing::Level::INFO,
"Reading {:?} file(s)",
Expand All @@ -403,16 +403,16 @@ fn read_all_files(changed_content: Vec<ChangedContent>) -> Vec<Vec<u8>> {
}

#[tracing::instrument(skip_all)]
fn parse_all_blobs(blobs: Vec<Vec<u8>>) -> Vec<String> {
fn parse_all_blobs(blobs: Vec<(Vec<u8>, String)>) -> Vec<String> {
let mut result: Vec<_> = blobs
.par_iter()
.flat_map(|blob| blob.par_split(|x| *x == b'\n'))
.filter_map(|blob| {
.flat_map(|(blob, extension)| blob.par_split(|x| *x == b'\n').map(move |x| (x, extension)))
.filter_map(|(blob, extension)| {
if blob.is_empty() {
return None;
}

let extracted = crate::extractor::Extractor::new(blob).extract();
let extracted = crate::extractor::Extractor::new(blob, Some(extension)).extract();
if extracted.is_empty() {
return None;
}
Expand Down