Skip to content

Commit

Permalink
Merge pull request #58 from kselden/minify-preserve-input
Browse files Browse the repository at this point in the history
add :preserve_input option to uglify filter
  • Loading branch information
krisselden committed Nov 16, 2012
2 parents 7eb2863 + 03e3339 commit 1a6dc17
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 27 deletions.
18 changes: 17 additions & 1 deletion lib/rake-pipeline-web-filters/uglify_filter.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ def initialize(options={}, &block)
end end
} }


@preserve_input = options.delete :preserve_input

super(&block) super(&block)
@options = options @options = options
end end


def should_skip_minify?(input, output)
(@preserve_input && input.path == output.path) ||
input.path =~ %r{.min.js$}
end

# Implement the {#generate_output} method required by # Implement the {#generate_output} method required by
# the {Filter} API. Compiles each input file with Uglify. # the {Filter} API. Compiles each input file with Uglify.
# #
Expand All @@ -49,7 +56,7 @@ def initialize(options={}, &block)
# object representing the output. # object representing the output.
def generate_output(inputs, output) def generate_output(inputs, output)
inputs.each do |input| inputs.each do |input|
if input.path =~ %r{.min.js$} if should_skip_minify?(input, output)
output.write input.read output.write input.read
else else
output.write Uglifier.compile(input.read, options) output.write Uglifier.compile(input.read, options)
Expand All @@ -59,6 +66,15 @@ def generate_output(inputs, output)


private private


def output_paths(input)
paths = super(input)
if @preserve_input
raise 'cannot preserve unminified input if output path is not different' if paths.include?(input.path)
paths.unshift(input.path)
end
paths
end

def external_dependencies def external_dependencies
[ 'uglifier' ] [ 'uglifier' ]
end end
Expand Down
79 changes: 53 additions & 26 deletions spec/uglify_filter_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@
%[var name = "Truckasaurus Gates";\n\nconsole.log(name);;] %[var name = "Truckasaurus Gates";\n\nconsole.log(name);;]
} }


def input_file(name, content) let(:filter_args) { [] }
MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content) let(:filter_block) { nil }
end


def output_file(name) let(:filter) {
MemoryFileWrapper.new("/path/to/output", name, "UTF-8") filter = UglifyFilter.new(*filter_args, &filter_block)
end

def setup_filter(filter)
filter.file_wrapper_class = MemoryFileWrapper filter.file_wrapper_class = MemoryFileWrapper
filter.manifest = MemoryManifest.new filter.manifest = MemoryManifest.new
filter.last_manifest = MemoryManifest.new filter.last_manifest = MemoryManifest.new
filter.input_files = [input_file("name.js", js_input)] filter.input_files = [input_file("name.js", js_input)]
filter.output_root = "/path/to/output" filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new filter.rake_application = Rake::Application.new
filter filter
}

def input_file(name, content)
MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
end end


it "generates output" do def output_file(name)
filter = setup_filter UglifyFilter.new MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
end


it "generates output" do
filter.output_files.should == [output_file("name.min.js")] filter.output_files.should == [output_file("name.min.js")]


tasks = filter.generate_rake_tasks tasks = filter.generate_rake_tasks
Expand All @@ -49,7 +51,6 @@ def setup_filter(filter)


describe "Skipping" do describe "Skipping" do
it "skips files ending in .min.js" do it "skips files ending in .min.js" do
filter = setup_filter UglifyFilter.new
filter.input_files = [input_file("name.min.js", 'fake-js')] filter.input_files = [input_file("name.min.js", 'fake-js')]


filter.output_files.should == [output_file("name.min.js")] filter.output_files.should == [output_file("name.min.js")]
Expand All @@ -63,7 +64,6 @@ def setup_filter(filter)
end end


it "does not count files ending in min.js as preminified" do it "does not count files ending in min.js as preminified" do
filter = setup_filter UglifyFilter.new
filter.input_files = [input_file("min.js", js_input)] filter.input_files = [input_file("min.js", js_input)]


filter.output_files.should == [output_file("min.min.js")] filter.output_files.should == [output_file("min.min.js")]
Expand All @@ -77,25 +77,52 @@ def setup_filter(filter)
end end
end end


describe "naming output files" do it "translates .js extensions to .min.js by default" do
it "translates .js extensions to .min.js by default" do filter.output_files.first.path.should == "name.min.js"
filter = setup_filter UglifyFilter.new end
filter.output_files.first.path.should == "name.min.js"
context "with preserve_input option" do
let(:filter_args) do
[{ :preserve_input => true }]
end end


it "accepts a block to customize output file names" do it "should output both the unminified and the minified files" do
filter = setup_filter(UglifyFilter.new { |input| "octopus" }) filter.output_files.should == [output_file("name.js"), output_file("name.min.js")]
filter.output_files.first.path.should == "octopus"
tasks = filter.generate_rake_tasks
tasks.each(&:invoke)

file = MemoryFileWrapper.files["/path/to/output/name.js"]
file.body.should == js_input
file.encoding.should == "UTF-8"

file = MemoryFileWrapper.files["/path/to/output/name.min.js"]
file.body.should == expected_js_output
file.encoding.should == "UTF-8"
end end
end end


it "accepts options to pass to the Uglify compressor" do context "with output name block" do
filter = setup_filter(UglifyFilter.new(:beautify => true)) let(:filter_block) do
filter.input_files = [input_file("name.js", js_input)] Proc.new { |input| "octopus" }
tasks = filter.generate_rake_tasks end
tasks.each(&:invoke)
file = MemoryFileWrapper.files["/path/to/output/name.min.js"] it "customizes output file names" do
file.body.should == expected_beautiful_js_output filter.output_files.first.path.should == "octopus"
end
end end


context "with Uglify options" do
let(:filter_args) do
[{ :beautify => true }]
end

it "passes options to the Uglify compressor" do
filter.input_files = [input_file("name.js", js_input)]
tasks = filter.generate_rake_tasks
tasks.each(&:invoke)
file = MemoryFileWrapper.files["/path/to/output/name.min.js"]
file.body.should == expected_beautiful_js_output
end
end
end end

0 comments on commit 1a6dc17

Please sign in to comment.