How to Use Webpack & Terser to Only Compress Code?

11 minutes read

Webpack is a powerful tool for bundling and optimizing front-end resources such as JavaScript files. One way to reduce the size of the bundled output is by using Terser, a JavaScript minifier. Terser can be integrated into the webpack configuration to only compress code without mangling or renaming variable names. This can be achieved by setting the "terserOptions" in the webpack configuration to specify the desired behavior.


By configuring Terser to only compress code, you can effectively reduce the size of the output bundle without sacrificing readability or debuggability. This can be particularly useful in cases where minimizing file size is a priority, such as when optimizing for performance or user experience.


Overall, leveraging webpack and Terser to only compress code can help improve the efficiency and performance of a front-end application by reducing the payload size while maintaining code quality.

Best Javascript Books to Read in July 2024

1
JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

Rating is 5 out of 5

JavaScript: The Definitive Guide: Master the World's Most-Used Programming Language

2
JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

Rating is 4.9 out of 5

JavaScript from Beginner to Professional: Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages

3
JavaScript Crash Course: A Hands-On, Project-Based Introduction to Programming

Rating is 4.8 out of 5

JavaScript Crash Course: A Hands-On, Project-Based Introduction to Programming

4
Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

Rating is 4.7 out of 5

Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming

  • It can be a gift option
  • Comes with secure packaging
  • It is made up of premium quality material.
5
JavaScript All-in-One For Dummies

Rating is 4.6 out of 5

JavaScript All-in-One For Dummies

6
Learning JavaScript Design Patterns: A JavaScript and React Developer's Guide

Rating is 4.5 out of 5

Learning JavaScript Design Patterns: A JavaScript and React Developer's Guide

7
JavaScript and jQuery: Interactive Front-End Web Development

Rating is 4.4 out of 5

JavaScript and jQuery: Interactive Front-End Web Development

  • JavaScript Jquery
  • Introduces core programming concepts in JavaScript and jQuery
  • Uses clear descriptions, inspiring examples, and easy-to-follow diagrams
8
Web Design with HTML, CSS, JavaScript and jQuery Set

Rating is 4.3 out of 5

Web Design with HTML, CSS, JavaScript and jQuery Set

  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
9
Head First JavaScript Programming: A Brain-Friendly Guide

Rating is 4.2 out of 5

Head First JavaScript Programming: A Brain-Friendly Guide

10
Murach's Modern JavaScript: Beginner to Pro

Rating is 4.1 out of 5

Murach's Modern JavaScript: Beginner to Pro


What is webpack bundling and how does it work?

Webpack bundling is the process of combining multiple web resources such as JavaScript files, images, and CSS files into a single file (or multiple files) for optimized delivery to a web browser. This process helps to reduce the number of requests made to the server and improve loading time for web applications.


Webpack works by capturing all the dependencies of a web application and creating a dependency graph. It then processes these files using loaders and plugins to transform and optimize the code. Finally, it bundles all the files together and generates a single bundle file or multiple bundle files that can be included in the HTML document.


Webpack also allows for code splitting, which means that the application code can be split into separate bundles that can be loaded on demand. This helps to optimize the initial loading time of the application.


Overall, webpack bundling helps to simplify the development process, improve performance, and optimize the delivery of web applications to users.


How to use terser plugin in webpack?

To use the terser plugin in webpack, follow these steps:

  1. Install the terser plugin by running the following command in your project directory:
1
npm install terser-webpack-plugin --save-dev


  1. Import the terser plugin at the top of your webpack configuration file (usually named webpack.config.js):
1
const TerserPlugin = require('terser-webpack-plugin');


  1. Add the terser plugin to the optimization section of your webpack configuration:
1
2
3
4
5
6
7
8
9
module.exports = {
  // Other webpack configuration settings...

  optimization: {
    minimizer: [
      new TerserPlugin()
    ]
  }
}


  1. Customize the terser plugin's options if needed by passing an object with configuration options to the plugin constructor. For example, you can specify the parallel option to enable parallel processing of files:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
module.exports = {
  // Other webpack configuration settings...

  optimization: {
    minimizer: [
      new TerserPlugin({
        parallel: true
      })
    ]
  }
}


  1. Run webpack to build your project with the terser plugin enabled:
1
npx webpack


The terser plugin will compress and minify your JavaScript code during the build process, resulting in smaller bundle sizes and improved performance for your web application.


How to handle CSS and images in webpack compression?

To handle CSS and images in webpack compression, you can use plugins like mini-css-extract-plugin for CSS and file-loader or url-loader for images. Here is how you can set them up in your webpack configuration file:

  1. For CSS compression using mini-css-extract-plugin:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].[contentHash].css'
    })
  ]
};


  1. For image compression using file-loader or url-loader:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
module.exports = {
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[hash].[ext]',
              outputPath: 'images/'
            }
          }
        ]
      }
    ]
  },
};


Alternatively, you can use url-loader which is a higher-level loader that falls back to file-loader when files are larger than a specified limit:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
module.exports = {
  module: {
    rules: [
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192, // Convert images < 8kb to base64 strings
              name: '[name].[hash].[ext]',
              outputPath: 'images/'
            }
          }
        ]
      }
    ]
  },
};


By setting up these plugins and loaders in your webpack configuration, you can effectively compress and optimize your CSS and images for production builds.


What is the difference between production and development mode in webpack?

In Webpack, production mode is optimized for performance and deployment, while development mode is optimized for speed and debugging.

  1. Production Mode:
  • In production mode, Webpack applies optimizations such as minification, tree shaking, code splitting, and scope hoisting to generate a smaller bundle size.
  • The code generated in production mode is usually not human-readable and is meant for deployment on a live server.
  • Error handling and debugging tools are minimized in production mode to improve performance.
  • Production mode is typically used when building the final version of a project for production deployment.
  1. Development Mode:
  • In development mode, Webpack generates a larger bundle size with more detailed error messages and source maps to aid in debugging.
  • Development mode is optimized for speed, with features such as hot module replacement for quick updates to the application without a full reload.
  • The code generated in development mode is more human-readable and easier to work with during development and testing.
  • Development mode is typically used during the development phase of a project to facilitate faster development and debugging.


Overall, the main difference between production and development mode in Webpack lies in the optimization settings applied to the code and the focus on performance versus debugging and development speed.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To use terser with webpack, you need to install the terser-webpack-plugin. You can do this by running the command &#34;npm install terser-webpack-plugin --save-dev&#34;. Next, you need to configure the plugin in your webpack configuration file. You can do this...
To set up webpack to work with React, you need to first install webpack and webpack-cli by running npm install webpack webpack-cli --save-dev in your project directory. Then, you need to create a webpack configuration file (webpack.config.js) where you specify...
To install webpack, you will first need to have Node.js installed on your system. Once you have Node.js installed, you can use npm (Node Package Manager) to install webpack globally by running the command &#34;npm install webpack -g&#34;. This will install web...