usemin
Version:
Replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views).
30 lines (23 loc) • 960 B
JavaScript
;
var HTMLMinifier = require('html-minifier').minify;
module.exports = function (content, blocks, htmlmin, config) {
var linefeed = /\r\n/g.test(content) ? '\r\n' : '\n';
blocks.forEach(function (block) {
var blockLines = block.raw.join(linefeed);
if (block.type === 'js') {
var defer = block.defer ? 'defer ' : '';
var async = block.async ? 'async ' : '';
content = content.replace(blockLines, block.indent + '<script ' + defer + async + 'src="' + block.dest + '"></script>');
} else if (block.type === 'css') {
content = content.replace(blockLines, block.indent + '<link rel="stylesheet" href="' + block.dest + '">');
} else if (block.type === 'livereload') {
content = content.replace(blockLines + linefeed, '');
} else if (block.type === 'remove') {
content = content.replace(blockLines + linefeed, '');
}
});
if (htmlmin) {
content = HTMLMinifier(content, config.htmlminifier);
}
return content;
};