babel-plugin-console-events
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

babel-plugin-console-events

Problem

Sometimes there is a situation when you need to execute certain code when calling the console.log(). For example, send a request to the server. One way to do this is to override the original method. But in this case, we lose the filename and the line number where the console.log call occurs. Something like this is needed:

console.on('log', () =>{})

What does this plugin do?

All it does is replace console method calls

console.log(/*...args*/)

with

console.log(/*...args*/);

if (console.events) {
    console.events.emit({
        type: "log",
        args: [/*...args*/],
        loc: ["code.ts", "2"],
    });
}

and provides a utility subscribe function to listen for these events

Installation

With npm:

npm install babel-plugin-console-events

With yarn:

yarn add babel-plugin-console-events

Setup

.babelrc

{
  "plugins": ["babel-plugin-console-events"]
}

.craco.config.js

module.exports = {
    babel: {
        plugins: [
            "babel-plugin-console-events",
        ],
        loaderOptions: { /* Any babel-loader configuration options: https://github.com/babel/babel-loader. */ },
        loaderOptions: (babelLoaderOptions, { env, paths }) => {
            return babelLoaderOptions;
        }
    }
};

APIs

subscribe()

Pass the listener to the subscribe function and it will be called when calling the log, warn, info, debug, error methods of the console object.

import { subscribe } from 'babel-plugin-console-events2'

subscribe((event) => {
//  code here
})

Attention! Do not call console methods inside the listener, otherwise it will lead to recursion.

LICENSE

MIT

Package Sidebar

Install

npm i babel-plugin-console-events

Weekly Downloads

11

Version

1.0.9

License

ISC

Unpacked Size

16.8 kB

Total Files

21

Last publish

Collaborators

  • jpike
  • rasimx