microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70cfd5650973cc6872aaae01ea2db7a924e6b67b

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

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