Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var test = require('tap').test;
2
var pack = require('../');
3
var path = require('path');
4
5
function decode(base64) {
6
return new Buffer(base64, 'base64').toString();
7
}
8
9
function unmountPrelude(sources) {
10
return sources.map(function (x) {
11
var basename = path.basename(x);
12
return basename === '_prelude.js' ? basename : x;
13
});
14
}
15
16
function grabSourceMap(lastLine) {
17
var base64 = lastLine.split(',').pop();
18
var sm = JSON.parse(decode(base64));
19
sm.sources = unmountPrelude(sm.sources);
20
return sm;
21
}
22
23
function grabLastLine(src) {
24
return src.split('\n').slice(-2)[0];
25
}
26
27
test('pack one file with source file field and one without', function (t) {
28
t.plan(7);
29
30
var p = pack();
31
var src = '';
32
p.on('data', function (buf) { src += buf });
33
p.on('end', function () {
34
var r = Function(['T'], 'return ' + src)(t);
35
t.equal(r('xyz')(5), 555);
36
t.equal(r('xyz')(5), 555);
37
38
var lastLine = grabLastLine(src);
39
var sm = grabSourceMap(lastLine);
40
41
t.ok(/^\/\/# sourceMappingURL/.test(lastLine), 'contains source mapping url as last line');
42
t.deepEqual(sm.sources, [ '_prelude.js', 'foo.js' ], 'includes mappings for sourceFile and prelude only');
43
t.equal(sm.mappings, 'AAAA;;;ACAA;AACA;AACA;AACA', 'adds offset mapping for each line' );
44
});
45
46
p.end(JSON.stringify([
47
{
48
id: 'abc',
49
source: 'T.equal(require("./xyz")(3), 333)',
50
entry: true,
51
deps: { './xyz': 'xyz' }
52
},
53
{
54
id: 'xyz',
55
source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}',
56
sourceFile: 'foo.js'
57
}
58
]));
59
});
60
61
test('pack two files with source file field', function (t) {
62
t.plan(7);
63
64
var p = pack();
65
var src = '';
66
p.on('data', function (buf) { src += buf });
67
p.on('end', function () {
68
var r = Function(['T'], 'return ' + src)(t);
69
t.equal(r('xyz')(5), 555);
70
t.equal(r('xyz')(5), 555);
71
72
var lastLine = grabLastLine(src);
73
var sm = grabSourceMap(lastLine);
74
75
t.ok(/^\/\/# sourceMappingURL/.test(lastLine), 'contains source mapping url as last line');
76
t.deepEqual(sm.sources, [ '_prelude.js', 'wunder/bar.js', 'foo.js' ], 'includes mappings for both files and prelude');
77
t.equal(sm.mappings, 'AAAA;ACAA;;ACAA;AACA;AACA;AACA', 'adds offset mapping for each line' );
78
});
79
80
p.end(JSON.stringify([
81
{
82
id: 'abc',
83
source: 'T.equal(require("./xyz")(3), 333)',
84
entry: true,
85
deps: { './xyz': 'xyz' },
86
sourceFile: 'wunder/bar.js'
87
},
88
{
89
id: 'xyz',
90
source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}',
91
sourceFile: 'foo.js'
92
}
93
]));
94
});
95
96
test('pack two files without source file field', function (t) {
97
t.plan(5);
98
99
var p = pack();
100
var src = '';
101
p.on('data', function (buf) { src += buf });
102
p.on('end', function () {
103
var r = Function(['T'], 'return ' + src)(t);
104
t.equal(r('xyz')(5), 555);
105
t.equal(r('xyz')(5), 555);
106
107
var lastLine = grabLastLine(src);
108
t.notOk(/^\/\/# sourceMappingURL/.test(lastLine), 'contains no source mapping url');
109
});
110
111
p.end(JSON.stringify([
112
{
113
id: 'abc',
114
source: 'T.equal(require("./xyz")(3), 333)',
115
entry: true,
116
deps: { './xyz': 'xyz' }
117
},
118
{
119
id: 'xyz',
120
source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}'
121
}
122
]));
123
});
124
125
test('pack two files with source file field, one with nomap flag', function (t) {
126
t.plan(7);
127
128
var p = pack();
129
var src = '';
130
p.on('data', function (buf) { src += buf });
131
p.on('end', function () {
132
var r = Function(['T'], 'return ' + src)(t);
133
t.equal(r('xyz')(5), 555);
134
t.equal(r('xyz')(5), 555);
135
136
var lastLine = grabLastLine(src);
137
var sm = grabSourceMap(lastLine);
138
139
t.ok(/^\/\/# sourceMappingURL/.test(lastLine), 'contains source mapping url as last line');
140
t.deepEqual(sm.sources, [ '_prelude.js', 'wunder/bar.js' ], 'includes mappings for only the file without the "nomap" flag and prelude');
141
t.equal(sm.mappings, 'AAAA;ACAA', 'adds offset mapping for each line of mapped file' );
142
t.end()
143
});
144
145
p.end(JSON.stringify([
146
{
147
id: 'abc',
148
source: 'T.equal(require("./xyz")(3), 333)',
149
entry: true,
150
deps: { './xyz': 'xyz' },
151
sourceFile: 'wunder/bar.js'
152
},
153
{
154
id: 'xyz',
155
source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}',
156
sourceFile: 'foo.js',
157
nomap: true
158
}
159
]));
160
});
161
162
test('custom sourceMapPrefix for //@', function (t) {
163
t.plan(7);
164
165
var p = pack({ sourceMapPrefix: '//@' });
166
var src = '';
167
p.on('data', function (buf) { src += buf });
168
p.on('end', function () {
169
var r = Function(['T'], 'return ' + src)(t);
170
t.equal(r('xyz')(5), 555);
171
t.equal(r('xyz')(5), 555);
172
173
var lastLine = grabLastLine(src);
174
var sm = grabSourceMap(lastLine);
175
176
t.ok(/^\/\/@ sourceMappingURL/.test(lastLine), 'contains source mapping url as last line');
177
t.deepEqual(sm.sources, [ '_prelude.js', 'foo.js' ], 'includes mappings for sourceFile and prelude only');
178
t.equal(sm.mappings, 'AAAA;;;ACAA;AACA;AACA;AACA', 'adds offset mapping for each line' );
179
});
180
181
p.end(JSON.stringify([
182
{
183
id: 'abc',
184
source: 'T.equal(require("./xyz")(3), 333)',
185
entry: true,
186
deps: { './xyz': 'xyz' }
187
},
188
{
189
id: 'xyz',
190
source: 'T.ok(true);\nmodule.exports=function(n){\n return n*111 \n}',
191
sourceFile: 'foo.js'
192
}
193
]));
194
});
195
196
197