You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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";}
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.
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
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.
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"
@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:
vara=1+importnumfrom'./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;}
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?
@geelenshorhand 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"ascolors).button{composes: primary from colors;}
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.
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;}
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 }.
@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.
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.
Activity
crossman commentedon Aug 19, 2015
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
geelen commentedon Aug 19, 2015
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
$brownto 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:In CSS Modules, you would do something like this:
With variables, you can only represent a single variable. With a single-purpose class that you compose, you get a chance to name something.
serapath commentedon Aug 21, 2015
How does
on-primarywork? I don't see it in the above code example that borrows fromes6If I create a new module, I can
importa lot of stuff and use it, similar to how irequirea lot of stuff when creating a new javascript module.In javascript modules it's common to get functions from
requirestatements and call them with certainargumentsto customize the behavior of thoserequiredfunctions.How would that work with
cssmodules? I imagine to define a website by importing anavbar, afooterand a maincontentarea and for each menu entry certaincontent modules, which themselves consists again of many other components each with their styling.Given this scenario, I'm basically editing just the
website componentitself and so i will import the styling fornavbar,footer,content areaand maybe for themenu 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
parametersso that instead ofimport, like you show above, thoseimportscan bepassed inby a parent component that uses the one from your example with its classes "foo" and "bar"NekR commentedon Aug 22, 2015
@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:
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):
geelen commentedon Aug 22, 2015
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
@traitsrule to set a working directory, then every.module:traits {}rule gets compiled tocomposesstatements. It took 20 lines of PostCSS to implement. Not saying you should go down the same route, but apostcss-modules-composes-shorthandplugin might do something similar?NekR commentedon Aug 24, 2015
@geelen
shorhandplugin is nice, but I think it's just better to have imports in file directly so it's more explicit. Butshorhandplugin could work for global/common modules very well, this is true 👍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:
geelen commentedon Aug 26, 2015
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.
NekR commentedon Aug 26, 2015
Then consider this:
And this could be compiled to:
I mean without desugaring
@import { primary, text } from './colors'tocomposes: primary from './colors.css'for each use ofprimary, but just directly transform to ICSS.P.S. Oh.. and then keyword "as" should be supported, like
{ primary as primaryColor }.jasonLaster commentedon Aug 28, 2015
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
geelen commentedon Aug 29, 2015
@jasonLaster 👍 looking forward to it. Sass compatibility is going to be tricky if you use
@importbecause 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 tocomposesstatements) then we get a syntax that is both acomposesshorthand and a variables syntax. Super cool!For variables, though, we'll need a corresponding way to
@exportthem from another file. So keep that in mind.nsisodiya commentedon Sep 4, 2015
Please add this feature.. I will love to write like this
@import { primary, text } from './colors'
NekR commentedon Sep 4, 2015
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.
This works fine for all 3 pre-processor, but looks a bit uncomfortable;
geelen commentedon Sep 7, 2015
I like the first variant, but using
x as yinstead of(x: y):Thoughts?
geelen commentedon Sep 7, 2015
Oh and thinking
@aliasisn't a bad term, since it can apply to both local and remote aliases.NekR commentedon Sep 7, 2015
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:
geelen commentedon Sep 7, 2015
Seems fine. Once Sass realises that
@aliasisn't a thing it can understand it doesn't try to parse it. Except if it has a{, for some reason.NekR commentedon Sep 7, 2015
I believe this is because then SASS treats it a dead (empty) statement, e.g. empty media query. Okay, if it works fine with
asthen it looks fine to me. But I am not sure about@aliaskeyword, need more thoughts here.gajus commentedon Sep 16, 2015
Has anyone written a plugin for this? I cannot see a reference in this thread.
geelen commentedon Sep 29, 2015
This is now implemented, just tidying up for a release. Follow along here: css-modules/css-modules-loader-core#28