microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
10136ee86a53d80ade52ebf07175f4acbed01d7f

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

150lines · 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
876df2a0dlebu10 years ago4import url = require("url");
5import path = require("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 {
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 {
c205b801Vladimir Kotikov9 years ago25private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m;
32558f9eYuri Skorokhodov6 years ago26private static SourceURLRegex: RegExp = /^\/\/[#@] ?sourceURL=(.+)$/m;
4e7a6f0edlebu10 years ago27
876df2a0dlebu10 years ago28/**
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*/
5c8365a6Artem Egorov8 years ago32public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null {
33let result: IStrictUrl | null = null;
876df2a0dlebu10 years ago34
35// scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
36let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
37if (sourceMappingRelativeUrl) {
38let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
39sourceMappingUrl.protocol = scriptUrl.protocol;
40sourceMappingUrl.host = scriptUrl.host;
41// parse() repopulates all the properties of the URL
5c8365a6Artem Egorov8 years ago42result = <IStrictUrl>url.parse(url.format(sourceMappingUrl));
876df2a0dlebu10 years ago43}
44
45return 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*/
6eeec3c0Serge Svekolnikov8 years ago59public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string): string {
876df2a0dlebu10 years ago60try {
61let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
f16656e9frogcjn9 years ago62
63if (sourceMap.sections) {
64
65// TODO: there is a need to handle value.map == null, make a fake map
66sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
67return value.map != null;
68});
69
70sourceMap = require("flatten-source-map")(sourceMap);
71}
72
48644043Dmitry Zinovyev9 years ago73let sourceMapsCombinator = new SourceMapsCombinator();
74sourceMap = sourceMapsCombinator.convert(sourceMap);
75
f16656e9frogcjn9 years ago76if (sourceMap.sources) {
77sourceMap.sources = sourceMap.sources.map(sourcePath => {
6eeec3c0Serge Svekolnikov8 years ago78return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath, packagerRemoteRoot, packagerLocalRoot);
f16656e9frogcjn9 years ago79});
80}
876df2a0dlebu10 years ago81
82delete sourceMap.sourcesContent;
83sourceMap.sourceRoot = "";
84sourceMap.file = scriptPath;
85return JSON.stringify(sourceMap);
86} catch (exception) {
87return sourceMapBody;
88}
89}
90
979d7bfemax-mironov8 years ago91public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string) {
92scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`;
93return scriptBody;
94}
95
3736c251dlebu10 years ago96/**
97* Updates source map URLs in the script body.
98*/
5c8365a6Artem Egorov8 years ago99public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) {
3736c251dlebu10 years ago100// Update the body with the new location of the source map on storage.
101return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
102"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
103}
104
2f5c780bYuri Skorokhodov6 years ago105/**
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*/
109public removeSourceURL(scriptBody: string) {
110return scriptBody.replace(SourceMapUtil.SourceURLRegex, "");
111}
112
876df2a0dlebu10 years ago113/**
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*/
6eeec3c0Serge Svekolnikov8 years ago118private updateSourceMapPath(sourcePath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) {
119if (packagerRemoteRoot && packagerLocalRoot) {
120packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
121packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
122sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
123}
876df2a0dlebu10 years ago124let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
125return 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*/
132private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago133let pathArgs = p.split(path.sep);
134return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago135}
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*/
145private getSourceMapRelativeUrl(body: string) {
4e7a6f0edlebu10 years ago146let match = body.match(SourceMapUtil.SourceMapURLRegex);
876df2a0dlebu10 years ago147// If match is null, the body doesn't contain the source map
148return match ? match[2] : null;
149}
f16656e9frogcjn9 years ago150}