Skip to content

Composes should work with import semantics #25

Closed

Description

@jasonLaster

ES6 introduces the idea of structured imports as a way of sharing re-usable code. It would be great if css-modules had import and composes flag supported it. Here's an example of what I mean

before:

.element {
  composes: large from "./typography.css";
  composes: dark-text from "./colors.css";
}

after:

import $typography from "typography";
import $colors from "colors";

.element {
  composes: $typography.large;
  composes: $colors.dark-text;
}

Benefits

  • consistent with ES6 semantics
  • modules are imported once on top
  • styles are referenced the same in JS land and CSS land (submitStyles.normal) is similar to (colors.darkBlue)

Proposed change

I'm not sure if postcss-modules-extract-imports can be changed here or if there should be a second postcss preprocessor, that does a transform. I'm interested in the postcss plugin semantics, so if you provide some guidelines I'd like to see if i can jump in and try to implement this.

Semantics

List of supported import semantics

import name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import name , { member [ , [...] ] } from "module-name";
import "module-name";

While the list is pretty long I could see many of them being relavant

import $colors from "colors";
import { $dark-blue } from "colors";
import { $dark-blue as $darkBlue } from "colors";
import { $dark-blue, $dark-red } from "colors";
import $colors , { $dark-blue, $dark-red } from "colors";

