1 | var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
---|
2 | var all = require('./helpers').all;
|
---|
3 |
|
---|
4 | var isRemoteResource = require('../utils/is-remote-resource');
|
---|
5 |
|
---|
6 | var isWindows = process.platform == 'win32';
|
---|
7 |
|
---|
8 | var NIX_SEPARATOR_PATTERN = /\//g;
|
---|
9 | var UNKNOWN_SOURCE = '$stdin';
|
---|
10 | var WINDOWS_SEPARATOR = '\\';
|
---|
11 |
|
---|
12 | function store(serializeContext, element) {
|
---|
13 | var fromString = typeof element == 'string';
|
---|
14 | var value = fromString ? element : element[1];
|
---|
15 | var mappings = fromString ? null : element[2];
|
---|
16 | var wrap = serializeContext.wrap;
|
---|
17 |
|
---|
18 | wrap(serializeContext, value);
|
---|
19 | track(serializeContext, value, mappings);
|
---|
20 | serializeContext.output.push(value);
|
---|
21 | }
|
---|
22 |
|
---|
23 | function wrap(serializeContext, value) {
|
---|
24 | if (serializeContext.column + value.length > serializeContext.format.wrapAt) {
|
---|
25 | track(serializeContext, serializeContext.format.breakWith, false);
|
---|
26 | serializeContext.output.push(serializeContext.format.breakWith);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | function track(serializeContext, value, mappings) {
|
---|
31 | var parts = value.split('\n');
|
---|
32 |
|
---|
33 | if (mappings) {
|
---|
34 | trackAllMappings(serializeContext, mappings);
|
---|
35 | }
|
---|
36 |
|
---|
37 | serializeContext.line += parts.length - 1;
|
---|
38 | serializeContext.column = parts.length > 1 ? 0 : (serializeContext.column + parts.pop().length);
|
---|
39 | }
|
---|
40 |
|
---|
41 | function trackAllMappings(serializeContext, mappings) {
|
---|
42 | for (var i = 0, l = mappings.length; i < l; i++) {
|
---|
43 | trackMapping(serializeContext, mappings[i]);
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | function trackMapping(serializeContext, mapping) {
|
---|
48 | var line = mapping[0];
|
---|
49 | var column = mapping[1];
|
---|
50 | var originalSource = mapping[2];
|
---|
51 | var source = originalSource;
|
---|
52 | var storedSource = source || UNKNOWN_SOURCE;
|
---|
53 |
|
---|
54 | if (isWindows && source && !isRemoteResource(source)) {
|
---|
55 | storedSource = source.replace(NIX_SEPARATOR_PATTERN, WINDOWS_SEPARATOR);
|
---|
56 | }
|
---|
57 |
|
---|
58 | serializeContext.outputMap.addMapping({
|
---|
59 | generated: {
|
---|
60 | line: serializeContext.line,
|
---|
61 | column: serializeContext.column
|
---|
62 | },
|
---|
63 | source: storedSource,
|
---|
64 | original: {
|
---|
65 | line: line,
|
---|
66 | column: column
|
---|
67 | }
|
---|
68 | });
|
---|
69 |
|
---|
70 | if (serializeContext.inlineSources && (originalSource in serializeContext.sourcesContent)) {
|
---|
71 | serializeContext.outputMap.setSourceContent(
|
---|
72 | storedSource,
|
---|
73 | serializeContext.sourcesContent[originalSource]
|
---|
74 | );
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | function serializeStylesAndSourceMap(tokens, context) {
|
---|
79 | var serializeContext = {
|
---|
80 | column: 0,
|
---|
81 | format: context.options.format,
|
---|
82 | indentBy: 0,
|
---|
83 | indentWith: '',
|
---|
84 | inlineSources: context.options.sourceMapInlineSources,
|
---|
85 | line: 1,
|
---|
86 | output: [],
|
---|
87 | outputMap: new SourceMapGenerator(),
|
---|
88 | sourcesContent: context.sourcesContent,
|
---|
89 | spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
|
---|
90 | store: store,
|
---|
91 | wrap: context.options.format.wrapAt
|
---|
92 | ? wrap
|
---|
93 | : function() { /* noop */ }
|
---|
94 | };
|
---|
95 |
|
---|
96 | all(serializeContext, tokens);
|
---|
97 |
|
---|
98 | return {
|
---|
99 | sourceMap: serializeContext.outputMap,
|
---|
100 | styles: serializeContext.output.join('')
|
---|
101 | };
|
---|
102 | }
|
---|
103 |
|
---|
104 | module.exports = serializeStylesAndSourceMap;
|
---|