source-maps.js
2.86 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
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var all = require('./helpers').all;
var lineBreak = require('os').EOL;
var isRemoteResource = require('../utils/is-remote-resource');
var isWindows = process.platform == 'win32';
var NIX_SEPARATOR_PATTERN = /\//g;
var UNKNOWN_SOURCE = '$stdin';
var WINDOWS_SEPARATOR = '\\';
function store(serializeContext, element) {
var fromString = typeof element == 'string';
var value = fromString ? element : element[1];
var mappings = fromString ? null : element[2];
var wrap = serializeContext.wrap;
wrap(serializeContext, value);
track(serializeContext, value, mappings);
serializeContext.output.push(value);
}
function wrap(serializeContext, value) {
if (serializeContext.column + value.length > serializeContext.format.wrapAt) {
track(serializeContext, lineBreak, false);
serializeContext.output.push(lineBreak);
}
}
function track(serializeContext, value, mappings) {
var parts = value.split('\n');
if (mappings) {
trackAllMappings(serializeContext, mappings);
}
serializeContext.line += parts.length - 1;
serializeContext.column = parts.length > 1 ? 0 : (serializeContext.column + parts.pop().length);
}
function trackAllMappings(serializeContext, mappings) {
for (var i = 0, l = mappings.length; i < l; i++) {
trackMapping(serializeContext, mappings[i]);
}
}
function trackMapping(serializeContext, mapping) {
var line = mapping[0];
var column = mapping[1];
var originalSource = mapping[2];
var source = originalSource;
var storedSource = source || UNKNOWN_SOURCE;
if (isWindows && source && !isRemoteResource(source)) {
storedSource = source.replace(NIX_SEPARATOR_PATTERN, WINDOWS_SEPARATOR);
}
serializeContext.outputMap.addMapping({
generated: {
line: serializeContext.line,
column: serializeContext.column
},
source: storedSource,
original: {
line: line,
column: column
}
});
if (serializeContext.inlineSources && (originalSource in serializeContext.sourcesContent)) {
serializeContext.outputMap.setSourceContent(storedSource, serializeContext.sourcesContent[originalSource]);
}
}
function serializeStylesAndSourceMap(tokens, context) {
var serializeContext = {
column: 0,
format: context.options.format,
indentBy: 0,
indentWith: '',
inlineSources: context.options.sourceMapInlineSources,
line: 1,
output: [],
outputMap: new SourceMapGenerator(),
sourcesContent: context.sourcesContent,
spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
store: store,
wrap: context.options.format.wrapAt ?
wrap :
function () { /* noop */ }
};
all(serializeContext, tokens);
return {
sourceMap: serializeContext.outputMap,
styles: serializeContext.output.join('')
};
}
module.exports = serializeStylesAndSourceMap;