3 var fs = require('fs');
4 var path = require('path');
5 var define = require('define-property');
6 var utils = require('./utils');
10 * This code is based on `source-maps-support.js` in reworkcss/css
11 * https://github.com/reworkcss/css/blob/master/lib/stringify/source-map-support.js
12 * Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
15 module.exports = mixin;
18 * Mixin source map support into `compiler`.
20 * @param {Object} `compiler`
24 function mixin(compiler) {
25 define(compiler, '_comment', compiler.comment);
26 compiler.map = new utils.SourceMap.SourceMapGenerator();
27 compiler.position = { line: 1, column: 1 };
28 compiler.content = {};
31 for (var key in exports) {
32 define(compiler, key, exports[key]);
42 exports.updatePosition = function(str) {
43 var lines = str.match(/\n/g);
44 if (lines) this.position.line += lines.length;
45 var i = str.lastIndexOf('\n');
46 this.position.column = ~i ? str.length - i : this.position.column + str.length;
50 * Emit `str` with `position`.
53 * @param {Object} [pos]
57 exports.emit = function(str, node) {
58 var position = node.position || {};
59 var source = position.source;
61 if (position.filepath) {
62 source = utils.unixify(position.filepath);
68 line: this.position.line,
69 column: Math.max(this.position.column - 1, 0)
72 line: position.start.line,
73 column: position.start.column - 1
77 if (position.content) {
78 this.addContent(source, position);
80 if (position.filepath) {
81 this.addFile(source, position);
84 this.updatePosition(str);
91 * Adds a file to the source map output if it has not already been added
92 * @param {String} `file`
93 * @param {Object} `pos`
96 exports.addFile = function(file, position) {
97 if (typeof position.content !== 'string') return;
98 if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
99 this.files[file] = position.content;
103 * Adds a content source to the source map output if it has not already been added
104 * @param {String} `source`
105 * @param {Object} `position`
108 exports.addContent = function(source, position) {
109 if (typeof position.content !== 'string') return;
110 if (Object.prototype.hasOwnProperty.call(this.content, source)) return;
111 this.map.setSourceContent(source, position.content);
115 * Applies any original source maps to the output and embeds the source file
116 * contents in the source map.
119 exports.applySourceMaps = function() {
120 Object.keys(this.files).forEach(function(file) {
121 var content = this.files[file];
122 this.map.setSourceContent(file, content);
124 if (this.options.inputSourcemaps === true) {
125 var originalMap = utils.sourceMapResolve.resolveSync(content, file, fs.readFileSync);
127 var map = new utils.SourceMap.SourceMapConsumer(originalMap.map);
128 var relativeTo = originalMap.sourcesRelativeTo;
129 this.map.applySourceMap(map, file, utils.unixify(path.dirname(relativeTo)));
136 * Process comments, drops sourceMap comments.
137 * @param {Object} node
140 exports.comment = function(node) {
141 if (/^# sourceMappingURL=/.test(node.comment)) {
142 return this.emit('', node.position);
144 return this._comment(node);