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.
npm install --save metalsmith-html-unusedconst Metalsmith = require('metalsmith');
const unused = require('metalsmith-html-unused');
Metalsmith(__dirname)
.use(unused({
// options here
}))
.build((err) => {
if (err) {
throw err;
}
});Type: string
A micromatch glob pattern for files to consider for removal.
Example: **/*.@(css|js|bmp|gif|jpg|jpeg|png|svg|tif|tiff|webp).
Type: string
A micromatch glob pattern for files to exclude from removal. If no pattern is defined then no files will be ignored.
Type: string Default: **/*.html
A micromatch glob pattern to find HTML files.
Type: string[] Default: ['href', 'src', 'data-src', 'content']
An array of HTML attributes that link to files.
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