Resolve the source map and/or sources for a generated file.


\begin{DoxyCode}{0}
\DoxyCodeLine{var sourceMapResolve = require("{}source-\/map-\/resolve"{})}
\DoxyCodeLine{var sourceMap        = require("{}source-\/map"{})}
\DoxyCodeLine{}
\DoxyCodeLine{var code = [}
\DoxyCodeLine{  "{}!function()\{...\}();"{},}
\DoxyCodeLine{  "{}/*\# sourceMappingURL=foo.js.map */"{}}
\DoxyCodeLine{].join("{}\(\backslash\)n"{})}
\DoxyCodeLine{}
\DoxyCodeLine{sourceMapResolve.resolveSourceMap(code, "{}/js/foo.js"{}, fs.readFile, function(error, result) \{}
\DoxyCodeLine{  if (error) \{}
\DoxyCodeLine{    return notifyFailure(error)}
\DoxyCodeLine{  \}}
\DoxyCodeLine{  result}
\DoxyCodeLine{  // \{}
\DoxyCodeLine{  //   map: \{file: "{}foo.js"{}, mappings: "{}..."{}, sources: ["{}/coffee/foo.coffee"{}], names: []\},}
\DoxyCodeLine{  //   url: "{}/js/foo.js.map"{},}
\DoxyCodeLine{  //   sourcesRelativeTo: "{}/js/foo.js.map"{},}
\DoxyCodeLine{  //   sourceMappingURL: "{}foo.js.map"{}}
\DoxyCodeLine{  // \}}
\DoxyCodeLine{}
\DoxyCodeLine{  sourceMapResolve.resolveSources(result.map, result.sourcesRelativeTo, fs.readFile, function(error, result) \{}
\DoxyCodeLine{    if (error) \{}
\DoxyCodeLine{      return notifyFailure(error)}
\DoxyCodeLine{    \}}
\DoxyCodeLine{    result}
\DoxyCodeLine{    // \{}
\DoxyCodeLine{    //   sourcesResolved: ["{}/coffee/foo.coffee"{}],}
\DoxyCodeLine{    //   sourcesContent: ["{}<contents of /coffee/foo.coffee>"{}]}
\DoxyCodeLine{    // \}}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{sourceMapResolve.resolve(code, "{}/js/foo.js"{}, fs.readFile, function(error, result) \{}
\DoxyCodeLine{  if (error) \{}
\DoxyCodeLine{    return notifyFailure(error)}
\DoxyCodeLine{  \}}
\DoxyCodeLine{  result}
\DoxyCodeLine{  // \{}
\DoxyCodeLine{  //   map: \{file: "{}foo.js"{}, mappings: "{}..."{}, sources: ["{}/coffee/foo.coffee"{}], names: []\},}
\DoxyCodeLine{  //   url: "{}/js/foo.js.map"{},}
\DoxyCodeLine{  //   sourcesRelativeTo: "{}/js/foo.js.map"{},}
\DoxyCodeLine{  //   sourceMappingURL: "{}foo.js.map"{},}
\DoxyCodeLine{  //   sourcesResolved: ["{}/coffee/foo.coffee"{}],}
\DoxyCodeLine{  //   sourcesContent: ["{}<contents of /coffee/foo.coffee>"{}]}
\DoxyCodeLine{  // \}}
\DoxyCodeLine{  result.map.sourcesContent = result.sourcesContent}
\DoxyCodeLine{  var map = new sourceMap.sourceMapConsumer(result.map)}
\DoxyCodeLine{  map.sourceContentFor("{}/coffee/foo.coffee"{})}
\DoxyCodeLine{  // "{}<contents of /coffee/foo.coffee>"{}}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28453}{}\doxysection{Installation}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28453}

\begin{DoxyItemize}
\item {\ttfamily npm install source-\/map-\/resolve}
\item {\ttfamily bower install source-\/map-\/resolve}
\item {\ttfamily component install lydell/source-\/map-\/resolve}
\end{DoxyItemize}

Works with Common\+JS, AMD and browser globals, through UMD.

Note\+: This module requires {\ttfamily set\+Immediate} and {\ttfamily atob}. Use polyfills if needed, such as\+:


\begin{DoxyItemize}
\item \href{https://github.com/NobleJS/setImmediate}{\texttt{ https\+://github.\+com/\+Noble\+JS/set\+Immediate}}
\item \href{https://github.com/davidchambers/Base64.js}{\texttt{ https\+://github.\+com/davidchambers/\+Base64.\+js}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28454}{}\doxysection{Usage}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28454}
\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28455}{}\doxysubsubsection{$<$tt$>$source\+Map\+Resolve.\+resolve\+Source\+Map(code, code\+Url, read, callback)$<$/tt$>$}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28455}

\begin{DoxyItemize}
\item {\ttfamily code} is a string of code that may or may not contain a source\+Mapping\+URL comment. Such a comment is used to resolve the source map.
\item {\ttfamily code\+Url} is the url to the file containing {\ttfamily code}. If the source\+Mapping\+URL is relative, it is resolved against {\ttfamily code\+Url}.
\item {\ttfamily read(url, callback)} is a function that reads {\ttfamily url} and responds using {\ttfamily callback(error, content)}. In Node.\+js you might want to use {\ttfamily fs.\+read\+File}, while in the browser you might want to use an asynchronus {\ttfamily XMLHttp\+Request}.
\item {\ttfamily callback(error, result)} is a function that is invoked with either an error or {\ttfamily null} and the result.
\end{DoxyItemize}

The result is an object with the following properties\+:


\begin{DoxyItemize}
\item {\ttfamily map}\+: The source map for {\ttfamily code}, as an object (not a string).
\item {\ttfamily url}\+: The url to the source map. If the source map came from a data uri, this property is {\ttfamily null}, since then there is no url to it.
\item {\ttfamily sources\+Relative\+To}\+: The url that the sources of the source map are relative to. Since the sources are relative to the source map, and the url to the source map is provided as the {\ttfamily url} property, this property might seem superfluos. However, remember that the {\ttfamily url} property can be {\ttfamily null} if the source map came from a data uri. If so, the sources are relative to the file containing the data uri—{\ttfamily code\+Url}. This property will be identical to the {\ttfamily url} property or {\ttfamily code\+Url}, whichever is appropriate. This way you can conveniently resolve the sources without having to think about where the source map came from.
\item {\ttfamily source\+Mapping\+URL}\+: The url of the source\+Mapping\+URL comment in {\ttfamily code}.
\end{DoxyItemize}

If {\ttfamily code} contains no source\+Mapping\+URL, the result is {\ttfamily null}.\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28456}{}\doxysubsubsection{$<$tt$>$source\+Map\+Resolve.\+resolve\+Sources(map, map\+Url, read, \mbox{[}options\mbox{]}, callback)$<$/tt$>$}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28456}

\begin{DoxyItemize}
\item {\ttfamily map} is a source map, as an object (not a string).
\item {\ttfamily map\+Url} is the url to the file containing {\ttfamily map}. Relative sources in the source map, if any, are resolved against {\ttfamily map\+Url}.
\item {\ttfamily read(url, callback)} is a function that reads {\ttfamily url} and responds using {\ttfamily callback(error, content)}. In Node.\+js you might want to use {\ttfamily fs.\+read\+File}, while in the browser you might want to use an asynchronus {\ttfamily XMLHttp\+Request}.
\item {\ttfamily options} is an optional object with any of the following properties\+:
\begin{DoxyItemize}
\item {\ttfamily source\+Root}\+: Override the {\ttfamily source\+Root} property of the source map, which might only be relevant when resolving sources in the browser. This lets you bypass it when using the module outside of a browser, if needed. Pass a string to replace the {\ttfamily source\+Root} property with, or {\ttfamily false} to ignore it. Defaults to {\ttfamily undefined}.
\end{DoxyItemize}
\item {\ttfamily callback(error, result)} is a function that is invoked with either an error or {\ttfamily null} and the result.
\end{DoxyItemize}

The result is an object with the following properties\+:


\begin{DoxyItemize}
\item {\ttfamily sources\+Resolved}\+: The same as {\ttfamily map.\+sources}, except all the sources are fully resolved.
\item {\ttfamily sources\+Content}\+: An array with the contents of all sources in {\ttfamily map.\+sources}, in the same order as {\ttfamily map.\+sources}. If getting the contents of a source fails, an error object is put into the array instead.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28457}{}\doxysubsubsection{$<$tt$>$source\+Map\+Resolve.\+resolve(code, code\+Url, read, \mbox{[}options\mbox{]}, callback)$<$/tt$>$}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28457}
The arguments are identical to {\ttfamily source\+Map\+Resolve.\+resolve\+Source\+Map}, except that you may also provide the same {\ttfamily options} as in {\ttfamily source\+Map\+Resolve.\+resolve\+Sources}.

This is a convenience method that first resolves the source map and then its sources. You could also do this by first calling {\ttfamily source\+Map\+Resolve.\+resolve\+Source\+Map} and then {\ttfamily source\+Map\+Resolve.\+resolve\+Sources}.

The result is identical to {\ttfamily source\+Map\+Resolve.\+resolve\+Source\+Map}, with the properties from {\ttfamily source\+Map\+Resolve.\+resolve\+Sources} merged into it.

There is one extra feature available, though. If {\ttfamily code} is {\ttfamily null}, {\ttfamily code\+Url} is treated as a url to the source map instead of to {\ttfamily code}, and will be read. This is handy if you {\itshape sometimes} get the source map url from the {\ttfamily Source\+Map\+: $<$url$>$} header (see the \href{\#notes}{\texttt{ Notes}} section). In this case, the {\ttfamily source\+Mapping\+URL} property of the result is {\ttfamily null}.\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28458}{}\doxysubsubsection{$<$tt$>$source\+Map\+Resolve.$\ast$\+Sync()$<$/tt$>$}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28458}
There are also sync versions of the three previous functions. They are identical to the async versions, except\+:


\begin{DoxyItemize}
\item They expect a sync reading function. In Node.\+js you might want to use {\ttfamily fs.\+read\+File\+Sync}, while in the browser you might want to use a synchronus {\ttfamily XMLHttp\+Request}.
\item They throw errors and return the result instead of using a callback.
\end{DoxyItemize}

{\ttfamily source\+Map\+Resolve.\+resolve\+Sources\+Sync} also accepts {\ttfamily null} as the {\ttfamily read} parameter. The result is the same as when passing a function as the {\ttfamily read parameter}, except that the {\ttfamily sources\+Content} property of the result will be an empty array. In other words, the sources aren’t read. You only get the {\ttfamily sources\+Resolved} property. (This only supported in the synchronus version, since there is no point doing it asynchronusly.)\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28459}{}\doxysubsubsection{$<$tt$>$source\+Map\+Resolve.\+parse\+Map\+To\+JSON(string, \mbox{[}data\mbox{]})$<$/tt$>$}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28459}
The spec says that if a source map (as a string) starts with `)\mbox{]}\}'\`{}, it should be stripped off. This is to prevent XSSI attacks. This function does that and returns the result of {\ttfamily JSON.\+parse}ing what’s left.

If this function throws {\ttfamily error}, {\ttfamily error.\+source\+Map\+Data === data}.\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28460}{}\doxysubsubsection{Errors}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28460}
All errors passed to callbacks or thrown by this module have a {\ttfamily source\+Map\+Data} property that contain as much as possible of the intended result of the function up until the error occurred.

Note that while the {\ttfamily map} property of result objects always is an object, {\ttfamily error.\+source\+Map\+Data.\+map} will be a string if parsing that string fails.\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28461}{}\doxysection{Note}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28461}
This module resolves the source map for a given generated file by looking for a source\+Mapping\+URL comment. The spec defines yet a way to provide the URL to the source map\+: By sending the {\ttfamily Source\+Map\+: $<$url$>$} header along with the generated file. Since this module doesn’t retrive the generated code for you (instead {\itshape you} give the generated code to the module), it’s up to you to look for such a header when you retrieve the file (should the need arise).\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28462}{}\doxysection{Development}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28462}
\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28463}{}\doxysubsection{Tests}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28463}
First off, run {\ttfamily npm install} to install testing modules and browser polyfills.

{\ttfamily npm test} lints the code and runs the test suite in Node.\+js.\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28464}{}\doxysubsection{x-\/package.\+json5}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28464}
package.\+json, component.\+json and bower.\+json are all generated from x-\/package.\+json5 by using \href{https://github.com/kof/node-xpkg}{\texttt{ {\ttfamily xpkg}}}. Only edit x-\/package.\+json5, and remember to run {\ttfamily xpkg} before commiting!\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28465}{}\doxysubsection{Generating the browser version}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28465}
source-\/map-\/resolve.\+js is generated from source-\/map-\/resolve-\/node.\+js and source-\/map-\/resolve-\/template.\+js. Only edit the two latter files, {\itshape not} source-\/map-\/resolve.\+js! To generate it, run {\ttfamily npm run build}.\hypertarget{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28466}{}\doxysection{License}\label{md_sfcode_node_modules_source_map_resolve_readme_autotoc_md28466}
\mbox{[}MIT\mbox{]}(LICENSE). 