microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix-mocha-this-typing

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/sourceMap.ts

212lines · modeblame

9f036952Nisheet Jain10 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
ce5e88eeYuri Skorokhodov5 years ago4import * as url from "url";
5import * as path from "path";
70bb7c83Vladimir Kotikov8 years ago6import { RawSourceMap } from "source-map";
09f6024fHeniker4 years ago7import { SourceMapsCombinator } from "./sourceMapsCombinator";
876df2a0dlebu10 years ago8
f68a3466EmmaYuan10152 months ago9const IS_REMOTE = /^[A-Za-z]{2,}:\/\//; // Detection remote sources or specific protocols (like "webpack:///")
66b52ec9Artem Egorov8 years ago10
70bb7c83Vladimir Kotikov8 years ago11interface ISourceMap extends RawSourceMap {
f16656e9frogcjn9 years ago12sections?: ISourceMapSection[];
13}
14interface ISourceMapSection {
57fc50d2EmmaYuan10151 months ago15map: ISourceMap | null;
34472878RedMickey5 years ago16offset: { column: number; line: number };
876df2a0dlebu10 years ago17}
18
5c8365a6Artem Egorov8 years ago19export interface IStrictUrl extends url.Url {
20pathname: string;
21href: string;
22}
23
876df2a0dlebu10 years ago24export class SourceMapUtil {
09f6024fHeniker4 years ago25private static SourceMapURLGlobalRegex: RegExp =
716c1b85RedMickey4 years ago26/\/\/(#|@) sourceMappingURL=((?!data:)[^ ]+?)\s*$/gm;
27private static SourceMapURLRegex: RegExp = /\/\/(#|@) sourceMappingURL=((?!data:)[^ ]+?)\s*$/m;
32558f9eYuri Skorokhodov6 years ago28private static SourceURLRegex: RegExp = /^\/\/[#@] ?sourceURL=(.+)$/m;
4e7a6f0edlebu10 years ago29
876df2a0dlebu10 years ago30/**
31* Given a script body and URL, this method parses the body and finds the corresponding source map URL.
32* If the source map URL is not found in the body in the expected form, null is returned.
33*/
5c8365a6Artem Egorov8 years ago34public getSourceMapURL(scriptUrl: url.Url, scriptBody: string): IStrictUrl | null {
35let result: IStrictUrl | null = null;
876df2a0dlebu10 years ago36
37// scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
09f6024fHeniker4 years ago38const sourceMappingRelativeUrl = this.getSourceMapRelativeUrl(scriptBody); // sourceMappingRelativeUrl = "/index.ios.map?platform=ios&dev=true"
876df2a0dlebu10 years ago39if (sourceMappingRelativeUrl) {
09f6024fHeniker4 years ago40const sourceMappingUrl = url.parse(sourceMappingRelativeUrl);
876df2a0dlebu10 years ago41sourceMappingUrl.protocol = scriptUrl.protocol;
42sourceMappingUrl.host = scriptUrl.host;
43// parse() repopulates all the properties of the URL
5c8365a6Artem Egorov8 years ago44result = <IStrictUrl>url.parse(url.format(sourceMappingUrl));
876df2a0dlebu10 years ago45}
46
47return result;
48}
49
50/**
51* Updates the contents of a source map file to be VS Code friendly:
52* - makes source paths unix style and relative to the sources root path
53* - updates the url of the script file
54* - deletes the script content from the source map
55*
56* @parameter sourceMapBody - body of the source map as generated by the RN Packager.
57* @parameter scriptPath - path of the script file asssociated with this source map.
58* @parameter sourcesRootPath - root path of sources
59*
60*/
34472878RedMickey5 years ago61public updateSourceMapFile(
62sourceMapBody: string,
63scriptPath: string,
64sourcesRootPath: string,
65packagerRemoteRoot?: string,
66packagerLocalRoot?: string,
67): string {
876df2a0dlebu10 years ago68try {
69let sourceMap = <ISourceMap>JSON.parse(sourceMapBody);
f16656e9frogcjn9 years ago70
71if (sourceMap.sections) {
57fc50d2EmmaYuan10151 months ago72// Preserve indexed sourcemap offsets even when Metro emits a null section map.
73sourceMap.sections = sourceMap.sections.map(section => ({
74...section,
75map: section.map ?? SourceMapUtil.createEmptySourceMap(),
76}));
f16656e9frogcjn9 years ago77
09f6024fHeniker4 years ago78// eslint-disable-next-line @typescript-eslint/no-var-requires
f16656e9frogcjn9 years ago79sourceMap = require("flatten-source-map")(sourceMap);
80}
81
09f6024fHeniker4 years ago82const sourceMapsCombinator = new SourceMapsCombinator();
48644043Dmitry Zinovyev9 years ago83sourceMap = sourceMapsCombinator.convert(sourceMap);
84
f16656e9frogcjn9 years ago85if (sourceMap.sources) {
09f6024fHeniker4 years ago86sourceMap.sources = sourceMap.sources.map(sourcePath =>
87IS_REMOTE.test(sourcePath)
34472878RedMickey5 years ago88? sourcePath
89: this.updateSourceMapPath(
90sourcePath,
91sourcesRootPath,
92packagerRemoteRoot,
93packagerLocalRoot,
09f6024fHeniker4 years ago94),
95);
f16656e9frogcjn9 years ago96}
876df2a0dlebu10 years ago97
98delete sourceMap.sourcesContent;
99sourceMap.sourceRoot = "";
100sourceMap.file = scriptPath;
101return JSON.stringify(sourceMap);
102} catch (exception) {
103return sourceMapBody;
104}
105}
106
34472878RedMickey5 years ago107public appendSourceMapPaths(scriptBody: string, sourceMappingUrl: string): string {
979d7bfemax-mironov8 years ago108scriptBody += `//# sourceMappingURL=${sourceMappingUrl}`;
109return scriptBody;
110}
111
3736c251dlebu10 years ago112/**
113* Updates source map URLs in the script body.
114*/
34472878RedMickey5 years ago115public updateScriptPaths(scriptBody: string, sourceMappingUrl: IStrictUrl): string {
716c1b85RedMickey4 years ago116const sourceMapMatch = this.searchSourceMapURL(scriptBody);
117if (sourceMapMatch) {
118// Update the body with the new location of the source map on storage.
119return scriptBody.replace(
120sourceMapMatch[0],
121`//# sourceMappingURL=${path.basename(sourceMappingUrl.pathname)}`,
122);
123}
124return scriptBody;
3736c251dlebu10 years ago125}
126
2f5c780bYuri Skorokhodov6 years ago127/**
128* Removes sourceURL from the script body since RN 0.61 because it breaks sourcemaps.
129* Example: //# sourceURL=http://localhost:8081/index.bundle?platform=android&dev=true&minify=false -> ""
130*/
34472878RedMickey5 years ago131public removeSourceURL(scriptBody: string): string {
2f5c780bYuri Skorokhodov6 years ago132return scriptBody.replace(SourceMapUtil.SourceURLRegex, "");
133}
134
4cf8fdf4Yuri Skorokhodov6 years ago135/**
136* Parses the body of a script searching for a source map URL.
137* It supports the following source map url styles:
138*
139* `//# sourceMappingURL=path/to/source/map`
140*
141* `//@ sourceMappingURL=path/to/source/map`
142*
143* Returns the last match if found, null otherwise.
144*/
34472878RedMickey5 years ago145public getSourceMapRelativeUrl(body: string): string | null {
716c1b85RedMickey4 years ago146const sourceMapMatch = this.searchSourceMapURL(body);
4cf8fdf4Yuri Skorokhodov6 years ago147// If match is null, the body doesn't contain the source map
716c1b85RedMickey4 years ago148if (sourceMapMatch) {
149// On React Native macOS 0.62 and RN Windows 0.65 sourceMappingUrl looks like:
150// # sourceMappingURL=//localhost:8081/index.map?platform=macos&dev=true&minify=false
151// Add 'http:' protocol to avoid errors in further processing
152const el = sourceMapMatch[2];
153const macOsOrWin =
154(el.includes("platform=macos") || el.includes("platform=window")) &&
155el.startsWith("//") &&
156!el.includes("http:");
157
158return macOsOrWin ? `http:${el}` : el;
4cf8fdf4Yuri Skorokhodov6 years ago159}
716c1b85RedMickey4 years ago160return null;
161}
09f6024fHeniker4 years ago162
716c1b85RedMickey4 years ago163private searchSourceMapURL(str: string): RegExpMatchArray | null {
164const matchesList = str
165.match(SourceMapUtil.SourceMapURLGlobalRegex)
166?.filter(s => !s.includes("\\n"));
167if (matchesList && matchesList.length) {
168return matchesList[matchesList.length - 1].match(SourceMapUtil.SourceMapURLRegex);
169}
09f6024fHeniker4 years ago170
716c1b85RedMickey4 years ago171return null;
4cf8fdf4Yuri Skorokhodov6 years ago172}
173
876df2a0dlebu10 years ago174/**
175* Given an absolute source path, this method does two things:
176* 1. It changes the path from absolute to be relative to the sourcesRootPath parameter.
177* 2. It changes the path separators to Unix style.
178*/
34472878RedMickey5 years ago179private updateSourceMapPath(
180sourcePath: string,
181sourcesRootPath: string,
182packagerRemoteRoot?: string,
183packagerLocalRoot?: string,
184) {
6eeec3c0Serge Svekolnikov8 years ago185if (packagerRemoteRoot && packagerLocalRoot) {
186packagerRemoteRoot = this.makeUnixStylePath(packagerRemoteRoot);
187packagerLocalRoot = this.makeUnixStylePath(packagerLocalRoot);
188sourcePath = sourcePath.replace(packagerRemoteRoot, packagerLocalRoot);
189}
09f6024fHeniker4 years ago190const relativeSourcePath = path.relative(sourcesRootPath, sourcePath);
876df2a0dlebu10 years ago191return this.makeUnixStylePath(relativeSourcePath);
192}
193
194/**
195* Visual Studio Code source mapping requires Unix style path separators.
196* This method replaces all back-slash characters in a given string with forward-slash ones.
197*/
198private makeUnixStylePath(p: string): string {
09f6024fHeniker4 years ago199const pathArgs = p.split(path.sep);
4e7a6f0edlebu10 years ago200return path.posix.join.apply(null, pathArgs);
876df2a0dlebu10 years ago201}
57fc50d2EmmaYuan10151 months ago202
203private static createEmptySourceMap(): ISourceMap {
204return {
205version: 3,
206sources: [],
207names: [],
208mappings: "",
209file: "",
210};
211}
f16656e9frogcjn9 years ago212}