Repository navigation
css: make the css_modules test option work and turn its bare test calls into real tests #38547
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2856,8 +2856,14 @@ impl<'a> Transpiler<'a> { | |
|
|
||
| let mut file_path = Fs::Path::init(file_path_text); | ||
|
|
||
| // Computed the way the bundler computes `Source.path.pretty` (see | ||
| // `generic_path_with_pretty_initialized`: forward slashes on every | ||
| // platform), because CSS module names are hashed from it. | ||
| let top_level_dir = self.fs().top_level_dir; | ||
| let rel = bun_paths::resolve_path::relative(top_level_dir, file_path_text); | ||
| let rel = bun_paths::resolve_path::relative_platform::< | ||
| bun_paths::resolve_path::platform::Loose, | ||
| false, | ||
| >(top_level_dir, file_path_text); | ||
| file_path.pretty = crate::linker::dupe(rel); | ||
|
|
||
| let mut output_file = options::OutputFile::zero_value(); | ||
|
|
@@ -3067,12 +3073,16 @@ impl<'a> Transpiler<'a> { | |
| // `'bump`-threading note). | ||
| let alloc: &'static Arena = unsafe { bun_ptr::detach_lifetime_ref::<Arena>(self.arena) }; | ||
|
|
||
| // Every class/id selector of a CSS module becomes a `LocalCss` symbol | ||
| // `Ref` carrying this source index; the printer resolves those through | ||
| // the symbol map and local names built below, so the three must agree. | ||
|
Comment on lines
+3076
to
+3078
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| let source_index = bun_ast::Index::RUNTIME; | ||
| let (mut sheet, extra) = match bun_css::StyleSheet::<bun_css::DefaultAtRule>::parse( | ||
| alloc, | ||
| entry.contents(), | ||
| opts, | ||
| None, | ||
| bun_ast::Index::INVALID, | ||
| source_index, | ||
| ) { | ||
| Ok(v) => v, | ||
| Err(e) => { | ||
|
|
@@ -3092,7 +3102,8 @@ impl<'a> Transpiler<'a> { | |
| ); | ||
| return None; | ||
| } | ||
| let symbols = bun_ast::symbol::Map::init_list(Default::default()); | ||
| let local_names = sheet.local_names(source_index, file_path_pretty); | ||
| let symbols = bun_ast::symbol::Map::init_with_one_list(extra.symbols); | ||
| let result = match sheet.to_css( | ||
| alloc, | ||
| &bun_css::PrinterOptions { | ||
|
|
@@ -3101,7 +3112,7 @@ impl<'a> Transpiler<'a> { | |
| ..bun_css::PrinterOptions::default() | ||
| }, | ||
| None, | ||
| None, | ||
| Some(&local_names), | ||
| &symbols, | ||
| ) { | ||
| Ok(v) => v, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -453,12 +453,15 @@ impl<'a> CssModuleReference<'a> { | |
| } | ||
| } | ||
|
|
||
| /// LAYERING: canonical implementation lives in `bun_base64::wyhash_url_safe` | ||
| /// (a leaf crate) so `bun_bundler::LinkerContext::mangle_local_css` can call | ||
| /// the *same* hasher without depending on `bun_css`. Re-export here so | ||
| /// in-crate callers (`dependencies.rs`, `rules/import.rs`) keep the | ||
| /// `css_modules::hash` path. | ||
| #[inline] | ||
| pub(crate) fn hash<'a>(bump: &'a Bump, args: Arguments<'_>, at_start: bool) -> &'a [u8] { | ||
| bun_base64::wyhash_url_safe(bump, args, at_start) | ||
| } | ||
|
|
||
| /// The printed name of a CSS module's local (class or id): `<local>_<hash of path>`, | ||
| /// `path` being the sheet's project-relative path (`Source.path.pretty` in the bundler). | ||
|
Comment on lines
+461
to
+462
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| pub fn local_name(local: &[u8], path: &[u8]) -> Box<[u8]> { | ||
| let scratch = Bump::new(); | ||
| let path_hash = hash(&scratch, format_args!("{}", bstr::BStr::new(path)), false); | ||
| [local, b"_", path_hash].concat().into_boxed_slice() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2555,6 +2555,20 @@ mod stylesheet_impl { | |
| return Ok(ToCssResult { code: dest }); | ||
| } | ||
|
|
||
| /// The `local_names` for printing this sheet outside a bundle (the bundler | ||
| /// builds the whole graph's in `LinkerContext::mangle_local_css`). | ||
| /// `source_index` must be the one the sheet was parsed with: the refs embed it. | ||
|
Comment on lines
+2558
to
+2560
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code |
||
| pub fn local_names(&self, source_index: SrcIndex, path: &[u8]) -> LocalsResultsMap { | ||
| let mut local_names = LocalsResultsMap::default(); | ||
| for (local, entry) in self.local_scope.iter() { | ||
| local_names.insert( | ||
| entry.ref_.to_real_ref(source_index.get()), | ||
| css_modules::local_name(local, path), | ||
| ); | ||
| } | ||
| local_names | ||
| } | ||
|
|
||
| pub fn parse( | ||
| arena: &'static Bump, | ||
| code: &[u8], | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code