microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6eeec3c0610e032491c57fe0fc3426016e5177ed

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMapsCombinator.ts

121lines · 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";
5c8365a6Artem Egorov8 years ago8import { SourceMapConsumer, RawSourceMap, SourceMapGenerator, MappingItem, Mapping, Position, NullableMappedPosition } from "source-map";
70bb7c83Vladimir Kotikov8 years ago9import sourceMapResolve = require("source-map-resolve");
48644043Dmitry Zinovyev9 years ago10
9f75364dArtem Egorov8 years ago11const DISK_LETTER_RE: RegExp = /^[a-z]:/i;
12
48644043Dmitry Zinovyev9 years ago13export class SourceMapsCombinator {
14
15public convert(rawBundleSourcemap: RawSourceMap): RawSourceMap {
16
17// Find user files from bundle files list
70bb7c83Vladimir Kotikov8 years ago18const consumers: { [key: string]: SourceMapConsumer } = rawBundleSourcemap.sources
5c8365a6Artem Egorov8 years ago19.reduce((result: { [key: string]: SourceMapConsumer }, file) => {
70bb7c83Vladimir Kotikov8 years ago20// Skip files inside node_modules
21if (file.indexOf("node_modules") >= 0) return result;
48644043Dmitry Zinovyev9 years ago22
70bb7c83Vladimir Kotikov8 years ago23try {
5c8365a6Artem Egorov8 years ago24let consumer: SourceMapConsumer | null = this.getSourceMapConsumerFrom(file);
70bb7c83Vladimir Kotikov8 years ago25if (consumer)
26result[file] = consumer;
27} finally {
28return result;
29}
30}, {});
48644043Dmitry Zinovyev9 years ago31
32if (Object.keys(consumers).length === 0) {
70bb7c83Vladimir Kotikov8 years ago33// Sourcemaps not found, so return original bundle sourcemap
48644043Dmitry Zinovyev9 years ago34return rawBundleSourcemap;
35}
36
70bb7c83Vladimir Kotikov8 years ago37const generator = new SourceMapGenerator();
38const bundleConsumer = new SourceMapConsumer(rawBundleSourcemap);
48644043Dmitry Zinovyev9 years ago39
40bundleConsumer.eachMapping((item: MappingItem) => {
41if (item.source === null) {
42// Some mappings in react native bundle have no sources
43return;
44}
45
70bb7c83Vladimir Kotikov8 years ago46// Copy mappings
47let mapping: Mapping = {
48generated: { line: item.generatedLine, column: item.generatedColumn },
49original: { line: item.originalLine, column: item.originalColumn },
50source: item.source,
51name: item.name,
52};
53
54if (consumers[item.source]) {
55let jsPosition: Position = { line: item.originalLine, column: item.originalColumn };
5c8365a6Artem Egorov8 years ago56let tsPosition: NullableMappedPosition = consumers[item.source].originalPositionFor(jsPosition);
48644043Dmitry Zinovyev9 years ago57
58if (tsPosition.source === null) {
59// Some positions from react native generated bundle can not translate to TS source positions
60// skip them
61return;
62}
63
954784f3Vladimir Kotikov8 years ago64// Resolve TS source path to absolute because it might be relative to generated JS
65// (this depends on whether "sourceRoot" option is specified in tsconfig.json)
9f75364dArtem Egorov8 years ago66if (!tsPosition.source.match(DISK_LETTER_RE)) { // This check for Windows tests which were run on MacOs
67tsPosition.source = path.resolve(
68rawBundleSourcemap.sourceRoot,
69path.dirname(item.source),
70tsPosition.source
71);
72}
954784f3Vladimir Kotikov8 years ago73
70bb7c83Vladimir Kotikov8 years ago74// Update mapping w/ mapped position values
5c8365a6Artem Egorov8 years ago75mapping.source = tsPosition.source;
76mapping.name = tsPosition.name || mapping.name;
77if (tsPosition.line !== null && tsPosition.column !== null) {
78mapping.original = { line: tsPosition.line, column: tsPosition.column};
79}
70bb7c83Vladimir Kotikov8 years ago80}
48644043Dmitry Zinovyev9 years ago81
70bb7c83Vladimir Kotikov8 years ago82try {
83generator.addMapping(mapping);
84} catch (err) {
48644043Dmitry Zinovyev9 years ago85
86}
87});
88
89return generator.toJSON();
90}
91
92private getSourceMapConsumerFrom(generatedFile: string): SourceMapConsumer | null {
93let code = fs.readFileSync(generatedFile);
94
95let consumer = this.readSourcemap(generatedFile, code.toString());
96return consumer;
97}
98
99private readSourcemap(file: string, code: string): SourceMapConsumer | null {
9f75364dArtem Egorov8 years ago100let result = sourceMapResolve.resolveSync(code, file, readFileSync.bind(null, getDiskLetter(file)));
48644043Dmitry Zinovyev9 years ago101if (result === null) {
102return null;
103}
104return new SourceMapConsumer(result.map);
105}
106}
9f75364dArtem Egorov8 years ago107
108// Hack for source-map-resolve and cutted disk letter
109// https://github.com/lydell/source-map-resolve/issues/9
110function readFileSync(diskLetter: string, filePath: string) {
111if (filePath.match(DISK_LETTER_RE)) {
112return fs.readFileSync(filePath);
113} else {
114return fs.readFileSync(`${diskLetter}${filePath}`);
115}
116}
117
118function getDiskLetter(filePath: string): string {
119const matched = filePath.match(DISK_LETTER_RE);
120return matched ? matched[0] : "";
27710197Vladimir Kotikov8 years ago121}