Skip to content

Instantly share code, notes, and snippets.

@threepointone
Created November 28, 2019 12:45
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save threepointone/abd21d8c09d60b3d0a81f07571293404 to your computer and use it in GitHub Desktop.
Save threepointone/abd21d8c09d60b3d0a81f07571293404 to your computer and use it in GitHub Desktop.
const bypass = [
// function names to avoid logging
];
const collapsed = [
// function names to groupCollapsed
];
module.exports = function(babel) {
const { types: t } = babel;
const wrapFunctionBody = babel.template(`{
try{
console.group(NAME)
BODY
}
finally {
console.groupEnd();
}
}`);
const wrapFunctionBodyCollapsed = babel.template(`{
try{
console.groupCollapsed(NAME)
BODY
}
finally {
console.groupEnd();
}
}`);
return {
visitor: {
FunctionDeclaration(path) {
if (path.node.id && path.node.id.name) {
if (bypass.includes(path.node.id.name)) {
return;
}
if (collapsed.includes(path.node.id.name)) {
path.get("body").replaceWith(
wrapFunctionBodyCollapsed({
BODY: path.node.body.body,
NAME: t.stringLiteral(path.node.id.name)
})
);
return;
}
path.get("body").replaceWith(
wrapFunctionBody({
BODY: path.node.body.body,
NAME: t.stringLiteral(path.node.id.name)
})
);
}
}
}
};
};
@threepointone
Copy link
Author

threepointone commented Nov 28, 2019

ref: https://twitter.com/threepointone/status/1200032119805947905

This is how I use it with the React codebase

  • save this to a file somewhere (I put it in ./scripts/babel)
  • add the path to babel.config.js ('./scripts/babel/console-function-names')
  • run yarn build

This will generate a ./build folder with assets (npm, umd, etc). Use them in your app!

@threepointone
Copy link
Author

My lists of bypass/collapsed for React.

const bypass = [
  // scheduler
  'peek',
  // "advanceTimers",
  'markTaskYield',
  'markSchedulerSuspended',
  // "workLoop",
  'markTaskRun',
  'markSchedulerUnsuspended',
  'markTaskStart',
  'pop',
  'push',
  'siftUp',
  'siftDown',
  // "flushWork",
  'markTaskCompleted',
  // "unstable_shouldYield",
  // "unstable_getCurrentPriorityLevel",
  // "unstable_scheduleCallback",
  // "unstable_runWithPriority",
  'compare',
  // others
  'PropertyInfoRecord',
  'unsafeCastStringToDOMTopLevelType',
  'addEventPoolingTo',
  'prefixKey',
  'makePrefixMap',
  'getVendorPrefixedEventName',
];
const collapsed = ['injectEventPluginsByName', 'dispatchEvent'];

@discrete-javascript
Copy link

  • add the path to babel.config.js ('./scripts/babel/console-function-names')

Hi Sunil, Glad you made it to Facebook. You're really an inspiration for me.

Instead of functions, I'm trying to print the component names!

Can you help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment