babel-plugin-auto-console-log
v1.1.20
Published
A Babel plugin that adds console.log statements automatically.
Downloads
28
Maintainers
Readme
Auto console log 🚀
babel-plugin-auto-console-log is a Babel plugin that automatically adds console.log() statements to your JavaScript code. This library is useful for debugging code, as it adds console.log() statements to specific parts of the code so that you can see the values of variables and the results of operations.
The main goal of babel-plugin-auto-console-log is to make code debugging easier and faster by automatically adding console.log() statements to your code. This means that you won't have to manually add console.log() statements to your code every time you need to debug it, which saves time and reduces errors.
Install
npm i babel-plugin-auto-console-log --save-dev
# or
yarn add babel-plugin-auto-console-log -D
# or
pnpm i babel-plugin-auto-console-log --dev
How to use 📝
Auto Console Log is a Babel transform that can be used both on the client-side and server-side of a JavaScript project.
Here is an example on how you can implement it using the @babel/standalone
package:
- Install both packages:
npm i @babel/standalone babel-plugin-auto-console-log --save-dev
- Register the plugin and create a function to transform your code:
import Babel from '@babel/standalone';
import autoConsoleLog from 'babel-plugin-auto-console-log';
// const autoConsoleLog = require("babel-plugin-auto-console-log")
Babel.registerPlugin('babel-plugin-auto-console-log', autoConsoleLog());
- Create a function to transform your code:
const transform = (source) =>
Babel.transform(source, {
plugins: ['babel-plugin-auto-console-log']
}).code;
- You are now ready to transform your code! using the
transform()
function.
Here is the complete code:
import Babel from '@babel/standalone';
import autoConsoleLog from 'babel-plugin-auto-console-log';
// const autoConsoleLog = require("babel-plugin-auto-console-log")
Babel.registerPlugin('babel-plugin-auto-console-log', autoConsoleLog());
const transform = (source) =>
Babel.transform(source, {
plugins: ['babel-plugin-auto-console-log']
}).code;
Options
consoleMethod
default: log : The consoleMethod parameter is used to specify the logging method to be used to print the output of a function. For example, if "warn" is provided as the value for the consoleMethod parameter, the output will be printed with the "< consoleObject >.warn" method instead of the default console.log method.consoleObject
default: console : The consoleObject parameter is an argument that can be provided to a function and its purpose is to specify the console object that will be used to print the output of the function. For example if "logger" is the given value the output will be "logger.< consoleMethod >".
Constants
CONSOLE_METHODS
CONSOLE_METHODS gives you a list of all the methods that console has, so that you don't make a mistake when using a method other than "log".
To use this constant you must import it from "babel-plugin-auto-console-log/constants" as follows:
import { CONSOLE_METHODS } from "babel-plugin-auto-console-log/constants"
// or
const { CONSOLE_METHODS } = require("babel-plugin-auto-console-log/constants")
Examples
Here are some examples of how the babel-plugin-auto-console-log plugin works:
Input
const foo = 42;
foo;
Output
const foo = 42;
console.log(foo);
Input
1 + 1;
Output
console.log(1 + 1);
Input
10 > 5 ? "Yes!" : "No!";
Output
console.log(10 > 5 ? "Yes!" : "No!");
Input
function sayHi (name) {
return `Hello ${name}!!!`
}
sayHi("foo");
Output
function sayHi (name) {
return `Hello ${name}!!!`
}
console.log(sayHi("foo"));
Contributing 🤝
Thank you for your interest in contributing to babel-plugin-auto-console-log! We appreciate your support and welcome any contributions to this project.
To get started, please refer to the CONTRIBUTING.md file for guidelines on how to contribute to this project.
Once again, thank you for your interest in contributing to babel-plugin-auto-console-log!