microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/sourceMap.ts
143lines · 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 url = require("url"); |
| 5 | import path = require("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 SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:).+?)\s*$/m; |
| 26 | |
| 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 | */ |
| 31 | public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null { |
| 32 | let result: IStrictUrl | null = null; |
| 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 |
| 41 | result = <IStrictUrl>url.parse(url.format(sourceMappingUrl)); |
| 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, packagerRemoteRoot?: string, packagerLocalRoot?: string): string { |
| 59 | try { |
| 60 | let sourceMap = <ISourceMap>JSON.parse(sourceMapBody); |
| 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 | |
| 72 | let sourceMapsCombinator = new SourceMapsCombinator(); |
| 73 | sourceMap = sourceMapsCombinator.convert(sourceMap); |
| 74 | |
| 75 | if (sourceMap.sources) { |
| 76 | sourceMap.sources = sourceMap.sources.map(sourcePath => { |
| 77 | return IS_REMOTE.test(sourcePath) ? sourcePath : this.updateSourceMapPath(sourcePath, sourcesRootPath, packagerRemoteRoot, packagerLocalRoot); |
| 78 | }); |
| 79 | } |
| 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 | |
| 90 | public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string) { |
| 91 | scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`; |
| 92 | return scriptBody; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Updates source map URLs in the script body. |
| 97 | */ |
| 98 | public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl) { |
| 99 | // Update the body with the new location of the source map on storage. |
| 100 | return scriptBody.replace(SourceMapUtil.SourceMapURLRegex, |
| 101 | "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 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 | */ |
| 109 | private updateSourceMapPath(sourcePath: string, sourcesRootPath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) { |
| 110 | if (packagerRemoteRoot && packagerLocalRoot) { |
| 111 | packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot); |
| 112 | packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot); |
| 113 | sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot); |
| 114 | } |
| 115 | let relativeSourcePath = path.relative(sourcesRootPath, sourcePath); |
| 116 | return 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 | */ |
| 123 | private makeUnixStylePath(p: string): string { |
| 124 | let pathArgs = p.split(path.sep); |
| 125 | return path.posix.join.apply(null, pathArgs); |
| 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 | * //# sourceMappingURL=path/to/source/map |
| 132 | * //@ sourceMappingURL=path/to/source/map |
| 133 | * |
| 134 | * Returns the first match if found, null otherwise. |
| 135 | */ |
| 136 | private getSourceMapRelativeUrl(body: string) { |
| 137 | let match = body.match(SourceMapUtil.SourceMapURLRegex); |
| 138 | // If match is null, the body doesn't contain the source map |
| 139 | return match ? match[2] : null; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | } |
| 144 | |