toJson method

Map<String, dynamic> toJson({
  1. bool includeSourceContents = false,
})

Encodes the Mapping mappings as a json map.

If includeSourceContents is true, this includes the source file contents from files in the map if possible.

Implementation

Map<String, dynamic> toJson({bool includeSourceContents = false}) {
  var buff = StringBuffer();
  var line = 0;
  var column = 0;
  var srcLine = 0;
  var srcColumn = 0;
  var srcUrlId = 0;
  var srcNameId = 0;
  var first = true;

  for (var entry in lines) {
    var nextLine = entry.line;
    if (nextLine > line) {
      for (var i = line; i < nextLine; ++i) {
        buff.write(';');
      }
      line = nextLine;
      column = 0;
      first = true;
    }

    for (var segment in entry.entries) {
      if (!first) buff.write(',');
      first = false;
      column = _append(buff, column, segment.column);

      // Encoding can be just the column offset if there is no source
      // information.
      var newUrlId = segment.sourceUrlId;
      if (newUrlId == null) continue;
      srcUrlId = _append(buff, srcUrlId, newUrlId);
      srcLine = _append(buff, srcLine, segment.sourceLine!);
      srcColumn = _append(buff, srcColumn, segment.sourceColumn!);

      if (segment.sourceNameId == null) continue;
      srcNameId = _append(buff, srcNameId, segment.sourceNameId!);
    }
  }

  var result = <String, dynamic>{
    'version': 3,
    'sourceRoot': sourceRoot ?? '',
    'sources': urls,
    'names': names,
    'mappings': buff.toString(),
  };
  if (targetUrl != null) result['file'] = targetUrl!;

  if (includeSourceContents) {
    result['sourcesContent'] = files.map((file) => file?.getText(0)).toList();
  }
  extensions.forEach((name, value) => result[name] = value);

  return result;
}