UNPKG

2.94 kBJavaScriptView Raw
1var SourceMapGenerator = require('source-map').SourceMapGenerator;
2var all = require('./helpers').all;
3
4var isRemoteResource = require('../utils/is-remote-resource');
5
6var isWindows = process.platform == 'win32';
7
8var NIX_SEPARATOR_PATTERN = /\//g;
9var UNKNOWN_SOURCE = '$stdin';
10var WINDOWS_SEPARATOR = '\\';
11
12function 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
23function 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
30function 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
41function trackAllMappings(serializeContext, mappings) {
42 for (var i = 0, l = mappings.length; i < l; i++) {
43 trackMapping(serializeContext, mappings[i]);
44 }
45}
46
47function 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(storedSource, serializeContext.sourcesContent[originalSource]);
72 }
73}
74
75function serializeStylesAndSourceMap(tokens, context) {
76 var serializeContext = {
77 column: 0,
78 format: context.options.format,
79 indentBy: 0,
80 indentWith: '',
81 inlineSources: context.options.sourceMapInlineSources,
82 line: 1,
83 output: [],
84 outputMap: new SourceMapGenerator(),
85 sourcesContent: context.sourcesContent,
86 spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
87 store: store,
88 wrap: context.options.format.wrapAt ?
89 wrap :
90 function () { /* noop */ }
91 };
92
93 all(serializeContext, tokens);
94
95 return {
96 sourceMap: serializeContext.outputMap,
97 styles: serializeContext.output.join('')
98 };
99}
100
101module.exports = serializeStylesAndSourceMap;