3

カスタム プロセッサを作成しています。このプロセッサは、ロード時にいくつかのファイルを動的にブラックリストに登録する必要があります。具体的には、「allow」と「restrict」という 2 つのディレクティブを追加しています。セットアップの例を次に示します。

./manifest.js

#= allow Group1
#= require_tree .

./some_included_file.js

#= restrict Group1

./some_other_file.js

#= restrict Group2

上記の場合、manifest.js が読み込まれ、some_include_file と some_other_file を読み込もうとします。ただし、「allow Group1」ディレクティブのため、最初のファイルのみをバンドルする必要があり、他のファイルは Group2 の制限 (許可されていない) のためバンドルしないでください。

以下は私がこれまでに持っているものです:

class CustomProcessor < Sprockets::DirectiveProcessor

  def process_allow_directive group

    unless defined? self.context.environment.custom_allow_list
      self.context.environment.class.module_eval { attr_accessor :custom_allow_list }
    end

    self.context.environment.custom_allow_list ||= []
    unless self.context.environment.custom_allow_list.include? group
      self.context.environment.custom_allow_list += [group]
    end
  end

  def process_restricted_directive *groups

    groups.each do |group|
      if defined? self.context.environment.custom_allow_list
        if self.context.environment.custom_allow_list.include? group

          # Allow the file to be included
          # Don't intervene
          return
        end
      end
    end

    # File is restricted, prevent loading, but....
    # ....HOW ??

  end
end

Rails.application.assets.register_preprocessor('application/javascript', CustomProcessor)
4

0 に答える 0