From:  Andy Sterland <Andy.Sterland@microsoft.com>
Date:  03 Jun 2014 07:01:28 Hong Kong Time
Newsgroup:  news.mozilla.org/mozilla.dev.js-sourcemap
Subject:  

Adding media type for sources to source maps

NNTP-Posting-Host:  63.245.216.66

Hi all,

I'm looking to get some feedback on a proposal to add a prefixed* property to the source map schema to describe the language of a source file. The proposal is to cover both files that are compiled from one source language and the rarer case of having multiple languages contributing to the compiled file that executes. Though not covered in the proposal below it could be useful to also include the language of the generated file e.g. text/css or application/javascript etc.

For the case where there is only one source language the value of the x_ms_mediaTypes property is an array with only one string defining a media type for all the source files. The string itself should be a media type as defined by RFC6838. As there really isn't an exhaustive list of media types it's really an opaque string between source map producers and consumers but ideally producers should document their media type.

Single source language example:
{
  "version": 3,
  "file": "combined.js",
  "sources": ["a.ts", "b.ts"],
  "names": ["method"],
  "mappings": "AAAA,...",
  "x_ms_mediaTypes": ["application/x.typescript;version=1.0.3.0"]
}

For the case where there is mixed source content the source map would look like:
{
  "version": 3,
  "file": "combined.js",
  "sources": ["a.ts", "b_old.ts"],
  "names": ["method"],
  "mappings": "AAAA,...",
  "x_ms_mediaTypes": ["application/x.typescript;version=1.0.3.0", "application/x.typescript;version=1.0.0.0"],
  "x_ms_sourceMediaTypes": [0, 1]
}
In this case the x_ms_mediaTypes array is a list of all the unique media types and an additional property x_ms_sourceMediaTypes is added to map source files to media types using the array indices. In the example below the source file b_old.ts is at index 1 of the sources array and is at index 1 of the x_ms_sourceMediaTypes array which values points to index 1 of the x_ms_mediaTypes which has the value of "application/x.typescript;version=1.0.0.0".

The main scenario we want to support is removing ambiguity around the source language. In the IE F12 developers we load a language service that does things such as syntax coloring the text and determining the span of statements etc. In F12 there is a bunch of languages services for JavaScript, TypeScript, CSS, CoffeeScript, XML, HTML, C# etc. the trouble with source maps is that we don't always know 'which' language service to load for source mapped files. Today we base the language service on the URI and the content-type header which has had mixed success and more than a few sites send info that causes F12 to load the wrong language service, which can't be worked around. Beyond the language type itself the version of the language is virtually impossible to work out but can be important as language versions can have syntactic differences which would require a different language service.

We're looking at adding this in both the TypeScript compiler and the F12 debugger in IE and would love to get feedback from everyone as it seems like it would be useful for all.

Thoughts?

-Andy


* With the hope of it becoming part of the standard in the future.