From 3b8942a48f47de111d4dee442292a3f825a9c900 Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Mon, 21 Dec 2015 13:39:24 -0800 Subject: [PATCH] Fix #63 - OS X webpack sourcemap issue --- adapter/sourceMaps/sourceMaps.ts | 10 ++-------- test/webkit/utilities.test.ts | 12 ++++++++++++ webkit/utilities.ts | 10 ++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/adapter/sourceMaps/sourceMaps.ts b/adapter/sourceMaps/sourceMaps.ts index fa6ec28..130a27f 100644 --- a/adapter/sourceMaps/sourceMaps.ts +++ b/adapter/sourceMaps/sourceMaps.ts @@ -260,13 +260,7 @@ class SourceMap { // Overwrite the sourcemap's sourceRoot with the version that's resolved to an absolute path, // so the work above only has to be done once - if (this._absSourceRoot.startsWith('/')) { - // OSX paths - sm.sourceRoot = 'file://' + this._absSourceRoot; - } else { - // Windows paths - sm.sourceRoot = 'file:///' + this._absSourceRoot; - } + sm.sourceRoot = utils.pathToFileURL(this._absSourceRoot); sm.sources = sm.sources.map((sourcePath: string) => { // special-case webpack:/// prefixed sources which is kind of meaningless @@ -339,7 +333,7 @@ class SourceMap { */ public generatedPositionFor(src: string, line: number, column: number, bias = Bias.GREATEST_LOWER_BOUND): SourceMap.Position { if (this._sourcesAreURLs) { - src = 'file:///' + src; + src = utils.pathToFileURL(src); } else if (this._absSourceRoot) { // make input path relative to sourceRoot src = Path.relative(this._absSourceRoot, src); diff --git a/test/webkit/utilities.test.ts b/test/webkit/utilities.test.ts index 4a4e749..8810e12 100644 --- a/test/webkit/utilities.test.ts +++ b/test/webkit/utilities.test.ts @@ -439,4 +439,16 @@ suite('Utilities', () => { assert.equal(Utilities.lstrip('asdf', 'sdf'), 'asdf'); }); }); + + suite('pathToFileURL', () => { + const Utilities = getUtilities(); + + test('converts windows-style paths', () => { + assert.equal(Utilities.pathToFileURL('c:/code/app.js'), 'file:///c:/code/app.js'); + }); + + test('converts unix-style paths', () => { + assert.equal(Utilities.pathToFileURL('/code/app.js'), 'file:///code/app.js'); + }); + }); }); diff --git a/webkit/utilities.ts b/webkit/utilities.ts index 5c7836f..ca3531a 100644 --- a/webkit/utilities.ts +++ b/webkit/utilities.ts @@ -406,3 +406,13 @@ export function lstrip(s: string, lStr: string): string { s.substr(lStr.length) : s; } + +/** + * Convert a local path to a file URL, like + * C:/code/app.js => file:///C:/code/app.js + * /code/app.js => file:///code/app.js + */ +export function pathToFileURL(path: string): string { + return (path.startsWith('/') ? 'file://' : 'file:///') + + path; +} \ No newline at end of file