Activity

  1. crossman commented on Aug 19, 2015

    @crossman

    The current compose syntax (composes: large from "./typography.css";) is just leaning on the style of an ES7 proposal.

    But it would be nice to be able to import things more granular, like colors. Because you may want to use that color you're importing as a background color rather than a foreground color. I guess this could be combined with css next to achieve that though

  2. geelen commented on Aug 19, 2015

    @geelen
    Member

    First up, CSS Modules compiles to https://github.com/css-modules/icss which is our version of a lower-level format. The individual import statements there are deliberately limited in functionality, since we want to change as little as possible to make these new ideas work. This isn't JS, the scope of our module system is very different to what ES6 needs to support.

    That said, you could definitely write a PostCSS plugin to compile your example above to ICSS. That would be pretty neat, and support things like variables as @crossman suggested. That's why we broke out ICSS & made CSS Modules a series of PostCSS transforms

    But I really don't think variables are worth adding to CSS Modules. Within a single module file, it might be nice to use $brown to save yourself some repetition, and you can do that by adding simple-vars or custom-properties to the beginning of your build pipeline. But sharing variables across a file boundary is, I think, something that can be better solved by composition:

    /* Borrowing your ES6-style syntax */
    import { $primary, $secondary } from "./colors.css";
    
    .foo {
      color: $primary;
      background: $secondary;
    }
    
    .bar {
      color: $secondary;
      background: $primary;
    }

    In CSS Modules, you would do something like this:

    .foo {
      composes: primary on-secondary from "./colors.css";
    }
    
    .bar {
      composes: secondary on-primary from "./colors.css";
    }

    With variables, you can only represent a single variable. With a single-purpose class that you compose, you get a chance to name something.

  3. serapath commented on Aug 21, 2015

    @serapath

    How does on-primary work? I don't see it in the above code example that borrows from es6

    If I create a new module, I can import a lot of stuff and use it, similar to how i require a lot of stuff when creating a new javascript module.
    In javascript modules it's common to get functions from require statements and call them with certain arguments to customize the behavior of those required functions.

    How would that work with css modules? I imagine to define a website by importing a navbar, a footer and a main content area and for each menu entry certain content modules, which themselves consists again of many other components each with their styling.

    Given this scenario, I'm basically editing just the website component itself and so i will import the styling for navbar, footer, content area and maybe for the menu entry content pages, but I wonder how I can customize their styling to fit my overall theme.

    I imagine there needs to be a way to have something like parameters so that instead of import, like you show above, those imports can be passed in by a parent component that uses the one from your example with its classes "foo" and "bar"

  4. NekR commented on Aug 22, 2015

    @NekR

    @geelen key thing with any kind of all-imports is that you do not need repeat yourself again and again and write paths again and again. Imagine Babel supports only this syntax:

    var a = 1 + import num from './numbers';

    which allow you only write import inside an expression and when you asked them to support more real thing like current ES2015 imports, they say: "you could write another plugin/parser/tool/whatever to achieve such thing because it's not useful and we do not want to break semantics of ES5". Isn't it, hmm.. kind not right?

    For imports, I have in mind this (and this is not variables):

    /* this does not really CSS semantics since it still uses allowed tokens in CSS, I think */
    @import "./colors.css" as colors;
    
    .my-brand-button {
      composes: primary from colors;
    }
  5. geelen commented on Aug 22, 2015

    @geelen
    Member

    Totally agree. We almost did that, we could never agree on the exact syntax. We have a design goal to not break Sass or Less compilers (check http://sassmeister.com/ and http://lesscssismore.com/ftupih/), and sadly that suggestion breaks them. But I'm open to implementing this if we can find a syntax that works.

    For my own site, I actually implemented something a bit crazy (because I was going crazy on composes). Take a look at this. I use a @traits rule to set a working directory, then every .module:traits {} rule gets compiled to composes statements. It took 20 lines of PostCSS to implement. Not saying you should go down the same route, but a postcss-modules-composes-shorthand plugin might do something similar?

  6. NekR commented on Aug 24, 2015

    @NekR

    @geelen shorhand plugin is nice, but I think it's just better to have imports in file directly so it's more explicit. But shorhand plugin could work for global/common modules very well, this is true 👍

    For my own site, I actually implemented something a bit crazy (because I was going crazy on composes). Take a look at this. I use a @traits rule to set a working directory, then every .module:traits {} rule gets compiled to composes statements

    Seems like exactly what I meant, we just need some syntax updates for it. Also it's not a primary goal to have it as a core feature of CSS Modules, I think we can have such independent PostCSS transforms but maintained under CSS Modules umbrella, e.g. listed in README all the know (official/supported) transforms which could help with CSS Modules. Maybe even have those repos in css-modules org.

    I am not sure if this could break less/sass parsers, but if worked for your traits, then this probably could work too:

     // change to whatever name which does not break less/sass
    @include("./colors.css" as colors)
    
    .button {
      composes: primary from colors;
    }
  7. geelen commented on Aug 26, 2015

    @geelen
    Member

    So, Babel is moving to a no-plugins-by-default setup with certain "packs" of plugins to do, say, compile the ES6 spec. I think we could try to move to something similar. The "default" CSS Modules pack could eschew some of this syntax in favour of syntax compatibility with Sass, but we could also have convenience methods like this. And yep, I think they should be listed in the Readme & included within the organisation.

    If we are less constrained on syntax, I actually prefer something along the lines of @jasonlester's original syntax. But probably prefixed with : or @, and I'm not sure about the $ for variables. I think I might make a new thread for discussions around a single, flexible & reusable syntax for import shorthands & variables. There's clearly enough of a demand for something, but I'd like to cover as many use-cases with a single additional feature as possible.

    I appreciate the ideas you folks have been raising on this and other issues, it's really helping me to understand the wider landscape of usage patterns.

  8. NekR commented on Aug 26, 2015

    @NekR

    If we are less constrained on syntax, I actually prefer something along the lines of @jasonlester's original syntax. But probably prefixed with : or @, and I'm not sure about the $ for variables.

    Then consider this:

    @import { primary, text } from './colors' // optionally leave the extension, like for .js
    
    .button {
      composes: primary;
    }

    And this could be compiled to:

    :import('./colors.css') {
      primary: hashed_name_123;
      text: hashed_name_333;
    }
    
    .button {
      composes: hashed_name_123;
    }

    I mean without desugaring @import { primary, text } from './colors' to composes: primary from './colors.css' for each use of primary, but just directly transform to ICSS.

    P.S. Oh.. and then keyword "as" should be supported, like { primary as primaryColor }.

  9. jasonLaster commented on Aug 28, 2015

    @jasonLaster
    Author
    @import { primary, text } from './colors' // optionally leave the extension, like for .js

    is nice. I'll try and whip up a postcss plugin which would give us a chance to try out different syntaxes and also keep it sass compatible

  10. geelen commented on Aug 29, 2015

    @geelen
    Member

    @jasonLaster 👍 looking forward to it. Sass compatibility is going to be tricky if you use @import because it's going to try to find the file and import it! If we follow @NekR's advice and compile directly to ICSS (rather than desugaring to composes statements) then we get a syntax that is both a composes shorthand and a variables syntax. Super cool!

    For variables, though, we'll need a corresponding way to @export them from another file. So keep that in mind.

  11. nsisodiya commented on Sep 4, 2015

    @nsisodiya

    Please add this feature.. I will love to write like this
    @import { primary, text } from './colors'

  12. NekR commented on Sep 4, 2015

    @NekR

    Okay, I tried some syntaxes and how they will work with current pre-processors. Here is what I get:

    This one work correctly with SCSS, systax error with LESS and ignored with Stylus.

    @require test, blah from "test.css";
    @require (test: my-test), (blah: bar) from "test.css";

    This works fine for all 3 pre-processor, but looks a bit uncomfortable;

    :modules {
     import: test, blah from 'test.css';
    }
    
    :modules {
     import: test as test2, blah as blah2 from 'test.css';
    }
  13. geelen commented on Sep 7, 2015

    @geelen
    Member

    I like the first variant, but using x as y instead of (x: y):

    @alias foo as one, bar from "./test.css";
    @alias "../../../test.css" as test;
    
    .foo {
      composes: one bar;
      composes: something-else from test;
    }
    

    Thoughts?

  14. geelen commented on Sep 7, 2015

    @geelen
    Member

    Oh and thinking @alias isn't a bad term, since it can apply to both local and remote aliases.

  15. NekR commented on Sep 7, 2015

    @NekR

    I also like "as" more than ":", but I tried to not break SCSS syntax. Does
    version with "as" works fine with it?

    One more thing: does ICSS supports multiple :import declarations?
    On Sep 7, 2015 8:10 AM, "Glen Maddern" [email protected] wrote:

    I like the first variant, but using x as y instead of (x: y):

    @alias foo as one, bar from "./test.css";
    @alias test "../../../test.css";

    .foo {
    composes: one bar;
    composes: something-else from test;
    }

    Thoughts?

    —
    Reply to this email directly or view it on GitHub
    #25 (comment)
    .

  16. geelen commented on Sep 7, 2015

    @geelen
    Member

    Seems fine. Once Sass realises that @alias isn't a thing it can understand it doesn't try to parse it. Except if it has a {, for some reason.

  17. NekR commented on Sep 7, 2015

    @NekR

    Except if it has a {, for some reason.

    I believe this is because then SASS treats it a dead (empty) statement, e.g. empty media query. Okay, if it works fine with as then it looks fine to me. But I am not sure about @alias keyword, need more thoughts here.

  18. gajus commented on Sep 16, 2015

    @gajus

    Has anyone written a plugin for this? I cannot see a reference in this thread.

  19. geelen commented on Sep 29, 2015

    @geelen
    Member

    This is now implemented, just tidying up for a release. Follow along here: css-modules/css-modules-loader-core#28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions