|
1 | 1 | var sourceMap = require('source-map'); |
| 2 | +var SourceNode = sourceMap.SourceNode; |
| 3 | +var SourceMapConsumer = sourceMap.SourceMapConsumer; |
2 | 4 | var path = require('path'); |
3 | | -var fs = require('fs'); |
4 | 5 |
|
5 | | -var toFileURL = require('./utils').toFileURL; |
6 | | -var fromFileURL = require('./utils').fromFileURL; |
| 6 | +exports.concatenate = function(files, outFile) { |
| 7 | + var concatenated = new SourceNode(); |
7 | 8 |
|
8 | | -var wrapSourceMap = function(map) { |
9 | | - return new sourceMap.SourceMapConsumer(map); |
10 | | -}; |
11 | | - |
12 | | -var sourceMapRegEx = /\/\/[@#] ?(sourceURL|sourceMappingURL)=([^\n'"]+)/; |
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 | | - } |
| 9 | + files.forEach(function (file, index) { |
| 10 | + if (index !== 0) { |
| 11 | + concatenated.add("\n"); |
59 | 12 | } |
60 | 13 |
|
61 | | - wrapSourceMap(map).eachMapping(function(mapping) { |
62 | | - if (!mapping.originalLine || !mapping.originalColumn || !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 | | - // normalize source paths and inject sourcesContent if necessary |
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 ""; |
| 14 | + var node; |
| 15 | + var map = file.map; |
| 16 | + if (map) { |
| 17 | + if (typeof map.toJSON === "function") { |
| 18 | + map = map.toJSON(); |
93 | 19 | } |
94 | | - }); |
95 | | - } |
96 | | - |
97 | | - normalized.sources = normalized.sources.map(function(source) { |
98 | | - if (isFileURL(source)) |
99 | | - source = fromFileURL(source); |
| 20 | + node = SourceNode.fromStringWithSourceMap( |
| 21 | + file.code, |
| 22 | + new SourceMapConsumer(map), |
| 23 | + path.relative( |
| 24 | + path.dirname(outFile + ".map"), |
| 25 | + path.dirname(".") |
| 26 | + ) |
| 27 | + ); |
| 28 | + } else { |
| 29 | + node = new SourceNode(null, null, null, file.code); |
| 30 | + } |
100 | 31 |
|
101 | | - return path.relative(outPath, path.resolve(basePath, source)).replace(/\\/g, '/'); |
| 32 | + concatenated.add(node); |
102 | 33 | }); |
103 | 34 |
|
104 | | - return JSON.stringify(normalized); |
| 35 | + return concatenated; |
105 | 36 | }; |
0 commit comments