-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(graph): support CSS files #8329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
470cc22
refactor: document services
ematipico a7a7860
feat: CSS module graph
ematipico c3b80e6
[autofix.ci] apply automated fixes
autofix-ci[bot] 3c10306
Update crates/biome_module_graph/src/css_module_info.rs
ematipico 5287fd7
Apply suggestions from code review
ematipico ee975f9
fix compilation issues
ematipico ddcdc2f
[autofix.ci] apply automated fixes
autofix-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use crate::{AnyCssImportUrl, AnyCssUrlValue}; | ||
| use biome_rowan::TokenText; | ||
|
|
||
| impl AnyCssImportUrl { | ||
| /// Returns the inner text of specifier: | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ``` | ||
| /// use biome_css_factory::make; | ||
| /// use biome_css_syntax::AnyCssImportUrl; | ||
| /// ``` | ||
| pub fn inner_string_text(&self) -> Option<TokenText> { | ||
| match self { | ||
| Self::CssString(css) => css.inner_string_text().ok(), | ||
| Self::CssUrlFunction(url_function) => { | ||
| url_function.value().and_then(|value| match value { | ||
| AnyCssUrlValue::CssString(css) => css.inner_string_text().ok(), | ||
| AnyCssUrlValue::CssUrlValueRaw(raw) => raw | ||
| .value_token() | ||
| .ok() | ||
| .map(|token| token.token_text_trimmed()), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| mod visitor; | ||
|
|
||
| use biome_resolver::ResolvedPath; | ||
| use biome_rowan::Text; | ||
| use indexmap::IndexMap; | ||
| use std::collections::BTreeSet; | ||
| use std::ops::{Deref, DerefMut}; | ||
| use std::sync::Arc; | ||
|
|
||
| pub(crate) use visitor::CssModuleVisitor; | ||
|
|
||
| /// Information restricted to a single module in the [ModuleGraph]. | ||
| #[derive(Clone, Debug)] | ||
| pub struct CssModuleInfo(pub(super) Arc<CssModuleInfoInner>); | ||
|
|
||
| impl Deref for CssModuleInfo { | ||
| type Target = CssModuleInfoInner; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.0.as_ref() | ||
| } | ||
| } | ||
|
|
||
| impl CssModuleInfo { | ||
| pub(crate) fn new(imports: CssImports) -> Self { | ||
| let info = CssModuleInfoInner { imports }; | ||
| Self(Arc::new(info)) | ||
| } | ||
|
|
||
| pub(crate) fn dump(&self) -> SerializedCssModuleInfo { | ||
| SerializedCssModuleInfo { | ||
| imports: self | ||
| .0 | ||
| .imports | ||
| .iter() | ||
| .map(|(_, static_import)| static_import.specifier.to_string()) | ||
| .collect(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct CssModuleInfoInner { | ||
| /// Map of all static imports found in the module. | ||
| /// | ||
| /// Maps from the import specifier to a [CssImport] with the absolute path | ||
| /// it resolves to. The resolved path may be looked up as key in the | ||
| /// [ModuleGraph::data] map, although it is not required to exist | ||
| /// (for instance, if the path is outside the project's scope). | ||
| pub imports: CssImports, | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct CssImports(pub(crate) IndexMap<Text, CssImport>); | ||
|
|
||
| impl Deref for CssImports { | ||
| type Target = IndexMap<Text, CssImport>; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl DerefMut for CssImports { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.0 | ||
| } | ||
| } | ||
|
|
||
| /// Represents an import to one or more symbols from an external path. | ||
| /// | ||
| /// It could point to any kind of resource, such as JavaScript files, CSS files, | ||
| /// images, and so on. | ||
| #[derive(Clone, Debug, PartialEq, Hash, Eq)] | ||
| pub struct CssImport { | ||
| /// The specifier for the imported as it appeared in the source text. | ||
| pub specifier: Text, | ||
|
|
||
| /// Absolute path of the resource being imported, if it can be resolved. | ||
| /// | ||
| /// If the import statement referred to a package dependency, the path will | ||
| /// point towards the resolved entry point of the package. | ||
| /// | ||
| /// If `None`, import resolution failed. | ||
| pub resolved_path: ResolvedPath, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
| #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| pub struct SerializedCssModuleInfo { | ||
| /// Map of all static imports found in the module. | ||
| /// | ||
| /// Maps from the local imported name to the absolute path it resolves to. | ||
| pub imports: BTreeSet<String>, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| use crate::css_module_info::{CssImport, CssImports, CssModuleInfo}; | ||
| use crate::module_graph::ModuleGraphFsProxy; | ||
| use biome_css_syntax::{AnyCssImportUrl, CssRoot}; | ||
| use biome_resolver::{ResolveOptions, ResolvedPath, resolve}; | ||
| use biome_rowan::{AstNode, Text, WalkEvent}; | ||
| use camino::Utf8Path; | ||
| use std::ops::DerefMut; | ||
|
|
||
| pub const SUPPORTED_EXTENSIONS: &[&str] = &["css"]; | ||
|
|
||
| pub(crate) struct CssModuleVisitor<'a> { | ||
| root: CssRoot, | ||
| directory: &'a Utf8Path, | ||
| fs_proxy: &'a ModuleGraphFsProxy<'a>, | ||
| } | ||
|
|
||
| impl<'a> CssModuleVisitor<'a> { | ||
| pub(crate) fn new( | ||
| root: CssRoot, | ||
| directory: &'a Utf8Path, | ||
| fs_proxy: &'a ModuleGraphFsProxy, | ||
| ) -> Self { | ||
| Self { | ||
| root, | ||
| directory, | ||
| fs_proxy, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn visit(self) -> CssModuleInfo { | ||
| let mut imports = CssImports::default(); | ||
| let iter = self.root.syntax().preorder(); | ||
| for event in iter { | ||
| match event { | ||
| WalkEvent::Enter(node) => { | ||
| if let Some(node) = AnyCssImportUrl::cast(node) { | ||
| self.visit_any_css_import_url(node, &mut imports); | ||
| } | ||
| } | ||
| WalkEvent::Leave(_) => {} | ||
| } | ||
| } | ||
|
|
||
| CssModuleInfo::new(imports) | ||
| } | ||
|
|
||
| fn visit_any_css_import_url(&self, node: AnyCssImportUrl, imports: &mut CssImports) { | ||
| let Some(specifier) = node.inner_string_text() else { | ||
| return; | ||
| }; | ||
|
|
||
| let resolved_path = self.resolved_path_from_specifier(&specifier); | ||
|
|
||
| let text: Text = specifier.into(); | ||
| imports.deref_mut().insert( | ||
| text.clone(), | ||
| CssImport { | ||
| specifier: text, | ||
| resolved_path, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| fn resolved_path_from_specifier(&self, specifier: &str) -> ResolvedPath { | ||
| let options = ResolveOptions { | ||
| assume_relative: true, | ||
| condition_names: &[], | ||
| default_files: &[], | ||
| extensions: SUPPORTED_EXTENSIONS, | ||
| extension_aliases: &[], | ||
| ..Default::default() | ||
| }; | ||
| let resolved_path = resolve(specifier, self.directory, self.fs_proxy, &options); | ||
| ResolvedPath::new(resolved_path) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.