Rails资产:预编译奇怪的行为

mok*_*gio 9 ruby-on-rails less sprockets twitter-bootstrap

我发现自己面临着assets:precompile任务的奇怪行为,或者至少在我不完全理解的事情面前.

所以,我使用Rails 3.1.3,Sprockets 2.0.3,Less 2.0.11 for web application,而且我依赖Bootstrap进行布局,所以我也使用了less-rails 2.1.8和less-rails-bootstrap 2.0.8.我已经像他们在这里所说的那样定制了风格.

我的资产配置是:

stylesheets
|--application.css.scss
|--custom-style/
   |--variables.less
   |--mixins.less
   |--buttons.less
|--custom-style.css.less
Run Code Online (Sandbox Code Playgroud)

在application.css.scss我做

//=require custom-style
Run Code Online (Sandbox Code Playgroud)

在定制风格我做

@import "twitter/bootstrap/reset";
//@import "twitter/bootstrap/variables"; // Modify this for custom colors, font-sizes, etc
@import "custom-style/variables";
//@import "twitter/bootstrap/mixins";
@import "custom-style/mixins";
// And all the other standar twitter/bootstrap imports...

// Other custom-style files to import
@import "custom-style/buttons"
//...

// And other rules here
//...
Run Code Online (Sandbox Code Playgroud)

最后,buttons.less我使用了variables.lessmixins.lessBootstrap文件中定义的一些变量和mixin ,@white并且.buttonBackground更具体.

如果我bundle exec rake assets:precompile使用上面的配置启动,任务失败,我收到此错误:

$ bundle exec rake assets:precompile
/usr/local/rvm/rubies/ruby-1.9.3-p0/bin/ruby /usr/local/rvm/gems/ruby-1.9.3-p0/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
.buttonBackground is undefined
Run Code Online (Sandbox Code Playgroud)

但是,如果我做这个改变

buttons.less --> buttons.css.less

@import "buttons"  --> @import "buttons.css.less"
Run Code Online (Sandbox Code Playgroud)

一切正常!!

在使用嵌套导入时,它是否与较少变量和函数的范围相关?或者与订单有关的东西,较少的解析器或Sprockets处理导入树?

我错过了什么或以错误的方式做某事吗?

谢谢 :)

注意:即使使用原始变量和mixins文件,我也会收到错误,因此它与覆盖它们的覆盖没有关联.

Luk*_*ott 0

您是否尝试过将文件重命名为buttons.css.less但保留扩展名而不是@import(即@import "buttons")?

这就是我使用 SCSS 的方法,效果很好。

另一个想法是用指令替换//=require custom-styleapplication.css.less 中的@import "custom-style"。Rails Guide 这里推荐使用@import(用SCSS/LESS处理)over //=require(用Sprockets处理):

如果要使用多个 Sass 文件,通常应该使用 Sass @import 规则而不是这些 Sprockets 指令。使用 Sprockets 指令,所有 Sass 文件都存在于自己的范围内,使得变量或 mixins 仅在定义它们的文档中可用。