microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
d55f3c22ee18a37c605867c8bf588451292bd24e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

157lines · 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 * as url from "url";
5import * as path from "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 interface IStrictUrl extends url.Url {
20 pathname: string;
21 href: string;
22}
23
24export class SourceMapUtil {
25 private static SourceMapURLGlobalRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/gm;
26 private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m;
27 private static SourceURLRegex: RegExp = /^\/\/[#@] ?sourceURL=(.+)$/m;
28
29 /**
30 * Given a script body and URL, this method parses the body and finds the corresponding source map URL.
31 * If the source map URL is not found in the body in the expected form, null is returned.
32 */
33 public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null {
34 let result: IStrictUrl | null = null;
35
36 // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
37 let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
38 if (sourceMappingRelativeUrl) {
39 let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
40 sourceMappingUrl.protocol = scriptUrl.protocol;
41 sourceMappingUrl.host = scriptUrl.host;
42 // parse() repopulates all the properties of the URL
43 result = <IStrictUrl>url.parse(url.format(sourceMappingUrl));
44 }
45
46 return result;
47 }
48
49 /**
50 * Updates the contents of a source map file to be VS Code friendly:
51 * - makes source paths unix style and relative to the sources root path
52 * - updates the url of the script file
53 * - deletes the script content from the source map
54 *
55 * @parameter sourceMapBody - body of the source map as generated by the RN Packager.
56 * @parameter scriptPath - path of the script file asssociated with this source map.
57 * @parameter sourcesRootPath - root path of sources
58 *
59 */
60 public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string): string {
61 try {
62 let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
63
64 if (sourceMap.sections) {
65
66 // TODO: there is a need to handle value.map == null, make a fake map
67 sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
68 return value.map != null;
69 });
70
71 sourceMap = require("flatten-source-map")(sourceMap);
72 }
73
74 let sourceMapsCombinator = new SourceMapsCombinator();
75 sourceMap = sourceMapsCombinator.convert(sourceMap);
76
77 if (sourceMap.sources) {
78 sourceMap.sources = sourceMap.sources.map(sourcePath => {
79 return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath, packagerRemoteRoot, packagerLocalRoot);
80 });
81 }
82
83 delete sourceMap.sourcesContent;
84 sourceMap.sourceRoot = "";
85 sourceMap.file = scriptPath;
86 return JSON.stringify(sourceMap);
87 } catch (exception) {
88 return sourceMapBody;
89 }
90 }
91
92 public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string) {
93 scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`;
94 return scriptBody;
95 }
96
97 /**
98 * Updates source map URLs in the script body.
99 */
100 public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) {
101 // Update the body with the new location of the source map on storage.
102 return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
103 "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
104 }
105
106 /**
107 * Removes sourceURL from the script body since RN 0.61 because it breaks sourcemaps.
108 * Example: //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false -> ""
109 */
110 public removeSourceURL(scriptBody: string) {
111 return scriptBody.replace(SourceMapUtil.SourceURLRegex, "");
112 }
113
114 /**
115 * Parses the body of a script searching for a source map URL.
116 * It supports the following source map url styles:
117 *
118 * `//# sourceMappingURL=path/to/source/map`
119 *
120 * `//@ sourceMappingURL=path/to/source/map`
121 *
122 * Returns the last match if found, null otherwise.
123 */
124 public getSourceMapRelativeUrl(body: string) {
125 let matchesList = body.match(SourceMapUtil.SourceMapURLGlobalRegex);
126 // If match is null, the body doesn't contain the source map
127 if (matchesList) {
128 const sourceMapMatch = matchesList[matchesList.length - 1].match(SourceMapUtil.SourceMapURLRegex);
129 return sourceMapMatch ? sourceMapMatch[2] : null;
130 }
131 return null;
132 }
133
134 /**
135 * Given an absolute source path, this method does two things:
136 * 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
137 * 2. It changes the path separators to Unix style.
138 */
139 private updateSourceMapPath(sourcePath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) {
140 if (packagerRemoteRoot && packagerLocalRoot) {
141 packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
142 packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
143 sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
144 }
145 let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
146 return this.makeUnixStylePath(relativeSourcePath);
147 }
148
149 /**
150 * Visual Studio Code source mapping requires Unix style path separators.
151 * This method replaces all back-slash characters in a given string with forward-slash ones.
152 */
153 private makeUnixStylePath(p: string): string {
154 let pathArgs = p.split(path.sep);
155 return path.posix.join.apply(null, pathArgs);
156 }
157}
158