diff --git a/lib/rake-pipeline-web-filters/neuter_filter.rb b/lib/rake-pipeline-web-filters/neuter_filter.rb index 8f3bb8e..c57456a 100644 --- a/lib/rake-pipeline-web-filters/neuter_filter.rb +++ b/lib/rake-pipeline-web-filters/neuter_filter.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/neuter_filter_spec.rb b/spec/neuter_filter_spec.rb index 25ae42d..2f5abab 100644 --- a/spec/neuter_filter_spec.rb +++ b/spec/neuter_filter_spec.rb @@ -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