This plugin removes duplicated tags. It can be used to remove duplicated css and script tags when your markup is assembling code from different sources.
Before:
<html>
<body>
<link rel="stylesheet" type="text/css" href="component.css">
<div>... component...</div>
<link rel="stylesheet" type="text/css" href="component.css">
<div>... component...</div>
</body>
</html>
After:
<html>
<body>
<link rel="stylesheet" type="text/css" href="component.css">
<div>... component...</div>
<div>... component...</div>
</body>
</html>
npm i posthtml posthtml-plugin-remove-duplicates
const fs = require('fs');
const posthtml = require('posthtml');
const posthtmlPluginRemoveDuplicates = require('posthtml-plugin-remove-duplicates');
posthtml()
.use(posthtmlPluginRemoveDuplicates({ css: true, js: true }))
.process(html)
.then(result => fs.writeFileSync('./after.html', result.html));
- css: remove duplicated css tags (default false)
- js: remove duplicated script tags (default false)
- custom: object or array of objects, with tag (optional) and attribute (mandatory)
{ tag: 'div', attr: 'data-remove-dup' }