javascript - 为什么所有注释对rails中的application.css.scss和application.js没有影响

标签 javascript css ruby-on-rails comments

当我像这样在 rails 中使用 application.js 时,我有一个奇怪的困惑:

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
//= require underscore
//= require gmaps/google

然后像这样使用 application.css.scss:

/*

 * 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 any plugin's vendor/assets/stylesheets directory 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.
 *
 *= require_tree .
 *= require_self
 */

我认为//是JS文件中的注释,/* */也是CSS文件中的注释,我没有想太多,只是用过,但现在我想知道为什么JS中的所有注释和CSS 文件在 Rails 中没有效果?

最佳答案

是的,///* */分别是jscss文件的注释,但是 //=*=sprockets 使用的指令,来自 docs :

The DirectiveProcessor is responsible for parsing and evaluating directive comments in a source file.

A directive comment starts with a comment prefix, followed by an “=”, then the directive name, then any arguments.

// JavaScript
//= require "foo"

# CoffeeScript
#= require "bar"

/* CSS
 *= require "baz"
 */

因此 js.css 文件会将 //=*= 视为注释,但是 sprockets 将读取这些指令以将所需文件加载到 Rails Assets 管道中。

同时检查 sprockets guides获取更多详细信息。

关于javascript - 为什么所有注释对rails中的application.css.scss和application.js没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524116/

相关文章:

javascript - 鼠标悬停效果不起作用

html - 多彩多姿的图标字体

javascript - 如何让 jquery 工具提示悬停在我所有的单选按钮上

javascript - 如何切换 CZML 实体广告牌的可见性属性 (billboard.show)?

javascript - 在 Sencha CMD 的 Ext 之后包含 JS 文件

javascript - 单元测试 Angular 指令点击处理程序

android - 使用JS替换固定位置

ruby-on-rails - 解析 JSON 时出错 : undefined method 'gsub'

ruby-on-rails - Rails 验证以确保用户不会将自己加为好友

ruby-on-rails - 如何使用 Mongoid 和 Rails 对结果进行分组?