UNPKG

htmlnano

Version:

Modular HTML minifier, built on top of the PostHTML

79 lines (78 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractCssFromStyleNode = extractCssFromStyleNode; exports.isAmpBoilerplate = isAmpBoilerplate; exports.isComment = isComment; exports.isConditionalComment = isConditionalComment; exports.isEventHandler = isEventHandler; exports.isStyleNode = isStyleNode; exports.optionalImport = optionalImport; function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function __transformExtension(filepath, extMapping) { if (!filepath.startsWith('./') && !filepath.startsWith('../')) { // Package import return filepath; } const idx = filepath.lastIndexOf('.'); if (idx === -1 || filepath.includes('/', idx)) { // No extension const newExt = extMapping['']; if (newExt) { return filepath + newExt; } return filepath; } for (let [origExt, newExt] of Object.entries(extMapping).sort((a, b) => b[0].length - a[0].length)) { if (filepath.endsWith(origExt)) { return filepath.slice(0, -origExt.length) + newExt; } } return filepath; } const ampBoilerplateAttributes = ['amp-boilerplate', 'amp4ads-boilerplate', 'amp4email-boilerplate']; function isAmpBoilerplate(node) { if (!node.attrs) { return false; } for (const attr of ampBoilerplateAttributes) { if (attr in node.attrs) { return true; } } return false; } function isComment(content) { if (typeof content === 'string') { return content.trim().startsWith('<!--'); } return false; } function isConditionalComment(content) { const clean = (content || '').trim(); return clean.startsWith('<!--[if') || clean === '<!--<![endif]-->'; } function isStyleNode(node) { return node.tag === 'style' && !isAmpBoilerplate(node) && 'content' in node && node.content.length > 0; } function extractCssFromStyleNode(node) { return Array.isArray(node.content) ? node.content.join(' ') : node.content; } function isEventHandler(attributeName) { return attributeName && attributeName.slice && attributeName.slice(0, 2).toLowerCase() === 'on' && attributeName.length >= 5; } async function optionalImport(moduleName) { try { const module = await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(__transformExtension(moduleName, { ".mjs": ".cjs" })); return module.default || module; } catch (e) { if (e.code === 'MODULE_NOT_FOUND' || e.code === 'ERR_MODULE_NOT_FOUND') { return null; } throw e; } }