metalsmith-html-unused
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

metalsmith-html-unused

npm: version npm: downloads

Snyk: vulnerabilities codecov: coverage license

A Metalsmith plugin to exclude resources that aren't referenced in HTML files.

Removing unreferenced files such as JavaScript, CSS, images, and documents helps optimize your build output.

Installation

npm install --save metalsmith-html-unused

JavaScript Usage

const Metalsmith = require('metalsmith');
const unused     = require('metalsmith-html-unused');

Metalsmith(__dirname)
    .use(unused({
        pattern: '**/*.@(css|js)'
        // other options here
    }))
    .build((err) => {
        if (err) {
            throw err;
        }
    });

Options

pattern (required)

Type: string

A micromatch glob pattern for files to consider for removal.

Example: "**/*.@(css|js|bmp|gif|jpg|jpeg|png|svg|tif|tiff|webp)"

ignore (optional)

Type: string

A micromatch glob pattern for files to exclude from removal. If no pattern is defined then no files will be ignored.

html (optional)

Type: string Default: "**/*.html"

A micromatch glob pattern to find HTML files.

attributes (optional)

Type: string[] Default: ['href', 'src', 'data-src', 'content']

An array of HTML attributes that link to files.

Example

Given the config:

{
    "pattern": "**/*.@(css|js|png)",
    "ignore": "**/logo.png"
}

And a file tree:

.
├── index.html
└── static
    ├── css
    │   ├── bootstrap.min.css
    │   └── fontawesome.all.min.css
    ├── img
    │   └── logo.png
    └── js
        ├── bootstrap.min.js
        └── popper.js

And index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="static/css/bootstrap.min.css">
    </head>
    <body>
        <script src="static/js/bootstrap.min.js"></script>
    </body>
</html>

Both static/js/fontawesome.all.min.css and static/js/popper.js would be excluded from build output because they are not referenced, and static/img/logo.png would persist because it was ignored. The final file tree would be:

.
├── index.html
└── static
    ├── css
    │   └── bootstrap.min.css
    ├── img
    │   └── logo.png
    └── js
        └── bootstrap.min.js

Changelog

Changelog

Package Sidebar

Install

npm i metalsmith-html-unused

Weekly Downloads

4

Version

2.0.1

License

GPL-3.0-or-later

Unpacked Size

49.8 kB

Total Files

6

Last publish

Collaborators

  • emmercm