From e768835e98780231a3bb2cd175dc93ca4ba87cc6 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 11 Mar 2025 12:23:23 +0100 Subject: [PATCH 1/2] add JSON pre-processor --- .../src/extractor/pre_processors/json.rs | 63 +++++++++++++++++++ .../oxide/src/extractor/pre_processors/mod.rs | 2 + crates/oxide/src/lib.rs | 1 + 3 files changed, 66 insertions(+) create mode 100644 crates/oxide/src/extractor/pre_processors/json.rs diff --git a/crates/oxide/src/extractor/pre_processors/json.rs b/crates/oxide/src/extractor/pre_processors/json.rs new file mode 100644 index 000000000000..809eb501e679 --- /dev/null +++ b/crates/oxide/src/extractor/pre_processors/json.rs @@ -0,0 +1,63 @@ +use crate::cursor; +use crate::extractor::pre_processors::pre_processor::PreProcessor; + +#[derive(Debug, Default)] +pub struct Json; + +impl PreProcessor for Json { + fn process(&self, content: &[u8]) -> Vec { + let len = content.len(); + let mut result = content.to_vec(); + let mut cursor = cursor::Cursor::new(content); + + while cursor.pos < len { + match cursor.curr { + // Consume strings as-is + b'"' => { + cursor.advance(); + + while cursor.pos < len { + match cursor.curr { + // Escaped character, skip ahead to the next character + b'\\' => cursor.advance_twice(), + + // End of the string + b'"' => break, + + // Everything else is valid + _ => cursor.advance(), + }; + } + } + + // Replace brackets and curlies with spaces + b'[' | b'{' | b']' | b'}' => { + result[cursor.pos] = b' '; + } + + // Consume everything else + _ => {} + }; + + cursor.advance(); + } + + result + } +} + +#[cfg(test)] +mod tests { + use super::Json; + use crate::extractor::pre_processors::pre_processor::PreProcessor; + + #[test] + fn test_json_pre_processor() { + let (input, expected) = ( + r#"[1,[2,[3,4,["flex flex-1 content-['hello_world']"]]], {"flex": true}]"#, + r#" 1, 2, 3,4, "flex flex-1 content-['hello_world']" , "flex": true "#, + ); + + Json::test(input, expected); + } +} diff --git a/crates/oxide/src/extractor/pre_processors/mod.rs b/crates/oxide/src/extractor/pre_processors/mod.rs index 1a7bbdd97fae..133bf345c94b 100644 --- a/crates/oxide/src/extractor/pre_processors/mod.rs +++ b/crates/oxide/src/extractor/pre_processors/mod.rs @@ -1,4 +1,5 @@ pub mod haml; +pub mod json; pub mod pre_processor; pub mod pug; pub mod razor; @@ -7,6 +8,7 @@ pub mod slim; pub mod svelte; pub use haml::*; +pub use json::*; pub use pre_processor::*; pub use pug::*; pub use razor::*; diff --git a/crates/oxide/src/lib.rs b/crates/oxide/src/lib.rs index 1a1daf5d5f84..72cee5942d46 100644 --- a/crates/oxide/src/lib.rs +++ b/crates/oxide/src/lib.rs @@ -470,6 +470,7 @@ pub fn pre_process_input(content: &[u8], extension: &str) -> Vec { match extension { "cshtml" | "razor" => Razor.process(content), "haml" => Haml.process(content), + "json" => Json.process(content), "pug" => Pug.process(content), "rb" | "erb" => Ruby.process(content), "slim" => Slim.process(content), From a45a6c4b10330397b9b2109dcf61d3ea3111e71a Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 11 Mar 2025 13:08:16 +0100 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 178476346050..23469db88876 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Ensure classes between `>` and `<` are properly extracted ([#17094](https://github.com/tailwindlabs/tailwindcss/pull/17094)) - Treat starting single quote as verbatim text in Slim ([#17085](https://github.com/tailwindlabs/tailwindcss/pull/17085)) - Ensure `.node` and `.wasm` files are not scanned for utilities ([#17123](https://github.com/tailwindlabs/tailwindcss/pull/17123)) +- Improve performance when scanning `JSON` files ([#17125](https://github.com/tailwindlabs/tailwindcss/pull/17125)) ## [4.0.12] - 2025-03-07