| 1 |
var test = require('tape'); |
| 2 |
var fs = require('fs'); |
| 3 |
var concat = require('concat-stream'); |
| 4 |
var PassThrough = require('stream').PassThrough; |
| 5 |
var convertSourceMap = require('convert-source-map'); |
| 6 |
var SourceMapConsumer = require('source-map').SourceMapConsumer; |
| 7 |
var uglify = require('uglify-js'); |
| 8 |
var staticModule = require('../'); |
| 9 |
|
| 10 |
test('source maps', function (t) { |
| 11 |
t.plan(6); |
| 12 |
|
| 13 |
var transform = staticModule({ |
| 14 |
sheetify: function (filename) { |
| 15 |
var stream = PassThrough(); |
| 16 |
stream.write('`.css{\n'); |
| 17 |
stream.write(' color: red;\n'); |
| 18 |
stream.end('}`'); |
| 19 |
return stream; |
| 20 |
} |
| 21 |
}, { sourceMap: true, inputFilename: 'main.js' }); |
| 22 |
|
| 23 |
fs.createReadStream(__dirname + '/sourcemap/main.js').pipe(transform).pipe(concat({ encoding: 'string' }, function (res) { |
| 24 |
var consumer = new SourceMapConsumer(convertSourceMap.fromSource(res).toObject()); |
| 25 |
|
| 26 |
var mapped = consumer.originalPositionFor({ |
| 27 |
line: 8, |
| 28 |
column: 0 |
| 29 |
}); |
| 30 |
t.equal(mapped.line, 8); |
| 31 |
t.equal(mapped.column, 0); |
| 32 |
|
| 33 |
mapped = consumer.originalPositionFor({ |
| 34 |
line: 10, |
| 35 |
column: 2 |
| 36 |
}); |
| 37 |
t.equal(mapped.line, 8); |
| 38 |
t.equal(mapped.column, 19); |
| 39 |
|
| 40 |
mapped = consumer.originalPositionFor({ |
| 41 |
line: 12, |
| 42 |
column: 0 |
| 43 |
}); |
| 44 |
t.equal(mapped.line, 10); |
| 45 |
t.equal(mapped.column, 0); |
| 46 |
})); |
| 47 |
}); |
| 48 |
|
| 49 |
test('input source map', function (t) { |
| 50 |
t.plan(4); |
| 51 |
|
| 52 |
var content = fs.readFileSync(__dirname + '/sourcemap/main.js', 'utf8'); |
| 53 |
var minified = uglify.minify({ 'main.js': content }, { |
| 54 |
output: { beautify: true }, |
| 55 |
sourceMap: { |
| 56 |
url: 'inline', |
| 57 |
includeSources: true |
| 58 |
} |
| 59 |
}); |
| 60 |
|
| 61 |
var transform = staticModule({ |
| 62 |
sheetify: function (filename) { |
| 63 |
var stream = PassThrough(); |
| 64 |
stream.end('`.css{\n color: orange;\n}`'); |
| 65 |
return stream; |
| 66 |
} |
| 67 |
}, { sourceMap: true, inputFilename: 'main.js' }); |
| 68 |
|
| 69 |
transform.pipe(concat({ encoding: 'string' }, function (res) { |
| 70 |
var consumer = new SourceMapConsumer(convertSourceMap.fromSource(res).toObject()); |
| 71 |
|
| 72 |
var mapped = consumer.originalPositionFor({ |
| 73 |
line: 7, |
| 74 |
column: 0 |
| 75 |
}); |
| 76 |
t.equal(mapped.line, 8); |
| 77 |
t.equal(mapped.column, 0); |
| 78 |
|
| 79 |
mapped = consumer.originalPositionFor({ |
| 80 |
line: 9, |
| 81 |
column: 4 |
| 82 |
}); |
| 83 |
t.equal(mapped.line, 10); |
| 84 |
t.equal(mapped.column, 0); |
| 85 |
})); |
| 86 |
|
| 87 |
transform.end(minified.code); |
| 88 |
}); |
| 89 |
|
| 90 |
test('retain input source map when file has no static-module use', function (t) { |
| 91 |
t.plan(4); |
| 92 |
|
| 93 |
var content = fs.readFileSync(__dirname + '/sourcemap/main.js', 'utf8'); |
| 94 |
var minified = uglify.minify({ 'main.js': content }, { |
| 95 |
output: { beautify: true }, |
| 96 |
sourceMap: { |
| 97 |
url: 'inline', |
| 98 |
includeSources: true |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
var transform = staticModule({ |
| 103 |
not_sheetify: function () { return 'whatever'; } |
| 104 |
}, { sourceMap: true, inputFilename: 'main.js' }); |
| 105 |
|
| 106 |
transform.pipe(concat({ encoding: 'string' }, function (res) { |
| 107 |
var consumer = new SourceMapConsumer(convertSourceMap.fromSource(res).toObject()); |
| 108 |
|
| 109 |
var mapped = consumer.originalPositionFor({ |
| 110 |
line: 7, |
| 111 |
column: 0 |
| 112 |
}); |
| 113 |
t.equal(mapped.line, 8); |
| 114 |
t.equal(mapped.column, 0); |
| 115 |
|
| 116 |
mapped = consumer.originalPositionFor({ |
| 117 |
line: 7, |
| 118 |
column: 21 |
| 119 |
}); |
| 120 |
t.equal(mapped.line, 10); |
| 121 |
t.equal(mapped.column, 0); |
| 122 |
})); |
| 123 |
|
| 124 |
|
| 125 |
transform.end(minified.code); |
| 126 |
}); |