Created
January 13, 2016 23:35
-
-
Save heyimalex/c968d7e21e51ce6d887e to your computer and use it in GitHub Desktop.
Monkey patches webpack/tapable with some debug information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Tapable = require("tapable"); | |
Tapable.prototype.plugin = function plugin(name, fn) { | |
if(Array.isArray(name)) { | |
name.forEach(function(name) { | |
this.plugin(name, fn); | |
}, this); | |
return; | |
} | |
register(name, this); | |
if(!this._plugins[name]) this._plugins[name] = [fn]; | |
else this._plugins[name].push(fn); | |
}; | |
Tapable.prototype.applyPlugins = wrapSync(Tapable.prototype.applyPlugins); | |
Tapable.prototype.applyPluginsWaterfall = wrapSync(Tapable.prototype.applyPluginsWaterfall); | |
Tapable.prototype.applyPluginsBailResult = wrapSync(Tapable.prototype.applyPluginsBailResult); | |
Tapable.prototype.applyPluginsAsyncSeries = Tapable.prototype.applyPluginsAsync = wrapAsync(Tapable.prototype.applyPluginsAsyncSeries); | |
Tapable.prototype.applyPluginsAsyncSeriesBailResult = wrapAsync(Tapable.prototype.applyPluginsAsyncSeriesBailResult); | |
Tapable.prototype.applyPluginsAsyncWaterfall = wrapAsync(Tapable.prototype.applyPluginsAsyncWaterfall); | |
Tapable.prototype.applyPluginsParallel = wrapAsync(Tapable.prototype.applyPluginsParallel); | |
Tapable.prototype.applyPluginsParallelBailResult = wrapAsync(Tapable.prototype.applyPluginsParallelBailResult); | |
var stack = []; | |
function taplog() { | |
var prefix = ' '.repeat(stack.length); | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(prefix); | |
console.log.apply(console, args); | |
} | |
function pushstack(name) { | |
taplog("entering '" + name + "'"); | |
stack.push(name); | |
if (name === 'seal') { | |
debugger; | |
} | |
} | |
function popstack() { | |
var name = stack.pop(); | |
taplog("leaving '" + name + "'"); | |
} | |
function wrapSync(fn) { | |
return function wrappedSync(name) { | |
pushstack(name); | |
var args = Array.prototype.slice.call(arguments); | |
var rv = fn.apply(this, args); | |
popstack(); | |
return rv; | |
} | |
} | |
function wrapAsync(fn) { | |
return function wrappedAsync(name) { | |
pushstack(name); | |
var args = Array.prototype.slice.call(arguments); | |
var originalCallback = args[args.length - 1]; | |
args[args.length - 1] = function wrappedCallback() { | |
popstack(); | |
var cbargs = Array.prototype.slice.call(arguments); | |
originalCallback.apply(this, cbargs); | |
} | |
fn.apply(this, args); | |
} | |
} | |
function register(name, target) { | |
var stack = getStack(); | |
var frameIndex = 2; | |
var thisFilename = stack[0].getFileName(); | |
while (stack[frameIndex].getFileName() == thisFilename) { | |
frameIndex += 1; | |
} | |
taplog( | |
"registering", "'" + name + "'", | |
"on", target.constructor.name, | |
"from", stack[frameIndex].getFileName() | |
); | |
} | |
function getStack() { | |
var orig = Error.prepareStackTrace; | |
Error.prepareStackTrace = function(_, stack) { | |
return stack; | |
}; | |
var err = new Error; | |
Error.captureStackTrace(err, arguments.callee); | |
var stack = err.stack; | |
Error.prepareStackTrace = orig; | |
return stack; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment