Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sprockets - multiple entry points?

Sprockets official documentation clearly says that:

Sprockets takes any number of source files and preprocesses them
line-by-line in order to build a `single` concatenation.

I'm a big fan of sprockets in Rails but here's the problem - my application has to support multiple layouts(desktop browsers) and mobile clients(iphone, ipad, android phones etc).

Both of this layouts require their own HTML reset CSS rules. Concatenated rules of desktop&mobile reset files would make a conflict because they override low level CSS directives.

How can I fix that?

like image 776
Nikita Fedyashev Avatar asked Jun 14 '11 12:06

Nikita Fedyashev


People also ask

What are sprockets in rails?

Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application's JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files. It provides structure and practices on how to include assets in our projects.

What is sprockets gem?

Sprockets is a Rack-based asset packaging system that concatenates and serves JavaScript, CoffeeScript, CSS, LESS, Sass, and SCSS.

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.


2 Answers

You can get multiple top-level CSS files by making a Sprocket file for each. For example, say you want desktop.css to be comprised of reset.css, common.css, and ie.css and mobile.css to be comprised of common.css and ios.css. You would have the following files:

  • app/assets/stylesheets/desktop.css
  • app/assets/stylesheets/mobile.css
  • app/assets/stylesheets/reset.css
  • app/assets/stylesheets/common.css
  • app/assets/stylesheets/ie.css
  • app/assets/stylesheets/ios.css

In desktop.css, you would have the following:

/*
 *= require reset.css
 *= require common.css
 *= require ie.css
 */

In mobile.css, you would have the following:

/*
 *= require common.css
 *= require ios.css
 */

Then, in app/views/layouts/desktop.html.erb, you would do

<%= stylesheet_link_tag :desktop, :debug => Rails.env.development? %>

and similarly for mobile.html.erb.

Lastly, you'll need to set the precompiled asset list in config/environments/production.rb:

config.assets.precompile = %w( desktop.css mobile.css )
like image 54
James A. Rosen Avatar answered Nov 07 '22 09:11

James A. Rosen


I'm not really sure if sprockets supports this but I know that if you use the Jammit gem. You can setup different packages each with it's own cocktail of your JS or css files. e.g. have a :workspace package for desktop and and :mobile package for mobiles. It is all defined in a config yaml file and it will concat them in the order you list them, which can help get plugin dependencies correct etc.

javascripts:
  workspace:
    - public/javascripts/vendor/jquery.js
    - public/javascripts/lib/*.js
    - public/javascripts/views/**/*.js
    - app/views/workspace/*.jst

  mobile:
    - public/javascripts/vendor/jquery.js
    - public/javascripts/lib/mobile.js


stylesheets:
  common:
    - public/stylesheets/reset.css
    - public/stylesheets/widgets/*.css
  workspace:
    - public/stylesheets/pages/workspace.css
  mobile:
    - public/stylesheets/pages/mobile.css

Jammit might be worth a look for your needs

Hope this helps.

like image 23
David Barlow Avatar answered Nov 07 '22 09:11

David Barlow