microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

138lines · 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 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 {
c205b801Vladimir Kotikov9 years ago25private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m;
4e7a6f0edlebu10 years ago26
876df2a0dlebu10 years ago27/**
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*/
5c8365a6Artem Egorov8 years ago31public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null {
32let result: IStrictUrl | null = null;
876df2a0dlebu10 years ago33
34// scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
35let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
36if (sourceMappingRelativeUrl) {
37let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
38sourceMappingUrl.protocol = scriptUrl.protocol;
39sourceMappingUrl.host = scriptUrl.host;
40// parse() repopulates all the properties of the URL
5c8365a6Artem Egorov8 years ago41result = <IStrictUrl>url.parse(url.format(sourceMappingUrl));
876df2a0dlebu10 years ago42}
43
44return 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*/
6eeec3c0Serge Svekolnikov8 years ago58public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string): string {
876df2a0dlebu10 years ago59try {
60let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
f16656e9frogcjn9 years ago61
62if (sourceMap.sections) {
63
64// TODO: there is a need to handle value.map == null, make a fake map
65sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
66return value.map != null;
67});
68
69sourceMap = require("flatten-source-map")(sourceMap);
70}
71
48644043Dmitry Zinovyev9 years ago72let sourceMapsCombinator = new SourceMapsCombinator();
73sourceMap = sourceMapsCombinator.convert(sourceMap);
74
f16656e9frogcjn9 years ago75if (sourceMap.sources) {
76sourceMap.sources = sourceMap.sources.map(sourcePath => {
6eeec3c0Serge Svekolnikov8 years ago77return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath, packagerRemoteRoot, packagerLocalRoot);
f16656e9frogcjn9 years ago78});
79}
876df2a0dlebu10 years ago80
81delete sourceMap.sourcesContent;
82sourceMap.sourceRoot = "";
83sourceMap.file = scriptPath;
84return JSON.stringify(sourceMap);
85} catch (exception) {
86return sourceMapBody;
87}
88}
89
3736c251dlebu10 years ago90/**
91* Updates source map URLs in the script body.
92*/
5c8365a6Artem Egorov8 years ago93public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) {
3736c251dlebu10 years ago94// Update the body with the new location of the source map on storage.
95return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
96"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
97}
98
876df2a0dlebu10 years ago99/**
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*/
6eeec3c0Serge Svekolnikov8 years ago104private updateSourceMapPath(sourcePath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) {
105if (packagerRemoteRoot && packagerLocalRoot) {
106packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
107packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
108sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
109}
876df2a0dlebu10 years ago110let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
111return 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*/
118private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago119let pathArgs = p.split(path.sep);
120return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago121}
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*/
131private getSourceMapRelativeUrl(body: string) {
4e7a6f0edlebu10 years ago132let match = body.match(SourceMapUtil.SourceMapURLRegex);
876df2a0dlebu10 years ago133// If match is null, the body doesn't contain the source map
134return match ? match[2] : null;
135}
136
3736c251dlebu10 years ago137
f16656e9frogcjn9 years ago138}