Skip to content
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

Using css modules with jade/haml/slim/etc or other templating library/language. #70

Closed
fesor opened this issue Oct 12, 2015 · 11 comments
Closed

Comments

@fesor
Copy link

fesor commented Oct 12, 2015

Well... I like the idea of css modules, but what I don't get is how to make it work with jade for example.

Let's took jade for example. Since we have access to template AST we can do something like this:

/* alerts.css *?
.alert {}
.success{}
.error{}
.warning{}
//- alerts.jade
include 'alerts.css'

.alert.success Some success text

I could prepare some proof-of-concept implementation, but... I really not sure about it and I need to hear other people's thoughts about this problem.

@geelen
Copy link
Member

geelen commented Oct 12, 2015

YES YES YES I really want to support templating languages like this as first class citizens.

With Jade, you can currently do this sort of thing:

doctype html
- styles = require("./styles.css");
html(lang="en")
  body
    h1.global(class=styles.local) CSS Modules & Jade

And it works! If you hop on over to http://jade-lang.com/ and replace require("./styles.css") with { local: 'local__abcd1234' } you get this output:

<!DOCTYPE html>
<html lang="en">
  <body>
    <h1 class="global local__abcd1234">CSS Modules & Jade</h1>
  </body>
</html>

Would that be enough? Or would you want to go further and make local styles the default so you could use the short-hand?

As for other templating languages, that's a bit more work. I'm going to start a new issue to discuss.

@fesor
Copy link
Author

fesor commented Oct 12, 2015

Your solutions seems to be more flexible. This way I don't need to use some other solutions, for angular's ng-class for example.

// - foo.jade
var styl = require("./foo.css")
.global(ng-class="{'#{styl.local}': vm.someCondition}")

@nkbt
Copy link

nkbt commented Oct 13, 2015

@fesor the very basic example of using css-modules with angular https://github.com/nkbt/ng-modular

@fesor
Copy link
Author

fesor commented Oct 13, 2015

@nkbt I've seen it. With jade I can do the same without angular be knowing anything about css.

@nkbt
Copy link

nkbt commented Oct 13, 2015

Sure 👌

@nostalgicdodo
Copy link

how did you get this to work?
jade says "require is not a function"

@fesor
Copy link
Author

fesor commented Oct 18, 2015

@nostalgicdodo this is a feature of jade-loader for webpack i think. I didn't tried this so far.

@nostalgicdodo
Copy link

@fesor yes, it is. thanks.

@zhouwenbin
Copy link

https://github.com/zhouwenbin/css-modules-jade-demo

@geelen
Copy link
Member

geelen commented Dec 26, 2015

Nice work @zhouwenbin!

@kulakowka
Copy link

Pug (ex-Jade)

Let's say you have a Pug template about.jade:

h1(class=css.title) postcss-modules
article(class=css.article) 

A PostCSS plugin to use CSS Modules everywhere
Render it:

var jade = require('jade');
var cssModules = require('./cssModules.json');
var html = jade.compileFile('about.jade')({css: cssModules});
console.log(html);

And you'll get the following HTML:

<h1 class="_title_xkpkl_5 _title_116zl_1">postcss-modules</h1>
<article class="_article_xkpkl_10">A PostCSS plugin to use CSS Modules everywhere</article>

I copied this from https://github.com/css-modules/postcss-modules

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

No branches or pull requests

7 participants