'use strict'; function _defu(baseObject, defaults, namespace = ".", merger) { if (!_isPlainObject(defaults)) { return _defu(baseObject, {}, namespace, merger); } const object = Object.assign({}, defaults); for (const key in baseObject) { if (key === "__proto__" || key === "constructor") { continue; } const value = baseObject[key]; if (value === null || value === void 0) { continue; } if (merger && merger(object, key, value, namespace)) { continue; } if (Array.isArray(value) && Array.isArray(object[key])) { object[key] = [...value, ...object[key]]; } else if (_isPlainObject(value) && _isPlainObject(object[key])) { object[key] = _defu( value, object[key], (namespace ? `${namespace}.` : "") + key.toString(), merger ); } else { object[key] = value; } } return object; } function _isPlainObject(value) { if (value === null || typeof value !== "object") { return false; } const prototype = Object.getPrototypeOf(value); return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value); } function createDefu(merger) { return (...arguments_) => ( // eslint-disable-next-line unicorn/no-array-reduce arguments_.reduce((p, c) => _defu(p, c, "", merger), {}) ); } const defu = createDefu(); const PropResolvedMarkSymbol = Symbol("PropResolved"); const ValueResolvedMarkSymbol = Symbol("ValueResolved"); function matchCustomPropertyFromValue(str, cb) { let arr; let index = 0; const regex = /var\(\s*(--\w[\w-]*)/g; while ((arr = regex.exec(str)) !== null) { cb(arr, index); index++; } } function makeCustomProperty(customProperty, prefix) { return `--${prefix}${customProperty.slice(2)}`; } const postcssPlugin = "postcss-custom-property-prefixer"; function makePrefixFunction(prefix) { return typeof prefix === "string" ? () => { return prefix; } : prefix; } const postcssCustomPropertyPrefixer = (options) => { const { ignoreProp, ignoreValue, prefix: _prefix, transformProp, transformValue, ignoreDecl, ignoreValueCustomProperty, propPrefix: _propPrefix } = defu(options, { ignoreProp: () => false, ignoreValue: () => false, ignoreValueCustomProperty: () => false, ignoreDecl: () => false, transformProp: true, transformValue: true }); const prefix = makePrefixFunction(_prefix); const propPrefix = _propPrefix === void 0 ? prefix : makePrefixFunction(_propPrefix); const PropResolvedWeakmap = /* @__PURE__ */ new WeakMap(); const ValueResolvedWeakmap = /* @__PURE__ */ new WeakMap(); function getPropResolvedMark(decl) { return PropResolvedWeakmap.get(decl) === PropResolvedMarkSymbol; } function addPropResolvedMark(decl) { PropResolvedWeakmap.set(decl, PropResolvedMarkSymbol); } function getValueResolvedMark(decl) { return ValueResolvedWeakmap.get(decl) === ValueResolvedMarkSymbol; } function addValueResolvedMark(decl) { ValueResolvedWeakmap.set(decl, ValueResolvedMarkSymbol); } return { postcssPlugin, prepare() { return { DeclarationExit(decl) { if (ignoreDecl(decl)) { return; } if (transformProp && decl.prop.startsWith("--") && !ignoreProp(decl) && !getPropResolvedMark(decl)) { decl.prop = makeCustomProperty(decl.prop, propPrefix(decl, "prop")); addPropResolvedMark(decl); } if (transformValue && !ignoreValue(decl) && !getValueResolvedMark(decl)) { let value = decl.value; matchCustomPropertyFromValue(decl.value, (arr) => { const customProperty = arr[1]; if (ignoreValueCustomProperty(customProperty, decl)) { return; } value = value.replaceAll(customProperty, makeCustomProperty(customProperty, prefix(decl, "value"))); }); decl.value = value; addValueResolvedMark(decl); } } }; } }; }; postcssCustomPropertyPrefixer.postcss = true; module.exports = postcssCustomPropertyPrefixer;