我正在创建一个自定义处理器,它需要在加载某些文件时将它们动态列入黑名单。特别是,我添加了两个指令“允许”和“限制”。以下是设置示例:
./manifest.js
#= allow Group1
#= require_tree .
./some_included_file.js
#= restrict Group1
./some_other_file.js
#= restrict Group2
在上述情况下,manifest.js 被加载,并尝试加载 some_included_file 和 some_other_file。然而,由于“允许 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)