Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add :preserve_input option to uglify filter #58

Merged
merged 1 commit into from

1 participant

Kris Selden
Kris Selden
Collaborator

No description provided.

Kris Selden krisselden merged commit 1a6dc17 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 16, 2012
  1. Kris Selden
This page is out of date. Refresh to see the latest.
18 lib/rake-pipeline-web-filters/uglify_filter.rb
View
@@ -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
79 spec/uglify_filter_spec.rb
View
@@ -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
Something went wrong with that request. Please try again.