Skip to content

CSS modules #17958

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 20 commits into from
Mar 7, 2025
Merged

CSS modules #17958

merged 20 commits into from
Mar 7, 2025

Conversation

zackradisic
Copy link
Member

What does this PR do?

This implements support for CSS modules in Bun's bundlers.

The following features are supported right now:

  • CSS files named with the .module.css suffix are parsed as CSS modules
  • globally unique names are generated for locally scoped classes and ids
  • composes
    • composing class names in current file: composes: foo
    • composing class names from another CSS modules file: composes: foo from './foo.module.css'
    • composing class names from the global scope: composes: foo from global
  • Not supported:
    • :local or :global

Fixes #16916

@robobun
Copy link

robobun commented Mar 7, 2025

Updated 10:18 PM PT - Mar 6th, 2025

@zackradisic, your commit fb8ac4e has 1 failures in Build #12820:


🧪   try this PR locally:

bunx bun-pr 17958

@RiskyMH RiskyMH added the release-notes This PR should be mentioned in release notes for the Bun version in which it lands label Mar 7, 2025
@@ -462,14 +490,14 @@ pub fn hash(allocator: Allocator, comptime fmt: []const u8, args: anytype, at_st
var h_bytes: [4]u8 = undefined;
std.mem.writeInt(u32, &h_bytes, h, .little);

const encode_len = bun.base64.encodeLen(h_bytes[0..]);
const encode_len = bun.base64.simdutfEncodeLenUrlSafe(h_bytes[0..].len);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you check the code

the length here is usually done like len * 3 + 2 or something like that - it's not iterating through the contents so the simdutf special-casing is unlikely to be helpful

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to use this encode Len function because we use another encoding function which uses characters that look nicer in CSS source code

if (project_root) |root| {
if (bun.path.Platform.auto.isAbsolute(root)) {
alloced = true;
// TODO: should we use this allocator or something else
const relative_to_root = bun.path.relative(root, path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are calling bun.path.relative twice here

@@ -138,71 +147,17 @@ pub const CssModule = struct {
}

pub fn handleComposes(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code supposed to do nothing?

@@ -37,10 +37,12 @@ pub const Composes = struct {
/// Where the class names are composed from.
from: ?Specifier,
/// The source location of the `composes` property.
loc: Location,
loc: bun.logger.Loc,
loc2: Location,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give a more descriptive name than "loc2"?

part.value.loc,
),
.e_dot => brk: {
// TODO: handle this here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this mean?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I do styles["123456"] will it work


### Browser Compatibility

By default, Bun's CSS bundler targets the following browsers:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show how this can be configured from Bun.build

Mention how we derive the list from the popular browserlist package and how we support that (right?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently there is no api to configure it

@@ -218,6 +218,9 @@ export default {
page("bundler/html", "Bundle frontend & static sites", {
description: `Zero-config HTML bundler for single-page apps and multi-page apps. Automatic bundling, TailwindCSS plugins, TypeScript, JSX, React support, and incredibly fast builds`,
}),
page("bundler/css", "Bundle, transpile, and minify CSS", {
description: `Production ready CSS bundler with support for modern CSS features, CSS modules, and more.`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Production ready"

Are you sure you want to word it that strongly right now?

/// The referenced name comes from a source index (used during bundling).
source_index: u32,
// source_index: u32,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you meant to keep this commented out, then please delete it so we don't confuse it in the future.

file: []const u8,
///
/// Is an import record index
file: u32,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please call this import_record_index instead of file so that we are consistent with the rest of the codebase.

@Jarred-Sumner Jarred-Sumner merged commit e6cb0de into main Mar 7, 2025
68 of 70 checks passed
@Jarred-Sumner Jarred-Sumner deleted the zack/css-modules branch March 7, 2025 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes This PR should be mentioned in release notes for the Bun version in which it lands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bundler: Support CSS Modules *.module.css
4 participants