Skip to content

Commit 90fd68f

Browse files
committed
added SourceMapGenerator.applySourceMap
1 parent 68ed4f4 commit 90fd68f

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

lib/source-map/source-map-generator.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,77 @@ define(function (require, exports, module) {
8989
}
9090
};
9191

92+
/**
93+
* Applies a SourceMap for a source file to the SourceMap.
94+
* Each mapping to the supplied source file is rewritten using the
95+
* supplied SourceMap. Note: The resolution for the resulting mappings
96+
* is the minimium of this map and the supplied map.
97+
*/
98+
SourceMapGenerator.prototype.applySourceMap =
99+
function SourceMapGenerator_applySourceMap(aSourceFile, aSourceMapConsumer) {
100+
// aSourceFile is an optional argument
101+
if(!aSourceMapConsumer) {
102+
aSourceMapConsumer = aSourceFile;
103+
aSourceFile = aSourceMapConsumer.file;
104+
}
105+
var sourceRoot = this._sourceRoot;
106+
// Applying the SourceMap can add and remove items from the sources and
107+
// the names array.
108+
var newSources = new ArraySet();
109+
var newNames = new ArraySet();
110+
111+
// Find mappings for the "aSourceFile"
112+
this._mappings.forEach(function (mapping) {
113+
if(mapping.source === aSourceFile && mapping.original) {
114+
// Check if it can be mapped by the SourceMap.
115+
// Than update the mapping.
116+
var original = aSourceMapConsumer.originalPositionFor({
117+
line: mapping.original.line,
118+
column: mapping.original.column
119+
});
120+
if(original && original.source !== null) {
121+
// Copy mapping
122+
mapping.source = original.source;
123+
mapping.original.line = original.line;
124+
mapping.original.column = original.column;
125+
mapping.name = mapping.name && original.name || mapping.name;
126+
// Try to make source file relative to our sourceRoot
127+
if(sourceRoot) {
128+
var relativeUrl = util.relative(sourceRoot, mapping.source);
129+
if(relativeUrl) {
130+
mapping.source = relativeUrl;
131+
}
132+
}
133+
}
134+
}
135+
136+
var source = mapping.source;
137+
if (source && !newSources.has(source)) {
138+
newSources.add(source);
139+
}
140+
141+
var name = mapping.name;
142+
if (name && !newNames.has(name)) {
143+
newNames.add(name);
144+
}
145+
146+
}, this);
147+
this._sources = newSources;
148+
this._names = newNames;
149+
aSourceMapConsumer.sources.forEach(function (sourceFile) {
150+
var content = aSourceMapConsumer.sourceContentFor(sourceFile);
151+
if(content) {
152+
if(sourceRoot) {
153+
var relativeUrl = util.relative(sourceRoot, sourceFile);
154+
if(relativeUrl) {
155+
sourceFile = relativeUrl;
156+
}
157+
}
158+
generator.setSourceContent(sourceFile, content);
159+
}
160+
});
161+
};
162+
92163
/**
93164
* A mapping can have one of the three levels of data:
94165
*

0 commit comments

Comments
 (0)