Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

emmercm/metalsmith-html-unused

Repository files navigation

metalsmith-html-unused

⚠️ This repistory has been moved to metalsmith-plugins. ⚠️

npm Version npm Weekly Downloads

Known Vulnerabilities Test Coverage Maintainability

GitHub 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({
        // 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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published