winton / stasis

Static sites made powerful
http://stasis.me
MIT License
677 stars 56 forks source link

Stasis + Compass (or other gems)? #31

Closed joshuaclayton closed 11 years ago

joshuaclayton commented 12 years ago

I was wondering if there was a way to use compass (or other gems providing css/scss) with stasis. I've tried requiring compass at the top of controller.rb but @import "compass"; raises a Sass::SyntaxError letting me know it can't import compass.

Thanks for any insight!

ghost commented 12 years ago

Big +1 for this. I'm having the same issue, and seem unable to find a solution. And Stasis + Compass CSS would be a perfect match!

heydiplo commented 12 years ago

Here's working solution:

create config.rb for compass in project directory:

http_path = "/"
css_dir = "public/css"
sass_dir = "css"
images_dir = "images"

add this to controller.rb to ignore css files and invoke compass before template rendering:

ignore /css/
ignore /config\.rb/

before 'index.html.haml' do
    system "cd .. && bundle exec compass compile"
end
JeanMertz commented 12 years ago

It seems that if I do

before "index.html" do
    system "pwd"
end

the current directory returned is some deep/nested/directory/inside/images/. Is there any way to get the root from which you fired the stasis command or anything? Because right now I can't use any system commands unless I hardcode the path.

heydiplo commented 12 years ago

Quick and dirty.

root = Dir.pwd

before 'index.html.haml' do
    system "cd #{ root } && bundle exec compass compile"
end
allard commented 12 years ago

With added sprockets:

config.rb:

http_path = "/"
css_dir = ".stylesheets-cache"
sass_dir = "stylesheets"
images_dir = "images"

and then in controller.rb

ignore /\/_.*/
ignore /stylesheets/
ignore /javascripts/
ignore /config\.rb/

root = Dir.pwd

before 'index.html.erb' do
  system "cd #{ root } && compass compile"
  system "cd #{ root } && sprocketize -cgj uglifier public .stylesheets-cache/stylesheet.css javascripts/javascript.js; echo 'sprocketize complete'"
end

And now I get one stylesheet and one javascript file to include in my project, compiled and minimized. Next step if someone care to improve is to use the manifest feature from sprockets to always force updating the caches for stylesheets and javascripts.

Aupajo commented 12 years ago

For Compass, I simply used the following (in controller.rb):

require 'compass'

# Compass support
Stasis::Options.set_template_option 'scss', { :load_paths => Compass.configuration.sass_load_paths }
whitehat101 commented 11 years ago

@Aupajo what am I missing?

I'm using compass (0.12.2), sass (3.2.5), stasis (0.1.23)

I attempted both of the following, separately, inside the top level controller, inside before blocks, and with set_template_option 'sass' instead of 'scss':

I get the following error:

[...]/_style.css.scss:1: File to import not found or unreadable: compass. (Sass::SyntaxError)
Load path: [path-to-project]/assets
...

It seems to be refusing to add the load_paths. Compass.configuration.sass_load_paths returns correct paths.

My project is to create HTML fragments that will be pasted into a CMS. Right now I'm rendering the scss into a variable, and printing it inside a style tag in a view. Running compass in a separate process seems heavy and prone to race conditions.

before %r{index|fragment} do
  @css = render '_style.css.scss'
  @content = render '_content.html.haml'
end
Aupajo commented 11 years ago

@whitehat101 Just tried it from scratch: https://gist.github.com/4542474

Can you post a more complete example of your controller.rb and your stylesheet?

whitehat101 commented 11 years ago

Interesting. I had to add the gem 'redcarpet' because of instructions.md but it did compile the stylesheet correctly.

Found something!

#...
Stasis::Options.set_template_option 'scss', { :load_paths => Compass.configuration.sass_load_paths }

before 'instructions.md' do
  render 'example.css.scss'
end

Amending your gist like so, causes Sass::SyntaxError

So, implicit compilation and a call to render are not quite the same.

whitehat101 commented 11 years ago

The dirty workaround to my issue:

before 'instructions.md' do
  render 'example.css.scss', :template => { :load_paths => Compass.configuration.sass_load_paths }
end

Thanks @Aupajo

I'll try to collect my findings, and create a separate issue for this quirk.

whitehat101 commented 11 years ago

I have released a gem, stasis-compass, that aims to simplify compass integration. It is hardly more complex than @Aupajo suggestion at this point, but I plan on adding stasis helper methods to configure some common options, and environment variable detection to build for production/development. Whatever people think is useful.

Enjoy!

https://github.com/whitehat101/stasis-compass

winton commented 11 years ago

Thanks for the discussion/contribution on this guys. Going to close this, if you think it should still be open please comment.