1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
# encoding: UTF-8
require 'stringio'
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Uglifier" do
it "generates source maps" do
source = File.open("lib/uglify.js", "r:UTF-8").read
minified, map = Uglifier.new.compile_with_map(source)
expect(minified.length).to be < source.length
expect(map.length).to be > 0
expect { JSON.parse(map) }.not_to raise_error
end
it "generates source maps with the correct meta-data" do
source = <<-JS
function hello () {
function world () {
return 2;
};
return world() + world();
};
JS
_, map = Uglifier.compile_with_map(source,
:source_filename => "ahoy.js",
:output_filename => "ahoy.min.js",
:source_root => "http://localhost/")
map = SourceMap.from_s(map)
expect(map.file).to eq("ahoy.min.js")
expect(map.sources).to eq(["ahoy.js"])
expect(map.names).to eq(%w(hello world))
expect(map.source_root).to eq("http://localhost/")
expect(map.mappings.first[:generated_line]).to eq(1)
end
it "should skip copyright lines in source maps" do
source = <<-JS
/* @copyright Conrad Irwin */
function hello () {
function world () {
return 2;
};
return world() + world();
};
JS
_, map = Uglifier.compile_with_map(source,
:source_filename => "ahoy.js",
:source_root => "http://localhost/")
map = SourceMap.from_s(map)
expect(map.mappings.first[:generated_line]).to eq(2)
end
it "should be able to handle an input source map" do
source = <<-JS
function hello () {
function world () {
return 2;
};
return world() + world();
};
JS
minified1, map1 = Uglifier.compile_with_map(
source,
:source_filename => "ahoy.js",
:source_root => "http://localhost/",
:mangle => false
)
_, map2 = Uglifier.compile_with_map(source,
:input_source_map => map1,
:mangle => true)
expect(minified1.lines.to_a.length).to eq(1)
map = SourceMap.from_s(map2)
expect(map.sources).to eq(["http://localhost/ahoy.js"])
expect(map.mappings.first[:source_line]).to eq(1)
expect(map.mappings.last[:source_line]).to eq(6)
end
end
|