metalsmith-html-unused
v2.0.3
Published
A Metalsmith plugin to exclude files unused in HTML.
Downloads
65
Maintainers
Readme
metalsmith-html-unused
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
import Metalsmith from 'metalsmith';
import unused from '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