microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ba165ebb01f1e8585dc18ace0ca06d95221d7fbb

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

143lines · 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
979d7bfemax-mironov8 years ago90public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string) {
91scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`;
92return scriptBody;
93}
94
3736c251dlebu10 years ago95/**
96* Updates source map URLs in the script body.
97*/
5c8365a6Artem Egorov8 years ago98public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) {
3736c251dlebu10 years ago99// Update the body with the new location of the source map on storage.
100return scriptBody.replace(SourceMapUtil.SourceMapURLRegex,
101"//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname));
102}
103
876df2a0dlebu10 years ago104/**
105* Given an absolute source path, this method does two things:
106* 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
107* 2. It changes the path separators to Unix style.
108*/
6eeec3c0Serge Svekolnikov8 years ago109private updateSourceMapPath(sourcePath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) {
110if (packagerRemoteRoot && packagerLocalRoot) {
111packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
112packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
113sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
114}
876df2a0dlebu10 years ago115let relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
116return this.makeUnixStylePath(relativeSourcePath);
117}
118
119/**
120* Visual Studio Code source mapping requires Unix style path separators.
121* This method replaces all back-slash characters in a given string with forward-slash ones.
122*/
123private makeUnixStylePath(p: string): string {
4e7a6f0edlebu10 years ago124let pathArgs = p.split(path.sep);
125return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago126}
127
128/**
129* Parses the body of a script searching for a source map URL.
130* It supports the following source map url styles:
131* //# sourceMappingURL=path/to/source/map
132* //@ sourceMappingURL=path/to/source/map
133*
134* Returns the first match if found, null otherwise.
135*/
136private getSourceMapRelativeUrl(body: string) {
4e7a6f0edlebu10 years ago137let match = body.match(SourceMapUtil.SourceMapURLRegex);
876df2a0dlebu10 years ago138// If match is null, the body doesn't contain the source map
139return match ? match[2] : null;
140}
141
3736c251dlebu10 years ago142
f16656e9frogcjn9 years ago143}