forked from reworkcss/css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.js
More file actions
112 lines (97 loc) · 4.23 KB
/
stringify.js
File metadata and controls
112 lines (97 loc) · 4.23 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
var stringify = require('../').stringify;
var parse = require('../').parse;
var path = require('path');
var read = require('fs').readFileSync;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var SourceMapGenerator = require('source-map').SourceMapGenerator;
describe('stringify(obj, {sourcemap: true})', function() {
var file = 'test/source-map/test.css';
var src = read(file, 'utf8');
var stylesheet = parse(src, { source: file });
function loc(line, column) {
return { line: line, column: column, source: file, name: null };
}
var locs = {
tobiSelector: loc(1, 0),
tobiNameName: loc(2, 2),
tobiNameValue: loc(2, 2),
mediaBlock: loc(11, 0),
mediaOnly: loc(12, 2),
comment: loc(17, 0),
};
it('should generate source maps alongside when using identity compiler', function() {
var result = stringify(stylesheet, { sourcemap: true });
result.should.have.property('code');
result.should.have.property('map');
var map = new SourceMapConsumer(result.map);
map.originalPositionFor({ line: 1, column: 0 }).should.eql(locs.tobiSelector);
map.originalPositionFor({ line: 2, column: 2 }).should.eql(locs.tobiNameName);
map.originalPositionFor({ line: 2, column: 8 }).should.eql(locs.tobiNameValue);
map.originalPositionFor({ line: 11, column: 0 }).should.eql(locs.mediaBlock);
map.originalPositionFor({ line: 12, column: 2 }).should.eql(locs.mediaOnly);
map.originalPositionFor({ line: 17, column: 0 }).should.eql(locs.comment);
map.sourceContentFor(file).should.eql(src);
});
it('should generate source maps alongside when using compress compiler', function() {
var result = stringify(stylesheet, { compress: true, sourcemap: true });
result.should.have.property('code');
result.should.have.property('map');
var map = new SourceMapConsumer(result.map);
map.originalPositionFor({ line: 1, column: 0 }).should.eql(locs.tobiSelector);
map.originalPositionFor({ line: 1, column: 5 }).should.eql(locs.tobiNameName);
map.originalPositionFor({ line: 1, column: 10 }).should.eql(locs.tobiNameValue);
map.originalPositionFor({ line: 1, column: 50 }).should.eql(locs.mediaBlock);
map.originalPositionFor({ line: 1, column: 64 }).should.eql(locs.mediaOnly);
map.sourceContentFor(file).should.eql(src);
});
it('should apply included source maps, with paths adjusted to CWD', function() {
var file = 'test/source-map/apply.css';
var src = read(file, 'utf8');
var stylesheet = parse(src, { source: file });
var result = stringify(stylesheet, { sourcemap: true });
result.should.have.property('code');
result.should.have.property('map');
var map = new SourceMapConsumer(result.map);
map.originalPositionFor({ line: 1, column: 0 }).should.eql({
column: 0,
line: 1,
name: null,
source: 'test/source-map/apply.scss'
});
map.originalPositionFor({ line: 2, column: 2 }).should.eql({
column: 7,
line: 1,
name: null,
source: 'test/source-map/apply.scss'
});
});
it('should not apply included source maps when inputSourcemap is false', function() {
var file = 'test/source-map/apply.css';
var src = read(file, 'utf8');
var stylesheet = parse(src, { source: file });
var result = stringify(stylesheet, { sourcemap: true, inputSourcemaps: false });
var map = new SourceMapConsumer(result.map);
map.originalPositionFor({ line: 1, column: 0 }).should.eql({
column: 0,
line: 1,
name: null,
source: file
});
});
it('should convert Windows-style paths to URLs', function() {
var originalSep = path.sep;
path.sep = '\\'; // Pretend we’re on Windows (if we aren’t already).
var src = 'C:\\test\\source.css';
var css = 'a { color: black; }'
var stylesheet = parse(css, { source: src });
var result = stringify(stylesheet, { sourcemap: true });
result.map.sources.should.eql(['/test/source.css']);
path.sep = originalSep;
});
it('should return source map generator when sourcemap: "generator"', function(){
var css = 'a { color: black; }';
var stylesheet = parse(css);
var result = stringify(stylesheet, { sourcemap: 'generator' });
result.map.should.be.an.instanceOf(SourceMapGenerator);
});
});