npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adamlui/minify.js

v1.6.1

Published

Recursively minify all JavaScript files

Downloads

1,450

Readme

</> minify.js

Recursively minify all JavaScript files.

⚡ Installation

As a global utility:

$ npm install -g @adamlui/minify.js

As a dev dependency (e.g. for build scripts), from your project root:

$ npm install -D @adamlui/minify.js

As a runtime dependency (e.g. for on-the-fly minification), from your project root:

$ npm install @adamlui/minify.js

💻 Command line usage

The basic global command is:

$ minify-js

💡 Note: Pass -n or --dry-run to only see what files will be processed.

To specify input/output paths:

$ minify-js [input_path] [output_path]
  • [input_path]: Path to JS file or directory containing JS files to be minified, relative to the current working directory.
  • [output_path]: Path to file or directory where minified files will be stored, relative to original file location (if not provided, min/ is used).

💡 Note: If folders are passed, files will be processed recursively unless -R or --no-recursion is passed.

To use as a package script, in your project's package.json:

  "scripts": {
    "build:js": "<minify-js-cmd>"
  },

Replace <minify-js-cmd> with minify-js + optional args. Then, npm run build:js can be used to run the command.

Example commands

Minify all JavaScript files in the current directory (outputs to min/):

$ minify-js

Minify all JavaScript files in a specific directory (outputs to path/to/your/directory/min/):

$ minify-js path/to/your/directory

Minify a specific file (outputs to path/to/your/min/file.min.js):

$ minify-js path/to/your/file.js

Specify both input and output directories (outputs to output_folder/):

$ minify-js input_folder output_folder

Command line options

Boolean options:
 -n, --dry-run                        Don't actually minify the file(s),
                                      just show if they will be processed.
 -d, --include-dotfolders             Include dotfolders in file search.
 -D, --include-dotfiles               Include dotfiles in file search.
 -R, --no-recursion                   Disable recursive file searching.
 -M, --no-mangle                      Disable mangling names.
 -q, --quiet                          Suppress all logging except errors.

Parameter options:
 --ignore-files="file1.js,file2.js"   Files to exclude from minification.
 --comment="comment"                  Prepend comment to minified code.
                                      Separate by line using '\n'.

Info commands:
 -h, --help                           Display help screen.
 -v, --version                        Show version number.

🔌 API usage

You can also import minify.js into your app to use its API methods, both as an ECMAScript module or a CommonJS module.

ESM*:

import * as minifyJS from '@adamlui/minify.js';

CJS:

const minifyJS = require('@adamlui/minify.js');
*Node.js version 14 or higher required

minify(input[, options])

Minifies JavaScript code based on the string input supplied.

If source code is passed, it is directly minified, then an object containing srcPath + code + error is returned:

const srcCode = 'function add(first, second) { return first + second; }',
      minifyResult = minifyJS.minify(srcCode);

console.log(minifyResult.error); // outputs runtime error, or `undefined` if no error
console.log(minifyResult.code);  // outputs minified JS: 'function add(n,d){return n+d}'

If a file path is passed, the file's code is loaded then minified, returning an object like above.

If a directory path is passed, JavaScript files are searched for (recursively by default), each one's code is loaded then minified, then an array of objects containing srcPath + code + error is returned:

// Outputs paths to source JS files in working directory + all nested directories
const minifyResults = minifyJS.minify('.');
minifyResults.forEach(result => console.log(result.srcPath));

// Outputs minified code of 2nd JS file if found, or `undefined` if not found
console.log(minifyResults[1].code);

Options are boolean, passed as object properties. For example:

// Returns array of data objects where dotfiles are also processed if `input` is a path
minifyJS.minify(input, { dotFiles: true });

Available parameters (and their default settings) are:

Name | Type | Desciption | Default value --------------|---------|-------------------------------------------------------------------|--------------- recursive | Boolean | Recursively search for nested files if dir path passed. | true verbose | Boolean | Show logging in console/terminal. | true dotFolders | Boolean | Include dotfolders in file search. | false dotFiles | Boolean | Include dotfiles in file search. | false mangle | Boolean | Shorten variable names (typically to one character). | true ignoreFiles | Array | Files (by name) to exclude from minification. | [] comment | String | Comment to prepend to minified code. Separate by line using '\n'. | ''

findJS(searchDir[, options])

Searches for all unminified JavaScript files within the searchDir string passed (useful for discovering what files minify() will process) and returns an array containing their filepaths.

Options are boolean, passed as object properties. For example:

// Search for unminified JS files in exactly assets/js
const searchResults = minifyJS.findJS('assets/js', { recursive: false });
console.log(searchResults);

/* sample output:

findJS() » Searching for unminified JS files...
findJS() » Search complete! 2 files found.
findJS() » Check returned array.
[
  'E:\\js\\utils\\minify.js\\assets\\js\\foo.js',
  'E:\\js\\utils\\minify.js\\assets\\js\\bar.js'
]
*/

Available parameters (and their default settings) are:

Name | Type | Desciption | Default value --------------|---------|----------------------------------------------------------|--------------- recursive | Boolean | Recursively search for nested files in searchDir passed. | true verbose | Boolean | Show logging in console/terminal. | true dotFolders | Boolean | Include dotfolders in file search. | false dotFiles | Boolean | Include dotfiles in file search. | false ignoreFiles | Array | Files (by name) to exclude from search results. | []

🏛️ MIT License

Copyright © 2023–2024 Adam Lui & contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

🛠️ Related utilities

</> minify.js (Gulp)

Gulp plug-in to recursively minify all JavaScript files. Install / Readme / Discuss

{ } scss-to-css

Recursively compile all SCSS files into minified CSS. Install / Readme / CLI usage / API usage / Discuss

More JavaScript utilities / Discuss / Back to top ↑