11

the new guy here. I've been looking for a good solution to using Stylus (compiled CSS) client side.

Now, I know the tips regarding not using compiled CSS client side because:

  1. It breaks if JS is not in use.
  2. It takes extra time to compile in a live client environment.
  3. It needs to be recompiled at every single client, which just isn't green.

However, my environment is an extension made for Chrome and Opera. It works in a JS environment and it works offline, so neither 1, 2 or 3 applies. What I'm really looking for here is just a way to write CSS more efficiently with less headaches, more variables, nesting and mixins.

I have tried Less, which is the only one of the trio Less, Sass and Stylus which currently works nicely client side. So, does anyone know a good solution for Stylus?

Michael Johansen
  • 4,688
  • 5
  • 29
  • 47

4 Answers4

4

CSS Preprocessors aren't actually meant to be run client-side. Some tools (i.e. LESS) provide a development-time client-side (JavaScript) compiler that will compile on the fly; however, this isn't meant for production.

The fact that Stylus/Sass do not provide this by default is actually a good thing and I personally wish that LESS did not; however, at the same time, I do realize that having it opens the door to people that may prefer to have some training wheels which can help them along in the beginning. Everyone learns in a different way so this may be just the feature that can get certain groups of people in the door initially. So, for development, it may be fine, but at the time of this writing, this workflow is not the most performant thing to do in production. Hopefully, at some point, most of the useful features in these tools will be added to native CSS then this will be a moot point.

Right now, my advice would be to deploy the compiled CSS only and use something like watch or guard or live-reload or codekit (or any suitable equivalent file watcher) in development so your stylus files are getting re-compiled as you code.

Wil Moore III
  • 6,968
  • 3
  • 36
  • 49
  • I'm happy as long as i get to write Stylus in dev, then deploy CSS to the clients browsers. Thanks! I'll try Watch. – Michael Johansen Jul 06 '12 at 07:30
  • I would also recommend this version of watch (https://github.com/visionmedia/watch#about). I've linked to the about section of the README which lists the enhancements over the original. – Wil Moore III Jul 06 '12 at 17:35
  • I know this is kind of an old topic/thread, but I'm curious as to why exactly you think it shouldn't be compiled on the fly. I've been using build processes with preprocessors like Grunt for years, building out behemoth build/deployment processes and frankly, I'm getting a little tired of them. I wish modern browsers would just support these alternative syntaxes already... – mmmeff Oct 23 '14 at 21:59
  • @mmmeff Hopefully my latest edit answers your question. – Wil Moore III Oct 24 '14 at 04:12
  • compiled LESS / CSS on the fly in browsers in development mode is a totally valid technique. Even more so with things like hot-module swapping. – Justin Meyer Mar 01 '16 at 15:46
4

This page likely has the solution: http://learnboost.github.io/stylus/try.html

It seems to be compiling Stylus on the fly.

Justin Meyer
  • 1,677
  • 13
  • 15
  • 2
    This has it. stylus(str).render(function(err, cssStr){ ... }); Then just add the CSS inside style element. – JussiR Nov 17 '14 at 08:51
  • what is the lib to do that? – SuperUberDuper Mar 01 '16 at 16:28
  • They are including a minified version of stylus: http://stylus-lang.com/try/stylus.min.js. If you put that script on any page you will have access to the `stylus(src).render()` functionality – Sam Eaton Jul 05 '16 at 20:35
2

Stylus is capable of running in the browser

There's a client branch available in the GitHub repo

Steven Pribilinskiy
  • 1,862
  • 1
  • 19
  • 21
0

I don't totally understand your question but I'll offer some of the experience I've have with compiled css using LESS.

Earlier implementations needed javascript to compile the LESS files into CSS in the browser, I've never tried to work this way didn't seem that great to me and as you say if JS is switched off your in for a rough time.

I've been using applications recently to compile the LESS code into valid CSS, this gets around the need for JS to convert the source code.

The first application I used was crunch http://crunchapp.net/ which worked quite well but didn't compile the css on the fly.

The application I'm using now is called simpless http://wearekiss.com/simpless and this creates valid css on the fly so as soon as I've hit save in sublime text and refresh in the browser I can see my changes to the css.

Using this work flow, I'm able to get around the issues your raised above, when I'm done doing development I just upload my css file outputted from simpless which is also heavily minified which also saves time in terms of needing to optimise the css further.

I hope I have understood the question correctly, if not apologies.

Cheers, Stefan

Stefan Burt
  • 181
  • 10
  • Extensions (kind of) requires JS, so I'm certain it's switched on. Simpless looks interesting, and Sublime too for that matter. I'll give that workaround a try. – Michael Johansen Jul 06 '12 at 07:23