1 | var sourceMap = require('source-map');
|
2 | var path = require('path');
|
3 | var fs = require('fs');
|
4 |
|
5 | var toFileURL = require('./utils').toFileURL;
|
6 | var fromFileURL = require('./utils').fromFileURL;
|
7 |
|
8 | var wrapSourceMap = function(map) {
|
9 | return new sourceMap.SourceMapConsumer(map);
|
10 | };
|
11 |
|
12 | var sourceMapRegEx = /^\s*\/\/[@#] ?(sourceURL|sourceMappingURL)=([^\n'"]+)/m;
|
13 | exports.removeSourceMaps = function(source) {
|
14 | return source.replace(sourceMapRegEx, '');
|
15 | };
|
16 |
|
17 | function getMapObject(map) {
|
18 | if (typeof map != 'string')
|
19 | return map;
|
20 |
|
21 | try {
|
22 | return JSON.parse(map);
|
23 | }
|
24 | catch(error) {
|
25 | throw new Error('Invalid JSON: ' + map);
|
26 | }
|
27 | }
|
28 |
|
29 | function isFileURL(url) {
|
30 | return url.substr(0, 8) == 'file:///';
|
31 | }
|
32 |
|
33 | exports.concatenateSourceMaps = function(outFile, mapsWithOffsets, basePath, sourceMapContents) {
|
34 | var generated = new sourceMap.SourceMapGenerator({
|
35 | file: path.basename(outFile)
|
36 | });
|
37 |
|
38 | var outPath = path.dirname(outFile);
|
39 |
|
40 | var contentsBySource = sourceMapContents ? {} : null;
|
41 |
|
42 | mapsWithOffsets.forEach(function(pair) {
|
43 | var offset = pair[0];
|
44 | var map = getMapObject(pair[1]);
|
45 |
|
46 | if (sourceMapContents && map.sourcesContent) {
|
47 | for (var i=0; i<map.sources.length; i++) {
|
48 | var source = (map.sourceRoot || '') + map.sources[i];
|
49 | if (!source.match(/\/@traceur/)) {
|
50 | if (!contentsBySource[source]) {
|
51 | contentsBySource[source] = map.sourcesContent[i];
|
52 | } else {
|
53 | if (contentsBySource[source] != map.sourcesContent[i]) {
|
54 | throw new Error("Mismatched sourcesContent for: " + source);
|
55 | }
|
56 | }
|
57 | }
|
58 | }
|
59 | }
|
60 |
|
61 | wrapSourceMap(map).eachMapping(function(mapping) {
|
62 | if (mapping.originalLine == null || mapping.originalColumn == null || !mapping.source || mapping.source.match(/(\/|^)@traceur/))
|
63 | return;
|
64 |
|
65 | generated.addMapping({
|
66 | generated: {
|
67 | line: offset + mapping.generatedLine,
|
68 | column: mapping.generatedColumn
|
69 | },
|
70 | original: {
|
71 | line: mapping.originalLine,
|
72 | column: mapping.originalColumn
|
73 | },
|
74 | source: mapping.source,
|
75 | name: mapping.name
|
76 | });
|
77 | });
|
78 | });
|
79 |
|
80 |
|
81 | var normalized = JSON.parse(JSON.stringify(generated));
|
82 |
|
83 | if (sourceMapContents) {
|
84 | normalized.sourcesContent = normalized.sources.map(function(source) {
|
85 | if (contentsBySource[source])
|
86 | return contentsBySource[source];
|
87 |
|
88 | try {
|
89 | return fs.readFileSync(path.resolve(basePath, source)).toString();
|
90 | }
|
91 | catch (e) {
|
92 | return "";
|
93 | }
|
94 | });
|
95 | }
|
96 |
|
97 | normalized.sources = normalized.sources.map(function(source) {
|
98 | if (isFileURL(source))
|
99 | source = fromFileURL(source);
|
100 |
|
101 | return path.relative(outPath, path.resolve(basePath, source)).replace(/\\/g, '/');
|
102 | });
|
103 |
|
104 | return JSON.stringify(normalized);
|
105 | };
|