链轮中的条件要求
如何使用Sprockets有条件地要求资产?
我在询问之前搜索了解决方案,并在Sprockets存储库中找到了这个讨论 – 条件要求
在那里讨论的解决方案是使用ERB:
我试过这样的方式:
app.js.erb
Rake文件
def bundle_app(debug) env = Sprockets::Environment.new env.append_path "app/" env.js_compressor = Uglifier.new assets = env.find_asset("app.js.erb") return assets.to_s end
但它会导致以下错误:
未定义的局部变量或方法`debug’用于#
肯定有一些易于修复的错误,但我是Ruby的新手并且无法发现它。
也许,因为您的示例使用debug作为参数,您可以使用结算在开发环境中拥有资产?
如果是这样,请在config / environments / development.rb中
config.assets.precompile << 'lib-debug.js'
一种可能的解决方案是将以下内容添加到bundle_app方法中:
env.context_class.class_eval "def debug; #{!!debug}; end"
更新的bundle_app()方法:
def bundle_app(debug) env = Sprockets::Environment.new env.append_path "app/" env.context_class.class_eval "def debug; #{!!debug}; end" env.js_compressor = Uglifier.new assets = env.find_asset("app.js.erb") return assets.to_s end