microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
66b52ec91bf71c509ff18c0c35c78a126d7f8dc0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMapsCombinator.ts

99lines · 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 * as path from "path";
6import { SourceMapConsumer, RawSourceMap, SourceMapGenerator, MappingItem, Mapping, Position, MappedPosition } from "source-map";
7import sourceMapResolve = require("source-map-resolve");
8
9export class SourceMapsCombinator {
10
11 public convert(rawBundleSourcemap: RawSourceMap): RawSourceMap {
12
13 // Find user files from bundle files list
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;
18
19 try {
20 let consumer: SourceMapConsumer = this.getSourceMapConsumerFrom(file);
21 if (consumer)
22 result[file] = consumer;
23 } finally {
24 return result;
25 }
26 }, {});
27
28 if (Object.keys(consumers).length === 0) {
29 // Sourcemaps not found, so return original bundle sourcemap
30 return rawBundleSourcemap;
31 }
32
33 const generator = new SourceMapGenerator();
34 const bundleConsumer = new SourceMapConsumer(rawBundleSourcemap);
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
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 };
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
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
68 // Update mapping w/ mapped position values
69 mapping = {
70 ...mapping, ...tsPosition,
71 original: { line: tsPosition.line, column: tsPosition.column },
72 };
73 }
74
75 try {
76 generator.addMapping(mapping);
77 } catch (err) {
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}