microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c0b32993595df760282a903082cf37d59de18ea8

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

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