"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); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default, unplugin: () => unplugin, unpluginFactory: () => unpluginFactory }); module.exports = __toCommonJS(src_exports); var import_pathe = __toESM(require("pathe"), 1); var import_unplugin = require("unplugin"); // src/constants.ts var PLUGIN_NAME = "unplugin-polish-tagged-templates"; var PLUGIN_ABBR = "uptt"; var IS_DEV = process.env.NODE_ENV === "development"; // src/presets/cls.ts function polishClsString(str) { return str.split("\n").map((item) => item.trim()).filter((item) => { return !item.startsWith("//"); }).map((item) => { return item.replace(/[ \t]{2,}/g, " "); }).join(" "); } // src/helpers/transform/ast/index.ts var import_gogocode = __toESM(require("gogocode"), 1); // src/log.ts var import_consola = __toESM(require("consola"), 1); var logger = import_consola.default.withTag(PLUGIN_ABBR).withDefaults({ level: 3 }); // src/helpers/transform/ast/index.ts var transformByAst = (code, polishTags, options = {}) => { const { beforeTransform } = options; const ast = (0, import_gogocode.default)(code); const targetTagNodes = ast.find( polishTags.map((item) => { return `${item.tag}\`$_$str\``; }) ); if (!targetTagNodes.length) { return code; } beforeTransform == null ? void 0 : beforeTransform(); logger.debug("With", targetTagNodes.length, "tag(s)"); const polishCode = targetTagNodes.each((node) => { var _a, _b; if (node.match.str.length > 1) { return; } const tag = node.attr("tag.name"); if (typeof tag !== "string") { return; } const polishCallback = (_a = polishTags.find((item) => item.tag === tag)) == null ? void 0 : _a.polish; if (!polishCallback) { return; } const str = node.match.str[0].value; if (str.includes('"')) { return; } const polishedStr = ((_b = polishCallback(str)) == null ? void 0 : _b.trim()) || ""; node.replaceBy(`"${polishedStr}"`); }).root().generate(); return polishCode; }; // src/helpers/transform/magic-string/index.ts var import_magic_string = __toESM(require("magic-string"), 1); // src/helpers/transform/magic-string/helpers.ts var templateRegExp = new RegExp("(? item.includes("`")); } // src/helpers/transform/magic-string/index.ts var transformByMagicString = (code, polishTags, options = {}) => { const { beforeTransform } = options; const ms = new import_magic_string.default(code); let transformed = false; const tagNames = polishTags.map((item) => item.tag); ms.replace(getTaggedTemplatesRegExp(tagNames), (raw, tag, str) => { var _a; const polishCallback = (_a = polishTags.find((item) => item.tag === tag)) == null ? void 0 : _a.polish; if (!polishCallback) { return raw; } if (templateStringContainsExpression(str)) { return raw; } if (str.includes('"')) { return raw; } if (!transformed) { beforeTransform == null ? void 0 : beforeTransform(); transformed = true; } const polishedStr = polishCallback(str.trim()); const result = typeof polishedStr === "string" ? `"${polishedStr}"` : raw; return result; }); return ms.hasChanged() ? ms.toString() : code; }; // src/helpers/transform/index.ts function transformTags(code, options = {}) { const { clsTags = [], polishTags = [], beforeTransform, processor = "auto" } = options; const mergedPolishTags = [ ...clsTags.map((tag) => { return { tag, polish: polishClsString }; }), ...polishTags ]; const tagNames = mergedPolishTags.map((item) => item.tag); if (!containsTaggedTemplate(tagNames, code)) { return; } const processorType = processor === "auto" ? checkTemplateNested(code) ? "ast" : "string" : processor; if (processorType === "ast") { return { type: processorType, code: transformByAst(code, mergedPolishTags, { beforeTransform }) }; } return { type: processorType, code: transformByMagicString(code, mergedPolishTags, { beforeTransform }) }; } // src/index.ts var unpluginFactory = (options) => { const { extensions: moreExtensions = [], clsTags = [], polishTags = [], debug = false, exclude, processor } = options || {}; if (debug) { logger.level = 4; } return { name: PLUGIN_NAME, /** In next.js without the config, the plugin can not transform origin source code */ enforce: "pre", transformInclude(id) { if (IS_DEV) { return false; } const normalizedId = import_pathe.default.normalize(id); if (normalizedId.includes("node_modules")) { return false; } if (exclude && exclude(normalizedId)) { return false; } const extensions = ["ts", "tsx", "js", "jsx", ...moreExtensions]; return extensions.some((item) => id.endsWith(`.${item}`)); }, transform(code, id) { const normalizedId = import_pathe.default.normalize(id); try { const polishResult = transformTags(code, { clsTags, polishTags, beforeTransform: () => { logger.debug("Transform", normalizedId); }, processor: processor ? processor(normalizedId) : "auto" }); if (!polishResult) { logger.debug("No transform required", normalizedId); return code; } logger.debug("Transformed by", polishResult.type); return polishResult.code; } catch (err) { logger.warn("Transform failed", normalizedId); } return code; } }; }; var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory); var src_default = unplugin; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { unplugin, unpluginFactory });