microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4cd259621ddfbd348fade892a2f3ee87fd1924c5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMapsCombinator.ts

133lines · 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
5c8365a6Artem Egorov8 years ago4/// <reference path="./../typings/debugger/sourceMapsCombinator.d.ts" />
5
48644043Dmitry Zinovyev9 years ago6import * as fs from "fs";
954784f3Vladimir Kotikov8 years ago7import * as path from "path";
34472878RedMickey5 years ago8import {
9SourceMapConsumer,
10RawSourceMap,
11SourceMapGenerator,
12MappingItem,
13Mapping,
14Position,
15NullableMappedPosition,
16} from "source-map";
ce5e88eeYuri Skorokhodov5 years ago17import * as sourceMapResolve from "source-map-resolve";
48644043Dmitry Zinovyev9 years ago18
478da0f7Serge Svekolnikov8 years ago19const DISK_LETTER_RE: RegExp = /^(?:[a-z]{2,}:\/\/\/)?[a-z]:/i;
9f75364dArtem Egorov8 years ago20
48644043Dmitry Zinovyev9 years ago21export class SourceMapsCombinator {
22public convert(rawBundleSourcemap: RawSourceMap): RawSourceMap {
23// Find user files from bundle files list
34472878RedMickey5 years ago24const consumers: { [key: string]: SourceMapConsumer } = rawBundleSourcemap.sources.reduce(
25(result: { [key: string]: SourceMapConsumer }, file) => {
70bb7c83Vladimir Kotikov8 years ago26// Skip files inside node_modules
27if (file.indexOf("node_modules") >= 0) return result;
48644043Dmitry Zinovyev9 years ago28
70bb7c83Vladimir Kotikov8 years ago29try {
5c8365a6Artem Egorov8 years ago30let consumer: SourceMapConsumer | null = this.getSourceMapConsumerFrom(file);
34472878RedMickey5 years ago31if (consumer) result[file] = consumer;
70bb7c83Vladimir Kotikov8 years ago32} finally {
33return result;
34}
34472878RedMickey5 years ago35},
36{},
37);
48644043Dmitry Zinovyev9 years ago38
39if (Object.keys(consumers).length === 0) {
70bb7c83Vladimir Kotikov8 years ago40// Sourcemaps not found, so return original bundle sourcemap
48644043Dmitry Zinovyev9 years ago41return rawBundleSourcemap;
42}
43
70bb7c83Vladimir Kotikov8 years ago44const generator = new SourceMapGenerator();
45const bundleConsumer = new SourceMapConsumer(rawBundleSourcemap);
48644043Dmitry Zinovyev9 years ago46
47bundleConsumer.eachMapping((item: MappingItem) => {
48if (item.source === null) {
49// Some mappings in react native bundle have no sources
50return;
51}
52
70bb7c83Vladimir Kotikov8 years ago53// Copy mappings
54let mapping: Mapping = {
55generated: { line: item.generatedLine, column: item.generatedColumn },
56original: { line: item.originalLine, column: item.originalColumn },
57source: item.source,
58name: item.name,
59};
60
61if (consumers[item.source]) {
62let jsPosition: Position = { line: item.originalLine, column: item.originalColumn };
34472878RedMickey5 years ago63let tsPosition: NullableMappedPosition = consumers[item.source].originalPositionFor(
64jsPosition,
65);
48644043Dmitry Zinovyev9 years ago66
67if (tsPosition.source === null) {
68// Some positions from react native generated bundle can not translate to TS source positions
69// skip them
70return;
71}
72
954784f3Vladimir Kotikov8 years ago73// Resolve TS source path to absolute because it might be relative to generated JS
74// (this depends on whether "sourceRoot" option is specified in tsconfig.json)
34472878RedMickey5 years ago75if (!tsPosition.source.match(DISK_LETTER_RE)) {
76// This check for Windows tests which were run on MacOs
9f75364dArtem Egorov8 years ago77tsPosition.source = path.resolve(
fbbc4447Sergey Akhalkov8 years ago78<string>rawBundleSourcemap.sourceRoot,
9f75364dArtem Egorov8 years ago79path.dirname(item.source),
34472878RedMickey5 years ago80tsPosition.source,
9f75364dArtem Egorov8 years ago81);
82}
954784f3Vladimir Kotikov8 years ago83
70bb7c83Vladimir Kotikov8 years ago84// Update mapping w/ mapped position values
5c8365a6Artem Egorov8 years ago85mapping.source = tsPosition.source;
86mapping.name = tsPosition.name || mapping.name;
87if (tsPosition.line !== null && tsPosition.column !== null) {
34472878RedMickey5 years ago88mapping.original = { line: tsPosition.line, column: tsPosition.column };
5c8365a6Artem Egorov8 years ago89}
70bb7c83Vladimir Kotikov8 years ago90}
48644043Dmitry Zinovyev9 years ago91
70bb7c83Vladimir Kotikov8 years ago92try {
93generator.addMapping(mapping);
34472878RedMickey5 years ago94} catch (err) {}
48644043Dmitry Zinovyev9 years ago95});
96
97return generator.toJSON();
98}
99
100private getSourceMapConsumerFrom(generatedFile: string): SourceMapConsumer | null {
101let code = fs.readFileSync(generatedFile);
102
103let consumer = this.readSourcemap(generatedFile, code.toString());
104return consumer;
105}
106
107private readSourcemap(file: string, code: string): SourceMapConsumer | null {
34472878RedMickey5 years ago108let result = sourceMapResolve.resolveSync(
109code,
110file,
111readFileSync.bind(null, getDiskLetter(file)),
112);
48644043Dmitry Zinovyev9 years ago113if (result === null) {
114return null;
115}
116return new SourceMapConsumer(result.map);
117}
118}
9f75364dArtem Egorov8 years ago119
120// Hack for source-map-resolve and cutted disk letter
121// https://github.com/lydell/source-map-resolve/issues/9
122function readFileSync(diskLetter: string, filePath: string) {
123if (filePath.match(DISK_LETTER_RE)) {
124return fs.readFileSync(filePath);
125} else {
126return fs.readFileSync(`${diskLetter}${filePath}`);
127}
128}
129
130function getDiskLetter(filePath: string): string {
131const matched = filePath.match(DISK_LETTER_RE);
132return matched ? matched[0] : "";
27710197Vladimir Kotikov8 years ago133}