Skip to content

Commit

Permalink
Refactor the neuterfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
twinturbo committed Oct 7, 2012
1 parent 933a776 commit d655213
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
24 changes: 14 additions & 10 deletions lib/rake-pipeline-web-filters/neuter_filter.rb
Expand Up @@ -49,13 +49,13 @@ def batch(batch)
@batch.required(fullpath)
end

def read
source = super
def neuter
source = read

required_files = @batch.strip_requires(source).map do |req|
req_path = @batch.transform_path(req, self)
if req_path && !@batch.required?(File.expand_path(req_path, root))
@batch.file_wrapper(self.class, root, req_path, encoding).read
@batch.file_wrapper(self.class, root, req_path, encoding).neuter
else
nil
end
Expand All @@ -65,6 +65,12 @@ def read

(required_files << file).join("\n\n")
end

def dependencies
@batch.strip_requires(read).map do |req|
req_path = @batch.transform_path(req, self)
end
end
end

# A filter that takes files with requires and collapses them into a single
Expand Down Expand Up @@ -95,17 +101,15 @@ def generate_output(inputs, output)
known_files = [input.fullpath] + additional_dependencies(input)
batch = NeuterBatch.new(@config, known_files)
file = batch.file_wrapper(file_wrapper_class, input.root, input.path, input.encoding)
output.write file.read
output.write file.neuter
end
end

def additional_dependencies(input)
regexp = @config[:require_regexp] || %r{^\s*require\(['"]([^'"]*)['"]\);?\s*}
requires = input.read.scan regexp

requires.flatten.map do |file|
@config[:path_transform] ? @config[:path_transform].call(file) : file
end
return []
batch = NeuterBatch.new @config, [input.fullpath]
wrapper = batch.file_wrapper(file_wrapper_class, input.root, input.path, input.encoding)
wrapper.dependencies
end
end
end
3 changes: 1 addition & 2 deletions spec/neuter_filter_spec.rb
Expand Up @@ -175,10 +175,9 @@ def capture(*streams)
describe "additional_dependencies" do
let!(:main_input_file) { make_input("a.js", %Q{require("b");}) }
let!(:required_input_file) { make_input("b.js", "foo") }
let!(:path_transform) { proc { |f| "/path/to/input/#{f}.js" } }

it "determines them from require statments in the file" do
filter = Rake::Pipeline::Web::Filters::NeuterFilter.new :path_transform => path_transform
filter = Rake::Pipeline::Web::Filters::NeuterFilter.new
filter.input_files = [main_input_file]
filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new
Expand Down

0 comments on commit d655213

Please sign in to comment.