SourceMapTools 3.0.0

dotnet add package SourceMapTools --version 3.0.0
NuGet\Install-Package SourceMapTools -Version 3.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SourceMapTools" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SourceMapTools --version 3.0.0
#r "nuget: SourceMapTools, 3.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SourceMapTools as a Cake Addin
#addin nuget:?package=SourceMapTools&version=3.0.0

// Install SourceMapTools as a Cake Tool
#tool nuget:?package=SourceMapTools&version=3.0.0

Source Map Tools License

This is a C# library for working with JavaScript source maps and deminifying JavaScript callstacks, based on microsoft/sourcemap-toolkit project with following new features:

  • .NET Core support
  • EcmaScript 6+ support

Source Map Parsing

The SourcemapTools.dll provides an API for parsing a souce map into an object that is easy to work with and an API for serializing source map object back to json string. The source map class has a method GetMappingEntryForGeneratedSourcePosition, which can be used to find a source map mapping entry that likely corresponds to a piece of generated code.

Usage

The top level API for source map parsing is the SourceMapParser.ParseSourceMap method. The input is a Stream that can be used to access the contents of the source map. The top level API for source map serializing is the SourceMapGenerator.SerializeMapping method. The input is a SourceMap that to be serialized and an optional JsonSerializerSettings that can be used to control the json serialization. A sample usage of the library is shown below.

// Parse the source map from file
SourceMap sourceMap;
using (var stream = new FileStream(@"sample.sourcemap", FileMode.Open))
{
    sourceMap = SourceMapParser.ParseSourceMap(stream);
}

// Manipulate the source map
...

// Save to source map to file
string serializedMap = SourceMapGenerator.SerializeMapping(sourceMap);
File.WriteAllText(@"updatedSample.sourcemap", serializedMap);

Chaining source maps

A common use case when dealing with source maps is multiple mapping layers. You can use ApplySourceMap to chain maps together to link back to the source

var inOriginal = new SourcePosition(34, 23);
var inBundled  = new SourcePosition(23, 12);
var inMinified = new SourcePosition(3 , 2 );

var originalToBundledEntry = new MappingEntry(inBundled, inOriginal, null, "original.js");
var bundledToMinifiedEntry = new MappingEntry(inMinified, inBundled, null, "bundle.js");

var bundledToOriginal = new SourceMap()
{
  File = "bundled.js",
  Sources = new List<string> { "original.js" },
  ParsedMappings = new List<MappingEntry> { originalToBundledEntry }
}

var minifiedToBundled = new SourceMap()
{
  File = "bundled.min.js",
  Sources = new List<string> { "bundled.js" },
  ParsedMappings = new List<MappingEntry> { bundledToMinifiedEntry }
}

// will contain mapping for line 3, column 2 in the minified file to line 34, column 23 in the original file
var minifiedToOriginal = minifiedToBundled.ApplySourceMap(bundledToOriginal);

Call Stack Deminification

The SourcemapToolkit.dll allows for the deminification of JavaScript call stacks.

Example

Call stack string
TypeError: Cannot read property 'length' of undefined
    at i (http://localhost:11323/crashcauser.min.js:1:113)
    at t (http://localhost:11323/crashcauser.min.js:1:75)
    at n (http://localhost:11323/crashcauser.min.js:1:50)
    at causeCrash (http://localhost:11323/crashcauser.min.js:1:222)
    at HTMLButtonElement.<anonymous> (http://localhost:11323/crashcauser.min.js:1:326)
Sample Minified StackFrame entry
    FilePath: "http://localhost:11323/crashcauser.min.js"
    MethodName: "i"
    SourcePosition.Column: 49
    SourcePosition.Line: 0
Sample Deminified StackFrame entry
    FilePath: "crashcauser.js"
    MethodName: "level1"
    SourcePosition.Column: 8
    SourcePosition.Line: 5

Usage

The top level API for call stack deminification is the StackTraceDeminifier.DeminifyStackTrace method. For each url that appears in a JavaScript callstack, the library requires the contents of the JavaScript file and corresponding source map in order to determine the original method name and code location. This information is provided by the consumer of the API by implementing the ISourceMapProvider and ISourceCodeProvider interfaces. These interfaces are expected to return a Stream that can be used to access the contents of the requested JavaScript code or corresponding source map. A StackTraceDeminifier can be instantiated using one of the methods on StackTraceDeminfierFactory. A sample usage of the library is shown below.

var sourceMapCallstackDeminifier = StackTraceDeminifierFactory.GetStackTraceDeminfier(new SourceMapProvider(), new SourceCodeProvider());
var deminifyStackTraceResult     = sourceMapCallstackDeminifier.DeminifyStackTrace(callstack, false);
var deminifiedCallstack          = deminifyStackTraceResult.ToString();

The result of DeminifyStackTrace is a DeminifyStackTraceResult, which is an object that contains a list of StackFrameDeminificationResults which contains the parsed minified StackFrame objects in the MinifiedStackFrame property and an enum indicating if any errors occured when attempting to deminify the StackFrame. The DeminifiedStackFrame property contains the best guess StackFrame object that maps to the MinifiedStackFrame element with the same index. Note that any of the properties on a StackTrace object may be null if no value could be extracted from the input callstack string or source map.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 4,903 10/24/2023
2.0.0 9,887 3/3/2022
2.0.0-preview.1 372 2/12/2022
1.0.2 1,823 8/12/2021
1.0.1 347 7/28/2021
1.0.0 561 6/30/2021
1.0.0-rc.3 182 2/15/2021
1.0.0-rc.2 200 2/13/2021