Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Minify JavaScript Files Using UglifyJS on Windows

UglifyJS is a JavaScript compressor/minifier that can significantly reduce the size of your JavaScript files. This is particularly useful for web developers looking to optimize their web applications for faster load times and better performance. While UglifyJS is typically associated with Unix-like environments, it can also be used effectively on Windows. This article will guide you through the process of installing UglifyJS on a Windows system and demonstrate how to use it to minify JavaScript files via the Command Prompt (CMD).

Examples:

  1. Installing Node.js and npm:

    UglifyJS is a Node.js-based tool, so the first step is to install Node.js and npm (Node Package Manager) on your Windows machine.

    • Download the Node.js installer from the official website.
    • Run the installer and follow the on-screen instructions to complete the installation.
    • Verify the installation by opening CMD and typing:
      node -v
      npm -v

      This should display the versions of Node.js and npm installed on your system.

  2. Installing UglifyJS:

    Once Node.js and npm are installed, you can install UglifyJS globally using npm.

    • Open CMD and run the following command:
      npm install -g uglify-js

      This will install UglifyJS globally, making it accessible from any directory on your system.

  3. Minifying a JavaScript File:

    To minify a JavaScript file using UglifyJS, follow these steps:

    • Navigate to the directory containing your JavaScript file. For example, if your file is located in C:\Projects\MyApp, you would use:
      cd C:\Projects\MyApp
    • Run UglifyJS with the input file and specify an output file. For instance, to minify app.js and save the result as app.min.js, use:
      uglifyjs app.js -o app.min.js
    • You can also add options to further customize the minification process. For example, to enable mangling (renaming of variables to shorter names), use:
      uglifyjs app.js -o app.min.js -m
  4. Automating the Process with a Batch Script:

    If you frequently need to minify JavaScript files, you can automate the process using a batch script.

    • Create a new text file and rename it to minify.bat.
    • Edit the file and add the following content:
      @echo off
      uglifyjs %1 -o %2 -m
      echo Minification complete: %1 -> %2
    • Save the file and use it by passing the input and output filenames as arguments. For example:
      minify.bat app.js app.min.js

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.