【问题标题】:What's the difference in changing application.js vs config.assets.precompile?更改 application.js 与 config.assets.precompile 有什么区别?
【发布时间】:2013-10-10 03:48:02
【问题描述】:

我正在研究资产预编译,我很困惑。

假设我创建了一个新应用程序,例如 rails new dummy

这是生成的app/assets/javascripts/application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

上面写着This is a manifest file that'll be compiled into application.js。到这一步还是很清楚的,但是我发现在config/application.rb下还有另外一个配置可以设置:config.assets.precompile << \some_regex\

现在我不清楚更改 application.jsconfig.assets.precompile 以选择要编译的内容有什么区别。

我觉得我在这里错过了大局,有人可以帮忙解释一下吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 asset-pipeline


    【解决方案1】:

    顾名思义,application.js 文件用于 Javascript。由于文件中的以下行,jquery、jquery_ujs、turbolinks javascript 库以及 /app/assets/javascripts 文件夹中的每个 Javascript 文件都将被预编译。

    //= require jquery
    //= require jquery_ujs
    //= require turbolinks
    //= require_tree .
    

    您可以使用“config.assets.precompile”添加其他需要预编译的东西,例如字体文件和其他Rails默认不识别的文件。您也可以使用它来包含 javascript 文件。但是,这种需求很少见。

    一个例子是......

    config.assets.precompile += %w( .svg .eot .woff .ttf )
    

    我希望这是有道理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-10
      • 2021-06-20
      • 2011-02-19
      • 2013-03-14
      • 2018-09-14
      • 2014-01-03
      • 2021-12-22
      相关资源
      最近更新 更多