joakimbeng / unistyle

Write modular and scalable CSS using the next version of ECMAScript
312 stars 11 forks source link

Add ability to read from stdin #3

Closed queckezz closed 9 years ago

queckezz commented 9 years ago

With this PR, unistyle doesn't automatically build js files with babel anymore. Instead it allows arbitrary js to go through.

CLI changes

Supports stdin now:

babel -o src/index.js | unistyle -o dist/app.css

API changes

I changed #compile() so it just accepts js css definitions as input and css as output (still, when given an output it creates a file) and then let the cli handle the file reading. This way, it can more easily get composed and integrated into a build pipeline (thinking autoprefixer, rework etc.).

#compile() no longer accepts a filename but instead just a css source object:

const css = { body: { margin: '100px' } }

unistyle.compile(css, cb)

tests

Also added some tests for the cli especially for stdin.


If you agree with the changes I made, I will update the readme accordingly :)

joakimbeng commented 9 years ago

I like this, have been thinking about this for a v1.0 as well!

There is one problem with this though... Run for instance babel test/fixtures/index.js in the project root and babel will only transform the entry point and not all the required modules as well.

I don't know how to address that at the moment, ideas?

queckezz commented 9 years ago

True, this doesn't work currently but you can something like this:

browserify -s css -t babelify test/fixtures/index.js | bin/unistyle -o test.css

note the --standalone which makes this possible to consume from node. Might be a bit verbose though but definitely more flexible.

joakimbeng commented 9 years ago

I agree with it being more flexible, but quite cumbersome to use.

What do you think about only running babel/register if there is no stdin?

queckezz commented 9 years ago

Yes better. The more I think about it though, stdin might be a really rare use case and they could just use the api if we support js objects as input.

See #7 for a simpler change for now