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