microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/sourceMap.ts
42lines · modecode
| 1 | import url = require("url"); |
| 2 | import path = require("path"); |
| 3 | |
| 4 | export class SourceMapUtil { |
| 5 | /** |
| 6 | * Updates source map URLs in the script body. |
| 7 | */ |
| 8 | public updateScriptPaths(scriptBody: string, sourceMappingUrl: url.Url) { |
| 9 | // Update the body with the new location of the source map on storage. |
| 10 | return scriptBody.replace(/^\/\/# sourceMappingURL=(.*)$/m, "//# sourceMappingURL=" + path.basename(sourceMappingUrl.pathname)); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Given a script body and URL, this method parses the body and finds the corresponding source map URL. |
| 15 | * If the source map URL is not found in the body in the expected form, null is returned. |
| 16 | */ |
| 17 | public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): url.Url { |
| 18 | let result: url.Url = null; |
| 19 | |
| 20 | // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" |
| 21 | let sourceMappingRelativeUrl = this.sourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true" |
| 22 | if (sourceMappingRelativeUrl) { |
| 23 | let sourceMappingUrl = url.parse(sourceMappingRelativeUrl); |
| 24 | sourceMappingUrl.protocol = scriptUrl.protocol; |
| 25 | sourceMappingUrl.host = scriptUrl.host; |
| 26 | // parse() repopulates all the properties of the URL |
| 27 | result = url.parse(url.format(sourceMappingUrl)); |
| 28 | } |
| 29 | |
| 30 | return result; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Parses the body of a script searching for a source map URL. |
| 35 | * Returns the first match if found, null otherwise. |
| 36 | */ |
| 37 | private sourceMapRelativeUrl(body: string) { |
| 38 | let match = body.match(/^\/\/# sourceMappingURL=(.*)$/m); |
| 39 | // If match is null, the body doesn't contain the source map |
| 40 | return match ? match[1] : null; |
| 41 | } |
| 42 | } |