Skip to content

Instantly share code, notes, and snippets.

@heyimalex
Created January 13, 2016 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyimalex/c968d7e21e51ce6d887e to your computer and use it in GitHub Desktop.
Save heyimalex/c968d7e21e51ce6d887e to your computer and use it in GitHub Desktop.
Monkey patches webpack/tapable with some debug information
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