microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
7be1388c3442500874ff28fc32e6f332ad9724ef

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

110lines · modeblame

9f036952Nisheet Jain10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
876df2a0dlebu10 years ago4import url = require("url");
5import path = require("path");
6
7interface ISourceMap {
8file: string;
9sources: string[];
10version: number;
11names: string[];
12mappings: string;
13sourceRoot?: string;
14sourcesContent?: string[];
15}
16
17export class SourceMapUtil {
4e7a6f0edlebu10 years ago18private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=(.+?)\s*$/m;
19
876df2a0dlebu10 years ago20/**
21* Given a script body and URL, this method parses the body and finds the corresponding source map URL.
22* If the source map URL is not found in the body in the expected form, null is returned.
23*/
24public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): url.Url {
25let result: url.Url = null;
26
27// scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
28let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
29if (sourceMappingRelativeUrl) {
30let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
31sourceMappingUrl.protocol = scriptUrl.protocol;
32sourceMappingUrl.host = scriptUrl.host;
33// parse() repopulates all the properties of the URL
34result = url.parse(url.format(sourceMappingUrl));
35}
36
37return result;
38}
39
40/**
41* Updates the contents of a source map file to be VS Code friendly:
42* - makes source paths unix style and relative to the sources root path
43* - updates the url of the script file
44* - deletes the script content from the source map
45*
46* @parameter sourceMapBody - body of the source map as generated by the RN Packager.
47* @parameter scriptPath - path of the script file asssociated with this source map.
48* @parameter sourcesRootPath - root path of sources
49*
50*/
51public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string): string {
52try {
53let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
54sourceMap.sources = sourceMap.sources.map(sourcePath => {
55return this.updateSourceMapPath(sourcePath, sourcesRootPath);
56});
57
58delete sourceMap.sourcesContent;
59sourceMap.sourceRoot = "";
60sourceMap.file = scriptPath;
61return JSON.stringify(sourceMap);
62} catch (exception) {
63return sourceMapBody;
64}
65}
66
3736c251dlebu10 years ago67/**
68* Updates source map URLs in the script body.
69*/
70public updateScriptPaths(scriptBody: string, sourceMappingUrl: url.Url) {
71// Update the body with the new location of the source map on storage.
72return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
73"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
74}
75
876df2a0dlebu10 years ago76/**
77* Given an absolute source path, this method does two things:
78* 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
79* 2. It changes the path separators to Unix style.
80*/
81private updateSourceMapPath(sourcePath: string, sourcesRootPath: string) {
82let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
83return this.makeUnixStylePath(relativeSourcePath);
84}
85
86/**
87* Visual Studio Code source mapping requires Unix style path separators.
88* This method replaces all back-slash characters in a given string with forward-slash ones.
89*/
90private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago91let pathArgs = p.split(path.sep);
92return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago93}
94
95/**
96* Parses the body of a script searching for a source map URL.
97* It supports the following source map url styles:
98* //# sourceMappingURL=path/to/source/map
99* //@ sourceMappingURL=path/to/source/map
100*
101* Returns the first match if found, null otherwise.
102*/
103private getSourceMapRelativeUrl(body: string) {
4e7a6f0edlebu10 years ago104let match = body.match(SourceMapUtil.SourceMapURLRegex);
876df2a0dlebu10 years ago105// If match is null, the body doesn't contain the source map
106return match ? match[2] : null;
107}
108
3736c251dlebu10 years ago109
876df2a0dlebu10 years ago110}