diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/clean-css/lib/writer/source-maps.js | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/clean-css/lib/writer/source-maps.js')
-rw-r--r-- | node_modules/clean-css/lib/writer/source-maps.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/node_modules/clean-css/lib/writer/source-maps.js b/node_modules/clean-css/lib/writer/source-maps.js new file mode 100644 index 000000000..4729eb055 --- /dev/null +++ b/node_modules/clean-css/lib/writer/source-maps.js @@ -0,0 +1,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; |