microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8df5011e47ada44be3951868ba32e5fae98f48bb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

166lines · 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 Kotikov8 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 Kotikov8 years ago11interface ISourceMap extends RawSourceMap {
f16656e9frogcjn9 years ago12sections?: ISourceMapSection[];
13}
14interface ISourceMapSection {
15map: ISourceMap;
16offset: { 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*/
6eeec3c0Serge Svekolnikov8 years ago60public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string): string {
876df2a0dlebu10 years ago61try {
62let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
f16656e9frogcjn9 years ago63
64if (sourceMap.sections) {
65
66// TODO: there is a need to handle value.map == null, make a fake map
67sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
68return value.map != null;
69});
70
71sourceMap = require("flatten-source-map")(sourceMap);
72}
73
48644043Dmitry Zinovyev9 years ago74let sourceMapsCombinator = new SourceMapsCombinator();
75sourceMap = sourceMapsCombinator.convert(sourceMap);
76
f16656e9frogcjn9 years ago77if (sourceMap.sources) {
78sourceMap.sources = sourceMap.sources.map(sourcePath => {
6eeec3c0Serge Svekolnikov8 years ago79return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath, packagerRemoteRoot, packagerLocalRoot);
f16656e9frogcjn9 years ago80});
81}
876df2a0dlebu10 years ago82
83delete sourceMap.sourcesContent;
84sourceMap.sourceRoot = "";
85sourceMap.file = scriptPath;
86return JSON.stringify(sourceMap);
87} catch (exception) {
88return sourceMapBody;
89}
90}
91
979d7bfemax-mironov8 years ago92public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string) {
93scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`;
94return scriptBody;
95}
96
3736c251dlebu10 years ago97/**
98* Updates source map URLs in the script body.
99*/
5c8365a6Artem Egorov8 years ago100public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) {
3736c251dlebu10 years ago101// Update the body with the new location of the source map on storage.
102return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
103"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
104}
105
2f5c780bYuri Skorokhodov6 years ago106/**
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*/
110public removeSourceURL(scriptBody: string) {
111return scriptBody.replace(SourceMapUtil.SourceURLRegex, "");
112}
113
4cf8fdf4Yuri Skorokhodov6 years ago114/**
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*/
124public getSourceMapRelativeUrl(body: string) {
125let matchesList = body.match(SourceMapUtil.SourceMapURLGlobalRegex);
126// If match is null, the body doesn't contain the source map
127if (matchesList) {
128const sourceMapMatch = matchesList[matchesList.length - 1].match(SourceMapUtil.SourceMapURLRegex);
341dba36Yuri Skorokhodov5 years ago129if (sourceMapMatch) {
130// On React Native macOS 0.62 sourceMappingUrl for macOS looks like:
131// # sourceMappingURL=//localhost:8081/index.map?platform=macos&dev=true&minify=false
132// Add 'http:' protocol to avoid errors in further processing
133if (sourceMapMatch[2].includes("platform=macos") && !sourceMapMatch[2].includes("http:") && sourceMapMatch[2].startsWith("//")) {
134return "http:" + sourceMapMatch[2];
135} else {
136return sourceMapMatch[2];
137}
138}
4cf8fdf4Yuri Skorokhodov6 years ago139}
140return null;
141}
142
876df2a0dlebu10 years ago143/**
144* Given an absolute source path, this method does two things:
145* 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
146* 2. It changes the path separators to Unix style.
147*/
6eeec3c0Serge Svekolnikov8 years ago148private updateSourceMapPath(sourcePath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) {
149if (packagerRemoteRoot && packagerLocalRoot) {
150packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
151packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
152sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
153}
876df2a0dlebu10 years ago154let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
155return this.makeUnixStylePath(relativeSourcePath);
156}
157
158/**
159* Visual Studio Code source mapping requires Unix style path separators.
160* This method replaces all back-slash characters in a given string with forward-slash ones.
161*/
162private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago163let pathArgs = p.split(path.sep);
164return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago165}
f16656e9frogcjn9 years ago166}