Skip to content

Commit

Permalink
Reworked _loadSourceMapForScript overriding through prehook.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Minetti committed May 19, 2015
1 parent f0dc3d4 commit 642e801
Showing 1 changed file with 17 additions and 45 deletions.
62 changes: 17 additions & 45 deletions front-end-node/main/MainOverrides.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/*jshint browser:true, nonew:false*/
/*global WebInspector:true*/

// see Blink inspector > ContentSearchUtils.cpp > findMagicComment()
var INLINE_SOURCE_MAP_REGEX = /^(data:application\/json;base64,)(.*)$/;

WebInspector.MainOverrides = function() {
this._unregisterShortcuts();
this._allowToSaveModifiedFiles();
Expand Down Expand Up @@ -50,10 +46,14 @@ WebInspector.MainOverrides.prototype = {
],

_avoidSourceMapFetchWhenInline: function() {
// Base code from front-end/bindings/CompilerScriptMapping.js
WebInspector.CompilerScriptMapping.prototype.orig_loadSourceMapForScript =
WebInspector.CompilerScriptMapping.prototype._loadSourceMapForScript;

WebInspector.CompilerScriptMapping.prototype._loadSourceMapForScript = function(script, callback) {
var inspectedURL = script.target().resourceTreeModel.inspectedPageURL();
var scriptURL = WebInspector.ParsedURL.completeURL(inspectedURL, script.sourceURL);
var scriptURL = WebInspector.ParsedURL.completeURL(
script.target().resourceTreeModel.inspectedPageURL(),
script.sourceURL
);
if (!scriptURL) {
callback(null);
return;
Expand All @@ -68,46 +68,18 @@ WebInspector.MainOverrides.prototype = {
return;
}

// Extracting SourceMap object from inline sourceMapURL
var virtualSourceMapURL = null;
var payload = null;
if (sourceMapURL.match(INLINE_SOURCE_MAP_REGEX) != null) {
virtualSourceMapURL = script.sourceURL + '.map';
payload = JSON.parse(window.atob(sourceMapURL.substr(29)));
this._sourceMapForSourceMapURL[virtualSourceMapURL] =
new WebInspector.SourceMap(virtualSourceMapURL, payload);
sourceMapURL = virtualSourceMapURL;
script.sourceMapURL = virtualSourceMapURL;
}

var sourceMap = this._sourceMapForSourceMapURL[sourceMapURL];
if (sourceMap) {
callback(sourceMap);
return;
}

var pendingCallbacks = this._pendingSourceMapLoadingCallbacks[sourceMapURL];
if (pendingCallbacks) {
pendingCallbacks.push(callback);
return;
}
var INLINE_SOURCE_MAP_REGEX = /^(data:application\/json;base64,)(.*)$/;
var matched = INLINE_SOURCE_MAP_REGEX.exec(sourceMapURL);
if (!matched) return;

pendingCallbacks = [callback];
this._pendingSourceMapLoadingCallbacks[sourceMapURL] = pendingCallbacks;

WebInspector.SourceMap.load(sourceMapURL, scriptURL, sourceMapLoaded.bind(this));
// Extracting SourceMap object from inline sourceMapURL
script.sourceMapURL = script.sourceURL + '.map';
var payload = JSON.parse(window.atob(matched[2]));

this._sourceMapForSourceMapURL[script.sourceMapURL] =
new WebInspector.SourceMap(script.sourceMapURL, payload);

function sourceMapLoaded(sourceMap) {
var url = (sourceMapURL);
var callbacks = this._pendingSourceMapLoadingCallbacks[url];
delete this._pendingSourceMapLoadingCallbacks[url];
if (!callbacks)
return;
if (sourceMap)
this._sourceMapForSourceMapURL[url] = sourceMap;
for (var i = 0; i < callbacks.length; ++i)
callbacks[i](sourceMap);
}
this.orig_loadSourceMapForScript(script, callback);
};
}
};
Expand Down

0 comments on commit 642e801

Please sign in to comment.