当我像这样在 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 中没有效果?
最佳答案
是的,//
和/* */
分别是js
和css
文件的注释,但是 //=
和 *=
是 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/