1 |
|
2 |
|
3 |
|
4 |
|
5 | var CodeNode = require("./CodeNode");
|
6 | var SourceNode = require("./SourceNode");
|
7 | var MappingsContext = require("./MappingsContext");
|
8 |
|
9 | function SourceListMap(generatedCode, source, originalSource) {
|
10 | if(Array.isArray(generatedCode)) {
|
11 | this.children = generatedCode;
|
12 | } else {
|
13 | this.children = [];
|
14 | if(generatedCode || source)
|
15 | this.add(generatedCode, source, originalSource);
|
16 | }
|
17 | }
|
18 | module.exports = SourceListMap;
|
19 |
|
20 | SourceListMap.prototype.add = function(generatedCode, source, originalSource) {
|
21 | if(typeof generatedCode === "string") {
|
22 | if(source) {
|
23 | this.children.push(new SourceNode(generatedCode, source, originalSource));
|
24 | } else if(this.children.length > 0 && this.children[this.children.length - 1].addGeneratedCode) {
|
25 | this.children[this.children.length - 1].addGeneratedCode(generatedCode);
|
26 | } else {
|
27 | this.children.push(new CodeNode(generatedCode));
|
28 | }
|
29 | } else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
|
30 | this.children.push(generatedCode);
|
31 | } else if(generatedCode.children) {
|
32 | generatedCode.children.forEach(function(sln) {
|
33 | this.children.push(sln);
|
34 | }, this);
|
35 | } else {
|
36 | throw new Error("Invalid arguments to SourceListMap.prototype.add: Expected string, Node or SourceListMap");
|
37 | }
|
38 | };
|
39 |
|
40 | SourceListMap.prototype.preprend = function(source) {
|
41 | if(typeof generatedCode === "string") {
|
42 | if(source) {
|
43 | this.children.unshift(new SourceNode(generatedCode, source, originalSource));
|
44 | } else if(this.children.length > 0 && this.children[this.children.length - 1].preprendGeneratedCode) {
|
45 | this.children[this.children.length - 1].preprendGeneratedCode(generatedCode);
|
46 | } else {
|
47 | this.children.unshift(new CodeNode(generatedCode));
|
48 | }
|
49 | } else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
|
50 | this.children.unshift(generatedCode);
|
51 | } else if(generatedCode.children) {
|
52 | generatedCode.children.slice().reverse().forEach(function(sln) {
|
53 | this.children.unshift(sln);
|
54 | }, this);
|
55 | } else {
|
56 | throw new Error("Invalid arguments to SourceListMap.prototype.prerend: Expected string, Node or SourceListMap");
|
57 | }
|
58 | };
|
59 |
|
60 | SourceListMap.prototype.mapGeneratedCode = function(fn) {
|
61 | this.children.forEach(function(sln) {
|
62 | sln.mapGeneratedCode(fn);
|
63 | });
|
64 | };
|
65 |
|
66 | SourceListMap.prototype.toString = function() {
|
67 | return this.children.map(function(sln) {
|
68 | return sln.getGeneratedCode();
|
69 | }).join("");
|
70 | };
|
71 |
|
72 | SourceListMap.prototype.toStringWithSourceMap = function(options) {
|
73 | var mappingsContext = new MappingsContext();
|
74 | var source = this.children.map(function(sln) {
|
75 | return sln.generatedCode;
|
76 | }).join("");
|
77 | var mappings = this.children.map(function(sln) {
|
78 | return sln.getMappings(mappingsContext);
|
79 | }).join("");
|
80 | return {
|
81 | source: source,
|
82 | map: {
|
83 | version: 3,
|
84 | file: options && options.file,
|
85 | sources: mappingsContext.sources,
|
86 | sourcesContent: mappingsContext.hasSourceContent ? mappingsContext.sourcesContent : undefined,
|
87 | mappings: mappings
|
88 | }
|
89 | };
|
90 | }
|