1.0.0 • Published 3 years ago

metalsmith-mangle-names v1.0.0

Weekly downloads
-
License
AGPLv3
Repository
github
Last release
3 years ago

metalsmith-mangle-names

A metalsmith plugin for mangling names in specified source files.

This plugin mangles names provided in source files into smaller valid names to minify the files. Under the hood, metalsmith-mangle-names uses a simple regular expression replace of names given a specified delimiter. For example, the if you have the CSS class name .___hello___, after running the name mangler this will be replaced in all source files with a. Note that the characters between the delimiter must be alphanumeric or a dash (-): [a-zA-Z0-9-].

Installation

npm install metalsmith-mangle-names

Usage

To use this plugin, simply add it to the existing plugins in your Metalsmith source file or include it in the Metalsmith JSON file:

JavaScript

const Metalsmith = require('metalsmith');
const mangleNames = require('metalsmith-mangle-names');

Metalsmith(__dirname)
  .use(mangleNames())
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "plugins": {
    "metalsmith-mangle-names": {}
  }
}

Options

You can pass options to metalsmith-mangle-names with the Javascript API or CLI. The options are:

  • pattern: optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is **/*.html, **/*.css, **/*.js.
  • mangle: optional. If set to false, names will not be mangled, but instead just the delimiters will be removed. Accepts a boolean. The default is true.
  • delimiter: optional. The delimiter used by the regular expression to determine which names to mangle. Accepts a string. The default is ___.
  • offset: optional. A numeric offset that determines which letters in the alphabet to start mangling names at. For example, with a value of 0, the first mangled name will be a, but with a value of 2 the first mangled name will start at c and increment from there. Accepts a number. The default is 0.

pattern

Only files that match this pattern will be processed. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const mangleNames = require('metalsmith-mangle-names');

Metalsmith(__dirname)
  .use(mangleNames({
    pattern: [
      'blog/**/*.html',
      'blog/**/*.css',
      'blog/**/*.js',
    ],
  }))
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-mangle-names": {
      "pattern": [
        "blog/**/*.html",
        "blog/**/*.css",
        "blog/**/*.js"
      ]
    }
  }
}

Would only process HTML, CSS, and JS files within the ./src/blog folder, because the pattern is relative to your source folder. See multimatch for further details.

mangle

Whether names should be mangled or just remove the delimiters. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const mangleNames = require('metalsmith-mangle-names');

Metalsmith(__dirname)
  .use(mangleNames({
    mangle: true,
  }))
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-mangle-names": {
      "mangle": true
    }
  }
}

Would mangle the name ___hello___ into a, while a value of false would convert ___hello___ into hello.

delimiter

The delimiter used by the regular expression to determine which names to name. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const mangleNames = require('metalsmith-mangle-names');

Metalsmith(__dirname)
  .use(mangleNames({
    delimiter: '___',
  }))
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-mangle-names": {
      "delimiter": "___"
    }
  }
}

Would parse out all names between the delimiter ___. For example, the name ___hello___ will be mangled to a.

offset

The offset used to calculate mangled names. So this Metalsmith JavaScript configuration or metalsmith.json:

JavaScript

const Metalsmith = require('metalsmith');
const mangleNames = require('metalsmith-mangle-names');

Metalsmith(__dirname)
  .use(mangleNames({
    offset: 0,
  }))
  .build((err, files) => {
    if (err) { throw err; }
  });

JSON

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "metalsmith-mangle-names": {
      "offset": 0
    }
  }
}

Would parse start mangling names at a, while and offset of 2 would start mangling names at c.

License

AGPLv3

1.0.0

3 years ago