Rails 3.1 资产管道:忽略来自 gem 的资产

发布于 2024-12-01 06:37:46 字数 330 浏览 3 评论 0原文

我不太确定实际行为是什么,所以我的第一个问题是:
来自 gem(在我的例子中 Spree)的资产(例如 javascript)是否总是被编译?我不使用 Spree 的 javascript,因此不希望编译它们。我的 application.js 或任何其他 javascript 文件中不需要它们,但

rake assets:precompile

仍然编译它们。我只是不想让它们躺在我的 public/assets 文件夹中。

所以我想我的问题是,有没有办法禁用从 gem 编译 javascript?

I'm not quite sure what the actual behavior is, so my first question is:
Are assets (e.g. javascripts) from a gem (in my case Spree) always compiled? I don't use Spree's javascripts, and therefore don't want them to be compiled. I don't require them in my application.js or any other javascript file, but

rake assets:precompile

compiles them nonetheless. I just don't want them lying around in my public/assets folder.

So I guess my question is, is there a way to disable compiling javascripts from a gem?

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

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

发布评论

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

评论(2

难忘№最初的完美 2024-12-08 06:37:46

Rails 4.X

它在 Rails 4.X 上不起作用,一个可能的(肮脏的)解决方法是:

require 'sprockets/railtie'

Bundler.require(:default, Rails.env)

module Sprockets
  module Paths
    SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]

    def append_path_with_rails_assets(path)
      append_path_without_rails_assets(path) unless SKIP_GEMS.any? { |gem| path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path) }
    end

    alias_method_chain :append_path, :rails_assets
  end
end

自 Rails 5.X 起,Rails 5.X alias_method_chain 的更新

已被弃用。这是使用 prepend 的更新版本,并覆盖 Sprockets::Environment 模块而不是 Sprockets::Paths

module SprocketsPathsOverride
  SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]

  def append_path(path)
    should_skip = SKIP_GEMS.any? do |gem|
      path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path)
    end
    super(path) unless should_skip
  end
end

Sprockets::Environment.prepend(SprocketsPathsOverride)

Rails 4.X

It didn't work on Rails 4.X, a possible (dirty) workaround is:

require 'sprockets/railtie'

Bundler.require(:default, Rails.env)

module Sprockets
  module Paths
    SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]

    def append_path_with_rails_assets(path)
      append_path_without_rails_assets(path) unless SKIP_GEMS.any? { |gem| path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path) }
    end

    alias_method_chain :append_path, :rails_assets
  end
end

Update for Rails 5.X

alias_method_chain is deprecated since Rails 5.X. Here is an updated version using prepend, and overriding the Sprockets::Environment module instead of Sprockets::Paths.

module SprocketsPathsOverride
  SKIP_GEMS = ["rails-assets-jquery", "rails-assets-bootstrap"]

  def append_path(path)
    should_skip = SKIP_GEMS.any? do |gem|
      path.to_s.start_with?(Gem.loaded_specs[gem].full_gem_path)
    end
    super(path) unless should_skip
  end
end

Sprockets::Environment.prepend(SprocketsPathsOverride)
姜生凉生 2024-12-08 06:37:46

我想有一种聪明的方法可以使用sprockets来实现您的目标。也许是一些 require_directory 而不是 require_tree

但最直接的方法是将这些资产从您的资产路径中删除。要实现此目的,请将此添加到 application.rb 文件的最末尾(在初始化程序中不起作用):

class Engine < Rails::Engine
   initializer "remove assets directories from pipeline" do |app|
     app.config.assets.paths = app.config.assets.paths - app.config.assets.paths.grep(/nice_regexp_here_to_match_the_dir_where_the_unwanted_files_live/)
   end
end

刚刚尝试了hack:将代码放入 initializer 中,但在 application.rb 末尾需要它:

require "config/initializers/your_file'

我更喜欢以这种方式显示非常具体的代码。

I guess there is a smart way to achieve your goal using sprockets. Maybe some require_directory instead of require_tree.

But the most direct thing would be to remove theses assets from your assets paths. To achieve this, add this at the very end of your application.rb file (doesn't work in an initializer):

class Engine < Rails::Engine
   initializer "remove assets directories from pipeline" do |app|
     app.config.assets.paths = app.config.assets.paths - app.config.assets.paths.grep(/nice_regexp_here_to_match_the_dir_where_the_unwanted_files_live/)
   end
end

Just tried a hack: put the code in an initializer but require it at the end of your application.rb:

require "config/initializers/your_file'

I prefer very specific code to be visible this way.

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