forked from postcss/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.es6
508 lines (416 loc) · 14.9 KB
/
map.es6
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import PreviousMap from '../lib/previous-map';
import postcss from '../lib/postcss';
import mozilla from 'source-map';
import fs from 'fs-extra';
import { expect } from 'chai';
import path from 'path';
let consumer = map => mozilla.SourceMapConsumer.fromSourceMap(map);
let read = function (result) {
let prev = new PreviousMap(result.css, { });
return prev.consumer();
};
let dir = path.join(__dirname, 'fixtures');
let doubler = postcss( (css) => {
css.walkDecls( decl => decl.parent.prepend(decl.clone()) );
});
let lighter = postcss( (css) => {
css.walkDecls( decl => decl.value = 'white' );
});
describe('source maps', () => {
afterEach( () => {
if ( fs.existsSync(dir) ) fs.removeSync(dir);
});
it('adds map field only on request', () => {
expect(postcss().process('a {}').map).to.not.exist;
});
it('return map generator', () => {
let map = postcss().process('a {}', { map: { inline: false } }).map;
expect(map).to.be.instanceOf(mozilla.SourceMapGenerator);
});
it('generate right source map', () => {
let css = 'a {\n color: black;\n }';
let processor = postcss( (root) => {
root.walkRules( (rule) => {
rule.selector = 'strong';
});
root.walkDecls( (decl) => {
decl.parent.prepend( decl.clone({ prop: 'background' }) );
});
});
let result = processor.process(css, {
from: 'a.css',
to: 'b.css',
map: true
});
let map = read(result);
expect(map.file).to.eql('b.css');
expect(map.originalPositionFor({ line: 1, column: 0 })).to.eql({
source: 'a.css',
line: 1,
column: 0,
name: null
});
expect(map.originalPositionFor({ line: 2, column: 2 })).to.eql({
source: 'a.css',
line: 2,
column: 2,
name: null
});
expect(map.originalPositionFor({ line: 3, column: 2 })).to.eql({
source: 'a.css',
line: 2,
column: 2,
name: null
});
});
it('changes previous source map', () => {
let css = 'a { color: black }';
let doubled = doubler.process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let lighted = lighter.process(doubled.css, {
from: 'b.css',
to: 'c.css',
map: { prev: doubled.map }
});
let map = consumer(lighted.map);
expect(map.originalPositionFor({ line: 1, column: 18 })).to.eql({
source: 'a.css',
line: 1,
column: 4,
name: null
});
});
it('adds source map annotation', () => {
let css = 'a { }/*# sourceMappingURL=a.css.map */';
let result = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
expect(result.css).to.eql('a { }\n/*# sourceMappingURL=b.css.map */');
});
it('misses source map annotation, if user ask', () => {
let css = 'a { }';
let result = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { annotation: false }
});
expect(result.css).to.eql(css);
});
it('misses source map annotation, if previous map missed it', () => {
let css = 'a { }';
let step1 = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { annotation: false }
});
let step2 = postcss().process(step1.css, {
from: 'b.css',
to: 'c.css',
map: { prev: step1.map }
});
expect(step2.css).to.eql(css);
});
it('uses user path in annotation, relative to options.to', () => {
let result = postcss().process('a { }', {
from: 'source/a.css',
to: 'build/b.css',
map: { annotation: 'maps/b.map' }
});
expect(result.css).to.eql('a { }\n/*# sourceMappingURL=maps/b.map */');
let map = consumer(result.map);
expect(map.file).to.eql('../b.css');
expect(map.originalPositionFor({ line: 1, column: 0 }).source)
.to.eql('../../source/a.css');
});
it('generates inline map', () => {
let css = 'a { }';
let inline = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: true }
});
expect(inline.map).to.not.exist;
expect(inline.css).to.match(/# sourceMappingURL=data:/);
let separated = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let base64 = new Buffer(separated.map.toString()).toString('base64');
expect(inline.css.endsWith(base64 + ' */')).to.be.true;
});
it('generates inline map by default', () => {
let inline = postcss().process('a { }', {
from: 'a.css',
to: 'b.css',
map: true
});
expect(inline.css).to.match(/# sourceMappingURL=data:/);
});
it('generates separated map if previous map was not inlined', () => {
let step1 = doubler.process('a { color: black }', {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let step2 = lighter.process(step1.css, {
from: 'b.css',
to: 'c.css',
map: { prev: step1.map }
});
expect(step2.map).to.exist;
});
it('generates separated map on annotation option', () => {
let result = postcss().process('a { }', {
from: 'a.css',
to: 'b.css',
map: { annotation: false }
});
expect(result.map).to.exist;
});
it('allows change map type', () => {
let step1 = postcss().process('a { }', {
from: 'a.css',
to: 'b.css',
map: { inline: true }
});
let step2 = postcss().process(step1.css, {
from: 'b.css',
to: 'c.css',
map: { inline: false }
});
expect(step2).to.have.property('map');
expect(step2.css).to.not.match(/# sourceMappingURL=data:/);
});
it('misses check files on requires', () => {
let file = path.join(dir, 'a.css');
let step1 = doubler.process('a { }', {
from: 'a.css',
to: file,
map: true
});
fs.outputFileSync(file + '.map', step1.map);
let step2 = lighter.process(step1.css, {
from: file,
to: 'b.css',
map: false
});
expect(step2.map).to.not.exist;
});
it('works in subdirs', () => {
let result = doubler.process('a { }', {
from: 'from/a.css',
to: 'out/b.css',
map: { inline: false }
});
expect(result.css).to.match(/sourceMappingURL=b.css.map/);
let map = consumer(result.map);
expect(map.file).to.eql('b.css');
expect(map.sources).to.eql(['../from/a.css']);
});
it('uses map from subdir', () => {
let step1 = doubler.process('a { }', {
from: 'a.css',
to: 'out/b.css',
map: { inline: false }
});
let step2 = doubler.process(step1.css, {
from: 'out/b.css',
to: 'out/two/c.css',
map: { prev: step1.map }
});
let source = consumer(step2.map)
.originalPositionFor({ line: 1, column: 0 }).source;
expect(source).to.eql('../../a.css');
let step3 = doubler.process(step2.css, {
from: 'c.css',
to: 'd.css',
map: { prev: step2.map }
});
source = consumer(step3.map)
.originalPositionFor({ line: 1, column: 0 }).source;
expect(source).to.eql('../../a.css');
});
it('uses map from subdir if it inlined', () => {
let step1 = doubler.process('a { }', {
from: 'a.css',
to: 'out/b.css',
map: true
});
let step2 = doubler.process(step1.css, {
from: 'out/b.css',
to: 'out/two/c.css',
map: { inline: false }
});
let source = consumer(step2.map)
.originalPositionFor({ line: 1, column: 0 }).source;
expect(source).to.eql('../../a.css');
});
it('uses map from subdir if it written as a file', () => {
let step1 = doubler.process('a { }', {
from: 'source/a.css',
to: 'one/b.css',
map: { annotation: 'maps/b.css.map', inline: false }
});
let source = consumer(step1.map)
.originalPositionFor({ line: 1, column: 0 }).source;
expect(source).to.eql('../../source/a.css');
let file = path.join(dir, 'one', 'maps', 'b.css.map');
fs.outputFileSync(file, step1.map);
let step2 = doubler.process(step1.css, {
from: path.join(dir, 'one', 'b.css'),
to: path.join(dir, 'two', 'c.css'),
map: true
});
source = consumer(step2.map)
.originalPositionFor({ line: 1, column: 0 }).source;
expect(source).to.eql('../source/a.css');
});
it('works with different types of maps', () => {
let step1 = doubler.process('a { }', {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
let map = step1.map;
let maps = [map, consumer(map), map.toJSON(), map.toString()];
for ( let i of maps ) {
let step2 = doubler.process(step1.css, {
from: 'b.css',
to: 'c.css',
map: { prev: i }
});
let source = consumer(step2.map)
.originalPositionFor({ line: 1, column: 0 }).source;
expect(source).to.eql('a.css');
}
});
it('sets source content by default', () => {
let result = doubler.process('a { }', {
from: 'a.css',
to: 'out/b.css',
map: true
});
expect(read(result).sourceContentFor('../a.css')).to.eql('a { }');
});
it('misses source content on request', () => {
let result = doubler.process('a { }', {
from: 'a.css',
to: 'out/b.css',
map: { sourcesContent: false }
});
expect(read(result).sourceContentFor('../a.css')).to.not.exist;
});
it('misses source content if previous not have', () => {
let step1 = doubler.process('a { }', {
from: 'a.css',
to: 'out/a.css',
map: { sourcesContent: false }
});
let file1 = postcss.parse(step1.css, {
from: 'a.css',
map: { prev: step1.map }
});
let file2 = postcss.parse('b { }', { from: 'b.css', map: true });
file2.append( file1.first.clone() );
let step2 = file2.toResult({ to: 'c.css', map: true });
expect(read(step2).sourceContentFor('b.css')).to.not.exist;
});
it('misses source content on request', () => {
let step1 = doubler.process('a { }', {
from: 'a.css',
to: 'out/a.css',
map: { sourcesContent: true }
});
let file1 = postcss.parse(step1.css, {
from: 'a.css',
map: { prev: step1.map }
});
let file2 = postcss.parse('b { }', { from: 'b.css', map: true });
file2.append( file1.first.clone() );
let step2 = file2.toResult({
to: 'c.css',
map: { sourcesContent: false }
});
let map = read(step2);
expect(map.sourceContentFor('b.css')).to.not.exist;
expect(map.sourceContentFor('../a.css')).to.not.exist;
});
it('detects input file name from map', () => {
let one = doubler.process('a { }', { to: 'a.css', map: true });
let two = doubler.process(one.css, { map: { prev: one.map } });
expect(two.root.first.source.input.file).to.eql(path.resolve('a.css'));
});
it('works without file names', () => {
let step1 = doubler.process('a { }', { map: true });
let step2 = doubler.process(step1.css);
expect(step2.css).to.include('a { }\n/*');
});
it('supports UTF-8', () => {
let step1 = doubler.process('a { }', {
from: 'вход.css',
to: 'шаг1.css',
map: true
});
let step2 = doubler.process(step1.css, {
from: 'шаг1.css',
to: 'выход.css'
});
expect(read(step2).file).to.eql('выход.css');
});
it('generates map for node created manually', () => {
let contenter = postcss( (css) => {
css.first.prepend({ prop: 'content', value: '""' });
});
let result = contenter.process('a:after{\n}', { map: true });
expect(read(result).originalPositionFor({ line: 2, column: 0 }))
.to.eql({ source: null, line: null, column: null, name: null });
});
it('uses input file name as output file name', () => {
let result = doubler.process('a{}', {
from: 'a.css',
map: { inline: false }
});
expect(result.map.toJSON().file).to.eql('a.css');
});
it('uses to.css as default output name', () => {
let result = doubler.process('a{}', { map: { inline: false } });
expect(result.map.toJSON().file).to.eql('to.css');
});
it('supports annotation comment in any place', () => {
let css = '/*# sourceMappingURL=a.css.map */a { }';
let result = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: false }
});
expect(result.css).to.eql('a { }\n/*# sourceMappingURL=b.css.map */');
});
it('does not update annotation on request', () => {
let css = 'a { }/*# sourceMappingURL=a.css.map */';
let result = postcss().process(css, {
from: 'a.css',
to: 'b.css',
map: { annotation: false, inline: false }
});
expect(result.css).to.eql('a { }/*# sourceMappingURL=a.css.map */');
});
it('clears source map', () => {
let css1 = postcss.root().toResult({ map: true }).css;
let css2 = postcss.root().toResult({ map: true }).css;
let root = postcss.root();
root.append(css1);
root.append(css2);
let css = root.toResult({ map: true }).css;
expect(css.match(/sourceMappingURL/g)).to.have.length(1);
});
it('uses Windows line separation too', () => {
let result = postcss().process('a {\r\n}', { map: true });
expect(result.css).to.include('a {\r\n}\r\n/*# sourceMappingURL=');
});
});