"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // lib/index.ts var lib_exports = {}; __export(lib_exports, { defaultGenerateSourceMap: () => defaultGenerateSourceMap, defaultShouldMinify: () => defaultShouldMinify, defaultShouldMinifyCSS: () => defaultShouldMinifyCSS, defaultValidation: () => defaultValidation, minifyHTMLLiterals: () => minifyHTMLLiterals }); module.exports = __toCommonJS(lib_exports); var import_magic_string = __toESM(require("magic-string"), 1); var import_parse_literals = require("parse-literals"); // lib/strategy.ts var import_clean_css = __toESM(require("clean-css"), 1); var import_html_minifier_terser = require("html-minifier-terser"); var defaultMinifyCSSOptions = {}; var defaultMinifyOptions = { caseSensitive: true, collapseWhitespace: true, decodeEntities: true, minifyCSS: defaultMinifyCSSOptions, minifyJS: true, processConditionalComments: true, removeAttributeQuotes: false, removeComments: true, removeEmptyAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, useShortDoctype: true }; var defaultStrategy = { getPlaceholder(parts) { const suffix = "();"; let placeholder = "@TEMPLATE_EXPRESSION"; while (parts.some((part) => part.text.includes(placeholder + suffix))) { placeholder += "_"; } return placeholder + suffix; }, combineHTMLStrings(parts, placeholder) { return parts.map((part) => part.text).join(placeholder); }, async minifyHTML(html, options = {}) { let minifyCSSOptions; if (html.match(//g)) { console.warn( "minify-literals: HTML minification is not supported for template expressions inside comments. Minification for this file will be skipped." ); return html; } html = html.replaceAll("<@TEMPLATE_EXPRESSION();", "= 0; i--) { parts.splice(i, 1, ...parts[i]?.split(withoutSemicolon) ?? []); } } return parts; } }; function adjustMinifyCSSOptions(options = {}) { const level = options.level; const plugin = { level1: { value: (_name, value) => { if (!value.startsWith("@TEMPLATE_EXPRESSION") || value.endsWith(";")) return value; return `${value};`; } } }; return { ...options, level, plugins: [plugin] }; } function fixCleanCssTidySelectors(original, result) { const regex = /(::?.+\((.*)\))[\s\r\n]*{/gm; let match; while ((match = regex.exec(original)) != null) { const pseudoClass = match[1] ?? ""; const parameters = match[2]; if (!parameters?.match(/\s/)) { continue; } const parametersWithoutSpaces = parameters.replace(/\s/g, ""); const resultPseudoClass = pseudoClass.replace(parameters, parametersWithoutSpaces); const resultStartIndex = result.indexOf(resultPseudoClass); if (resultStartIndex < 0) { continue; } const resultEndIndex = resultStartIndex + resultPseudoClass.length; result = result.substring(0, resultStartIndex) + pseudoClass + result.substring(resultEndIndex); } return result; } // lib/index.ts function defaultGenerateSourceMap(ms, fileName) { return ms.generateMap({ file: `${fileName}.map`, source: fileName, hires: true }); } function defaultShouldMinify(template) { const tag = template.tag?.toLowerCase(); return !!tag && (tag.includes("html") || tag.includes("svg")); } function defaultShouldMinifyCSS(template) { if (!template?.tag?.toLowerCase().includes("css")) return false; return true; } var defaultValidation = { ensurePlaceholderValid(placeholder) { if (typeof placeholder !== "string" || !placeholder.length) { throw new Error("getPlaceholder() must return a non-empty string"); } }, ensureHTMLPartsValid(parts, htmlParts) { if (parts.length !== htmlParts.length) { throw new Error("splitHTMLByPlaceholder() must return same number of strings as template parts"); } } }; async function minifyHTMLLiterals(source, options = {}) { options.MagicString = options.MagicString || import_magic_string.default; options.parseLiterals = options.parseLiterals || import_parse_literals.parseLiterals; options.shouldMinify = options.shouldMinify || defaultShouldMinify; options.shouldMinifyCSS = options.shouldMinifyCSS || defaultShouldMinifyCSS; options.minifyOptions = { ...defaultMinifyOptions, ...options.minifyOptions }; options.parseLiteralsOptions = { fileName: options.fileName, ...options.parseLiteralsOptions }; const templates = options.parseLiterals(source, options.parseLiteralsOptions); const strategy = options.strategy || defaultStrategy; const { shouldMinify, shouldMinifyCSS } = options; let validate; if (options.validate !== false) { validate = options.validate || defaultValidation; } let skipCSS = false; let skipHTML = false; if (strategy.minifyCSS && source.includes("unsafeCSS")) { console.warn( `minify-literals: unsafeCSS() detected in source. CSS minification will not be performed for this file.` ); skipCSS = true; } if (source.includes("unsafeHTML")) { console.warn( `minify-literals: unsafeHTML() detected in source. HTML minification will not be performed for this file.` ); skipHTML = true; } if (!options.MagicString) throw new Error("MagicString is required, this should never happen"); const ms = new options.MagicString(source); const promises = templates.map(async (template) => { const minifyHTML = !skipHTML && shouldMinify(template); const minifyCSS = !skipCSS && strategy.minifyCSS && shouldMinifyCSS(template); if (!(minifyHTML || minifyCSS)) return; const placeholder = strategy.getPlaceholder(template.parts); if (validate) { validate.ensurePlaceholderValid(placeholder); } const combined = strategy.combineHTMLStrings(template.parts, placeholder); let min; if (minifyCSS) { const minifyCSSOptions = options.minifyOptions?.minifyCSS; if (typeof minifyCSSOptions === "function") { min = minifyCSSOptions(combined); } else if (minifyCSSOptions === false) { min = combined; } else { const cssOptions = typeof minifyCSSOptions === "object" ? minifyCSSOptions : void 0; min = await strategy.minifyCSS?.(combined, cssOptions) ?? combined; } } else { min = await strategy.minifyHTML(combined, options.minifyOptions); } const minParts = strategy.splitHTMLByPlaceholder(min, placeholder); if (validate) validate.ensureHTMLPartsValid(template.parts, minParts); for (const [index, part] of template.parts.entries()) { if (part.start < part.end) ms.overwrite(part.start, part.end, minParts[index] ?? ""); } }); await Promise.all(promises); const sourceMin = ms.toString(); if (source === sourceMin) return null; let map; if (options.generateSourceMap !== false) { const generateSourceMap = options.generateSourceMap || defaultGenerateSourceMap; map = generateSourceMap(ms, options.fileName || ""); } return { map, code: sourceMin }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { defaultGenerateSourceMap, defaultShouldMinify, defaultShouldMinifyCSS, defaultValidation, minifyHTMLLiterals }); //# sourceMappingURL=index.cjs.map