microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMapsCombinator.ts

99lines · modeblame

48644043Dmitry Zinovyev9 years ago1// 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";
954784f3Vladimir Kotikov8 years ago5import * as path from "path";
48644043Dmitry Zinovyev9 years ago6import { SourceMapConsumer, RawSourceMap, SourceMapGenerator, MappingItem, Mapping, Position, MappedPosition } from "source-map";
70bb7c83Vladimir Kotikov9 years ago7import sourceMapResolve = require("source-map-resolve");
48644043Dmitry Zinovyev9 years ago8
9export class SourceMapsCombinator {
10
11public convert(rawBundleSourcemap: RawSourceMap): RawSourceMap {
12
13// Find user files from bundle files list
70bb7c83Vladimir Kotikov9 years ago14const consumers: { [key: string]: SourceMapConsumer } = rawBundleSourcemap.sources
15.reduce((result, file) => {
16// Skip files inside node_modules
17if (file.indexOf("node_modules") >= 0) return result;
48644043Dmitry Zinovyev9 years ago18
70bb7c83Vladimir Kotikov9 years ago19try {
20let consumer: SourceMapConsumer = this.getSourceMapConsumerFrom(file);
21if (consumer)
22result[file] = consumer;
23} finally {
24return result;
25}
26}, {});
48644043Dmitry Zinovyev9 years ago27
28if (Object.keys(consumers).length === 0) {
70bb7c83Vladimir Kotikov9 years ago29// Sourcemaps not found, so return original bundle sourcemap
48644043Dmitry Zinovyev9 years ago30return rawBundleSourcemap;
31}
32
70bb7c83Vladimir Kotikov9 years ago33const generator = new SourceMapGenerator();
34const bundleConsumer = new SourceMapConsumer(rawBundleSourcemap);
48644043Dmitry Zinovyev9 years ago35
36bundleConsumer.eachMapping((item: MappingItem) => {
37if (item.source === null) {
38// Some mappings in react native bundle have no sources
39return;
40}
41
70bb7c83Vladimir Kotikov9 years ago42// Copy mappings
43let mapping: Mapping = {
44generated: { line: item.generatedLine, column: item.generatedColumn },
45original: { line: item.originalLine, column: item.originalColumn },
46source: item.source,
47name: item.name,
48};
49
50if (consumers[item.source]) {
51let jsPosition: Position = { line: item.originalLine, column: item.originalColumn };
48644043Dmitry Zinovyev9 years ago52let tsPosition: MappedPosition = consumers[item.source].originalPositionFor(jsPosition);
53
54if (tsPosition.source === null) {
55// Some positions from react native generated bundle can not translate to TS source positions
56// skip them
57return;
58}
59
954784f3Vladimir Kotikov8 years ago60// 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)
62tsPosition.source = path.resolve(
63rawBundleSourcemap.sourceRoot,
64path.dirname(item.source),
65tsPosition.source
66);
67
70bb7c83Vladimir Kotikov9 years ago68// Update mapping w/ mapped position values
69mapping = {
70...mapping, ...tsPosition,
48644043Dmitry Zinovyev9 years ago71original: { line: tsPosition.line, column: tsPosition.column },
70bb7c83Vladimir Kotikov9 years ago72};
73}
48644043Dmitry Zinovyev9 years ago74
70bb7c83Vladimir Kotikov9 years ago75try {
76generator.addMapping(mapping);
77} catch (err) {
48644043Dmitry Zinovyev9 years ago78
79}
80});
81
82return generator.toJSON();
83}
84
85private getSourceMapConsumerFrom(generatedFile: string): SourceMapConsumer | null {
86let code = fs.readFileSync(generatedFile);
87
88let consumer = this.readSourcemap(generatedFile, code.toString());
89return consumer;
90}
91
92private readSourcemap(file: string, code: string): SourceMapConsumer | null {
93let result = sourceMapResolve.resolveSync(code, file, fs.readFileSync);
94if (result === null) {
95return null;
96}
97return new SourceMapConsumer(result.map);
98}
99}