Create a JavaScript comments remover
By: Manish Mandal
Tags: Nodejs, Programming, Javascript Development, Javascript Tips, JavaScript

In this article, I’ll show you how to create a comment remover that will strip all single and multiple comments from all your JavaScript files. Whenever we work on a project we create some single line or multiple line comments for understanding the necessity or usability of the current code. These single line or multiple line comments are good during the development phase but not suitable for production as this can increase the size of the files.

I know the increment in the size of the file is very tiny. We basically work on a small project and the small project doesn’t need optimization to that level that we need to remove comments in production 😂. Still, if someone is interested to know how to remove all the comments from the JavaScript files read the below article. This will be useful for all your Node, Angular, React, Vue, or any other JavaScript project.

Create a directory and initialize your project.

npm init -y #or yarn init -y

2. Then create two files inside your project directory.

index.js — This file will be the main file of our project that will hold all our code.

comment.js — This file contains some dummy JavaScript codes with single and multiple comments.

3. Now open the comment.js file and paste the below dummy JavaScript code.

4. Now it’s time to write code inside our index.js file but before that, we need to install some necessary modules. Install strip-comments to your project. This library basically strips line or block comments from a string.

npm i strip-comments or yarn add strip-comments

5. Now paste the below code inside the index.js file.

Running node index.js command inside the terminal will strip comments from the declared filepath which in our case is comment.js.

Now if we look inside our comment.js file we can see all our single and multiple comments have been removed from the file. But still, I am not satisfied with the result because blank spaces have been added in place of comments.

To fix this we will use the prettier module to format our file. So now install prettier to our project and then will modify some of our code.

npm i prettier or yarn add prettier

Now will use the prettier format method to format our code. Replace the previous code with the below-mentioned code from your index.js file.

Now run node index.js in your terminal and check comment.js file.

This is perfect all comments and blank spaces have been removed.😃

But still, this works only for a single JavaScript file but we need to remove comments from all JavaScript files available in our project. To do this we need to install the glob module to our project. This module will give all our JavaScript files available in our project in an array.

npm i glob or yarn add glob

After installing glob remove the previous code from your index.js file and paste the below code.

That’s it running the above code node index.js will remove comments from all your JavaScript files.

To simplify it better not to use node index.js , again and again, you can add a script to your package.json and then run npm run clean

The above code will work for all your JavaScript projects. All you need is to create a js file and paste the above code and install the required libraries (glob, prettier, strip-comments).

Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!

Thanks for Reading. Below I have shared a GitHub repository for reference.