【问题标题】:Is it it possible to have the Rails Asset Pipeline compile Coffeescript on every request?是否可以让 Rails 资产管道在每个请求上编译 Coffeescript?
【发布时间】:2013-07-20 17:15:15
【问题描述】:

我正在使用 Coffeescript 构建一个 JS 小部件。我正在玩一些技巧,以便在编译时将我的 CSS/SCSS 和 HTML/HAML 模板放入 JS 小部件中:

# widget.js.coffee.erb
class Widget
  constructor: (options) ->
  @template = <%= Haml::Engine.new(
    File.read(
    File.expand_path("../../templates/widget.html.haml", __FILE__))).
    render.to_json %>

  @css = <%= Sass.compile_file(
    File.expand_path("../../stylesheets/widget.css.scss", __FILE__)).
    to_json  %>

问题是当我更改 HTML 或 CSS 时,Rails 不会重新编译开发中的 Coffeescript,因为 coffeescript 文件没有更改。我需要进行愚蠢的空白更改以强制它重新编译东西。这越来越烦人了:-)

关于如何强制资产管道在每个请求上编译某些文件的任何想法?

【问题讨论】:

    标签: ruby-on-rails coffeescript asset-pipeline


    【解决方案1】:

    知道了。 Sprockets 有一个depend_on 指令。将以下行添加到我的 widget.js.coffee.erb 解决它:

    # coffee.js.coffee.erb
    #= depend_on '../templates/widget.html.haml'
    #= depend_on '../stylesheets/widget.css.scss'
    
    class Widget
    ...
    

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 2013-05-08
      • 1970-01-01
      • 2012-05-20
      • 2014-08-28
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多