microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/sourceMap.ts
199lines · 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 | | |
ce5e88eeYuri Skorokhodov5 years ago | 4 | import * as url from "url"; |
| 5 | import * as path from "path"; | |
70bb7c83Vladimir Kotikov8 years ago | 6 | import { RawSourceMap } from "source-map"; |
09f6024fHeniker4 years ago | 7 | import { SourceMapsCombinator } from "./sourceMapsCombinator"; |
876df2a0dlebu10 years ago | 8 | |
09f6024fHeniker4 years ago | 9 | const IS_REMOTE = /^[A-z]{2,}:\/\//; // Detection remote sources or specific protocols (like "webpack:///") |
66b52ec9Artem Egorov8 years ago | 10 | |
70bb7c83Vladimir Kotikov8 years ago | 11 | interface ISourceMap extends RawSourceMap { |
f16656e9frogcjn9 years ago | 12 | sections?: ISourceMapSection[]; |
| 13 | } | |
| 14 | interface ISourceMapSection { | |
| 15 | map: ISourceMap; | |
34472878RedMickey5 years ago | 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 { |
09f6024fHeniker4 years ago | 25 | private static SourceMapURLGlobalRegex: RegExp = |
716c1b85RedMickey4 years ago | 26 | /\/\/(#|@) sourceMappingURL=((?!data:)[^ ]+?)\s*$/gm; |
| 27 | private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:)[^ ]+?)\s*$/m; | |
32558f9eYuri Skorokhodov6 years ago | 28 | private static SourceURLRegex: RegExp = /^\/\/[#@] ?sourceURL=(.+)$/m; |
4e7a6f0edlebu10 years ago | 29 | |
876df2a0dlebu10 years ago | 30 | /** |
| 31 | * Given a script body and URL, this method parses the body and finds the corresponding source map URL. | |
| 32 | * If the source map URL is not found in the body in the expected form, null is returned. | |
| 33 | */ | |
5c8365a6Artem Egorov8 years ago | 34 | public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null { |
| 35 | let result: IStrictUrl | null = null; | |
876df2a0dlebu10 years ago | 36 | |
| 37 | // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" | |
09f6024fHeniker4 years ago | 38 | const sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true" |
876df2a0dlebu10 years ago | 39 | if (sourceMappingRelativeUrl) { |
09f6024fHeniker4 years ago | 40 | const sourceMappingUrl = url.parse(sourceMappingRelativeUrl); |
876df2a0dlebu10 years ago | 41 | sourceMappingUrl.protocol = scriptUrl.protocol; |
| 42 | sourceMappingUrl.host = scriptUrl.host; | |
| 43 | // parse() repopulates all the properties of the URL | |
5c8365a6Artem Egorov8 years ago | 44 | result = <IStrictUrl>url.parse(url.format(sourceMappingUrl)); |
876df2a0dlebu10 years ago | 45 | } |
| 46 | | |
| 47 | return result; | |
| 48 | } | |
| 49 | | |
| 50 | /** | |
| 51 | * Updates the contents of a source map file to be VS Code friendly: | |
| 52 | * - makes source paths unix style and relative to the sources root path | |
| 53 | * - updates the url of the script file | |
| 54 | * - deletes the script content from the source map | |
| 55 | * | |
| 56 | * @parameter sourceMapBody - body of the source map as generated by the RN Packager. | |
| 57 | * @parameter scriptPath - path of the script file asssociated with this source map. | |
| 58 | * @parameter sourcesRootPath - root path of sources | |
| 59 | * | |
| 60 | */ | |
34472878RedMickey5 years ago | 61 | public updateSourceMapFile( |
| 62 | sourceMapBody: string, | |
| 63 | scriptPath: string, | |
| 64 | sourcesRootPath: string, | |
| 65 | packagerRemoteRoot?: string, | |
| 66 | packagerLocalRoot?: string, | |
| 67 | ): string { | |
876df2a0dlebu10 years ago | 68 | try { |
| 69 | let sourceMap = <ISourceMap>JSON.parse(sourceMapBody); | |
f16656e9frogcjn9 years ago | 70 | |
| 71 | if (sourceMap.sections) { | |
| 72 | // TODO: there is a need to handle value.map == null, make a fake map | |
09f6024fHeniker4 years ago | 73 | sourceMap.sections = sourceMap.sections.filter(value => value.map != null); |
f16656e9frogcjn9 years ago | 74 | |
09f6024fHeniker4 years ago | 75 | // eslint-disable-next-line @typescript-eslint/no-var-requires |
f16656e9frogcjn9 years ago | 76 | sourceMap = require("flatten-source-map")(sourceMap); |
| 77 | } | |
| 78 | | |
09f6024fHeniker4 years ago | 79 | const sourceMapsCombinator = new SourceMapsCombinator(); |
48644043Dmitry Zinovyev9 years ago | 80 | sourceMap = sourceMapsCombinator.convert(sourceMap); |
| 81 | | |
f16656e9frogcjn9 years ago | 82 | if (sourceMap.sources) { |
09f6024fHeniker4 years ago | 83 | sourceMap.sources = sourceMap.sources.map(sourcePath => |
| 84 | IS_REMOTE.test(sourcePath) | |
34472878RedMickey5 years ago | 85 | ? sourcePath |
| 86 | : this.updateSourceMapPath( | |
| 87 | sourcePath, | |
| 88 | sourcesRootPath, | |
| 89 | packagerRemoteRoot, | |
| 90 | packagerLocalRoot, | |
09f6024fHeniker4 years ago | 91 | ), |
| 92 | ); | |
f16656e9frogcjn9 years ago | 93 | } |
876df2a0dlebu10 years ago | 94 | |
| 95 | delete sourceMap.sourcesContent; | |
| 96 | sourceMap.sourceRoot = ""; | |
| 97 | sourceMap.file = scriptPath; | |
| 98 | return JSON.stringify(sourceMap); | |
| 99 | } catch (exception) { | |
| 100 | return sourceMapBody; | |
| 101 | } | |
| 102 | } | |
| 103 | | |
34472878RedMickey5 years ago | 104 | public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string): string { |
979d7bfemax-mironov8 years ago | 105 | scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`; |
| 106 | return scriptBody; | |
| 107 | } | |
| 108 | | |
3736c251dlebu10 years ago | 109 | /** |
| 110 | * Updates source map URLs in the script body. | |
| 111 | */ | |
34472878RedMickey5 years ago | 112 | public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl): string { |
716c1b85RedMickey4 years ago | 113 | const sourceMapMatch = this.searchSourceMapURL(scriptBody); |
| 114 | if (sourceMapMatch) { | |
| 115 | // Update the body with the new location of the source map on storage. | |
| 116 | return scriptBody.replace( | |
| 117 | sourceMapMatch[0], | |
| 118 | `//# sourceMappingURL=${path.basename(sourceMappingUrl.pathname)}`, | |
| 119 | ); | |
| 120 | } | |
| 121 | return scriptBody; | |
3736c251dlebu10 years ago | 122 | } |
| 123 | | |
2f5c780bYuri Skorokhodov6 years ago | 124 | /** |
| 125 | * Removes sourceURL from the script body since RN 0.61 because it breaks sourcemaps. | |
| 126 | * Example: //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false -> "" | |
| 127 | */ | |
34472878RedMickey5 years ago | 128 | public removeSourceURL(scriptBody: string): string { |
2f5c780bYuri Skorokhodov6 years ago | 129 | return scriptBody.replace(SourceMapUtil.SourceURLRegex, ""); |
| 130 | } | |
| 131 | | |
4cf8fdf4Yuri Skorokhodov6 years ago | 132 | /** |
| 133 | * Parses the body of a script searching for a source map URL. | |
| 134 | * It supports the following source map url styles: | |
| 135 | * | |
| 136 | * `//# sourceMappingURL=path/to/source/map` | |
| 137 | * | |
| 138 | * `//@ sourceMappingURL=path/to/source/map` | |
| 139 | * | |
| 140 | * Returns the last match if found, null otherwise. | |
| 141 | */ | |
34472878RedMickey5 years ago | 142 | public getSourceMapRelativeUrl(body: string): string | null { |
716c1b85RedMickey4 years ago | 143 | const sourceMapMatch = this.searchSourceMapURL(body); |
4cf8fdf4Yuri Skorokhodov6 years ago | 144 | // If match is null, the body doesn't contain the source map |
716c1b85RedMickey4 years ago | 145 | if (sourceMapMatch) { |
| 146 | // On React Native macOS 0.62 and RN Windows 0.65 sourceMappingUrl looks like: | |
| 147 | // # sourceMappingURL=//localhost:8081/index.map?platform=macos&dev=true&minify=false | |
| 148 | // Add 'http:' protocol to avoid errors in further processing | |
| 149 | const el = sourceMapMatch[2]; | |
| 150 | const macOsOrWin = | |
| 151 | (el.includes("platform=macos") || el.includes("platform=window")) && | |
| 152 | el.startsWith("//") && | |
| 153 | !el.includes("http:"); | |
| 154 | | |
| 155 | return macOsOrWin ? `http:${el}` : el; | |
4cf8fdf4Yuri Skorokhodov6 years ago | 156 | } |
716c1b85RedMickey4 years ago | 157 | return null; |
| 158 | } | |
09f6024fHeniker4 years ago | 159 | |
716c1b85RedMickey4 years ago | 160 | private searchSourceMapURL(str: string): RegExpMatchArray | null { |
| 161 | const matchesList = str | |
| 162 | .match(SourceMapUtil.SourceMapURLGlobalRegex) | |
| 163 | ?.filter(s => !s.includes("\\n")); | |
| 164 | if (matchesList && matchesList.length) { | |
| 165 | return matchesList[matchesList.length - 1].match(SourceMapUtil.SourceMapURLRegex); | |
| 166 | } | |
09f6024fHeniker4 years ago | 167 | |
716c1b85RedMickey4 years ago | 168 | return null; |
4cf8fdf4Yuri Skorokhodov6 years ago | 169 | } |
| 170 | | |
876df2a0dlebu10 years ago | 171 | /** |
| 172 | * Given an absolute source path, this method does two things: | |
| 173 | * 1. It changes the path from absolute to be relative to the sourcesRootPath parameter. | |
| 174 | * 2. It changes the path separators to Unix style. | |
| 175 | */ | |
34472878RedMickey5 years ago | 176 | private updateSourceMapPath( |
| 177 | sourcePath: string, | |
| 178 | sourcesRootPath: string, | |
| 179 | packagerRemoteRoot?: string, | |
| 180 | packagerLocalRoot?: string, | |
| 181 | ) { | |
6eeec3c0Serge Svekolnikov8 years ago | 182 | if (packagerRemoteRoot && packagerLocalRoot) { |
| 183 | packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot); | |
| 184 | packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot); | |
| 185 | sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot); | |
| 186 | } | |
09f6024fHeniker4 years ago | 187 | const relativeSourcePath = path.relative(sourcesRootPath, sourcePath); |
876df2a0dlebu10 years ago | 188 | return this.makeUnixStylePath(relativeSourcePath); |
| 189 | } | |
| 190 | | |
| 191 | /** | |
| 192 | * Visual Studio Code source mapping requires Unix style path separators. | |
| 193 | * This method replaces all back-slash characters in a given string with forward-slash ones. | |
| 194 | */ | |
| 195 | private makeUnixStylePath(p: string): string { | |
09f6024fHeniker4 years ago | 196 | const pathArgs = p.split(path.sep); |
4e7a6f0edlebu10 years ago | 197 | return path.posix.join.apply(null, pathArgs); |
876df2a0dlebu10 years ago | 198 | } |
f16656e9frogcjn9 years ago | 199 | } |