microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/sourceMapsCombinator.ts
118lines · modeblame
48644043Dmitry Zinovyev9 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 | | |
| 4 | import * as fs from "fs"; | |
954784f3Vladimir Kotikov8 years ago | 5 | import * as path from "path"; |
48644043Dmitry Zinovyev9 years ago | 6 | import { SourceMapConsumer, RawSourceMap, SourceMapGenerator, MappingItem, Mapping, Position, MappedPosition } from "source-map"; |
70bb7c83Vladimir Kotikov8 years ago | 7 | import sourceMapResolve = require("source-map-resolve"); |
48644043Dmitry Zinovyev9 years ago | 8 | |
9f75364dArtem Egorov8 years ago | 9 | const DISK_LETTER_RE: RegExp = /^[a-z]:/i; |
| 10 | | |
48644043Dmitry Zinovyev9 years ago | 11 | export class SourceMapsCombinator { |
| 12 | | |
| 13 | public convert(rawBundleSourcemap: RawSourceMap): RawSourceMap { | |
| 14 | | |
| 15 | // Find user files from bundle files list | |
70bb7c83Vladimir Kotikov8 years ago | 16 | const consumers: { [key: string]: SourceMapConsumer } = rawBundleSourcemap.sources |
| 17 | .reduce((result, file) => { | |
| 18 | // Skip files inside node_modules | |
| 19 | if (file.indexOf("node_modules") >= 0) return result; | |
48644043Dmitry Zinovyev9 years ago | 20 | |
70bb7c83Vladimir Kotikov8 years ago | 21 | try { |
| 22 | let consumer: SourceMapConsumer = this.getSourceMapConsumerFrom(file); | |
| 23 | if (consumer) | |
| 24 | result[file] = consumer; | |
| 25 | } finally { | |
| 26 | return result; | |
| 27 | } | |
| 28 | }, {}); | |
48644043Dmitry Zinovyev9 years ago | 29 | |
| 30 | if (Object.keys(consumers).length === 0) { | |
70bb7c83Vladimir Kotikov8 years ago | 31 | // Sourcemaps not found, so return original bundle sourcemap |
48644043Dmitry Zinovyev9 years ago | 32 | return rawBundleSourcemap; |
| 33 | } | |
| 34 | | |
70bb7c83Vladimir Kotikov8 years ago | 35 | const generator = new SourceMapGenerator(); |
| 36 | const bundleConsumer = new SourceMapConsumer(rawBundleSourcemap); | |
48644043Dmitry Zinovyev9 years ago | 37 | |
| 38 | bundleConsumer.eachMapping((item: MappingItem) => { | |
| 39 | if (item.source === null) { | |
| 40 | // Some mappings in react native bundle have no sources | |
| 41 | return; | |
| 42 | } | |
| 43 | | |
70bb7c83Vladimir Kotikov8 years ago | 44 | // Copy mappings |
| 45 | let mapping: Mapping = { | |
| 46 | generated: { line: item.generatedLine, column: item.generatedColumn }, | |
| 47 | original: { line: item.originalLine, column: item.originalColumn }, | |
| 48 | source: item.source, | |
| 49 | name: item.name, | |
| 50 | }; | |
| 51 | | |
| 52 | if (consumers[item.source]) { | |
| 53 | let jsPosition: Position = { line: item.originalLine, column: item.originalColumn }; | |
48644043Dmitry Zinovyev9 years ago | 54 | let tsPosition: MappedPosition = consumers[item.source].originalPositionFor(jsPosition); |
| 55 | | |
| 56 | if (tsPosition.source === null) { | |
| 57 | // Some positions from react native generated bundle can not translate to TS source positions | |
| 58 | // skip them | |
| 59 | return; | |
| 60 | } | |
| 61 | | |
954784f3Vladimir Kotikov8 years ago | 62 | // Resolve TS source path to absolute because it might be relative to generated JS |
| 63 | // (this depends on whether "sourceRoot" option is specified in tsconfig.json) | |
9f75364dArtem Egorov8 years ago | 64 | if (!tsPosition.source.match(DISK_LETTER_RE)) { // This check for Windows tests which were run on MacOs |
| 65 | tsPosition.source = path.resolve( | |
| 66 | rawBundleSourcemap.sourceRoot, | |
| 67 | path.dirname(item.source), | |
| 68 | tsPosition.source | |
| 69 | ); | |
| 70 | } | |
954784f3Vladimir Kotikov8 years ago | 71 | |
70bb7c83Vladimir Kotikov8 years ago | 72 | // Update mapping w/ mapped position values |
| 73 | mapping = { | |
| 74 | ...mapping, ...tsPosition, | |
48644043Dmitry Zinovyev9 years ago | 75 | original: { line: tsPosition.line, column: tsPosition.column }, |
70bb7c83Vladimir Kotikov8 years ago | 76 | }; |
| 77 | } | |
48644043Dmitry Zinovyev9 years ago | 78 | |
70bb7c83Vladimir Kotikov8 years ago | 79 | try { |
| 80 | generator.addMapping(mapping); | |
| 81 | } catch (err) { | |
48644043Dmitry Zinovyev9 years ago | 82 | |
| 83 | } | |
| 84 | }); | |
| 85 | | |
| 86 | return generator.toJSON(); | |
| 87 | } | |
| 88 | | |
| 89 | private getSourceMapConsumerFrom(generatedFile: string): SourceMapConsumer | null { | |
| 90 | let code = fs.readFileSync(generatedFile); | |
| 91 | | |
| 92 | let consumer = this.readSourcemap(generatedFile, code.toString()); | |
| 93 | return consumer; | |
| 94 | } | |
| 95 | | |
| 96 | private readSourcemap(file: string, code: string): SourceMapConsumer | null { | |
9f75364dArtem Egorov8 years ago | 97 | let result = sourceMapResolve.resolveSync(code, file, readFileSync.bind(null, getDiskLetter(file))); |
48644043Dmitry Zinovyev9 years ago | 98 | if (result === null) { |
| 99 | return null; | |
| 100 | } | |
| 101 | return new SourceMapConsumer(result.map); | |
| 102 | } | |
| 103 | } | |
9f75364dArtem Egorov8 years ago | 104 | |
| 105 | // Hack for source-map-resolve and cutted disk letter | |
| 106 | // https://github.com/lydell/source-map-resolve/issues/9 | |
| 107 | function readFileSync(diskLetter: string, filePath: string) { | |
| 108 | if (filePath.match(DISK_LETTER_RE)) { | |
| 109 | return fs.readFileSync(filePath); | |
| 110 | } else { | |
| 111 | return fs.readFileSync(`${diskLetter}${filePath}`); | |
| 112 | } | |
| 113 | } | |
| 114 | | |
| 115 | function getDiskLetter(filePath: string): string { | |
| 116 | const matched = filePath.match(DISK_LETTER_RE); | |
| 117 | return matched ? matched[0] : ""; | |
| 118 | } |