microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70bb7c837ccaaa18600156aa8a698af0d78c3487

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/sourceMapsCombinator.ts

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