[d565449] | 1 | var SourceMapGenerator = require('source-map/lib/source-map-generator').SourceMapGenerator;
|
---|
| 2 | var trackNodes = {
|
---|
| 3 | Atrule: true,
|
---|
| 4 | Selector: true,
|
---|
| 5 | Declaration: true
|
---|
| 6 | };
|
---|
| 7 |
|
---|
| 8 | module.exports = function generateSourceMap(handlers) {
|
---|
| 9 | var map = new SourceMapGenerator();
|
---|
| 10 | var line = 1;
|
---|
| 11 | var column = 0;
|
---|
| 12 | var generated = {
|
---|
| 13 | line: 1,
|
---|
| 14 | column: 0
|
---|
| 15 | };
|
---|
| 16 | var original = {
|
---|
| 17 | line: 0, // should be zero to add first mapping
|
---|
| 18 | column: 0
|
---|
| 19 | };
|
---|
| 20 | var sourceMappingActive = false;
|
---|
| 21 | var activatedGenerated = {
|
---|
| 22 | line: 1,
|
---|
| 23 | column: 0
|
---|
| 24 | };
|
---|
| 25 | var activatedMapping = {
|
---|
| 26 | generated: activatedGenerated
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | var handlersNode = handlers.node;
|
---|
| 30 | handlers.node = function(node) {
|
---|
| 31 | if (node.loc && node.loc.start && trackNodes.hasOwnProperty(node.type)) {
|
---|
| 32 | var nodeLine = node.loc.start.line;
|
---|
| 33 | var nodeColumn = node.loc.start.column - 1;
|
---|
| 34 |
|
---|
| 35 | if (original.line !== nodeLine ||
|
---|
| 36 | original.column !== nodeColumn) {
|
---|
| 37 | original.line = nodeLine;
|
---|
| 38 | original.column = nodeColumn;
|
---|
| 39 |
|
---|
| 40 | generated.line = line;
|
---|
| 41 | generated.column = column;
|
---|
| 42 |
|
---|
| 43 | if (sourceMappingActive) {
|
---|
| 44 | sourceMappingActive = false;
|
---|
| 45 | if (generated.line !== activatedGenerated.line ||
|
---|
| 46 | generated.column !== activatedGenerated.column) {
|
---|
| 47 | map.addMapping(activatedMapping);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | sourceMappingActive = true;
|
---|
| 52 | map.addMapping({
|
---|
| 53 | source: node.loc.source,
|
---|
| 54 | original: original,
|
---|
| 55 | generated: generated
|
---|
| 56 | });
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | handlersNode.call(this, node);
|
---|
| 61 |
|
---|
| 62 | if (sourceMappingActive && trackNodes.hasOwnProperty(node.type)) {
|
---|
| 63 | activatedGenerated.line = line;
|
---|
| 64 | activatedGenerated.column = column;
|
---|
| 65 | }
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | var handlersChunk = handlers.chunk;
|
---|
| 69 | handlers.chunk = function(chunk) {
|
---|
| 70 | for (var i = 0; i < chunk.length; i++) {
|
---|
| 71 | if (chunk.charCodeAt(i) === 10) { // \n
|
---|
| 72 | line++;
|
---|
| 73 | column = 0;
|
---|
| 74 | } else {
|
---|
| 75 | column++;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | handlersChunk(chunk);
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | var handlersResult = handlers.result;
|
---|
| 83 | handlers.result = function() {
|
---|
| 84 | if (sourceMappingActive) {
|
---|
| 85 | map.addMapping(activatedMapping);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | return {
|
---|
| 89 | css: handlersResult(),
|
---|
| 90 | map: map
|
---|
| 91 | };
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | return handlers;
|
---|
| 95 | };
|
---|