DEV Community

Ruben
Ruben

Posted on • Updated on • Originally published at blog.linguinecode.com

7 Webpack plugins for your next React app

original post @ Linguine Blog

Webpack optimization

Webpack comes with a configuration property called optimization.

Inside optimization another property named minimize, and minimizer.

If minimize equals true, then everything inside minimizer will trigger.

Webpack optimize property

The next couple of Webpack plugins that I will cover, they should be inside the minimizer property.

Terser webpack plugin

The Terser plugin is a great tool to minimize the application JavaScript bundle file for production.

Another plus to this plugin is that it supports ES6+.

Here is a basic configuration for Terser.

Terser webpack plugin basic configuration

Source: Terser plugin

Optimize CSS assets Webpack plugin

The Optimize CSS assets Webpack plugin is another one for production build.

It helps optimize and minimize your React CSS code.

Source: Optimize css assets plugin

Webpack plugins

HTML Webpack plugin

The HTML Webpack plugin is a handy plugin for both development and production build.

It tells Webpack to generate an HTML file and inject a script tag with the JavaScript code.

All you need to do is supply the HTML template to use.

HTML webpack plugin basic configuration

You may also add minification rules such as removing comments, minifying the CSS, and JavaScript.

But if you’d like to only use the minify property for build you can do something like.

Mnify HTML webpack plugin only for production

Source: HTML Webpack plugin

Webpack Define plugin

This plugin comes with the Webpack node module, and it’s a handy little tool when developing your React application.

The DefinePlugin allows you to create environment variables and makes it accessible to your JavaScript code.

Webpack DefinePlugin basic use

Source: Webpack DefinePlugin

Mini CSS Extract plugin

This plugin is another great production tool.

It allows your to extract the CSS in your React app into separate files per JavaScript file.

Some other features that are enjoyable versus the Extract Text Webpack plugin is:

  • Async loading
  • No duplicate compilation
  • Easier to use

Mini CSS extract plugin webpack configuration

Webpack modules

Babel loader

This plugin allows you to write the latest generation of JavaScript and converts it to a compiled ES5 version.

Babel ES6 feature to transpiled version

Now to configure babel-loader for .js, .jsx, and as well for Typescript (if you’re using it).

Webpack babel loader configration

Source: Babel loader

Babel preset react app

This module is maintained by the Create React App team and it has a great set of presets for your React App.

All we need to do is add more to the babel loader configuration.

Babel loader configuration with react presets

Source: Babel preset react app

Top comments (0)