[6a3a178] | 1 |
|
---|
| 2 | /**
|
---|
| 3 | * Module dependencies.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | var SourceMap = require('source-map').SourceMapGenerator;
|
---|
| 7 | var SourceMapConsumer = require('source-map').SourceMapConsumer;
|
---|
| 8 | var sourceMapResolve = require('source-map-resolve');
|
---|
[e29cc2e] | 9 | var urix = require('urix');
|
---|
[6a3a178] | 10 | var fs = require('fs');
|
---|
| 11 | var path = require('path');
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Expose `mixin()`.
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | module.exports = mixin;
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Mixin source map support into `compiler`.
|
---|
| 21 | *
|
---|
| 22 | * @param {Compiler} compiler
|
---|
| 23 | * @api public
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | function mixin(compiler) {
|
---|
| 27 | compiler._comment = compiler.comment;
|
---|
| 28 | compiler.map = new SourceMap();
|
---|
| 29 | compiler.position = { line: 1, column: 1 };
|
---|
| 30 | compiler.files = {};
|
---|
| 31 | for (var k in exports) compiler[k] = exports[k];
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Update position.
|
---|
| 36 | *
|
---|
| 37 | * @param {String} str
|
---|
| 38 | * @api private
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | exports.updatePosition = function(str) {
|
---|
| 42 | var lines = str.match(/\n/g);
|
---|
| 43 | if (lines) this.position.line += lines.length;
|
---|
| 44 | var i = str.lastIndexOf('\n');
|
---|
| 45 | this.position.column = ~i ? str.length - i : this.position.column + str.length;
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Emit `str`.
|
---|
| 50 | *
|
---|
| 51 | * @param {String} str
|
---|
| 52 | * @param {Object} [pos]
|
---|
| 53 | * @return {String}
|
---|
| 54 | * @api private
|
---|
| 55 | */
|
---|
| 56 |
|
---|
| 57 | exports.emit = function(str, pos) {
|
---|
| 58 | if (pos) {
|
---|
[e29cc2e] | 59 | var sourceFile = urix(pos.source || 'source.css');
|
---|
[6a3a178] | 60 |
|
---|
| 61 | this.map.addMapping({
|
---|
| 62 | source: sourceFile,
|
---|
| 63 | generated: {
|
---|
| 64 | line: this.position.line,
|
---|
| 65 | column: Math.max(this.position.column - 1, 0)
|
---|
| 66 | },
|
---|
| 67 | original: {
|
---|
| 68 | line: pos.start.line,
|
---|
| 69 | column: pos.start.column - 1
|
---|
| 70 | }
|
---|
| 71 | });
|
---|
| 72 |
|
---|
| 73 | this.addFile(sourceFile, pos);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | this.updatePosition(str);
|
---|
| 77 |
|
---|
| 78 | return str;
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Adds a file to the source map output if it has not already been added
|
---|
| 83 | * @param {String} file
|
---|
| 84 | * @param {Object} pos
|
---|
| 85 | */
|
---|
| 86 |
|
---|
| 87 | exports.addFile = function(file, pos) {
|
---|
| 88 | if (typeof pos.content !== 'string') return;
|
---|
| 89 | if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
|
---|
| 90 |
|
---|
| 91 | this.files[file] = pos.content;
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Applies any original source maps to the output and embeds the source file
|
---|
| 96 | * contents in the source map.
|
---|
| 97 | */
|
---|
| 98 |
|
---|
| 99 | exports.applySourceMaps = function() {
|
---|
| 100 | Object.keys(this.files).forEach(function(file) {
|
---|
| 101 | var content = this.files[file];
|
---|
| 102 | this.map.setSourceContent(file, content);
|
---|
| 103 |
|
---|
| 104 | if (this.options.inputSourcemaps !== false) {
|
---|
| 105 | var originalMap = sourceMapResolve.resolveSync(
|
---|
| 106 | content, file, fs.readFileSync);
|
---|
| 107 | if (originalMap) {
|
---|
| 108 | var map = new SourceMapConsumer(originalMap.map);
|
---|
| 109 | var relativeTo = originalMap.sourcesRelativeTo;
|
---|
[e29cc2e] | 110 | this.map.applySourceMap(map, file, urix(path.dirname(relativeTo)));
|
---|
[6a3a178] | 111 | }
|
---|
| 112 | }
|
---|
| 113 | }, this);
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * Process comments, drops sourceMap comments.
|
---|
| 118 | * @param {Object} node
|
---|
| 119 | */
|
---|
| 120 |
|
---|
| 121 | exports.comment = function(node) {
|
---|
| 122 | if (/^# sourceMappingURL=/.test(node.comment))
|
---|
| 123 | return this.emit('', node.position);
|
---|
| 124 | else
|
---|
| 125 | return this._comment(node);
|
---|
| 126 | };
|
---|