microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
5940f996e19e33b2c611c0d447a7ffb66d249d80

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

191lines · modeblame

9f036952Nisheet Jain10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
ce5e88eeYuri Skorokhodov5 years ago4import * as url from "url";
5import * as path from "path";
48644043Dmitry Zinovyev9 years ago6import { SourceMapsCombinator } from "./sourceMapsCombinator";
70bb7c83Vladimir Kotikov9 years ago7import { RawSourceMap } from "source-map";
876df2a0dlebu10 years ago8
66b52ec9Artem Egorov8 years ago9const IS_REMOTE = /^[a-zA-z]{2,}:\/\//; // Detection remote sources or specific protocols (like "webpack:///")
10
70bb7c83Vladimir Kotikov9 years ago11interface ISourceMap extends RawSourceMap {
f16656e9frogcjn10 years ago12sections?: ISourceMapSection[];
13}
14interface ISourceMapSection {
15map: ISourceMap;
34472878RedMickey5 years ago16offset: { column: number; line: number };
876df2a0dlebu10 years ago17}
18
5c8365a6Artem Egorov8 years ago19export interface IStrictUrl extends url.Url {
20pathname: string;
21href: string;
22}
23
876df2a0dlebu10 years ago24export class SourceMapUtil {
4cf8fdf4Yuri Skorokhodov6 years ago25private static SourceMapURLGlobalRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/gm;
c205b801Vladimir Kotikov9 years ago26private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m;
32558f9eYuri Skorokhodov6 years ago27private static SourceURLRegex: RegExp = /^\/\/[#@] ?sourceURL=(.+)$/m;
4e7a6f0edlebu10 years ago28
876df2a0dlebu10 years ago29/**
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*/
5c8365a6Artem Egorov8 years ago33public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null {
34let result: IStrictUrl | null = null;
876df2a0dlebu10 years ago35
36// scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
37let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
38if (sourceMappingRelativeUrl) {
39let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
40sourceMappingUrl.protocol = scriptUrl.protocol;
41sourceMappingUrl.host = scriptUrl.host;
42// parse() repopulates all the properties of the URL
5c8365a6Artem Egorov8 years ago43result = <IStrictUrl>url.parse(url.format(sourceMappingUrl));
876df2a0dlebu10 years ago44}
45
46return 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*/
34472878RedMickey5 years ago60public updateSourceMapFile(
61sourceMapBody: string,
62scriptPath: string,
63sourcesRootPath: string,
64packagerRemoteRoot?: string,
65packagerLocalRoot?: string,
66): string {
876df2a0dlebu10 years ago67try {
68let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
f16656e9frogcjn10 years ago69
70if (sourceMap.sections) {
71// TODO: there is a need to handle value.map == null, make a fake map
34472878RedMickey5 years ago72sourceMap.sections = sourceMap.sections.filter(value => {
f16656e9frogcjn10 years ago73return value.map != null;
74});
75
76sourceMap = require("flatten-source-map")(sourceMap);
77}
78
48644043Dmitry Zinovyev9 years ago79let sourceMapsCombinator = new SourceMapsCombinator();
80sourceMap = sourceMapsCombinator.convert(sourceMap);
81
f16656e9frogcjn10 years ago82if (sourceMap.sources) {
83sourceMap.sources = sourceMap.sources.map(sourcePath => {
34472878RedMickey5 years ago84return IS_REMOTE.test(sourcePath)
85? sourcePath
86: this.updateSourceMapPath(
87sourcePath,
88sourcesRootPath,
89packagerRemoteRoot,
90packagerLocalRoot,
91);
f16656e9frogcjn10 years ago92});
93}
876df2a0dlebu10 years ago94
95delete sourceMap.sourcesContent;
96sourceMap.sourceRoot = "";
97sourceMap.file = scriptPath;
98return JSON.stringify(sourceMap);
99} catch (exception) {
100return sourceMapBody;
101}
102}
103
34472878RedMickey5 years ago104public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string): string {
979d7bfemax-mironov8 years ago105scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`;
106return scriptBody;
107}
108
3736c251dlebu10 years ago109/**
110* Updates source map URLs in the script body.
111*/
34472878RedMickey5 years ago112public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl): string {
3736c251dlebu10 years ago113// Update the body with the new location of the source map on storage.
34472878RedMickey5 years ago114return scriptBody.replace(
115SourceMapUtil.SourceMapURLRegex,
116"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname),
117);
3736c251dlebu10 years ago118}
119
2f5c780bYuri Skorokhodov6 years ago120/**
121* Removes sourceURL from the script body since RN 0.61 because it breaks sourcemaps.
122* Example: //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false -> ""
123*/
34472878RedMickey5 years ago124public removeSourceURL(scriptBody: string): string {
2f5c780bYuri Skorokhodov6 years ago125return scriptBody.replace(SourceMapUtil.SourceURLRegex, "");
126}
127
4cf8fdf4Yuri Skorokhodov6 years ago128/**
129* Parses the body of a script searching for a source map URL.
130* It supports the following source map url styles:
131*
132* `//# sourceMappingURL=path/to/source/map`
133*
134* `//@ sourceMappingURL=path/to/source/map`
135*
136* Returns the last match if found, null otherwise.
137*/
34472878RedMickey5 years ago138public getSourceMapRelativeUrl(body: string): string | null {
4cf8fdf4Yuri Skorokhodov6 years ago139let matchesList = body.match(SourceMapUtil.SourceMapURLGlobalRegex);
140// If match is null, the body doesn't contain the source map
141if (matchesList) {
34472878RedMickey5 years ago142const sourceMapMatch = matchesList[matchesList.length - 1].match(
143SourceMapUtil.SourceMapURLRegex,
144);
341dba36Yuri Skorokhodov5 years ago145if (sourceMapMatch) {
146// On React Native macOS 0.62 sourceMappingUrl for macOS looks like:
147// # sourceMappingURL=//localhost:8081/index.map?platform=macos&dev=true&minify=false
148// Add 'http:' protocol to avoid errors in further processing
34472878RedMickey5 years ago149if (
150sourceMapMatch[2].includes("platform=macos") &&
151!sourceMapMatch[2].includes("http:") &&
152sourceMapMatch[2].startsWith("//")
153) {
341dba36Yuri Skorokhodov5 years ago154return "http:" + sourceMapMatch[2];
155} else {
156return sourceMapMatch[2];
157}
158}
4cf8fdf4Yuri Skorokhodov6 years ago159}
160return null;
161}
162
876df2a0dlebu10 years ago163/**
164* Given an absolute source path, this method does two things:
165* 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
166* 2. It changes the path separators to Unix style.
167*/
34472878RedMickey5 years ago168private updateSourceMapPath(
169sourcePath: string,
170sourcesRootPath: string,
171packagerRemoteRoot?: string,
172packagerLocalRoot?: string,
173) {
6eeec3c0Serge Svekolnikov8 years ago174if (packagerRemoteRoot && packagerLocalRoot) {
175packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
176packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
177sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
178}
876df2a0dlebu10 years ago179let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
180return this.makeUnixStylePath(relativeSourcePath);
181}
182
183/**
184* Visual Studio Code source mapping requires Unix style path separators.
185* This method replaces all back-slash characters in a given string with forward-slash ones.
186*/
187private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago188let pathArgs = p.split(path.sep);
189return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago190}
f16656e9frogcjn10 years ago191}