microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ci-remove-global-npm-install

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMapsCombinator.ts

134lines · 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
09f6024fHeniker4 years ago4// eslint-disable-next-line @typescript-eslint/triple-slash-reference
5c8365a6Artem Egorov8 years ago5/// <reference path="./../typings/debugger/sourceMapsCombinator.d.ts" />
6
48644043Dmitry Zinovyev9 years ago7import * as fs from "fs";
954784f3Vladimir Kotikov8 years ago8import * as path from "path";
34472878RedMickey5 years ago9import {
10SourceMapConsumer,
11RawSourceMap,
12SourceMapGenerator,
13MappingItem,
14Mapping,
15Position,
16NullableMappedPosition,
17} from "source-map";
ce5e88eeYuri Skorokhodov5 years ago18import * as sourceMapResolve from "source-map-resolve";
48644043Dmitry Zinovyev9 years ago19
09f6024fHeniker4 years ago20const DISK_LETTER_RE = /^(?:[a-z]{2,}:\/{3})?[a-z]:/i;
9f75364dArtem Egorov8 years ago21
48644043Dmitry Zinovyev9 years ago22export class SourceMapsCombinator {
23public convert(rawBundleSourcemap: RawSourceMap): RawSourceMap {
24// Find user files from bundle files list
34472878RedMickey5 years ago25const consumers: { [key: string]: SourceMapConsumer } = rawBundleSourcemap.sources.reduce(
26(result: { [key: string]: SourceMapConsumer }, file) => {
70bb7c83Vladimir Kotikov8 years ago27// Skip files inside node_modules
09f6024fHeniker4 years ago28if (file.includes("node_modules")) return result;
48644043Dmitry Zinovyev9 years ago29
70bb7c83Vladimir Kotikov8 years ago30try {
09f6024fHeniker4 years ago31const consumer: SourceMapConsumer | null = this.getSourceMapConsumerFrom(file);
34472878RedMickey5 years ago32if (consumer) result[file] = consumer;
70bb7c83Vladimir Kotikov8 years ago33} finally {
34return result;
35}
34472878RedMickey5 years ago36},
37{},
38);
48644043Dmitry Zinovyev9 years ago39
40if (Object.keys(consumers).length === 0) {
70bb7c83Vladimir Kotikov8 years ago41// Sourcemaps not found, so return original bundle sourcemap
48644043Dmitry Zinovyev9 years ago42return rawBundleSourcemap;
43}
44
70bb7c83Vladimir Kotikov8 years ago45const generator = new SourceMapGenerator();
46const bundleConsumer = new SourceMapConsumer(rawBundleSourcemap);
48644043Dmitry Zinovyev9 years ago47
48bundleConsumer.eachMapping((item: MappingItem) => {
49if (item.source === null) {
50// Some mappings in react native bundle have no sources
51return;
52}
53
70bb7c83Vladimir Kotikov8 years ago54// Copy mappings
09f6024fHeniker4 years ago55const mapping: Mapping = {
70bb7c83Vladimir Kotikov8 years ago56generated: { line: item.generatedLine, column: item.generatedColumn },
57original: { line: item.originalLine, column: item.originalColumn },
58source: item.source,
59name: item.name,
60};
61
62if (consumers[item.source]) {
09f6024fHeniker4 years ago63const jsPosition: Position = {
64line: item.originalLine,
65column: item.originalColumn,
66};
67const tsPosition: NullableMappedPosition =
68consumers[item.source].originalPositionFor(jsPosition);
48644043Dmitry Zinovyev9 years ago69
70if (tsPosition.source === null) {
71// Some positions from react native generated bundle can not translate to TS source positions
72// skip them
73return;
74}
75
954784f3Vladimir Kotikov8 years ago76// Resolve TS source path to absolute because it might be relative to generated JS
77// (this depends on whether "sourceRoot" option is specified in tsconfig.json)
34472878RedMickey5 years ago78if (!tsPosition.source.match(DISK_LETTER_RE)) {
79// This check for Windows tests which were run on MacOs
9f75364dArtem Egorov8 years ago80tsPosition.source = path.resolve(
fbbc4447Sergey Akhalkov8 years ago81<string>rawBundleSourcemap.sourceRoot,
9f75364dArtem Egorov8 years ago82path.dirname(item.source),
34472878RedMickey5 years ago83tsPosition.source,
9f75364dArtem Egorov8 years ago84);
85}
954784f3Vladimir Kotikov8 years ago86
70bb7c83Vladimir Kotikov8 years ago87// Update mapping w/ mapped position values
5c8365a6Artem Egorov8 years ago88mapping.source = tsPosition.source;
89mapping.name = tsPosition.name || mapping.name;
90if (tsPosition.line !== null && tsPosition.column !== null) {
34472878RedMickey5 years ago91mapping.original = { line: tsPosition.line, column: tsPosition.column };
5c8365a6Artem Egorov8 years ago92}
70bb7c83Vladimir Kotikov8 years ago93}
48644043Dmitry Zinovyev9 years ago94
70bb7c83Vladimir Kotikov8 years ago95try {
96generator.addMapping(mapping);
34472878RedMickey5 years ago97} catch (err) {}
48644043Dmitry Zinovyev9 years ago98});
99
100return generator.toJSON();
101}
102
103private getSourceMapConsumerFrom(generatedFile: string): SourceMapConsumer | null {
09f6024fHeniker4 years ago104const code = fs.readFileSync(generatedFile);
48644043Dmitry Zinovyev9 years ago105
09f6024fHeniker4 years ago106const consumer = this.readSourcemap(generatedFile, code.toString());
48644043Dmitry Zinovyev9 years ago107return consumer;
108}
109
110private readSourcemap(file: string, code: string): SourceMapConsumer | null {
09f6024fHeniker4 years ago111const result = sourceMapResolve.resolveSync(
34472878RedMickey5 years ago112code,
113file,
114readFileSync.bind(null, getDiskLetter(file)),
115);
48644043Dmitry Zinovyev9 years ago116if (result === null) {
117return null;
118}
119return new SourceMapConsumer(result.map);
120}
121}
9f75364dArtem Egorov8 years ago122
123// Hack for source-map-resolve and cutted disk letter
124// https://github.com/lydell/source-map-resolve/issues/9
125function readFileSync(diskLetter: string, filePath: string) {
09f6024fHeniker4 years ago126return filePath.match(DISK_LETTER_RE)
127? fs.readFileSync(filePath)
128: fs.readFileSync(`${diskLetter}${filePath}`);
9f75364dArtem Egorov8 years ago129}
130
131function getDiskLetter(filePath: string): string {
132const matched = filePath.match(DISK_LETTER_RE);
133return matched ? matched[0] : "";
27710197Vladimir Kotikov8 years ago134}