Summary: Using Compass, need to compile SASS stylesheets twice with different output styles and file names.
I have this as my config.rb:
http_path = "/"
css_dir = "assets/css"
sass_dir = "assets/sass"
# …more stuff… #
output_style = :expanded
which perfectly compiles
assets
┗ sass
┣ style1.scss
┗ style2.scss
to
assets
┗ css
┣ style1.css
┗ style2.css
What I need to accomplish is an output like this:
assets
┗ css
┣ style1.css
┣ style1.min.css
┣ style2.css
┗ style2.min.css
where ".min.css" files contain minified CSS, obviously.
So I figured I need to come up with something like:
on_stylesheet_saved do |filename|
# switch output_style to :compressed
# compile again and include ".min" into file name
end
Can any one provide me with a basic sample on how to accomplish that?
Have not messed with ruby so far, but am coming to a basic understanding through all the reading I've been doing on this topic. ;)
Thanks a bunch!
I'd suggest Yeoman, or a custom Grunt script. You could also use Rake if you're more Ruby-oriented.
(Can't comment because of rep)
To answer Kevin Suttle, gulp could also do this job. It's kind of like grunt, but in gulp you get a code over configuration while grunt is mainly configuration and no (or almost no) code.
Many articles says that people learned grunt in over a day, while they spent only an hour learning about gulp.
It's up to you.
PS : Yeah, that post is from 2013 but answering it may help people having the same or a similar issue.
Related
Ok, so I have some problems with the Rails 5 ignoring certain image folders. Here what is some relevant info:
app/assets
javascripts/
stylesheets/
style1.scss
style2.scss
images/
subfolder1/
subfolder2/
config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( style1.css )
Rails.application.config.assets.precompile += %w( style2.css )
config/environments/development.rb
config.assets.debug = true
So the problem I am running into is that assets are being fingerprinted and served ONLY from subfolder1. I think I know this because I can go into the Chrome developer tools, under the sources tab look at the compiled CSS files generated from style1.scss and style2.scss.
In the generated CSS the image-url tags from subfolder1 look like this:
background: url(/assets/subfolder1/image-106e9d1ebbbc3111ae3e5b153c81a7237b3d033ecdabad3e107e122c3b457ca5.jpg)
In the same generated CSS file, the image-url tags from subfolder2 look like this:
background: url(/images/subfolder2/image.png)
The images from subfolder1 get rendered, but the images from subfolder2 are never retrieved and generate 404s. Why will the scss to css conversion only correctly translate images from subfolder1? Why is it fingerprinting at all in development mode?
In my case, it seems like I needed to run rake assets:clean to get this to right itself. Thanks Joel_Blum!
I have my own Grunt/Compass/SASS project with a config.rb file that has these settings:
http_path = "/"
css_dir = "stylesheets"
sass_dir = "sass"
add_import_path "./bower_components/slick-carousel/slick"
As you might guess, the problem here is the slick-carousel I am trying to compile together with my other sass-files. It works fine without that component. The slick-folder contains these files:
./ajax-loader.gif
./config.rb
./fonts
./fonts/slick.eot
./fonts/slick.svg
./fonts/slick.ttf
./fonts/slick.woff
./slick-theme.css
./slick-theme.scss
./slick.css
./slick.js
./slick.min.js
./slick.scss
When compiling the slick-theme.scss, I get warnings that it can not find the files which are referenced via relative urls.
WARNING: 'slick.woff' was not found (or cannot be read) in /project-root/fonts
Is there a way to tell the Compass/SASS compiler to use the "current" SASS file as base for the relative paths? So it would look in /project-root/bower_components/slick-carousel/slick/fonts instead?
Slick.js has it's own Sass variables for handling his fonts urls. So you have to properly assign the right path to that variable like this:
// Fonts
$slick-font-path: "./bower_components/slick-carousel/slick/fonts/";
You can check all slick's Sass variables here
Having completely given up attempting to touch the command line for SASS compiling, I have installed Prepos which seems to have a decent interface, and at least seems to be able to do the basics like compile SASS.
This compiles nicely when I save the file:
$my_color: #BADA55;
nav {
a{
color: $my_color;
&:hover {
color: darken($my_color, 10%);
}
}
}
The problem arises when I try to import compass, eg. if I add
#import 'compass';
Then I get a 'compilation failed' error- with a big red sad face, but naturally, no elaboration as to what the problem is.
To be honest, I barely know where to start to fix this. I just about grasp the basics of what I'm dealing with, but that's about it. I have found the available online documentation surrounding getting started with SASS nothing short of atrocious, which is why I have (reluctantly) turned to using a GUI. From my understanding Prepros comes with everything out of the box- including ruby and compass. So I really have no idea why this isn't working, and would greatly appreciate any pointers. Thanks.
EDIT: I am using the free version of Prepros (will happily pay for the full version if it actually works) and have tried adding a custom config.rb file which sets some basic configuration options:
relative_assets = true
css_dir = "css"
sass_dir = "scss"
images_dir = "images"
output_style = :expanded
line_comments = true
preferred_syntax = :scss
Doesn't work.
Sorry, adding the config.rb file (with the above content) DOES solve the issue (I'd just made an error in the path).
I don't know if I can explain the issue without pasting the whole code of all files but I'll try.
EDIT I've added the whole code to Github Account - My Sass structure
I use Windows 8.1 and Compass 0.12.6
In my public_html folder I have config.rb file and stylesheets folder. In stylesheets folder I have directory sass where I keep all sass files. CSS files are generated into stylesheets/preview folder (for development purposes) and into stylesheets folder (for production purposes).
I run compass watching running watch.bat script that I have in public_html folder
Content of watch.bat is:
START compass watch -c config.rb -e development
START compass watch -c config.rb -e production
EXIT
My config.rb file is:
require 'compass/import-once/activate'
# Require any additional compass plugins here.
# Set this to the root of your project when deployed:
http_path = "../"
sass_dir = "stylesheets/sass"
images_dir = "img"
javascripts_dir = "js"
cache = false
relative_assets = true
add_import_path "stylesheets/sass"
if environment == :development
css_dir = "stylesheets/preview"
line_comments = true
output_style = :nested
end
if environment == :production
css_dir = "stylesheets"
line_comments = false
output_style = :compressed
end
In stylsheets/sass I have 4 files print.scss , medium.scss, small.scss and large.scss
Both medium.scss and large.scss begin with:
#import "base/_init";
This file import partial _init from base folder and this one loads another partials and other partials load another partials.
The problem is when I change something in partial Compass doesn't always compile all necessary output files. For example I've just changed something in _settings.scss partial and this one is finally loaded by medium.scss and large.scss (I remind that those 2 files have the same beginning so they both should be compiled). Somehow only medium.scss file is being in fact watched and large.scss file is not compiled as it should be in this case.
I have had such situation just before. I stoped 2 watchers (CTRL + C and Y) and then run again my watch.bat not changing anything in files - just after running I have info:
Change detected at 15:51:33 to: large.scss overwrite stylesheets/preview/large.css
Compass is polling for changes. Press Ctrl-C to Stop.
and
Change detected at 15:51:33 to: large.scss overwrite stylesheets/large.css
Compass is polling for changes. Press Ctrl-C to Stop.
So in fact there was change that should be watched but somehow compass/sass doesn't always catch it.
Sometimes neither of 2 watchers catch changes and I have to stop them and once again run watch.bat so it's not very convenient to work that way.
Sometimes even happen that watchers are stopped (as I presed CTRL +C + y) but I haven't pressed anything and in cmd there's no question about closing visible. They've just stopped
Question: What could be wrong and how to fix it?
I've cloned and checked your project structure, and your problem is simply the unnecessary use of the Import Once plugin. If you comment the first line of your config.rb (where this plugin is activated), your stylesheets will be compiled normally. Each main scss file you have (large.css, medium.css, small.css) is rendered to a separated css file, and you're not repeating your imports for each one of these files.
Below is the sequence of your imports with the Import Once plugin disabled:
I dont't know exactly how to solve your problem.
I think basically it's a bug or problem with two instances of compass running at the same time on the same folder.
But due to your environment, I have a suggestion that can do the trick for you.
Why, instead of having two folders with the assets, one for prod. and dev., you just keep one, for both dev. and prod.
What you can set on compass is to output css in the way you want for dev. and then pass that css through a yui filter (or whatever) for prod. This will minify all assets. If you are using symfony, you can use assetics, for instance.
That's the way we do and works perfect. We don't use assetics for compass... that's another topic :D
If you don't use any framework or extra filters, you can alway manually dump the assets when this ones are ready for production.
This way you avoid using two compass instances at the same time and also to compile manually for every deploy on production.
I really think it have to do with having two instances of compass running.
I'm a front-end developer transitioning from CSS to SASS. I've got Ruby and Compass installed on my local machine, and Compass "watch" is working beautifully.
However, I still end up with local CSS files which I have to manually FTP over to the server after every tiny change, to see what the change made. I would like to automate this.
I did find this thread which suggested using rsync, but I use Windows and I feel setting up rsync would be really difficult.
Is there some way to automate this using Ruby? The workflow I'm trying to get:
I save the SCSS file in VIM.
Compass Watch detects the change and compiles a new CSS file
Some magical tool detects the change to the CSS file, uploads to my server
I switch over to Chrome, hit F5, and see the change
I can do everything, except for step 3. Any ideas? (That don't involve Linux or Mac-only software?)
I disagree with Roy and Adam, When working with Wordpress themes, I develop on a remote dev server. I have a team of people adding content and other edits which update the database, it would be difficult for me as the developer to work locally 100% of the time. Their sql file getting updated with content wouldn't mesh super well after the fact with my sql file (you know theme option settings and what not).
I have avoided using SASS for this reason until now.
My optimal workflow would be to edit my scss file -> auto compile to css -> auto upload to the search (like a upload on save would) -> livereload takes place and I see edits (I can live without this last step).
I haven't attempted this yet but I found this link which seems to be the closest answer thus far.Using SASS with Remote Setup
Side note: Working locally is not always an optimal set up. It isn't an answer and this is about the 8th time I have seen this question with similar answers.
UPDATE: Just tried it without Codekit just sass --watch and it worked great!
ANOTHER UPDATE: I have further modified the way I handle sass and remote development.
I know use Sublime, Open my .scss and .css file at the same time. I then use SFTP (a package for sublime) to "Monitor File" which will look for changes to the file outside of directly editing it, I then open up terminal and sass my scss file, now every time I save it complies locally and then the compiled css file auto uploads to my server! Hope that makes sense, maybe I will make a video showing.
Since the question was asked in 2011 Compass has evolved and now provides callback functions to do exactly what was asked in the question:
For Step 3 you can use on_stylesheet_saved and on_sourcemap_saved callback functions to upload your *.css and *.css.map files to the production server.
Sample code how to do this can be found in this StackOverflow answer
I am in a similar position developing in sass (scss) and previewing on a remote dev site. When developing on Windows I use Sublime Text 2 to edit with the FTP-Sync plugin to automatically upload on save.
This plugin has a convenient option to flag folders watched for for file saves that trigger it to check a different path for further file changes to upload. So you can flag your scss folder, make changes to an scss file and save, which alerts ST2 to watch the css folder (you can build in a delay to allow enough time to compile) and upload any changed files.
After setup of the software and setup of FTP Sync for the given project, your actions amount to 1) edit and save, 2) wait a couple of seconds, 3) refresh browser to view changes. (If the site looks broken at this point you may need to increase the delay setting by a second and save the file again to trigger the process another time.)
I'm not sure how to accomplish this on other platforms for a remote site, though I wonder if Coda 2 has some options that might work for Mac OS users.
In my experience the best way is to work this way:
1. you have a site somewhere - and it doesn't really matters where and how complex this site is;
2. you have local SASS files and CSS generated from these SASS files;
3. install Fiddler2 (web proxy)
4. configure it to catch a request for CSS file and replace the response with your local CSS file.
So, as you can see, you can use local CSS file instead of remote one. So there is no need to upload CSS every time you're building the SASS.
Automatically Upload CSS After Sass is Compiled.
Install:
gem install net-ssh
gem install net-sftp
Add to config.rb:
`
require 'net/ssh'
require 'net/sftp'
http_path = "/"
css_dir = "/"
sass_dir = "/sass"
images_dir = "images"
javascripts_dir = "js"
output_style = :expanded
environment = :development
remote_theme_dir_absolute = '/path/to/where/you/want/to/save/the/file/on/remote/server'
sftp_host = 'your-host.com' # Can be an IP
sftp_user = 'your-user.com' # SFTP Username
sftp_pass = 'xxxxxxxxxxx' # SFTP Password
on_stylesheet_saved do |filename|
Net::SFTP.start( sftp_host, sftp_user , :password => sftp_pass) do |sftp|
puts sftp.upload! '/path/to/local/style.css', remote_theme_dir_absolute + '/' + 'style.css'
end
puts ">>>> Compass is polling for changes. Press Ctrl-C to Stop"
end
`
For ftp only:
require 'net/ftp'
http_path = "/"
css_dir = ".."
sass_dir = ".."
images_dir = "images"
javascripts_dir = "js"
project_type = :stand_alone
output_style = :compressed
line_comments = false
environment = :development
ftp_host = 'your-host.com' # Can be an IP
ftp_user = 'your-user' # FTP Username
ftp_pass = 'xxxxxxxxx' # FTP Password
TXT_FILE_OBJECT = File.new('/path/to/local/style.css')
on_stylesheet_saved do |filename|
Net::FTP.open( ftp_host, ftp_user , ftp_pass) do |ftp|
ftp.putbinaryfile(TXT_FILE_OBJECT, "/path/on/remote/server/#{File.basename(TXT_FILE_OBJECT)}")
end
puts ">>>> Compass is polling for changes. Press Ctrl-C to Stop"
end
Change dirs and file paths to yours...
The simplest solution would be to develop locally (as is a best practice). Run a web server on your computer (try XAMP for Windows) and all changes will be instantly visible, without having to upload. Only upload when you're done.