microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/sourceMap.ts
133lines · modeblame
9f036952Nisheet Jain10 years ago | 1 | // 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 ago | 4 | import url = require("url"); |
| 5 | import path = require("path"); | |
48644043Dmitry Zinovyev9 years ago | 6 | import { SourceMapsCombinator } from "./sourceMapsCombinator"; |
70bb7c83Vladimir Kotikov8 years ago | 7 | import { RawSourceMap } from "source-map"; |
876df2a0dlebu10 years ago | 8 | |
66b52ec9Artem Egorov8 years ago | 9 | const IS_REMOTE = /^[a-zA-z]{2,}:\/\//; // Detection remote sources or specific protocols (like "webpack:///") |
| 10 | | |
70bb7c83Vladimir Kotikov8 years ago | 11 | interface ISourceMap extends RawSourceMap { |
f16656e9frogcjn9 years ago | 12 | sections?: ISourceMapSection[]; |
| 13 | } | |
| 14 | interface ISourceMapSection { | |
| 15 | map: ISourceMap; | |
| 16 | offset: { column: number, line: number }; | |
876df2a0dlebu10 years ago | 17 | } |
| 18 | | |
5c8365a6Artem Egorov8 years ago | 19 | export interface IStrictUrl extends url.Url { |
| 20 | pathname: string; | |
| 21 | href: string; | |
| 22 | } | |
| 23 | | |
876df2a0dlebu10 years ago | 24 | export class SourceMapUtil { |
c205b801Vladimir Kotikov9 years ago | 25 | private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m; |
4e7a6f0edlebu10 years ago | 26 | |
876df2a0dlebu10 years ago | 27 | /** |
| 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 ago | 31 | public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null { |
| 32 | let result: IStrictUrl | null = null; | |
876df2a0dlebu10 years ago | 33 | |
| 34 | // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" | |
| 35 | let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true" | |
| 36 | if (sourceMappingRelativeUrl) { | |
| 37 | let sourceMappingUrl = url.parse(sourceMappingRelativeUrl); | |
| 38 | sourceMappingUrl.protocol = scriptUrl.protocol; | |
| 39 | sourceMappingUrl.host = scriptUrl.host; | |
| 40 | // parse() repopulates all the properties of the URL | |
5c8365a6Artem Egorov8 years ago | 41 | result = <IStrictUrl>url.parse(url.format(sourceMappingUrl)); |
876df2a0dlebu10 years ago | 42 | } |
| 43 | | |
| 44 | return 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 | */ | |
| 58 | public updateSourceMapFile(sourceMapBody: string, scriptPath: string, sourcesRootPath: string): string { | |
| 59 | try { | |
| 60 | let sourceMap = <ISourceMap>JSON.parse(sourceMapBody); | |
f16656e9frogcjn9 years ago | 61 | |
| 62 | if (sourceMap.sections) { | |
| 63 | | |
| 64 | // TODO: there is a need to handle value.map == null, make a fake map | |
| 65 | sourceMap.sections = sourceMap.sections.filter((value, index, array) => { | |
| 66 | return value.map != null; | |
| 67 | }); | |
| 68 | | |
| 69 | sourceMap = require("flatten-source-map")(sourceMap); | |
| 70 | } | |
| 71 | | |
48644043Dmitry Zinovyev9 years ago | 72 | let sourceMapsCombinator = new SourceMapsCombinator(); |
| 73 | sourceMap = sourceMapsCombinator.convert(sourceMap); | |
| 74 | | |
f16656e9frogcjn9 years ago | 75 | if (sourceMap.sources) { |
| 76 | sourceMap.sources = sourceMap.sources.map(sourcePath => { | |
66b52ec9Artem Egorov8 years ago | 77 | return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath); |
f16656e9frogcjn9 years ago | 78 | }); |
| 79 | } | |
876df2a0dlebu10 years ago | 80 | |
| 81 | delete sourceMap.sourcesContent; | |
| 82 | sourceMap.sourceRoot = ""; | |
| 83 | sourceMap.file = scriptPath; | |
| 84 | return JSON.stringify(sourceMap); | |
| 85 | } catch (exception) { | |
| 86 | return sourceMapBody; | |
| 87 | } | |
| 88 | } | |
| 89 | | |
3736c251dlebu10 years ago | 90 | /** |
| 91 | * Updates source map URLs in the script body. | |
| 92 | */ | |
5c8365a6Artem Egorov8 years ago | 93 | public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) { |
3736c251dlebu10 years ago | 94 | // Update the body with the new location of the source map on storage. |
| 95 | return scriptBody.replace(SourceMapUtil.SourceMapURLRegex, | |
| 96 | "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname)); | |
| 97 | } | |
| 98 | | |
876df2a0dlebu10 years ago | 99 | /** |
| 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 | */ | |
| 104 | private updateSourceMapPath(sourcePath: string, sourcesRootPath: string) { | |
| 105 | let relativeSourcePath = path.relative(sourcesRootPath, sourcePath); | |
| 106 | return this.makeUnixStylePath(relativeSourcePath); | |
| 107 | } | |
| 108 | | |
| 109 | /** | |
| 110 | * Visual Studio Code source mapping requires Unix style path separators. | |
| 111 | * This method replaces all back-slash characters in a given string with forward-slash ones. | |
| 112 | */ | |
| 113 | private makeUnixStylePath(p: string): string { | |
4e7a6f0edlebu10 years ago | 114 | let pathArgs = p.split(path.sep); |
| 115 | return path.posix.join.apply(null, pathArgs); | |
876df2a0dlebu10 years ago | 116 | } |
| 117 | | |
| 118 | /** | |
| 119 | * Parses the body of a script searching for a source map URL. | |
| 120 | * It supports the following source map url styles: | |
| 121 | * //# sourceMappingURL=path/to/source/map | |
| 122 | * //@ sourceMappingURL=path/to/source/map | |
| 123 | * | |
| 124 | * Returns the first match if found, null otherwise. | |
| 125 | */ | |
| 126 | private getSourceMapRelativeUrl(body: string) { | |
4e7a6f0edlebu10 years ago | 127 | let match = body.match(SourceMapUtil.SourceMapURLRegex); |
876df2a0dlebu10 years ago | 128 | // If match is null, the body doesn't contain the source map |
| 129 | return match ? match[2] : null; | |
| 130 | } | |
| 131 | | |
3736c251dlebu10 years ago | 132 | |
f16656e9frogcjn9 years ago | 133 | } |