source-content.js
5.55 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
/*jshint asi: true*/
var test = require('tap').test
var generator = require('..');
var foo = '' + function foo () {
var hello = 'hello';
var world = 'world';
console.log('%s %s', hello, world);
}
var bar = '' + function bar () {
console.log('yes?');
}
function decode(base64) {
return new Buffer(base64, 'base64').toString();
}
function inspect(obj, depth) {
console.log(require('util').inspect(obj, false, depth || 5, true));
}
test('generated mappings', function (t) {
t.test('one file with source content', function (t) {
var gen = generator()
.addGeneratedMappings('foo.js', foo)
.addSourceContent('foo.js', foo)
t.deepEqual(
gen.toJSON()
, { "version": 3,
"file": "",
"sources": [
"foo.js"
],
"names": [],
"mappings": "AAAA;AACA;AACA;AACA;AACA",
"sourceRoot": "",
"sourcesContent": [
"function foo() {\n var hello = 'hello';\n var world = 'world';\n console.log('%s %s', hello, world);\n}"
],
}
, 'includes source content'
)
t.equal(
decode(gen.base64Encode())
, '{"version":3,"sources":["foo.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA","file":"","sourceRoot":"","sourcesContent":["function foo() {\\n var hello = \'hello\';\\n var world = \'world\';\\n console.log(\'%s %s\', hello, world);\\n}"]}'
, 'encodes generated mappings including source content'
)
t.equal(
gen.inlineMappingUrl()
, '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59Il19'
, 'returns correct inline mapping url including source content'
)
t.end()
})
t.test('two files with source content', function (t) {
var gen = generator()
.addGeneratedMappings('foo.js', foo)
.addSourceContent('foo.js', foo)
.addGeneratedMappings('bar.js', bar)
.addSourceContent('bar.js', bar)
t.deepEqual(
gen.toJSON()
, { "version": 3,
"file": "",
"sources": [
"foo.js",
"bar.js"
],
"names": [],
"mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
"sourceRoot": "",
"sourcesContent": [
"function foo() {\n var hello = 'hello';\n var world = 'world';\n console.log('%s %s', hello, world);\n}",
"function bar() {\n console.log('yes?');\n}"
],
}
, 'includes source content for both files'
)
t.deepEqual(
decode(gen.base64Encode())
, '{"version":3,"sources":["foo.js","bar.js"],"names":[],"mappings":"ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA","file":"","sourceRoot":"","sourcesContent":["function foo() {\\n var hello = \'hello\';\\n var world = \'world\';\\n console.log(\'%s %s\', hello, world);\\n}","function bar() {\\n console.log(\'yes?\');\\n}"]}'
, 'encodes generated mappings including source content'
)
t.equal(
gen.inlineMappingUrl()
, '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59IiwiZnVuY3Rpb24gYmFyKCkge1xuICBjb25zb2xlLmxvZygneWVzPycpO1xufSJdfQ=='
, 'returns correct inline mapping url including source content'
)
t.end()
})
t.test('two files, only one with source content', function (t) {
var gen = generator()
.addGeneratedMappings('foo.js', foo)
.addGeneratedMappings('bar.js', bar)
.addSourceContent('bar.js', bar)
t.deepEqual(
gen.toJSON()
, { "version": 3,
"file": "",
"sources": [
"foo.js",
"bar.js"
],
"names": [],
"mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
"sourcesContent": [ null, "function bar() {\n console.log('yes?');\n}" ],
"sourceRoot": ""
}
, 'includes source content for the file with source content and [null] for the other file'
)
t.deepEqual(
decode(gen.base64Encode())
, '{"version":3,"sources":["foo.js","bar.js"],"names":[],"mappings":"ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA","file":"","sourceRoot":"","sourcesContent":[null,"function bar() {\\n console.log(\'yes?\');\\n}"]}'
, 'encodes generated mappings including source content'
)
t.equal(
gen.inlineMappingUrl()
, '//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6W251bGwsImZ1bmN0aW9uIGJhcigpIHtcbiAgY29uc29sZS5sb2coJ3llcz8nKTtcbn0iXX0='
, 'returns correct inline mapping url including source content'
)
t.end()
})
t.test('one file with empty source', function (t) {
var gen = generator()
.addGeneratedMappings('empty.js', '')
.addSourceContent('empty.js', '')
t.deepEqual(gen.toJSON()["sourcesContent"], [""])
t.end()
});
})