myFavShrimp / turf

Macro based compile-time SCSS transpilation, CSS minification, and class name uniquification toolchain inspired by CSS modules.
MIT License
50 stars 2 forks source link

[Feature request] Make a style! macro to write style directly in Rust files #9

Closed sambonbonne closed 4 months ago

sambonbonne commented 12 months ago

Hello,

A nice feature provided by some other CSS tools for Rust (styled and stylers) have a style! macro to allow writing CSS directly in .rs files.

Instead of using `style_sheet!("path/to/file.scss"), this would allow us to write directly:

style! {
  .first {
    color: red;

    strong {
      color: blue;
    }
  }
}

Of course, this does not mean the style_sheet! macro should be remove, both can be complementary.

myFavShrimp commented 11 months ago

I like this idea. This change itself should be relatively easy to implement with the current state of turf. To keep everything consistent, it should definitely respect turf's settings, and with #8 planned, it's important to discuss how the proposed style! macro should behave in conjunction with file creation.

What seems particularly interesting to me is what the files should be called that are output when a user decides to set the separate_css_files_path.

sambonbonne commented 11 months ago

I can be the rust file name but with .css instead of .rs?

If you call the macro in my_component.rs, the file can be named my_component.css, but it implies you don't have filenames conflicts.

To avoid these conflicts, the output can re-use the complete path so you if you have style! in src/path/to/my_component.rs, the generated file can be in <output_dir>/path/to/my_component.css. It can also be in the form of a replacement for /, for example <output_dir>/path_to_my_component.css.

myFavShrimp commented 11 months ago

This is only possible on the nightly toolchain of Rust (see https://doc.rust-lang.org/proc_macro/struct.Span.html#method.source_file). Using this part of the Span api makes it necessary to hide this feature from users of stable Rust, which I don't want.

An idea that came to my mind is using a hash (maybe of the resulting css style sheet?) as a file name. This makes every file name unique. However the names are quite ugly and can be harder to use.

To make it easier to handle the resulting files, two new variables similar to the current STYLE_SHEET can be generated which have the name of the files stored in them.

sambonbonne commented 10 months ago

Wouldn't it work with the file! macro? (See the doc)

A hash would be working but if some user want to use an external tool to build some CSS together after turf compiled files (and group some files by components/purpose), it would become difficult, I'm not sure this is suitable (also, I just saw noticed you completed #8, thank you, I will give it a try!)

myFavShrimp commented 10 months ago

Macros are resolved in the file they are used in. This wouldn't work because if turf needs to read the macros output, file! has to be used inside turf and would therefore return the file name of turf's source file

sambonbonne commented 10 months ago

Oh, right, I understand

Unfortunately I have no other idea so I guess this won't be possible to have a "proper" filename

sambonbonne commented 4 months ago

@myFavShrimp it seems you also added this feature since 0.7.0, I tried it and it works as expected so I guess this issue can be closed?

myFavShrimp commented 4 months ago

This feature is implemented and available when building from the repository but it is not released yet. You are right, this issue can be closed