Skip to content

Commit 8deffe1

Browse files
authored
Improvements to source contents handling (#51)
* fix sourcecontent not being written on addEmptyMap * add getMappings, getSources and getNames and rework getMap to use the utils... * better test coverage * util for returning a sourcescontent map * version bump
1 parent 0a0cbfc commit 8deffe1

9 files changed

+309
-143
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@parcel/source-map",
3-
"version": "2.0.0-alpha.4.21",
3+
"version": "2.0.0-alpha.4.22",
44
"main": "./dist/node.js",
55
"types": "index.d.ts",
66
"browser": "./dist/wasm-browser.js",

src/MappingContainer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ std::string MappingContainer::getSourceContent(int sourceIndex) {
258258

259259
void MappingContainer::addEmptyMap(std::string& sourceName, std::string& sourceContent, int lineOffset) {
260260
int sourceIndex = addSource(sourceName);
261+
setSourceContent(sourceIndex, sourceContent);
261262
int currLine = 0;
262263
auto end = sourceContent.end();
263264
for (auto it = sourceContent.begin(); it != end; ++it) {

src/SourceMap.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ export default class SourceMap {
211211
return this.sourceMapInstance.getSource(index);
212212
}
213213

214+
/**
215+
* Get a list of all sources
216+
*/
217+
getSources(): Array<string> {
218+
return this.sourceMapInstance.getSources();
219+
}
220+
214221
/**
215222
* Set the sourceContent for a certain file
216223
* this is optional and is only recommended for files that we cannot read in at the end when we serialise the sourcemap
@@ -227,10 +234,30 @@ export default class SourceMap {
227234
*
228235
* @param sourceName filename
229236
*/
230-
getSourceContent(sourceName: string): string {
237+
getSourceContent(sourceName: string): string | null {
231238
return this.sourceMapInstance.getSourceContent(relatifyPath(sourceName, this.projectRoot));
232239
}
233240

241+
/**
242+
* Get a list of all sources
243+
*/
244+
getSourcesContent(): Array<string | null> {
245+
return this.sourceMapInstance.getSourcesContent();
246+
}
247+
248+
/**
249+
* Get a map of the source and it's corresponding source content
250+
*/
251+
getSourcesContentMap(): { [key: string]: string | null } {
252+
let sources = this.getSources();
253+
let sourcesContent = this.getSourcesContent();
254+
let results = {};
255+
for (let i = 0; i < sources.length; i++) {
256+
results[sources[i]] = sourcesContent[i] || null;
257+
}
258+
return results;
259+
}
260+
234261
/**
235262
* Get the index in the names array for a certain name
236263
*
@@ -249,6 +276,20 @@ export default class SourceMap {
249276
return this.sourceMapInstance.getName(index);
250277
}
251278

279+
/**
280+
* Get a list of all names
281+
*/
282+
getNames(): Array<string> {
283+
return this.sourceMapInstance.getNames();
284+
}
285+
286+
/**
287+
* Get a list of all mappings
288+
*/
289+
getMappings(): Array<IndexedMapping<number>> {
290+
return this.sourceMapInstance.getMappings();
291+
}
292+
252293
/**
253294
* Convert a Mapping object that uses indexes for name and source to the actual value of name and source
254295
*
@@ -295,7 +336,12 @@ export default class SourceMap {
295336
* Note: This is a fairly slow operation
296337
*/
297338
getMap(): ParsedMap {
298-
return this.sourceMapInstance.getMap();
339+
return {
340+
mappings: this.getMappings(),
341+
sources: this.getSources(),
342+
sourcesContent: this.getSourcesContent(),
343+
names: this.getNames(),
344+
};
299345
}
300346

301347
/**

src/napi/SourceMap.cpp

+73-56
Original file line numberDiff line numberDiff line change
@@ -170,60 +170,6 @@ Napi::Object SourceMapBinding::_mappingToObject(Napi::Env env, Mapping &mapping)
170170
return mappingObject;
171171
}
172172

173-
// returns the sorted and processed map with decoded vlqs and all other map data
174-
Napi::Value SourceMapBinding::getMap(const Napi::CallbackInfo &info) {
175-
Napi::Env env = info.Env();
176-
Napi::HandleScope scope(env);
177-
178-
Napi::Object obj = Napi::Object::New(env);
179-
180-
// Sort mappings
181-
_mapping_container.sort();
182-
183-
auto sourcesVector = _mapping_container.getSourcesVector();
184-
int len = sourcesVector.size();
185-
Napi::Array sourcesArray = Napi::Array::New(env, len);
186-
for (int i = 0; i < len; ++i) {
187-
sourcesArray.Set(i, sourcesVector[i]);
188-
}
189-
obj.Set("sources", sourcesArray);
190-
191-
auto sourcesContentVector = _mapping_container.getSourcesContentVector();
192-
len = sourcesContentVector.size();
193-
Napi::Array sourcesContentArray = Napi::Array::New(env, len);
194-
for (int i = 0; i < len; ++i) {
195-
sourcesContentArray.Set(i, sourcesContentVector[i]);
196-
}
197-
obj.Set("sourcesContent", sourcesContentArray);
198-
199-
auto namesVector = _mapping_container.getNamesVector();
200-
len = namesVector.size();
201-
Napi::Array namesArray = Napi::Array::New(env, len);
202-
for (int i = 0; i < len; ++i) {
203-
namesArray.Set(i, namesVector[i]);
204-
}
205-
obj.Set("names", namesArray);
206-
207-
auto mappingLinesVector = _mapping_container.getMappingLinesVector();
208-
Napi::Array mappingsArray = Napi::Array::New(env, _mapping_container.getTotalSegments());
209-
auto lineEnd = mappingLinesVector.end();
210-
int currentMapping = 0;
211-
for (auto lineIterator = mappingLinesVector.begin(); lineIterator != lineEnd; ++lineIterator) {
212-
auto &line = (*lineIterator);
213-
auto &segments = line._segments;
214-
auto segmentsEnd = segments.end();
215-
216-
for (auto segmentIterator = segments.begin(); segmentIterator != segmentsEnd; ++segmentIterator) {
217-
Mapping &mapping = *segmentIterator;
218-
mappingsArray.Set(currentMapping, _mappingToObject(env, mapping));
219-
++currentMapping;
220-
}
221-
}
222-
obj.Set("mappings", mappingsArray);
223-
224-
return obj;
225-
}
226-
227173
void SourceMapBinding::addIndexedMappings(const Napi::CallbackInfo &info) {
228174
Napi::Env env = info.Env();
229175
Napi::HandleScope scope(env);
@@ -269,7 +215,6 @@ Napi::Value SourceMapBinding::getSourceIndex(const Napi::CallbackInfo &info) {
269215
return Napi::Number::New(env, index);
270216
}
271217

272-
273218
Napi::Value SourceMapBinding::getSource(const Napi::CallbackInfo &info) {
274219
Napi::Env env = info.Env();
275220
Napi::HandleScope scope(env);
@@ -283,6 +228,20 @@ Napi::Value SourceMapBinding::getSource(const Napi::CallbackInfo &info) {
283228
return Napi::String::New(env, _mapping_container.getSource(index));
284229
}
285230

231+
Napi::Value SourceMapBinding::getSources(const Napi::CallbackInfo &info) {
232+
Napi::Env env = info.Env();
233+
Napi::HandleScope scope(env);
234+
235+
auto sourcesVector = _mapping_container.getSourcesVector();
236+
int len = sourcesVector.size();
237+
Napi::Array sourcesArray = Napi::Array::New(env, len);
238+
for (int i = 0; i < len; ++i) {
239+
sourcesArray.Set(i, sourcesVector[i]);
240+
}
241+
242+
return sourcesArray;
243+
}
244+
286245
Napi::Value SourceMapBinding::getNameIndex(const Napi::CallbackInfo &info) {
287246
Napi::Env env = info.Env();
288247
Napi::HandleScope scope(env);
@@ -311,6 +270,20 @@ Napi::Value SourceMapBinding::getName(const Napi::CallbackInfo &info) {
311270
return Napi::String::New(env, _mapping_container.getName(index));
312271
}
313272

273+
Napi::Value SourceMapBinding::getNames(const Napi::CallbackInfo &info) {
274+
Napi::Env env = info.Env();
275+
Napi::HandleScope scope(env);
276+
277+
auto namesVector = _mapping_container.getNamesVector();
278+
int len = namesVector.size();
279+
Napi::Array namesArray = Napi::Array::New(env, len);
280+
for (int i = 0; i < len; ++i) {
281+
namesArray.Set(i, namesVector[i]);
282+
}
283+
284+
return namesArray;
285+
}
286+
314287
std::vector<int> SourceMapBinding::_addNames(Napi::Array &namesArray) {
315288
std::vector<int> insertions;
316289
insertions.reserve(namesArray.Length());
@@ -395,6 +368,47 @@ Napi::Value SourceMapBinding::getSourceContent(const Napi::CallbackInfo &info) {
395368
return Napi::String::New(env, _mapping_container.getSourceContent(_mapping_container.getSourceIndex(sourceName)));
396369
}
397370

371+
Napi::Value SourceMapBinding::getSourcesContent(const Napi::CallbackInfo &info) {
372+
Napi::Env env = info.Env();
373+
Napi::HandleScope scope(env);
374+
375+
auto sourcesContentVector = _mapping_container.getSourcesContentVector();
376+
int len = sourcesContentVector.size();
377+
Napi::Array sourcesContentArray = Napi::Array::New(env, len);
378+
for (int i = 0; i < len; ++i) {
379+
sourcesContentArray.Set(i, sourcesContentVector[i]);
380+
}
381+
382+
return sourcesContentArray;
383+
}
384+
385+
// returns the sorted and processed mappings with decoded vlqs
386+
Napi::Value SourceMapBinding::getMappings(const Napi::CallbackInfo &info) {
387+
Napi::Env env = info.Env();
388+
Napi::HandleScope scope(env);
389+
390+
// Sort mappings
391+
_mapping_container.sort();
392+
393+
auto mappingLinesVector = _mapping_container.getMappingLinesVector();
394+
Napi::Array mappingsArray = Napi::Array::New(env, _mapping_container.getTotalSegments());
395+
auto lineEnd = mappingLinesVector.end();
396+
int currentMapping = 0;
397+
for (auto lineIterator = mappingLinesVector.begin(); lineIterator != lineEnd; ++lineIterator) {
398+
auto &line = (*lineIterator);
399+
auto &segments = line._segments;
400+
auto segmentsEnd = segments.end();
401+
402+
for (auto segmentIterator = segments.begin(); segmentIterator != segmentsEnd; ++segmentIterator) {
403+
Mapping &mapping = *segmentIterator;
404+
mappingsArray.Set(currentMapping, _mappingToObject(env, mapping));
405+
++currentMapping;
406+
}
407+
}
408+
409+
return mappingsArray;
410+
}
411+
398412
void SourceMapBinding::addEmptyMap(const Napi::CallbackInfo &info) {
399413
Napi::Env env = info.Env();
400414
Napi::HandleScope scope(env);
@@ -464,16 +478,19 @@ Napi::Object SourceMapBinding::Init(Napi::Env env, Napi::Object exports) {
464478
InstanceMethod("addBufferMappings", &SourceMapBinding::addBufferMappings),
465479
InstanceMethod("stringify", &SourceMapBinding::stringify),
466480
InstanceMethod("toBuffer", &SourceMapBinding::toBuffer),
467-
InstanceMethod("getMap", &SourceMapBinding::getMap),
468481
InstanceMethod("addIndexedMappings", &SourceMapBinding::addIndexedMappings),
469482
InstanceMethod("addName", &SourceMapBinding::addName),
470483
InstanceMethod("addSource", &SourceMapBinding::addSource),
471484
InstanceMethod("setSourceContent", &SourceMapBinding::setSourceContent),
472485
InstanceMethod("getSourceContent", &SourceMapBinding::getSourceContent),
486+
InstanceMethod("getSourcesContent", &SourceMapBinding::getSourcesContent),
473487
InstanceMethod("getSourceIndex", &SourceMapBinding::getSourceIndex),
474488
InstanceMethod("getSource", &SourceMapBinding::getSource),
489+
InstanceMethod("getSources", &SourceMapBinding::getSources),
475490
InstanceMethod("getNameIndex", &SourceMapBinding::getNameIndex),
476491
InstanceMethod("getName", &SourceMapBinding::getName),
492+
InstanceMethod("getNames", &SourceMapBinding::getNames),
493+
InstanceMethod("getMappings", &SourceMapBinding::getMappings),
477494
InstanceMethod("extends", &SourceMapBinding::extends),
478495
InstanceMethod("addEmptyMap", &SourceMapBinding::addEmptyMap),
479496
InstanceMethod("findClosestMapping", &SourceMapBinding::findClosestMapping),

src/napi/SourceMap.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class SourceMapBinding : public Napi::ObjectWrap<SourceMapBinding> {
3535

3636
Napi::Value addName(const Napi::CallbackInfo &info);
3737

38-
Napi::Value getMap(const Napi::CallbackInfo &info);
39-
4038
Napi::Value stringify(const Napi::CallbackInfo &info);
4139

4240
Napi::Value toBuffer(const Napi::CallbackInfo &info);
@@ -45,12 +43,20 @@ class SourceMapBinding : public Napi::ObjectWrap<SourceMapBinding> {
4543

4644
Napi::Value getSource(const Napi::CallbackInfo &info);
4745

46+
Napi::Value getSources(const Napi::CallbackInfo &info);
47+
4848
Napi::Value getSourceContent(const Napi::CallbackInfo &info);
4949

50+
Napi::Value getSourcesContent(const Napi::CallbackInfo &info);
51+
5052
Napi::Value getNameIndex(const Napi::CallbackInfo &info);
5153

5254
Napi::Value getName(const Napi::CallbackInfo &info);
5355

56+
Napi::Value getNames(const Napi::CallbackInfo &info);
57+
58+
Napi::Value getMappings(const Napi::CallbackInfo &info);
59+
5460
Napi::Value findClosestMapping(const Napi::CallbackInfo &info);
5561

5662
Napi::Object _mappingToObject(Napi::Env env, Mapping &mapping);

src/wasm.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,20 @@ export default class WasmSourceMap extends SourceMap {
104104
}
105105
}
106106

107-
getMap(): ParsedMap {
108-
let mappings = arrayFromEmbind(this.sourceMapInstance.getMappings(), patchMapping);
107+
getSourcesContent(): Array<string | null> {
108+
return arrayFromEmbind(this.sourceMapInstance.getSourcesContent());
109+
}
109110

110-
return {
111-
mappings,
112-
sources: arrayFromEmbind(this.sourceMapInstance.getSources()),
113-
sourcesContent: arrayFromEmbind(this.sourceMapInstance.getSourcesContent()),
114-
names: arrayFromEmbind(this.sourceMapInstance.getNames()),
115-
};
111+
getSources(): Array<string> {
112+
return arrayFromEmbind(this.sourceMapInstance.getSources());
113+
}
114+
115+
getNames(): Array<string> {
116+
return arrayFromEmbind(this.sourceMapInstance.getNames());
117+
}
118+
119+
getMappings(): Array<IndexedMapping<number>> {
120+
return arrayFromEmbind(this.sourceMapInstance.getMappings(), patchMapping);
116121
}
117122

118123
toVLQ(): VLQMap {

test/append.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,29 @@ describe('SourceMap - Append Mappings', () => {
270270
assert.deepEqual(map.getMap(), expectedResultTwo);
271271
});
272272

273+
it('Append buffer mappings with sourceContent', () => {
274+
let originalMap = new SourceMap('/test-root');
275+
originalMap.addRawMappings({
276+
mappings: SIMPLE_SOURCE_MAP.mappings,
277+
sources: SIMPLE_SOURCE_MAP.sources,
278+
names: SIMPLE_SOURCE_MAP.names,
279+
sourcesContent: ['a-content'],
280+
});
281+
let buffer = originalMap.toBuffer();
282+
283+
let newMap = new SourceMap('/test-root');
284+
newMap.addRawMappings({
285+
mappings: SIMPLE_SOURCE_MAP.mappings,
286+
sources: SIMPLE_SOURCE_MAP.sources,
287+
names: SIMPLE_SOURCE_MAP.names,
288+
});
289+
newMap.addBufferMappings(buffer, 10);
290+
assert.deepEqual(newMap.getMap(), {
291+
...expectedResultOne,
292+
sourcesContent: ['a-content'],
293+
});
294+
});
295+
273296
it('Append vlq mappings with line offset', () => {
274297
let map = new SourceMap('/test-root');
275298
map.addRawMappings({

0 commit comments

Comments
 (0)