microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/sourceMap.ts
191lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as url from "url"; |
| 5 | import * as path from "path"; |
| 6 | import { SourceMapsCombinator } from "./sourceMapsCombinator"; |
| 7 | import { RawSourceMap } from "source-map"; |
| 8 | |
| 9 | const IS_REMOTE = /^[a-zA-z]{2,}:\/\//; // Detection remote sources or specific protocols (like "webpack:///") |
| 10 | |
| 11 | interface ISourceMap extends RawSourceMap { |
| 12 | sections?: ISourceMapSection[]; |
| 13 | } |
| 14 | interface ISourceMapSection { |
| 15 | map: ISourceMap; |
| 16 | offset: { column: number; line: number }; |
| 17 | } |
| 18 | |
| 19 | export interface IStrictUrl extends url.Url { |
| 20 | pathname: string; |
| 21 | href: string; |
| 22 | } |
| 23 | |
| 24 | export class SourceMapUtil { |
| 25 | private static SourceMapURLGlobalRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/gm; |
| 26 | private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m; |
| 27 | private static SourceURLRegex: RegExp = /^\/\/[#@] ?sourceURL=(.+)$/m; |
| 28 | |
| 29 | /** |
| 30 | * Given a script body and URL, this method parses the body and finds the corresponding source map URL. |
| 31 | * If the source map URL is not found in the body in the expected form, null is returned. |
| 32 | */ |
| 33 | public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null { |
| 34 | let result: IStrictUrl | null = null; |
| 35 | |
| 36 | // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" |
| 37 | let sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true" |
| 38 | if (sourceMappingRelativeUrl) { |
| 39 | let sourceMappingUrl = url.parse(sourceMappingRelativeUrl); |
| 40 | sourceMappingUrl.protocol = scriptUrl.protocol; |
| 41 | sourceMappingUrl.host = scriptUrl.host; |
| 42 | // parse() repopulates all the properties of the URL |
| 43 | result = <IStrictUrl>url.parse(url.format(sourceMappingUrl)); |
| 44 | } |
| 45 | |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Updates the contents of a source map file to be VS Code friendly: |
| 51 | * - makes source paths unix style and relative to the sources root path |
| 52 | * - updates the url of the script file |
| 53 | * - deletes the script content from the source map |
| 54 | * |
| 55 | * @parameter sourceMapBody - body of the source map as generated by the RN Packager. |
| 56 | * @parameter scriptPath - path of the script file asssociated with this source map. |
| 57 | * @parameter sourcesRootPath - root path of sources |
| 58 | * |
| 59 | */ |
| 60 | public updateSourceMapFile( |
| 61 | sourceMapBody: string, |
| 62 | scriptPath: string, |
| 63 | sourcesRootPath: string, |
| 64 | packagerRemoteRoot?: string, |
| 65 | packagerLocalRoot?: string, |
| 66 | ): string { |
| 67 | try { |
| 68 | let sourceMap = <ISourceMap>JSON.parse(sourceMapBody); |
| 69 | |
| 70 | if (sourceMap.sections) { |
| 71 | // TODO: there is a need to handle value.map == null, make a fake map |
| 72 | sourceMap.sections = sourceMap.sections.filter(value => { |
| 73 | return value.map != null; |
| 74 | }); |
| 75 | |
| 76 | sourceMap = require("flatten-source-map")(sourceMap); |
| 77 | } |
| 78 | |
| 79 | let sourceMapsCombinator = new SourceMapsCombinator(); |
| 80 | sourceMap = sourceMapsCombinator.convert(sourceMap); |
| 81 | |
| 82 | if (sourceMap.sources) { |
| 83 | sourceMap.sources = sourceMap.sources.map(sourcePath => { |
| 84 | return IS_REMOTE.test(sourcePath) |
| 85 | ? sourcePath |
| 86 | : this.updateSourceMapPath( |
| 87 | sourcePath, |
| 88 | sourcesRootPath, |
| 89 | packagerRemoteRoot, |
| 90 | packagerLocalRoot, |
| 91 | ); |
| 92 | }); |
| 93 | } |
| 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 | |
| 104 | public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string): string { |
| 105 | scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`; |
| 106 | return scriptBody; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Updates source map URLs in the script body. |
| 111 | */ |
| 112 | public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl): string { |
| 113 | // Update the body with the new location of the source map on storage. |
| 114 | return scriptBody.replace( |
| 115 | SourceMapUtil.SourceMapURLRegex, |
| 116 | "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname), |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Removes sourceURL from the script body since RN 0.61 because it breaks sourcemaps. |
| 122 | * Example: //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false -> "" |
| 123 | */ |
| 124 | public removeSourceURL(scriptBody: string): string { |
| 125 | return scriptBody.replace(SourceMapUtil.SourceURLRegex, ""); |
| 126 | } |
| 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 | * |
| 132 | * `//# sourceMappingURL=path/to/source/map` |
| 133 | * |
| 134 | * `//@ sourceMappingURL=path/to/source/map` |
| 135 | * |
| 136 | * Returns the last match if found, null otherwise. |
| 137 | */ |
| 138 | public getSourceMapRelativeUrl(body: string): string | null { |
| 139 | let matchesList = body.match(SourceMapUtil.SourceMapURLGlobalRegex); |
| 140 | // If match is null, the body doesn't contain the source map |
| 141 | if (matchesList) { |
| 142 | const sourceMapMatch = matchesList[matchesList.length - 1].match( |
| 143 | SourceMapUtil.SourceMapURLRegex, |
| 144 | ); |
| 145 | if (sourceMapMatch) { |
| 146 | // On React Native macOS 0.62 sourceMappingUrl for macOS 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 | if ( |
| 150 | sourceMapMatch[2].includes("platform=macos") && |
| 151 | !sourceMapMatch[2].includes("http:") && |
| 152 | sourceMapMatch[2].startsWith("//") |
| 153 | ) { |
| 154 | return "http:" + sourceMapMatch[2]; |
| 155 | } else { |
| 156 | return sourceMapMatch[2]; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Given an absolute source path, this method does two things: |
| 165 | * 1. It changes the path from absolute to be relative to the sourcesRootPath parameter. |
| 166 | * 2. It changes the path separators to Unix style. |
| 167 | */ |
| 168 | private updateSourceMapPath( |
| 169 | sourcePath: string, |
| 170 | sourcesRootPath: string, |
| 171 | packagerRemoteRoot?: string, |
| 172 | packagerLocalRoot?: string, |
| 173 | ) { |
| 174 | if (packagerRemoteRoot && packagerLocalRoot) { |
| 175 | packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot); |
| 176 | packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot); |
| 177 | sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot); |
| 178 | } |
| 179 | let relativeSourcePath = path.relative(sourcesRootPath, sourcePath); |
| 180 | return this.makeUnixStylePath(relativeSourcePath); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Visual Studio Code source mapping requires Unix style path separators. |
| 185 | * This method replaces all back-slash characters in a given string with forward-slash ones. |
| 186 | */ |
| 187 | private makeUnixStylePath(p: string): string { |
| 188 | let pathArgs = p.split(path.sep); |
| 189 | return path.posix.join.apply(null, pathArgs); |
| 190 | } |
| 191 | } |