Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80556 views
1
'use strict';
2
/*jshint asi: true*/
3
4
var test = require('tap').test
5
var generator = require('..');
6
7
var foo = '' + function foo () {
8
var hello = 'hello';
9
var world = 'world';
10
console.log('%s %s', hello, world);
11
}
12
13
var bar = '' + function bar () {
14
console.log('yes?');
15
}
16
17
function decode(base64) {
18
return new Buffer(base64, 'base64').toString();
19
}
20
21
function inspect(obj, depth) {
22
console.log(require('util').inspect(obj, false, depth || 5, true));
23
}
24
25
test('generated mappings', function (t) {
26
27
t.test('one file with source content', function (t) {
28
var gen = generator()
29
.addGeneratedMappings('foo.js', foo)
30
.addSourceContent('foo.js', foo)
31
32
t.deepEqual(
33
gen.toJSON()
34
, { "version": 3,
35
"file": "",
36
"sources": [
37
"foo.js"
38
],
39
"names": [],
40
"mappings": "AAAA;AACA;AACA;AACA;AACA",
41
"sourceRoot": "",
42
"sourcesContent": [
43
"function foo() {\n var hello = 'hello';\n var world = 'world';\n console.log('%s %s', hello, world);\n}"
44
],
45
}
46
, 'includes source content'
47
)
48
49
t.equal(
50
decode(gen.base64Encode())
51
, '{"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}"]}'
52
, 'encodes generated mappings including source content'
53
)
54
t.equal(
55
gen.inlineMappingUrl()
56
, '//# sourceMappingURL=data:application/json;charset:utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59Il19'
57
, 'returns correct inline mapping url including source content'
58
)
59
t.end()
60
})
61
62
t.test('two files with source content', function (t) {
63
var gen = generator()
64
.addGeneratedMappings('foo.js', foo)
65
.addSourceContent('foo.js', foo)
66
.addGeneratedMappings('bar.js', bar)
67
.addSourceContent('bar.js', bar)
68
69
t.deepEqual(
70
gen.toJSON()
71
, { "version": 3,
72
"file": "",
73
"sources": [
74
"foo.js",
75
"bar.js"
76
],
77
"names": [],
78
"mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
79
"sourceRoot": "",
80
"sourcesContent": [
81
"function foo() {\n var hello = 'hello';\n var world = 'world';\n console.log('%s %s', hello, world);\n}",
82
"function bar() {\n console.log('yes?');\n}"
83
],
84
}
85
, 'includes source content for both files'
86
)
87
88
t.deepEqual(
89
decode(gen.base64Encode())
90
, '{"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}"]}'
91
, 'encodes generated mappings including source content'
92
)
93
t.equal(
94
gen.inlineMappingUrl()
95
, '//# sourceMappingURL=data:application/json;charset:utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6WyJmdW5jdGlvbiBmb28oKSB7XG4gIHZhciBoZWxsbyA9ICdoZWxsbyc7XG4gIHZhciB3b3JsZCA9ICd3b3JsZCc7XG4gIGNvbnNvbGUubG9nKCclcyAlcycsIGhlbGxvLCB3b3JsZCk7XG59IiwiZnVuY3Rpb24gYmFyKCkge1xuICBjb25zb2xlLmxvZygneWVzPycpO1xufSJdfQ=='
96
, 'returns correct inline mapping url including source content'
97
)
98
t.end()
99
})
100
101
t.test('two files, only one with source content', function (t) {
102
var gen = generator()
103
.addGeneratedMappings('foo.js', foo)
104
.addGeneratedMappings('bar.js', bar)
105
.addSourceContent('bar.js', bar)
106
107
t.deepEqual(
108
gen.toJSON()
109
, { "version": 3,
110
"file": "",
111
"sources": [
112
"foo.js",
113
"bar.js"
114
],
115
"names": [],
116
"mappings": "ACAA,ADAA;ACCA,ADAA;ACCA,ADAA;AACA;AACA",
117
"sourcesContent": [ null, "function bar() {\n console.log('yes?');\n}" ],
118
"sourceRoot": ""
119
}
120
, 'includes source content for the file with source content and [null] for the other file'
121
)
122
123
t.deepEqual(
124
decode(gen.base64Encode())
125
, '{"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}"]}'
126
, 'encodes generated mappings including source content'
127
)
128
t.equal(
129
gen.inlineMappingUrl()
130
, '//# sourceMappingURL=data:application/json;charset:utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5qcyIsImJhci5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUNBQSxBREFBO0FDQ0EsQURBQTtBQ0NBLEFEQUE7QUFDQTtBQUNBIiwiZmlsZSI6IiIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzQ29udGVudCI6W251bGwsImZ1bmN0aW9uIGJhcigpIHtcbiAgY29uc29sZS5sb2coJ3llcz8nKTtcbn0iXX0='
131
, 'returns correct inline mapping url including source content'
132
)
133
t.end()
134
})
135
})
136
137