预编译资产 - rails 4.1.1

Posted

技术标签:

【中文标题】预编译资产 - rails 4.1.1【英文标题】:precompile assets - rails 4.1.1 【发布时间】:2014-05-29 22:55:51 【问题描述】:

我正在生产中设置第一个 ruby​​ 站点,一切正常,但是当我运行时

rake assets:precompile

它在“public/assets/”目录中添加css/js ok,但是文件中有路径和行号?所以只是想知道如何在一条线上而不用线号/cmets 来获得它?我错过了一个设置或任何东西吗?这是我的application-428d86248ca363.css 出现的示例:

/* line 1, /home/joe/myapp/app/assets/stylesheets/main.scss */
body 
  background: #ccc;


/* line 6, /home/joe/myapp/app/assets/stylesheets/main.scss */
#head 
  background: #666;

/* line 4, /home/joe/myapp/app/assets/stylesheets/welcome.css.scss */
.block 
  color: #1e1e1e;

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 */

您还可以看到它在末尾包含大注释。如何让它在没有 cmets 的情况下全部运行在一条线上?

【问题讨论】:

【参考方案1】:

SCSS

它们在文件中有路径和行号

这是 SCSS 的问题,如果您使用 rake assets:precompile RAILS_ENV=production

应该解决

修复

在寻找参考资料时,我找到了this answer 和this github:

Just to update previous answer, by Chase T.

For me this is not working anymore.

     #config/compass.rb
     line_comments = false

should become

     line_comments = 0

考虑到资源使用compass.rb,我查看了如何在标准 Rails 应用程序中执行此操作。我发现这样做的方法是使用系统,看起来你可以使用这个命令:

#config/application.rb
config.sass.line_comments = false

这已通过this information 确认:

使用 Rails 4.1 并在查看和查看之后(可能会误读 文档)我通过将以下内容添加到 /config/environments/development.rd & production.rb

config.sass.preferred_syntax = :scss

config.sass.style = :compact

config.sass.line_comments = false

样式可以设置为:nested, :expanded, :compact, :compressed。你可以看到不同的选项 这里: http://sass-lang.com/documentation/file.SASS_REFERENCE.html#output_style

另外,请确保重新启动服务器以查看编译后的版本 改变

希望这对某人有所帮助。

【讨论】:

感谢 Rich 提供的信息,完成了这项工作! 使用“rake assets:precompile RAILS_ENV=production”做得很好。无需向应用程序文件添加任何内容。

以上是关于预编译资产 - rails 4.1.1的主要内容,如果未能解决你的问题,请参考以下文章

预编译 Rails AngularJS 资产

预编译资产失败的 Rails

Rails:预编译资产缺少节点模块

RAILS 轨道资产:预编译失败

未使用 Rails 测试环境中的预编译资产

黑名单Rails资产被预编译[重复]