microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70bb7c837ccaaa18600156aa8a698af0d78c3487

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

126lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import url = require("url");
5import path = require("path");
6import { SourceMapsCombinator } from "./sourceMapsCombinator";
7import { RawSourceMap } from "source-map";
8
9interface ISourceMap extends RawSourceMap {
10 sections?: ISourceMapSection[];
11}
12interface ISourceMapSection {
13 map: ISourceMap;
14 offset: { column: number, line: number };
15}
16
17export class SourceMapUtil {
18 private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m;
19
20 /**
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 */
24 public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): url.Url {
25 let result: url.Url = null;
26
27 // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
28 let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
29 if (sourceMappingRelativeUrl) {
30 let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
31 sourceMappingUrl.protocol = scriptUrl.protocol;
32 sourceMappingUrl.host = scriptUrl.host;
33 // parse() repopulates all the properties of the URL
34 result = url.parse(url.format(sourceMappingUrl));
35 }
36
37 return 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 */
51 public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string): string {
52 try {
53 let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
54
55 if (sourceMap.sections) {
56
57 // TODO: there is a need to handle value.map == null, make a fake map
58 sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
59 return value.map != null;
60 });
61
62 sourceMap = require("flatten-source-map")(sourceMap);
63 }
64
65 let sourceMapsCombinator = new SourceMapsCombinator();
66 sourceMap = sourceMapsCombinator.convert(sourceMap);
67
68 if (sourceMap.sources) {
69 sourceMap.sources = sourceMap.sources.map(sourcePath => {
70 return this.updateSourceMapPath(sourcePath, sourcesRootPath);
71 });
72 }
73
74 delete sourceMap.sourcesContent;
75 sourceMap.sourceRoot = "";
76 sourceMap.file = scriptPath;
77 return JSON.stringify(sourceMap);
78 } catch (exception) {
79 return sourceMapBody;
80 }
81 }
82
83 /**
84 * Updates source map URLs in the script body.
85 */
86 public updateScriptPaths(scriptBody: string, sourceMappingUrl: url.Url) {
87 // Update the body with the new location of the source map on storage.
88 return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
89 "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
90 }
91
92 /**
93 * Given an absolute source path, this method does two things:
94 * 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
95 * 2. It changes the path separators to Unix style.
96 */
97 private updateSourceMapPath(sourcePath: string, sourcesRootPath: string) {
98 let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
99 return this.makeUnixStylePath(relativeSourcePath);
100 }
101
102 /**
103 * Visual Studio Code source mapping requires Unix style path separators.
104 * This method replaces all back-slash characters in a given string with forward-slash ones.
105 */
106 private makeUnixStylePath(p: string): string {
107 let pathArgs = p.split(path.sep);
108 return path.posix.join.apply(null, pathArgs);
109 }
110
111 /**
112 * Parses the body of a script searching for a source map URL.
113 * It supports the following source map url styles:
114 * //# sourceMappingURL=path/to/source/map
115 * //@ sourceMappingURL=path/to/source/map
116 *
117 * Returns the first match if found, null otherwise.
118 */
119 private getSourceMapRelativeUrl(body: string) {
120 let match = body.match(SourceMapUtil.SourceMapURLRegex);
121 // If match is null, the body doesn't contain the source map
122 return match ? match[2] : null;
123 }
124
125
126}
127