Rails 何时编译 CoffeeScript?

发布于 2024-12-16 14:07:47 字数 62 浏览 3 评论 0原文

Rails 何时将 Coffeescript 资源编译为 JavaScript?它是按需发生还是在启动时发生?

When does Rails compile Coffeescript resources to JavaScript? Does it happen on-demand or at startup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与酒说心事 2024-12-23 14:07:47

当向包含 CoffeeScript 资源的视图发出请求时,CoffeeScript 会按需编译为 JavaScript。

但是,您可以使用以下命令提前将 CoffeeScript 编译为 JavaScript:

rake assets:precompile

请参阅此链接了解有关 Rails 资产管道的更多信息。

从链接:

资产上使用的文件扩展名决定了
应用预处理。当控制器或脚手架生成时
使用默认的 Rails gemset,一个 CoffeeScript 文件和一个 SCSS 文件是
生成代替常规 JavaScript 和 CSS 文件。例子
之前使用的是一个名为“projects”的控制器,它生成了一个
app/assets/javascripts/projects.js.coffee 和
app/assets/stylesheets/projects.css.scss 文件。

当请求这些文件时,它们由处理器处理
由 Coffee-Script 和 sass-rails gem 提供,然后发回
分别作为 JavaScript 和 CSS 发送给浏览器。

...

资源在服务器之后的第一个请求上编译并缓存
已开始。 Sprockets 设置必须重新验证 Cache-Control HTTP 标头
减少后续请求的请求开销 - 在这些请求上
浏览器收到 304(未修改)响应。

如果清单中的任何文件在请求之间发生了更改,
服务器响应一个新的编译文件。

...

Rails 捆绑了一个 rake 任务来编译资产清单
以及管道中的其他文件到磁盘。

编译后的资源被写入到指定的位置
配置.资产.前缀。默认设置将使用公共/资产
目录。

您必须在部署期间或在本地使用此任务(如果您这样做)
没有对生产文件系统的写入权限。

rake 任务是:

捆绑执行rake资产:预编译

UPDATE:
对于那些对预编译开发资源感兴趣的人,请首先将 RAILS_ENV 变量设置为开发(来自 在这里):

RAILS_ENV=development bundle exec rake assets:precompile

CoffeeScript compiles to JavaScript on demand when a request is made to a view that includes a CoffeeScript resource.

However, you can use the following to have CoffeeScript compile to JavaScript ahead of time:

rake assets:precompile

See this link for more information on the Rails asset pipeline.

From the link:

The file extensions used on an asset determine what
preprocessing is applied. When a controller or a scaffold is generated
with the default Rails gemset, a CoffeeScript file and a SCSS file are
generated in place of a regular JavaScript and CSS file. The example
used before was a controller called “projects”, which generated an
app/assets/javascripts/projects.js.coffee and an
app/assets/stylesheets/projects.css.scss file.

When these files are requested, they are processed by the processors
provided by the coffee-script and sass-rails gems and then sent back
to the browser as JavaScript and CSS respectively.

...

Assets are compiled and cached on the first request after the server
is started. Sprockets sets a must-revalidate Cache-Control HTTP header
to reduce request overhead on subsequent requests — on these the
browser gets a 304 (Not Modified) response.

If any of the files in the manifest have changed between requests, the
server responds with a new compiled file.

...

Rails comes bundled with a rake task to compile the asset manifests
and other files in the pipeline to the disk.

Compiled assets are written to the location specified in
config.assets.prefix. The default setting will use the public/assets
directory.

You must use this task either during deployment or locally if you do
not have write access to your production filesystem.

The rake task is:

bundle exec rake assets:precompile

UPDATE:
For those interested in precompiling assets for development, set the RAILS_ENV variable to development first (from here):

RAILS_ENV=development bundle exec rake assets:precompile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文