minifyHtml.js 492 B

12345678910111213141516171819
  1. 'use strict';
  2. const minify = require('html-minifier').minify;
  3. module.exports = function (data) {
  4. data = String(data);
  5. const fragments = [];
  6. const html = data.replace(/<%[\s\S]*?%>/g, function (match) {
  7. fragments.push(match);
  8. return `__frag_${fragments.length - 1}__`;
  9. });
  10. return minify(html, {
  11. removeComments: true,
  12. collapseWhitespace: true,
  13. conservativeCollapse: true,
  14. }).replace(/__frag_(\d+)__/g, function (match, id) {
  15. return fragments[id];
  16. });
  17. };