Skip to content

Use WHATWG's URL to implement all of source-map's URL operations. #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/source-map-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
throw new Error("Unsupported version: " + version);
}

that._sourceLookupCache = new Map();

// Pass `true` below to allow duplicate names and sources. While source maps
// are intended to be compressed and deduplicated, the TypeScript compiler
// sometimes generates source maps with duplicates in them. See Github issue
Expand Down Expand Up @@ -227,18 +229,30 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
* found.
*/
_findSourceIndex(aSource) {
// In the most common usecases, we'll be constantly looking up the index for the same source
// files, so we cache the index lookup to avoid constantly recomputing the full URLs.
const cachedIndex = this._sourceLookupCache.get(aSource);
if (typeof cachedIndex === "number") {
return cachedIndex;
}

// Treat the source as map-relative overall by default.
const sourceAsMapRelative = util.computeSourceURL(null, aSource, this._sourceMapURL);
if (this._absoluteSources.has(sourceAsMapRelative)) {
return this._absoluteSources.indexOf(sourceAsMapRelative);
const index = this._absoluteSources.indexOf(sourceAsMapRelative);
this._sourceLookupCache.set(aSource, index);
return index;
}

// Fall back to treating the source as sourceRoot-relative.
const sourceAsSourceRootRelative = util.computeSourceURL(this.sourceRoot, aSource, this._sourceMapURL);
if (this._absoluteSources.has(sourceAsSourceRootRelative)) {
return this._absoluteSources.indexOf(sourceAsSourceRootRelative);
const index = this._absoluteSources.indexOf(sourceAsSourceRootRelative);
this._sourceLookupCache.set(aSource, index);
return index;
}

// To avoid this cache growing forever, we do not cache lookup misses.
return -1;
}

Expand Down
21 changes: 21 additions & 0 deletions lib/url-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* -*- Mode: js; js-indent-level: 2; -*- */
/*
* Copyright 2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*/
"use strict";

/**
* Browser 'URL' implementations have been found to handle non-standard URL
* schemes poorly, and schemes like
*
* webpack:///src/folder/file.js
*
* are very common in source maps. For the time being we use a JS
* implementation in these contexts instead. See
*
* * https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
* * https://bugs.chromium.org/p/chromium/issues/detail?id=734880
*/
module.exports = require("whatwg-url").URL;
13 changes: 13 additions & 0 deletions lib/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* -*- Mode: js; js-indent-level: 2; -*- */
/*
* Copyright 2011 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*/
"use strict";

// Note: This file is overridden in the 'package.json#browser' field to
// substitute lib/url-browser.js instead.

// Use the URL global for Node 10, and the 'url' module for Node 8.
module.exports = typeof URL === "function" ? URL : require("url").URL;
Loading