microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c4a42a327d73c81a8ed065dfe1a0963f35bed44c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

128lines · 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");
6
7interface ISourceMap {
8file: string;
f16656e9frogcjn9 years ago9sources?: string[];
876df2a0dlebu10 years ago10version: number;
f16656e9frogcjn9 years ago11names?: string[];
12mappings?: string;
876df2a0dlebu10 years ago13sourceRoot?: string;
14sourcesContent?: string[];
f16656e9frogcjn9 years ago15sections?: ISourceMapSection[];
16}
17interface ISourceMapSection {
18map: ISourceMap;
19offset: { column: number, line: number };
876df2a0dlebu10 years ago20}
21
22export class SourceMapUtil {
4e7a6f0edlebu10 years ago23private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=(.+?)\s*$/m;
24
876df2a0dlebu10 years ago25/**
26* Given a script body and URL, this method parses the body and finds the corresponding source map URL.
27* If the source map URL is not found in the body in the expected form, null is returned.
28*/
29public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): url.Url {
30let result: url.Url = null;
31
32// scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
33let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
34if (sourceMappingRelativeUrl) {
35let sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
36sourceMappingUrl.protocol = scriptUrl.protocol;
37sourceMappingUrl.host = scriptUrl.host;
38// parse() repopulates all the properties of the URL
39result = url.parse(url.format(sourceMappingUrl));
40}
41
42return result;
43}
44
45/**
46* Updates the contents of a source map file to be VS Code friendly:
47* - makes source paths unix style and relative to the sources root path
48* - updates the url of the script file
49* - deletes the script content from the source map
50*
51* @parameter sourceMapBody - body of the source map as generated by the RN Packager.
52* @parameter scriptPath - path of the script file asssociated with this source map.
53* @parameter sourcesRootPath - root path of sources
54*
55*/
56public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string): string {
57try {
58let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
f16656e9frogcjn9 years ago59
60if (sourceMap.sections) {
61
62// TODO: there is a need to handle value.map == null, make a fake map
63sourceMap.sections = sourceMap.sections.filter((value, index, array) => {
64return value.map != null;
65});
66
67sourceMap = require("flatten-source-map")(sourceMap);
68}
69
70if (sourceMap.sources) {
71sourceMap.sources = sourceMap.sources.map(sourcePath => {
72return this.updateSourceMapPath(sourcePath, sourcesRootPath);
73});
74}
876df2a0dlebu10 years ago75
76delete sourceMap.sourcesContent;
77sourceMap.sourceRoot = "";
78sourceMap.file = scriptPath;
79return JSON.stringify(sourceMap);
80} catch (exception) {
81return sourceMapBody;
82}
83}
84
3736c251dlebu10 years ago85/**
86* Updates source map URLs in the script body.
87*/
88public updateScriptPaths(scriptBody: string, sourceMappingUrl: url.Url) {
89// Update the body with the new location of the source map on storage.
90return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
91"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
92}
93
876df2a0dlebu10 years ago94/**
95* Given an absolute source path, this method does two things:
96* 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
97* 2. It changes the path separators to Unix style.
98*/
99private updateSourceMapPath(sourcePath: string, sourcesRootPath: string) {
100let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
101return this.makeUnixStylePath(relativeSourcePath);
102}
103
104/**
105* Visual Studio Code source mapping requires Unix style path separators.
106* This method replaces all back-slash characters in a given string with forward-slash ones.
107*/
108private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago109let pathArgs = p.split(path.sep);
110return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago111}
112
113/**
114* Parses the body of a script searching for a source map URL.
115* It supports the following source map url styles:
116* //# sourceMappingURL=path/to/source/map
117* //@ sourceMappingURL=path/to/source/map
118*
119* Returns the first match if found, null otherwise.
120*/
121private getSourceMapRelativeUrl(body: string) {
4e7a6f0edlebu10 years ago122let match = body.match(SourceMapUtil.SourceMapURLRegex);
876df2a0dlebu10 years ago123// If match is null, the body doesn't contain the source map
124return match ? match[2] : null;
125}
126
3736c251dlebu10 years ago127
f16656e9frogcjn9 years ago128}