microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
66b52ec91bf71c509ff18c0c35c78a126d7f8dc0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

128lines · 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
9const IS_REMOTE = /^[a-zA-z]{2,}:\/\//; // Detection remote sources or specific protocols (like "webpack:///")
10
11interface ISourceMap extends RawSourceMap {
12 sections?: ISourceMapSection[];
13}
14interface ISourceMapSection {
15 map: ISourceMap;
16 offset: { column: number, line: number };
17}
18
19export class SourceMapUtil {
20 private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m;
21
22 /**
23 * Given a script body and URL, this method parses the body and finds the corresponding source map URL.
24 * If the source map URL is not found in the body in the expected form, null is returned.
25 */
26 public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): url.Url {
27 let result: url.Url = null;
28
29 // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
30 let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
31 if (sourceMappingRelativeUrl) {
32 let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
33 sourceMappingUrl.protocol = scriptUrl.protocol;
34 sourceMappingUrl.host = scriptUrl.host;
35 // parse() repopulates all the properties of the URL
36 result = url.parse(url.format(sourceMappingUrl));
37 }
38
39 return result;
40 }
41
42 /**
43 * Updates the contents of a source map file to be VS Code friendly:
44 * - makes source paths unix style and relative to the sources root path
45 * - updates the url of the script file
46 * - deletes the script content from the source map
47 *
48 * @parameter sourceMapBody - body of the source map as generated by the RN Packager.
49 * @parameter scriptPath - path of the script file asssociated with this source map.
50 * @parameter sourcesRootPath - root path of sources
51 *
52 */
53 public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string): string {
54 try {
55 let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
56
57 if (sourceMap.sections) {
58
59 // TODO: there is a need to handle value.map == null, make a fake map
60 sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
61 return value.map != null;
62 });
63
64 sourceMap = require("flatten-source-map")(sourceMap);
65 }
66
67 let sourceMapsCombinator = new SourceMapsCombinator();
68 sourceMap = sourceMapsCombinator.convert(sourceMap);
69
70 if (sourceMap.sources) {
71 sourceMap.sources = sourceMap.sources.map(sourcePath => {
72 return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath);
73 });
74 }
75
76 delete sourceMap.sourcesContent;
77 sourceMap.sourceRoot = "";
78 sourceMap.file = scriptPath;
79 return JSON.stringify(sourceMap);
80 } catch (exception) {
81 return sourceMapBody;
82 }
83 }
84
85 /**
86 * Updates source map URLs in the script body.
87 */
88 public updateScriptPaths(scriptBody: string, sourceMappingUrl: url.Url) {
89 // Update the body with the new location of the source map on storage.
90 return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
91 "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
92 }
93
94 /**
95 * Given an absolute source path, this method does two things:
96 * 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
97 * 2. It changes the path separators to Unix style.
98 */
99 private updateSourceMapPath(sourcePath: string, sourcesRootPath: string) {
100 let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
101 return this.makeUnixStylePath(relativeSourcePath);
102 }
103
104 /**
105 * Visual Studio Code source mapping requires Unix style path separators.
106 * This method replaces all back-slash characters in a given string with forward-slash ones.
107 */
108 private makeUnixStylePath(p: string): string {
109 let pathArgs = p.split(path.sep);
110 return path.posix.join.apply(null, pathArgs);
111 }
112
113 /**
114 * Parses the body of a script searching for a source map URL.
115 * It supports the following source map url styles:
116 * //# sourceMappingURL=path/to/source/map
117 * //@ sourceMappingURL=path/to/source/map
118 *
119 * Returns the first match if found, null otherwise.
120 */
121 private getSourceMapRelativeUrl(body: string) {
122 let match = body.match(SourceMapUtil.SourceMapURLRegex);
123 // If match is null, the body doesn't contain the source map
124 return match ? match[2] : null;
125 }
126
127
128}
129