Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #58 from kselden/minify-preserve-input

add :preserve_input option to uglify filter
  • Loading branch information...
commit 1a6dc173776b188836aa2ce2ac35b61c7f7daafe 2 parents 7eb2863 + 03e3339
@krisselden krisselden authored
View
18 lib/rake-pipeline-web-filters/uglify_filter.rb
@@ -35,10 +35,17 @@ def initialize(options={}, &block)
end
}
+ @preserve_input = options.delete :preserve_input
+
super(&block)
@options = options
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
# the {Filter} API. Compiles each input file with Uglify.
#
@@ -49,7 +56,7 @@ def initialize(options={}, &block)
# object representing the output.
def generate_output(inputs, output)
inputs.each do |input|
- if input.path =~ %r{.min.js$}
+ if should_skip_minify?(input, output)
output.write input.read
else
output.write Uglifier.compile(input.read, options)
@@ -59,6 +66,15 @@ def generate_output(inputs, output)
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
[ 'uglifier' ]
end
View
79 spec/uglify_filter_spec.rb
@@ -16,15 +16,11 @@
%[var name = "Truckasaurus Gates";\n\nconsole.log(name);;]
}
- def input_file(name, content)
- MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
- end
+ let(:filter_args) { [] }
+ let(:filter_block) { nil }
- def output_file(name)
- MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
- end
-
- def setup_filter(filter)
+ let(:filter) {
+ filter = UglifyFilter.new(*filter_args, &filter_block)
filter.file_wrapper_class = MemoryFileWrapper
filter.manifest = MemoryManifest.new
filter.last_manifest = MemoryManifest.new
@@ -32,11 +28,17 @@ def setup_filter(filter)
filter.output_root = "/path/to/output"
filter.rake_application = Rake::Application.new
filter
+ }
+
+ def input_file(name, content)
+ MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
end
- it "generates output" do
- filter = setup_filter UglifyFilter.new
+ def output_file(name)
+ MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
+ end
+ it "generates output" do
filter.output_files.should == [output_file("name.min.js")]
tasks = filter.generate_rake_tasks
@@ -49,7 +51,6 @@ def setup_filter(filter)
describe "Skipping" 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.output_files.should == [output_file("name.min.js")]
@@ -63,7 +64,6 @@ def setup_filter(filter)
end
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.output_files.should == [output_file("min.min.js")]
@@ -77,25 +77,52 @@ def setup_filter(filter)
end
end
- describe "naming output files" do
- it "translates .js extensions to .min.js by default" do
- filter = setup_filter UglifyFilter.new
- filter.output_files.first.path.should == "name.min.js"
+ it "translates .js extensions to .min.js by default" do
+ filter.output_files.first.path.should == "name.min.js"
+ end
+
+ context "with preserve_input option" do
+ let(:filter_args) do
+ [{ :preserve_input => true }]
end
- it "accepts a block to customize output file names" do
- filter = setup_filter(UglifyFilter.new { |input| "octopus" })
- filter.output_files.first.path.should == "octopus"
+ it "should output both the unminified and the minified files" do
+ filter.output_files.should == [output_file("name.js"), output_file("name.min.js")]
+
+ 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
- it "accepts options to pass to the Uglify compressor" do
- filter = setup_filter(UglifyFilter.new(:beautify => true))
- 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
+ context "with output name block" do
+ let(:filter_block) do
+ Proc.new { |input| "octopus" }
+ end
+
+ it "customizes output file names" do
+ filter.output_files.first.path.should == "octopus"
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.