From cfe89fc18079d642e7516b46d2d8dbdf1bf7073e Mon Sep 17 00:00:00 2001 From: llgcode Date: Thu, 5 Jan 2017 15:42:44 +0100 Subject: [PATCH] add path mapping --- src/debugAdapterInterfaces.d.ts | 1 + src/transformers/urlPathTransformer.ts | 33 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/debugAdapterInterfaces.d.ts b/src/debugAdapterInterfaces.d.ts index 3674863b7..597af1a36 100644 --- a/src/debugAdapterInterfaces.d.ts +++ b/src/debugAdapterInterfaces.d.ts @@ -15,6 +15,7 @@ export type ISourceMapPathOverrides = { [pattern: string]: string }; */ export interface ICommonRequestArgs { webRoot?: string; + pathMapping?: {[url: string]: string}; outDir?: string; outFiles?: string[]; sourceMaps?: boolean; diff --git a/src/transformers/urlPathTransformer.ts b/src/transformers/urlPathTransformer.ts index 31efd02fc..d580311fb 100644 --- a/src/transformers/urlPathTransformer.ts +++ b/src/transformers/urlPathTransformer.ts @@ -11,17 +11,20 @@ import * as ChromeUtils from '../chrome/chromeUtils'; import {ChromeDebugAdapter} from '../chrome/chromeDebugAdapter'; import * as path from 'path'; +import * as url from 'url'; /** * Converts a local path from Code to a path on the target. */ export class UrlPathTransformer extends BasePathTransformer { private _webRoot: string; + private _pathMapping: {[url: string]: string}; private _clientPathToTargetUrl = new Map(); private _targetUrlToClientPath = new Map(); public launch(args: ILaunchRequestArgs): Promise { this._webRoot = args.webRoot; + this._pathMapping = args.pathMapping; return super.launch(args); } @@ -61,8 +64,36 @@ export class UrlPathTransformer extends BasePathTransformer { } public scriptParsed(scriptUrl: string): string { - const clientPath = ChromeUtils.targetUrlToClientPath(this._webRoot, scriptUrl); + let clientPath: string; + const parsedUrl = url.parse(scriptUrl); + const origin = `${parsedUrl.protocol}//${parsedUrl.host}`; + + let p = parsedUrl.pathname; + while (p) { + let localPath = this._pathMapping[origin + p]; + if (localPath) { + clientPath = path.join(localPath, parsedUrl.pathname.substring(p.length)); + break; + } + localPath = this._pathMapping[p]; + if (localPath) { + clientPath = path.join(localPath, parsedUrl.pathname.substring(p.length)); + break; + } + if (p === "/") { + break; + } + p = path.dirname(p); + if (p !== "/") { + // We need to differianciate folder and files by having a leading '/' except for root. + p = p + "/"; + } + } + if (!clientPath) { + // Deprecated webRoot + clientPath = ChromeUtils.targetUrlToClientPath(this._webRoot, scriptUrl); + } if (!clientPath) { // It's expected that eval scripts (eval://) won't be resolved if (!scriptUrl.startsWith(ChromeDebugAdapter.PLACEHOLDER_URL_PROTOCOL)) {