Skip to content
Browse files

Merge pull request #41 from twinturbo/iife

Add an IIFE filter
  • Loading branch information...
2 parents 51915f6 + 8bdd917 commit f2354be96a5f36273750de0abe4af61a9600aa69 @wycats committed
View
1 lib/rake-pipeline-web-filters.rb
@@ -26,4 +26,5 @@ module Filters
require "rake-pipeline-web-filters/chained_filter"
require "rake-pipeline-web-filters/less_filter"
require "rake-pipeline-web-filters/handlebars_filter"
+require "rake-pipeline-web-filters/iife_filter"
require "rake-pipeline-web-filters/helpers"
View
8 lib/rake-pipeline-web-filters/helpers.rb
@@ -32,7 +32,7 @@ def sass(*args, &block)
filter(Rake::Pipeline::Web::Filters::SassFilter, *args, &block)
end
alias_method :scss, :sass
-
+
# Add a new {StylusFilter} to the pipeline.
# @see StylusFilter#initialize
def stylus(*args, &block)
@@ -98,6 +98,12 @@ def less(*args, &block)
def handlebars(*args, &block)
filter(Rake::Pipeline::Web::Filters::HandlebarsFilter, *args, &block)
end
+ #
+ # Add a new {IifeFilter} to the pipeline.
+ # @see IifeFilter#initialize
+ def iife(*args, &block)
+ filter(Rake::Pipeline::Web::Filters::IifeFilter, *args, &block)
+ end
end
module ProjectHelpers
View
38 lib/rake-pipeline-web-filters/iife_filter.rb
@@ -0,0 +1,38 @@
+module Rake::Pipeline::Web::Filters
+ # A filter that wraps input files in an IIFE (immediately invoked functional expression)
+ #
+ #
+ # @example
+ # !!!ruby
+ # Rake::Pipeline.build do
+ # input "app/assets", "**/*.js"
+ # output "public"
+ #
+ # # Wrap each file in: (function() { ... })();"
+ # filter Rake::Pipeline::Web::Filters::IifeFilter
+ # end
+ class IifeFilter < Rake::Pipeline::Filter
+ # @param [Proc] block a block to use as the Filter's
+ # {#output_name_generator}.
+ def initialize(&block)
+ block ||= proc { |input| input }
+ super(&block)
+ end
+
+ # Implement the {#generate_output} method required by
+ # the {Filter} API. Wraps each input in an IIFE.
+ #
+ # @param [Array<FileWrapper>] inputs an Array of
+ # {FileWrapper} objects representing the inputs to
+ # this filter.
+ # @param [FileWrapper] output a single {FileWrapper}
+ # object representing the output.
+ def generate_output(inputs, output)
+ inputs.each do |input|
+ output.write "(function() {\n"
+ output.write input.read
+ output.write "})();"
+ end
+ end
+ end
+end
View
7 spec/helpers_spec.rb
@@ -102,6 +102,13 @@ def filter
filter.should be_kind_of(Rake::Pipeline::Web::Filters::HandlebarsFilter)
end
end
+
+ describe "#iffe" do
+ it "creates a IifeFilter" do
+ dsl.iife
+ filter.should be_kind_of(Rake::Pipeline::Web::Filters::IifeFilter)
+ end
+ end
end
describe "ProjectHelpers" do
View
55 spec/iife_filter_spec.rb
@@ -0,0 +1,55 @@
+describe "IifeFilter" do
+ IifeFilter ||= Rake::Pipeline::Web::Filters::IifeFilter
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
+
+ let(:js_input) { <<-HERE }
+var name = "Truckasaurus Gates";
+HERE
+
+ let(:expected_js_output) { <<-HERE }
+(function() {
+var name = "Truckasaurus Gates";
+})();
+HERE
+
+ def input_file(name, content)
+ MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
+ end
+
+ def output_file(name)
+ MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
+ end
+
+ def setup_filter(filter)
+ filter.file_wrapper_class = MemoryFileWrapper
+ filter.input_files = [input_file("name.js", js_input)]
+ filter.output_root = "/path/to/output"
+ filter.rake_application = Rake::Application.new
+ filter
+ end
+
+ it "generates output" do
+ filter = setup_filter IifeFilter.new
+
+ filter.output_files.should == [output_file("name.js")]
+
+ tasks = filter.generate_rake_tasks
+ tasks.each(&:invoke)
+
+ file = MemoryFileWrapper.files["/path/to/output/name.js"]
+ file.body.should == expected_js_output.chomp
+ file.encoding.should == "UTF-8"
+ end
+
+ describe "naming output files" do
+ it "translates .js extensions to .min.js by default" do
+ filter = setup_filter IifeFilter.new
+ filter.output_files.first.path.should == "name.js"
+ end
+
+ it "accepts a block to customize output file names" do
+ filter = setup_filter(IifeFilter.new { |input| "octopus" })
+ filter.output_files.first.path.should == "octopus"
+ end
+ end
+end

0 comments on commit f2354be

Please sign in to comment.
Something went wrong with that request. Please try again.