Skip to content

Commit 02f3d6b

Browse files
authored
Use WHATWG's URL to implement all of source-map's URL operations. (#371)
* Use WHATWG's URL to implement all of source-map's URL operations. * Preserve string-concat sources behavior for absolute-path sources. * Only use whatwg-url in browser builds. * Optimize perf to avoid new URL where not strictly needed. * Cache url -> source index lookups in the consumer. * Simplify url cache size limiting.
1 parent 23104f3 commit 02f3d6b

7 files changed

+387
-427
lines changed

lib/source-map-consumer.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
194194
throw new Error("Unsupported version: " + version);
195195
}
196196

197+
that._sourceLookupCache = new Map();
198+
197199
// Pass `true` below to allow duplicate names and sources. While source maps
198200
// are intended to be compressed and deduplicated, the TypeScript compiler
199201
// sometimes generates source maps with duplicates in them. See Github issue
@@ -227,18 +229,30 @@ class BasicSourceMapConsumer extends SourceMapConsumer {
227229
* found.
228230
*/
229231
_findSourceIndex(aSource) {
232+
// In the most common usecases, we'll be constantly looking up the index for the same source
233+
// files, so we cache the index lookup to avoid constantly recomputing the full URLs.
234+
const cachedIndex = this._sourceLookupCache.get(aSource);
235+
if (typeof cachedIndex === "number") {
236+
return cachedIndex;
237+
}
238+
230239
// Treat the source as map-relative overall by default.
231240
const sourceAsMapRelative = util.computeSourceURL(null, aSource, this._sourceMapURL);
232241
if (this._absoluteSources.has(sourceAsMapRelative)) {
233-
return this._absoluteSources.indexOf(sourceAsMapRelative);
242+
const index = this._absoluteSources.indexOf(sourceAsMapRelative);
243+
this._sourceLookupCache.set(aSource, index);
244+
return index;
234245
}
235246

236247
// Fall back to treating the source as sourceRoot-relative.
237248
const sourceAsSourceRootRelative = util.computeSourceURL(this.sourceRoot, aSource, this._sourceMapURL);
238249
if (this._absoluteSources.has(sourceAsSourceRootRelative)) {
239-
return this._absoluteSources.indexOf(sourceAsSourceRootRelative);
250+
const index = this._absoluteSources.indexOf(sourceAsSourceRootRelative);
251+
this._sourceLookupCache.set(aSource, index);
252+
return index;
240253
}
241254

255+
// To avoid this cache growing forever, we do not cache lookup misses.
242256
return -1;
243257
}
244258

lib/url-browser.js

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

lib/url.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* -*- Mode: js; js-indent-level: 2; -*- */
2+
/*
3+
* Copyright 2011 Mozilla Foundation and contributors
4+
* Licensed under the New BSD license. See LICENSE or:
5+
* http://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
"use strict";
8+
9+
// Note: This file is overridden in the 'package.json#browser' field to
10+
// substitute lib/url-browser.js instead.
11+
12+
// Use the URL global for Node 10, and the 'url' module for Node 8.
13+
module.exports = typeof URL === "function" ? URL : require("url").URL;

0 commit comments

Comments
 (0)