forked from postcss/postcss
/
map.test.ts
executable file
·740 lines (629 loc) · 17.3 KB
/
map.test.ts
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
import { SourceMapConsumer, SourceMapGenerator } from 'source-map-js'
import { removeSync, outputFileSync } from 'fs-extra'
import { is, type, equal, match } from 'uvu/assert'
import { join, resolve, parse } from 'path'
import { pathToFileURL } from 'url'
import { existsSync } from 'fs'
import { test } from 'uvu'
import postcss, { SourceMap, Rule, Root } from '../lib/postcss.js'
import PreviousMap from '../lib/previous-map.js'
function consumer(map: SourceMap): any {
return (SourceMapConsumer as any).fromSourceMap(map)
}
function read(result: { css: string }): any {
let prev = new PreviousMap(result.css, {})
return prev.consumer()
}
let dir = join(__dirname, 'map-fixtures')
let doubler = postcss((css: Root) => {
css.walkDecls(decl => {
decl.parent?.prepend(decl.clone())
})
})
let lighter = postcss((css: Root) => {
css.walkDecls(decl => {
decl.value = 'white'
})
})
test.after.each(() => {
if (existsSync(dir)) removeSync(dir)
})
test('adds map field only on request', () => {
type(postcss([() => {}]).process('a {}').map, 'undefined')
})
test('return map generator', () => {
let map = postcss([() => {}]).process('a {}', {
map: { inline: false }
}).map
is(map instanceof SourceMapGenerator, true)
})
test('generate right source map', () => {
let css = 'a {\n color: black;\n }'
let processor = postcss((root: Root) => {
root.walkRules(rule => {
rule.selector = 'strong'
})
root.walkDecls(decl => {
decl.parent?.prepend(decl.clone({ prop: 'background', value: 'black' }))
})
})
let result = processor.process(css, {
from: 'a.css',
to: 'b.css',
map: true
})
let map = read(result)
is(map.file, 'b.css')
equal(map.originalPositionFor({ line: 1, column: 0 }), {
source: 'a.css',
line: 1,
column: 0,
name: null
})
equal(map.originalPositionFor({ line: 1, column: 2 }), {
source: 'a.css',
line: 1,
column: 0,
name: null
})
equal(map.originalPositionFor({ line: 2, column: 2 }), {
source: 'a.css',
line: 2,
column: 2,
name: null
})
equal(map.originalPositionFor({ line: 3, column: 2 }), {
source: 'a.css',
line: 2,
column: 2,
name: null
})
equal(map.originalPositionFor({ line: 3, column: 14 }), {
source: 'a.css',
line: 2,
column: 14,
name: null
})
equal(map.originalPositionFor({ line: 4, column: 2 }), {
source: 'a.css',
line: 3,
column: 2,
name: null
})
})
test('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)
equal(map.originalPositionFor({ line: 1, column: 18 }), {
source: 'a.css',
line: 1,
column: 4,
name: null
})
})
test('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 }
})
is(result.css, 'a { }\n/*# sourceMappingURL=b.css.map */')
})
test('misses source map annotation, if user ask', () => {
let css = 'a { }'
let result = postcss([() => {}]).process(css, {
from: 'a.css',
to: 'b.css',
map: { annotation: false }
})
is(result.css, css)
})
test('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 }
})
is(step2.css, css)
})
test('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' }
})
is(result.css, 'a { }\n/*# sourceMappingURL=maps/b.map */')
let map = consumer(result.map)
is(map.file, join('..', 'b.css'))
is(
map.originalPositionFor({ line: 1, column: 0 }).source,
'../../source/a.css'
)
})
test('generates inline map', () => {
let css = 'a { }'
let inline = postcss([() => {}]).process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: true }
})
type(inline.map, 'undefined')
match(inline.css, /# sourceMappingURL=data:/)
let separated = postcss([() => {}]).process(css, {
from: 'a.css',
to: 'b.css',
map: { inline: false }
})
let base64 = Buffer.from(separated.map.toString()).toString('base64')
let end = inline.css.slice(-base64.length - 3)
is(end, base64 + ' */')
})
test('generates inline map by default', () => {
let inline = postcss([() => {}]).process('a { }', {
from: 'a.css',
to: 'b.css',
map: true
})
match(inline.css, /# sourceMappingURL=data:/)
})
test('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 }
})
type(step2.map, 'object')
})
test('generates separated map on annotation option', () => {
let result = postcss([() => {}]).process('a { }', {
from: 'a.css',
to: 'b.css',
map: { annotation: false }
})
type(result.map, 'object')
})
test('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 }
})
type(step2.map, 'object')
match(step2.css, /# sourceMappingURL=c\.css\.map/)
})
test('misses check files on requires', () => {
let file = join(dir, 'a.css')
let step1 = doubler.process('a { }', {
from: 'a.css',
to: file,
map: { inline: false }
})
outputFileSync(file + '.map', step1.map.toString())
let step2 = lighter.process(step1.css, {
from: file,
to: 'b.css',
map: false
})
type(step2.map, 'undefined')
})
test('works in subdirs', () => {
let result = doubler.process('a { }', {
from: 'from/a.css',
to: 'out/b.css',
map: { inline: false }
})
match(result.css, /sourceMappingURL=b.css.map/)
let map = consumer(result.map)
is(map.file, 'b.css')
equal(map.sources, ['../from/a.css'])
})
test('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
is(source, '../../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
is(source, '../../a.css')
})
test('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
is(source, '../../a.css')
})
test('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
is(source, '../../source/a.css')
let file = join(dir, 'one', 'maps', 'b.css.map')
outputFileSync(file, step1.map.toString())
let step2 = doubler.process(step1.css, {
from: join(dir, 'one', 'b.css'),
to: join(dir, 'two', 'c.css'),
map: true
})
source = consumer(step2.map).originalPositionFor({
line: 1,
column: 0
}).source
is(source, '../source/a.css')
})
test('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
is(source, 'a.css')
}
})
test('sets source content by default', () => {
let result = doubler.process('a { }', {
from: 'a.css',
to: 'out/b.css',
map: true
})
is(read(result).sourceContentFor('../a.css'), 'a { }')
})
test('misses source content on request', () => {
let result = doubler.process('a { }', {
from: 'a.css',
to: 'out/b.css',
map: { sourcesContent: false }
})
is(read(result).sourceContentFor('../a.css'), null)
})
test('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 })
if (file1.first) file2.append(file1.first.clone())
let step2 = file2.toResult({ to: 'c.css', map: true })
is(read(step2).sourceContentFor('b.css'), null)
})
test('misses source content on request in multiple steps', () => {
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 })
if (file1.first) file2.append(file1.first.clone())
let step2 = file2.toResult({
to: 'c.css',
map: { sourcesContent: false }
})
let map = read(step2)
is(map.sourceContentFor('b.css'), null)
is(map.sourceContentFor('../a.css'), null)
})
test('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 } })
is(two.root.first?.source?.input.file, resolve('a.css'))
})
test('works without file names', () => {
let step1 = doubler.process('a { }', { map: true })
let step2 = doubler.process(step1.css)
match(step2.css, /a { }\n\/\*/)
})
test('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'
})
is(read(step2).file, 'выход.css')
})
test('generates map for node created manually', () => {
let contenter = postcss((css: Root) => {
if (css.first && css.first.type === 'rule') {
css.first.prepend({ selector: 'b' })
}
})
let result = contenter.process('a:after{\n}', { map: true })
let map = read(result)
equal(map.originalPositionFor({ line: 2, column: 5 }), {
source: '<no source>',
column: 0,
line: 1,
name: null
})
})
test('uses input file name as output file name', () => {
let result = doubler.process('a{}', {
from: 'a.css',
map: { inline: false }
})
is(result.map.toJSON().file, 'a.css')
})
test('uses to.css as default output name', () => {
let result = doubler.process('a{}', { map: { inline: false } })
is(result.map.toJSON().file, 'to.css')
})
test('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 }
})
is(result.css, 'a { }\n/*# sourceMappingURL=b.css.map */')
})
test('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 }
})
is(result.css, 'a { }/*# sourceMappingURL=a.css.map */')
})
test('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
is(css.match(/sourceMappingURL/g)?.length, 1)
})
test('uses Windows line separation too', () => {
let result = postcss([() => {}]).process('a {\r\n}', { map: true })
match(result.css, /a {\r\n}\r\n\/\*# sourceMappingURL=/)
})
test('`map.from` should override the source map sources', () => {
let result = postcss([() => {}]).process('a{}', {
map: {
inline: false,
from: 'file:///dir/a.css'
}
})
equal(result.map.toJSON().sources, ['file:///dir/a.css'])
})
test('preserves absolute urls in `to`', () => {
let result = postcss([() => {}]).process('a{}', {
from: '/dir/to/a.css',
to: 'http://example.com/a.css',
map: { inline: false }
})
is(result.map.toJSON().file, 'http://example.com/a.css')
})
test('preserves absolute urls in sources', () => {
let result = postcss([() => {}]).process('a{}', {
from: 'file:///dir/a.css',
to: 'http://example.com/a.css',
map: { inline: false }
})
equal(result.map.toJSON().sources, ['file:///dir/a.css'])
})
test('uses absolute path on request', () => {
let result = postcss([() => {}]).process('a{}', {
from: '/dir/a.css',
to: '/dir/b.css',
map: { inline: false, absolute: true }
})
let root = '/'
if (process.platform === 'win32') {
root = '/' + parse(process.cwd()).root.replace(/\\/g, '/')
}
equal(result.map.toJSON().sources, [`file://${root}dir/a.css`])
})
test('preserves absolute urls in sources from previous map', () => {
let result1 = postcss([() => {}]).process('a{}', {
from: 'http://example.com/a.css',
to: 'http://example.com/b.css',
map: true
})
let result2 = postcss([() => {}]).process(result1.css, {
to: 'http://example.com/c.css',
map: {
inline: false
}
})
is(result2.root.source?.input.file, 'http://example.com/b.css')
equal(result2.map.toJSON().sources, ['http://example.com/a.css'])
})
test('allows dynamic annotations', () => {
let result = postcss([() => {}]).process('a{}', {
to: 'out.css',
map: {
annotation(to, root) {
let rule = root.first as Rule
return to + '-' + rule.selector + '.map'
}
}
})
is(result.css, 'a{}\n/*# sourceMappingURL=out.css-a.map */')
})
test('uses URLs in sources', () => {
let result = postcss([() => {}]).process('a{}', {
from: 'a b.css',
to: 'dir/b.css',
map: { inline: false }
})
equal(result.map.toJSON().sources, ['../a%20b.css'])
})
test('generates correct inline map with empty processor', () => {
let result = postcss().process('a {} /*hello world*/', {
map: true
})
match(result.css, /a {} \/\*hello world\*\/\n\/\*# sourceMappingURL=/)
})
test('generates correct inline map and multiple comments', () => {
let css =
'a {}/*# sourceMappingURL=a.css.map */\n' +
'/*# sourceMappingURL=b.css.map */\n' +
'b {}\n/*# sourceMappingURL=c.css.map */'
let result = postcss().process(css, {
map: true
})
match(result.css, /a {}\nb {}\n\/\*# sourceMappingURL=/)
})
test('generates correct sources with empty processor', () => {
let result = postcss().process('a {} /*hello world*/', {
from: 'a.css',
to: 'b.css',
map: { inline: false }
})
equal(result.map.toJSON().sources, ['a.css'])
})
test('generates map object with empty processor', () => {
let result = postcss().process('a {} /*hello world*/', {
from: 'a.css',
to: 'b.css',
map: true
})
let map = read(result)
equal(map.originalPositionFor({ line: 1, column: 0 }), {
source: 'a.css',
line: 1,
column: 0,
name: null
})
})
test('supports previous map with empty processor', () => {
let result1 = postcss().process('a{}', {
from: '/a.css',
to: '/b.css',
map: {
sourcesContent: true,
inline: false
}
})
equal(result1.map.toJSON(), {
version: 3,
sources: ['a.css'],
names: [],
mappings: 'AAAA',
file: 'b.css',
sourcesContent: ['a{}']
})
let result2 = postcss().process(result1.css, {
from: '/b.css',
to: '/c.css',
map: {
prev: result1.map
}
})
equal(result2.map.toJSON().sources, ['a.css'])
equal(result2.map.toJSON().sourcesContent, ['a{}'])
})
test('supports previous inline map with empty processor', () => {
let result1 = postcss().process('a{}', {
from: '/a.css',
to: '/b.css',
map: true
})
let result2 = postcss().process(result1.css, {
from: '/b.css',
to: '/c.css'
})
let root3 = postcss.parse(result2.css, { from: '/c.css' })
match((root3.source?.input.origin(1, 0) as any).file, 'a.css')
})
test('absolute sourcemaps have source contents', () => {
let result = postcss([() => {}]).process('a{}', {
from: '/dir/to/a.css',
map: {
inline: false,
absolute: true
}
})
equal(result.map.toJSON().sources, [
pathToFileURL('/dir/to/a.css').toString()
])
equal(result.map.toJSON().sourcesContent, ['a{}'])
})
test.run()