Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails asset pipleline: compile to multiple stylesheets

Due to a specific setup, I would like to split the compiled stylesheets in two files. This is because (a part of) the CSS is needed for a Java application which can parse the CSS, but it is a bit buggy and can't handle some css-(hack)-syntax. Because I am unable to modify this Java application, I want to feed it only the CSS which it needs and of which I can make sure it is correct.

So, normally the assets pipeline would produce just one '/assets/application-[..].css' file. It would to let it also generate '/assets/custom-[..].css', based on a file selection I make. This still can be pre-compiled.

Is there a way to do this? Although I understand this is not the ideal setup..

like image 554
Roemer Avatar asked Jul 11 '12 14:07

Roemer


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

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.

What does rake assets Clean do?

Two cleanup tasks: rake assets:clean is now a safe cleanup that only removes older assets that are no longer used, while rake assets:clobber nukes the entire public/assets directory. The clean task allows for rolling deploys that may still be linking to an old asset while the new assets are being built.


1 Answers

To tell rails about additional files you wish to have precompiled, you can add them to the config.assets.precompile setting.

config.assets.precompile += ["other_application.css"]

You only see application.css in your HTML because that's the only file you're including

<%= stylesheet_link_tag "application" %>

If you have some custom.css.scss in your apps/assets/stylesheets directory, it will be compiled just like application.css.

For example, I might have

- _common.css.scss
- application.css.erb.scss
- other_application.css.erb.scss

in app/assets/stylesheets. In the top of the non-partial files I will put

@import "common";

to include _common.css.scss. I can now reference either stylesheet independent of one another in a layout.

<%= stylesheet_link_tag "application" %>
# or
<%= stylesheet_link_tag "other_application" %>
like image 179
deefour Avatar answered Nov 15 '22 20:11

deefour