Giter Site home page Giter Site logo

vjpr / babel-plugin-console Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattphillips/babel-plugin-console

1.0 2.0 2.0 5.71 MB

Babel Plugin that adds useful build time console functions ๐ŸŽฎ

Home Page: https://www.npmjs.com/package/babel-plugin-console

License: MIT License

JavaScript 100.00%

babel-plugin-console's Introduction

babel-plugin-console

๐ŸŽฎ

Adds useful build time console functions


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

  • obj1 ... objN A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

  • msg A JavaScript string containing zero or more substitution strings.

  • subst1 ... substN JavaScript objects with which to replace substitution strings within msg. This gives you additional control over the format of the output.

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:

Node console scoping add100

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


Matt Phillips

๐Ÿ’ป ๐Ÿ“– ๐Ÿš‡ โš ๏ธ

Stephen Bluck

๐Ÿ“–

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.