"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default, defaultOptions: () => defaultOptions, pxtorem: () => pxtorem }); module.exports = __toCommonJS(src_exports); // src/utils/constant.ts var MAYBE_REGEXP = ["selectorBlackList", "exclude", "include"]; var DISABLE_NEXT_COMMENT = "pxtorem-disable-next-line"; // src/utils/filter-prop-list.ts var filterPropList = { exact(list) { return list.filter((m) => m.match(/^[^*!]+$/)); }, contain(list) { return list.filter((m) => m.match(/^\*.+\*$/)).map((m) => m.substring(1, m.length - 1)); }, endWith(list) { return list.filter((m) => m.match(/^\*[^*]+$/)).map((m) => m.substring(1)); }, startWith(list) { return list.filter((m) => m.match(/^[^*!]+\*$/)).map((m) => m.substring(0, m.length - 1)); }, notExact(list) { return list.filter((m) => m.match(/^![^*].*$/)).map((m) => m.substring(1)); }, notContain(list) { return list.filter((m) => m.match(/^!\*.+\*$/)).map((m) => m.substring(2, m.length - 1)); }, notEndWith(list) { return list.filter((m) => m.match(/^!\*[^*]+$/)).map((m) => m.substring(2)); }, notStartWith(list) { return list.filter((m) => m.match(/^![^*]+\*$/)).map((m) => m.substring(1, m.length - 1)); } }; // node_modules/.pnpm/decode-uri-component@0.4.1/node_modules/decode-uri-component/index.js var token = "%[a-f0-9]{2}"; var singleMatcher = new RegExp("(" + token + ")|([^%]+?)", "gi"); var multiMatcher = new RegExp("(" + token + ")+", "gi"); function decodeComponents(components, split) { try { return [decodeURIComponent(components.join(""))]; } catch { } if (components.length === 1) { return components; } split = split || 1; const left = components.slice(0, split); const right = components.slice(split); return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right)); } function decode(input) { try { return decodeURIComponent(input); } catch { let tokens = input.match(singleMatcher) || []; for (let i = 1; i < tokens.length; i++) { input = decodeComponents(tokens, i).join(""); tokens = input.match(singleMatcher) || []; } return input; } } function customDecodeURIComponent(input) { const replaceMap = { "%FE%FF": "\uFFFD\uFFFD", "%FF%FE": "\uFFFD\uFFFD" }; let match = multiMatcher.exec(input); while (match) { try { replaceMap[match[0]] = decodeURIComponent(match[0]); } catch { const result = decode(match[0]); if (result !== match[0]) { replaceMap[match[0]] = result; } } match = multiMatcher.exec(input); } replaceMap["%C2"] = "\uFFFD"; const entries = Object.keys(replaceMap); for (const key of entries) { input = input.replace(new RegExp(key, "g"), replaceMap[key]); } return input; } function decodeUriComponent(encodedURI) { if (typeof encodedURI !== "string") { throw new TypeError("Expected `encodedURI` to be of type `string`, got `" + typeof encodedURI + "`"); } try { return decodeURIComponent(encodedURI); } catch { return customDecodeURIComponent(encodedURI); } } // node_modules/.pnpm/split-on-first@3.0.0/node_modules/split-on-first/index.js function splitOnFirst(string, separator) { if (!(typeof string === "string" && typeof separator === "string")) { throw new TypeError("Expected the arguments to be of type `string`"); } if (string === "" || separator === "") { return []; } const separatorIndex = string.indexOf(separator); if (separatorIndex === -1) { return []; } return [ string.slice(0, separatorIndex), string.slice(separatorIndex + separator.length) ]; } // src/utils/parse-query.ts function validateArrayFormatSeparator(value) { if (typeof value !== "string" || value.length !== 1) { throw new TypeError("arrayFormatSeparator must be single character string"); } } function parse(query, options) { options = { decode: true, sort: true, arrayFormat: "none", arrayFormatSeparator: ",", parseNumbers: false, parseBooleans: false, ...options }; validateArrayFormatSeparator(options.arrayFormatSeparator); const formatter = parserForArrayFormat(options); const returnValue = /* @__PURE__ */ Object.create(null); if (typeof query !== "string") { return returnValue; } query = query.trim().replace(/^[?#&]/, ""); if (!query) { return returnValue; } for (const parameter of query.split("&")) { if (parameter === "") { continue; } const parameter_ = options.decode ? parameter.replace(/\+/g, " ") : parameter; let [key, value] = splitOnFirst(parameter_, "="); if (key === void 0) { key = parameter_; } value = value === void 0 ? null : ["comma", "separator", "bracket-separator"].includes(options.arrayFormat) ? value : decode2(value, options); formatter(decode2(key, options), value, returnValue); } for (const [key, value] of Object.entries(returnValue)) { if (typeof value === "object" && value !== null) { for (const [key2, value2] of Object.entries(value)) { value[key2] = parseValue(value2, options); } } else { returnValue[key] = parseValue(value, options); } } if (options.sort === false) { return returnValue; } return (options.sort === true ? Object.keys(returnValue).sort() : Object.keys(returnValue).sort(options.sort)).reduce( (result, key) => { const value = returnValue[key]; if (Boolean(value) && typeof value === "object" && !Array.isArray(value)) { result[key] = keysSorter(value); } else { result[key] = value; } return result; }, /* @__PURE__ */ Object.create(null) ); } function parserForArrayFormat(options) { let result; switch (options.arrayFormat) { case "index": { return (key, value, accumulator) => { result = /\[(\d*)]$/.exec(key); key = key.replace(/\[\d*]$/, ""); if (!result) { accumulator[key] = value; return; } if (accumulator[key] === void 0) { accumulator[key] = {}; } accumulator[key][result[1]] = value; }; } case "bracket": { return (key, value, accumulator) => { result = /(\[])$/.exec(key); key = key.replace(/\[]$/, ""); if (!result) { accumulator[key] = value; return; } if (accumulator[key] === void 0) { accumulator[key] = [value]; return; } accumulator[key] = [...accumulator[key], value]; }; } case "colon-list-separator": { return (key, value, accumulator) => { result = /(:list)$/.exec(key); key = key.replace(/:list$/, ""); if (!result) { accumulator[key] = value; return; } if (accumulator[key] === void 0) { accumulator[key] = [value]; return; } accumulator[key] = [...accumulator[key], value]; }; } case "comma": case "separator": { return (key, value, accumulator) => { const isArray2 = typeof value === "string" && value.includes(options.arrayFormatSeparator); const isEncodedArray = typeof value === "string" && !isArray2 && decode2(value, options).includes(options.arrayFormatSeparator); value = isEncodedArray ? decode2(value, options) : value; const newValue = isArray2 || isEncodedArray ? value.split(options.arrayFormatSeparator).map((item) => decode2(item, options)) : value === null ? value : decode2(value, options); accumulator[key] = newValue; }; } case "bracket-separator": { return (key, value, accumulator) => { const isArray2 = /(\[])$/.test(key); key = key.replace(/\[]$/, ""); if (!isArray2) { accumulator[key] = value ? decode2(value, options) : value; return; } const arrayValue = value === null ? [] : value.split(options.arrayFormatSeparator).map((item) => decode2(item, options)); if (accumulator[key] === void 0) { accumulator[key] = arrayValue; return; } accumulator[key] = [...accumulator[key], ...arrayValue]; }; } default: { return (key, value, accumulator) => { if (accumulator[key] === void 0) { accumulator[key] = value; return; } accumulator[key] = [...[accumulator[key]].flat(), value]; }; } } } function decode2(value, options) { if (options.decode) { return decodeUriComponent(value); } return value; } function parseValue(value, options) { if (options.parseNumbers && !Number.isNaN(Number(value)) && typeof value === "string" && value.trim() !== "") { value = Number(value); } else if (options.parseBooleans && value !== null && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) { value = value.toLowerCase() === "true"; } return value; } function keysSorter(input) { if (Array.isArray(input)) { return input.sort(); } if (typeof input === "object") { return keysSorter(Object.keys(input)).sort((a, b) => Number(a) - Number(b)).map((key) => input[key]); } return input; } // src/utils/index.ts function reRegExp() { return /^\/((?:\\\/|[^\/])+)\/([imgy]*)$/; } function initOptions(options) { return Object.assign({}, defaultOptions, options); } function isOptionComment(node) { return node?.type === "comment"; } var processd = Symbol("processed"); function isRepeatRun(r) { if (!r) return false; if (r[processd]) { return true; } ; r[processd] = true; return false; } function parseRegExp(maybeRegExpArg) { const RE_REGEXP = reRegExp(); if (isString(maybeRegExpArg) && RE_REGEXP.test(maybeRegExpArg)) { return new RegExp(RE_REGEXP.exec(maybeRegExpArg)?.[1] || "", RE_REGEXP.exec(maybeRegExpArg)?.[2]); } return maybeRegExpArg; } var isPxtoremReg = /(?<=^pxtorem\?).+/g; function getOptionsFromComment(comment, parseOptions) { try { const index = comment.text.search(isPxtoremReg); const ret = {}; let query = comment.text.slice(index); if (!query || index === -1) return ret; query = query.replaceAll(/\s+/g, ""); const defaultKeys = Object.keys(defaultOptions); const parsed = parse(query, { parseBooleans: true, parseNumbers: true, arrayFormat: "bracket-separator", arrayFormatSeparator: "|", ...parseOptions }); const RE_REGEXP = reRegExp(); for (const k of Object.keys(parsed)) { if (defaultKeys.includes(k)) { let cur = parsed[k]; if (MAYBE_REGEXP.includes(k)) { if (Array.isArray(cur)) { cur = cur.map((t) => { return parseRegExp(t); }); } else { if (isString(cur) && RE_REGEXP.test(cur)) { cur = parseRegExp(cur); } } } ret[k] = cur; } } return ret; } catch { console.warn("Unexpected comment", { start: comment.source?.start, end: comment.source?.end }); } } function createPropListMatcher(propList) { const hasWild = propList.includes("*"); const matchAll = hasWild && propList.length === 1; const lists = { exact: filterPropList.exact(propList), contain: filterPropList.contain(propList), startWith: filterPropList.startWith(propList), endWith: filterPropList.endWith(propList), notExact: filterPropList.notExact(propList), notContain: filterPropList.notContain(propList), notStartWith: filterPropList.notStartWith(propList), notEndWith: filterPropList.notEndWith(propList) }; return function(prop) { if (matchAll) return true; return (hasWild || lists.exact.includes(prop) || lists.contain.some((m) => prop.includes(m)) || lists.startWith.some((m) => prop.indexOf(m) === 0) || lists.endWith.some((m) => prop.indexOf(m) === prop.length - m.length)) && !(lists.notExact.includes(prop) || lists.notContain.some((m) => prop.includes(m)) || lists.notStartWith.some((m) => prop.indexOf(m) === 0) || lists.notEndWith.some((m) => prop.indexOf(m) === prop.length - m.length)); }; } function toFixed(number, precision) { const multiplier = 10 ** (precision + 1); const wholeNumber = Math.floor(number * multiplier); return Math.round(wholeNumber / 10) * 10 / multiplier; } function createPxReplace(rootValue, unitPrecision, minPixelValue) { return (m, $1) => { if (!$1) return m; const pixels = parseFloat($1); if (pixels <= minPixelValue) return m; const fixedVal = toFixed(pixels / rootValue, unitPrecision); return fixedVal === 0 ? "0" : `${fixedVal}rem`; }; } function blacklistedSelector(blacklist, selector) { if (!isString(selector)) return; return blacklist.some((t) => { if (isString(t)) { return selector.includes(t); } return selector.match(t); }); } function declarationExists(decls, prop, value) { return decls.some((decl) => { return decl.prop === prop && decl.value === value; }); } function isXClude(Xclude, filePath) { return Xclude && filePath && (isFunction(Xclude) && Xclude(filePath) || isString(Xclude) && filePath.includes(Xclude) || isRegExp(Xclude) && filePath.match(Xclude)); } function judgeIsExclude(exclude, include, filePath) { if (include) { if (isXClude(include, filePath)) { return false; } return true; } if (isXClude(exclude, filePath)) { return true; } return false; } function convertUnit(value, convert) { if (typeof convert.sourceUnit === "string") { return value.replace(new RegExp(`${convert.sourceUnit}$`), convert.targetUnit); } else if (isRegExp(convert.sourceUnit)) { return value.replace(new RegExp(convert.sourceUnit), convert.targetUnit); } return value; } function checkIfDisable(p) { const { disable, isExcludeFile, r } = p; return disable || isExcludeFile || isRepeatRun(r); } var currentOptions = Symbol("currentOptions"); function setupCurrentOptions(h, { node, comment }) { const opts = h[currentOptions].originOpts; const filePath = node?.source?.input.file; if (isOptionComment(comment)) { h[currentOptions].originOpts = { ...opts, ...getOptionsFromComment(comment, opts.parseOptions) }; } const exclude = opts.exclude; const include = opts.include; h[currentOptions].isExcludeFile = judgeIsExclude(exclude, include, filePath); if (checkIfDisable({ disable: opts.disable, isExcludeFile: h[currentOptions].isExcludeFile })) { return; } h[currentOptions].rootValue = isFunction(opts.rootValue) ? opts.rootValue(node?.source.input) : opts.rootValue; h[currentOptions].pxReplace = createPxReplace(h[currentOptions].rootValue, opts.unitPrecision, opts.minPixelValue); } function is(val, type) { return Object.prototype.toString.call(val) === `[object ${type}]`; } function isString(data) { return is(data, "String" /* string */); } function isBoolean(data) { return is(data, "Boolean" /* boolean */); } function isArray(data) { return is(data, "Array" /* array */); } function isRegExp(data) { return is(data, "RegExp" /* regexp */); } function isFunction(data) { return is(data, "Function" /* function */); } // src/utils/pixel-unit-regex.ts function getUnitRegexp(unit) { return new RegExp(`"[^"]+"|'[^']+'|url\\([^\\)]+\\)|--[\\w-]+|(\\d*\\.?\\d+)${unit}`, "g"); } // src/index.ts var defaultOptions = { rootValue: 16, unitToConvert: "px", unitPrecision: 5, selectorBlackList: [], propList: ["*"], replace: true, atRules: false, minPixelValue: 0, include: null, exclude: null, disable: false, skipTag: null, convertUnitOnEnd: null, convertInMediaQuery: true, parseOptions: {} }; var postcssPlugin = "postcss-pxtorem"; function pxtorem(options) { const ORIGINAL_OPTIONS = initOptions(options); const plugin = { postcssPlugin, Once(r, h) { const node = r.root(); const firstNode = node.nodes[0]; h[currentOptions] = { isExcludeFile: false, pxReplace: void 0, rootValue: void 0, originOpts: ORIGINAL_OPTIONS }; setupCurrentOptions(h, { node, comment: firstNode }); }, Comment(node, h) { setupCurrentOptions(h, { node, comment: node }); }, CommentExit(comment) { if (comment.text.match(isPxtoremReg)?.length) { comment.remove(); } }, Declaration(decl, h) { const opts = h[currentOptions].originOpts; if (checkIfDisable({ disable: opts.disable, isExcludeFile: h[currentOptions].isExcludeFile, r: decl }) || opts.skipTag) { return; } const satisfyPropList = createPropListMatcher(opts.propList); if (!decl.value.includes(opts.unitToConvert) || !satisfyPropList(decl.prop) || blacklistedSelector(opts.selectorBlackList, decl.parent.selector)) { return; } const prev = decl.prev(); if (prev?.type === "comment" && prev.text === DISABLE_NEXT_COMMENT) { prev.remove(); return; } const pxRegex = getUnitRegexp(opts.unitToConvert); const value = h[currentOptions].pxReplace ? decl.value.replace(pxRegex, h[currentOptions].pxReplace) : decl.value; if (declarationExists(decl.parent, decl.prop, value)) return; if (opts.replace) { decl.value = value; } else { decl.cloneAfter({ value }); } }, DeclarationExit(decl, h) { const opts = h[currentOptions].originOpts; const { convertUnitOnEnd } = opts; if (convertUnitOnEnd) { if (Array.isArray(convertUnitOnEnd)) { convertUnitOnEnd.forEach((conv) => { decl.value = convertUnit(decl.value, conv); }); } else { decl.value = convertUnit(decl.value, convertUnitOnEnd); } } }, AtRule(atRule, h) { const opts = h[currentOptions].originOpts; if (!opts.convertInMediaQuery && atRule.name === "media") { opts.skipTag = "atRule:media"; } if (checkIfDisable({ disable: opts.disable, isExcludeFile: h[currentOptions].isExcludeFile, r: atRule })) { return; } function replacePxInRules() { if (!atRule.params.includes(opts.unitToConvert)) return; const pxRegex = getUnitRegexp(opts.unitToConvert); atRule.params = h[currentOptions].pxReplace ? atRule.params.replace(pxRegex, h[currentOptions].pxReplace) : atRule.params; } if (isBoolean(opts.atRules) && opts.atRules) { replacePxInRules(); } if (isArray(opts.atRules) && opts.atRules.length) { if (opts.atRules.includes(atRule.name)) { replacePxInRules(); } } }, AtRuleExit(atRule, h) { const opts = h[currentOptions].originOpts; if (!opts.convertInMediaQuery && atRule.name === "media" && opts.skipTag === "atRule:media") { opts.skipTag = null; } } }; if (options?.disable) { return { postcssPlugin }; } return plugin; } pxtorem.postcss = true; var src_default = pxtorem; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { defaultOptions, pxtorem }); module.exports = module.exports.default;