metalsmith-uglifyjs

A very lightweight Metalsmith uglifyjs2 plugin

Usage no npm install needed!

<script type="module">
  import metalsmithUglifyjs from 'https://cdn.skypack.dev/metalsmith-uglifyjs';
</script>

README

metalsmith-uglifyjs

Gitter Travis David David Codecov npm npm

An Uglifyjs2 plugin for Metalsmith.

Installation

npm install --save metalsmith-uglifyjs

Getting Started

If you haven't checked out Metalsmith before, head over to their website and check out the documentation.

Usage

var uglifyjs = require("metalsmith-uglifyjs");

metalsmith
  .use(uglifyjs({
    src: ["**/*.js", "!**/*.min.js"],
    target: function(inFile) { return inFile + ".customMinFileName.js"; },
    deleteSources: true,
    uglifyOptions: {
      mangle: true,
      compress: {
        unused: false,
        warnings: true
      }
    }
  }))

Options

You can check the tests out to see some usage examples.

src

A multimatch expression that can be used to limit the files that will be uglified. Default is ["**/*.js", "!**/*.min.js"] Please note that IT IS YOUR JOB to EXCLUDE already minified files using src setting if you are using a customized name convention.

target

A function that takes a file name as input and expected to return minified file name as output. Default function adds .min. before extension. (i.e. a.js -> a.min.js)

override

If true, then compressed JS will be written into the same files that were compressed. The target and deleteSources options are discarded in this case.

Default: false

deleteSources

A boolean, if true original files will be deleted, if false original files will be kept as-is. Default: false

active

A boolean flag, when set to false this whole stuff will be disabled. This can be used to disable uglify based on some variable. (e.g. Don't run this on dev but run this on prod.)

Default: true

uglifyOptions

A valid config that will be passed to uglifyjs. Please see here for your options. Default:

uglifyOptions: {
  mangle: false,
  compress: {}
}

This means use default compress options and do not mangle.

Contributing

Just open an issue or prepare a pull request.