Unobstructed

We can capture almost anything with our naked eye I wonder what it would take to break free of our filtered lens to see the world as it is and how it’s always been behind the smoke and mirrors…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Write your first webpack plugin

If you have clicked on the title, then I assume you have a basic understanding of webpack and webpack plugins.Plugins are great and helps you leverage the power of webpack.

While these are great but there are always cases when you need a project specific plugin and it gets worse when you are unable to find any plugin for your needs.

Sounds good? okay then !

In this post I will try to explain you how to write your very own webpack plugin. By the end of this post you’ll be able to write your own plugin with minimal knowledge .

Let’s begin by creating a simple react project.

If your react project is not setup then follow these steps to create a basic react starter.

I have created this simple boilerplate to easily get your hands dirty on react projects

After setting up the directory create a file named logRemover.js in your project directory and copy this following code.

Now before getting further lets look at the code and see what its doing.

For now we are just going to console a simple message just to check it working.

Before starting the app we need to add our plugin in webpack.config.prod.js. We are adding that to production config only because we just want to remove the logs from the production builds.

Now we’re going to start the app by

Now we are ready with the setup lets create logic for our plugin

Lets breakdown to see whats happening

compiler module is the main engine of the webpack which emits events just like dom. It extends Tapable lib to register and call plugins. In this logic we are tapping on hook “done” which is emitted after the compilation is completed and bundle is generated.

So here we are taking the bundled js and passing it through a regex and removing all the console.logs and outputting the bundled js.

To check if its working :

Write console.log() in App.js and then build the app using

Now check the bundled file in dist folder and see if there are any logs in the output file.

Here is the github project if you want to see the full source code :

Additional Reads:

Add a comment

Related posts:

Forex trading secrets

What is forex trading? Forex trading is the act of buying or selling currencies. Banks, central banks, corporations, institutional investors and individual traders exchange foreign currency for a…

Welcome to South Africa

Beach and the African Penguin colony walk in the white sands just meters away from the fun. With the contrasting colours of grey granite, pale-yellow sands and blue sea water, this penguin’s…

How to improve your V60 brew technique

I will not say something like place the filter paper in the dripper and boil your kettle so on and so forth. My aim is here to share some small details that can affect your brew technique. Although…