gpt4 book ai didi

javascript - Rails 什么时候编译 CoffeeScript?

转载 作者:行者123 更新时间:2023-12-03 13:29:40 25 4
gpt4 key购买 nike

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

最佳答案

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

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

rake assets:precompile

看到这个 link有关 Rails Assets 管道的更多信息。

从链接:

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



更新:
对于那些对为开发预编译 Assets 感兴趣的人,请先将 RAILS_ENV 变量设置为 development(来自 here):
RAILS_ENV=development bundle exec rake assets:precompile

关于javascript - Rails 什么时候编译 CoffeeScript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8162998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com