Home

Awesome

<div align="center"> <h1>babel-plugin-console</h1>

🎮

Adds useful build time console functions

</div> <hr />

Build Status Code Coverage version downloads MIT License PRs Welcome Roadmap Examples Babel Macro

Problem

Ever find yourself using console.log to work out what the heck is going on within your functions? And then put too many variables into the log and lose context of which value is which variable?

Solution

These problems can be solved by a computer at build time. This plugin allows you to inspect a functions parameters, variables, return value and parent scope by adding meaningful logs around the scope of the function - removing the hassle of manual console.log statements or debugging.

Installation

With npm:

npm install --save-dev babel-plugin-console

With yarn:

yarn add -D babel-plugin-console

Setup

.babelrc

{
  "plugins": ["console"]
}

CLI

babel --plugins console script.js

Node

require('babel-core').transform('code', {
  plugins: ['console'],
})

APIs

console.scope()

Outputs a collection of messages for a functions entire scope

Note: this API is the same as the standard console.log

Syntax

console.scope(obj1 [, obj2, ..., objN]);
console.scope(msg [, subst1, ..., substN]);

Parameters

Usage

Plugin

const add100 = (a) => {
  const oneHundred = 100;
  console.scope('Add 100 to another number');
  return add(a, oneHundred);
};

const add = (a, b) => {
  return a + b;
};

      ↓ ↓ ↓ ↓ ↓ ↓

Browser:

Browser console scoping add100

Node:

<img alt="Node console scoping add100" src="assets/add100-node.png" width="372">

Macros

babel-plugin-console also ships with babel-macros supported 🎉

To use a macro you will need to install and setup babel-macros.

Once you have enabled babel-macros import/require the scope macro with:

import scope from 'babel-plugin-console/scope.macro';
// OR
const scope = require('babel-plugin-console/scope.macro');

Example

Note: this is the same as the above usage and will generate the same logs - only difference is it uses the scope macro 😎

      ↑ ↑ ↑ ↑ ↑ ↑
import scope from 'babel-plugin-console/scope.macro';

const add100 = (a) => {
  const oneHundred = 100;
  scope('Add 100 to another number');
  return add(a, oneHundred);
};

const add = (a, b) => {
  return a + b;
};

NOTE: There's also a separate package available called scope.macro which you can install and use instead if you prefer to type less.

Inspiration

This tweet led to me watching @kentcdodds's live presentation on ASTs. This plugin is an extension on the captains-log demoed - Thanks Kent!

Other Solutions

You could use a debugger with breakpoints to pause the execution of a function and inspect the values of each variable.

The above add100 function would look like the following if you used a debugger with a breakpoint:

Debugging add100

Contributors

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<img src="https://avatars0.githubusercontent.com/u/5610087?v=4" width="100px;"/><br /><sub>Matt Phillips</sub><br />💻 📖 🚇 ⚠️<img src="https://avatars1.githubusercontent.com/u/611927?v=4" width="100px;"/><br /><sub>Stephen Bluck</sub><br />📖
<!-- ALL-CONTRIBUTORS-LIST:END -->

LICENSE

MIT