@barocss/browser 0.8.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdn/barocss.js +2 -10056
- package/dist/cdn/barocss.js.map +1 -1
- package/dist/cdn/barocss.umd.cjs +2 -2
- package/dist/cdn/barocss.umd.cjs.map +1 -1
- package/dist/index.cjs +9 -0
- package/dist/index.d.cts +435 -0
- package/dist/index.es.js +582 -756
- package/dist/index.umd.js +6 -9
- package/package.json +12 -6
package/dist/index.umd.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@barocss/kit")):typeof define==`function`&&define.amd?define([`exports`,`@barocss/kit`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.BaroCSSBrowser={},e.BaroCSSKit))})(this,function(e,t){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var n=class{insertSorted(e,n,r){let i=e.keys??=[],a=(0,t.upperBound)(i,r),o=e.styleElement.sheet;o&&o.cssRules.length===i.length?(o.insertRule(this.escapeCssRule(n),a),e.styles.splice(a,0,n)):(e.styles.splice(a,0,n),e.styleElement.textContent=e.styles.join(`
|
|
2
2
|
`)+`
|
|
3
|
-
`),i.splice(r,0,
|
|
4
|
-
`}catch(
|
|
5
|
-
`)}`,
|
|
3
|
+
`),i.splice(a,0,r)}constructor(e,n=50,r=`barocss-style-partition-`,i=e=>(0,t.parseClassName)(e).utility?.category){this.partitions=[],this.categoryPartitions=new Map,this.partitionCounter=0,this.maxRulesPerPartition=50,this.classToPartitionMap=new Map,this.classToCategoryPartitionMap=new Map,this.styleIdPrefix=`barocss-style-partition-`,this.insertionPoint=e,this.maxRulesPerPartition=n,this.styleIdPrefix=r,this.getCategory=i,this.initializeDefaultPartition()}initializeDefaultPartition(){this.createNewPartition()}createNewCategoryPartition(e,t=!1){let n={id:this.styleIdPrefix+`-${e}`,styles:[],styleElement:document.createElement(`style`)};n.styleElement.id=n.id,n.styleElement.setAttribute(`data-barocss`,`partition`),n.styleElement.setAttribute(`data-category`,e);let r=this.insertionPoint.ownerDocument?.head;return t&&r?r.insertBefore(n.styleElement,r.firstChild):this.insertionPoint.appendChild(n.styleElement),this.categoryPartitions.set(e,n),n}createNewPartition(){let e={id:this.styleIdPrefix+`-${this.partitionCounter++}`,styles:[],styleElement:document.createElement(`style`)};return this.partitions.push(e),e.styleElement.id=e.id,e.styleElement.setAttribute(`data-barocss`,`partition`),e.styleElement.setAttribute(`data-partition-index`,this.partitionCounter.toString()),this.insertionPoint.appendChild(e.styleElement),e}hasRule(e){return this.classToPartitionMap.has(e)}hasCategoryRule(e,t){return this.classToCategoryPartitionMap.get(e)===t}setRuleCache(e,t){this.classToPartitionMap.set(e,t)}setCategoryRuleCache(e,t){this.classToCategoryPartitionMap.set(e,t)}get currentPartition(){return this.partitions[this.partitions.length-1]}getCategoryPartition(e){return this.categoryPartitions.get(e)}hasDetachedPartitions(){return[...this.partitions,...this.categoryPartitions.values()].some(e=>!e.styleElement.isConnected)}escapeCssRule(e){return e.replace(/\\\//g,`\\/`)}addRule(e){if(this.hasRule(e))return!1;let n=(0,t.ruleSortKey)(e),r=this.partitions.findIndex(e=>{let r=e.keys;return!!r&&r.length>0&&(0,t.compareKeys)(r[r.length-1],n)>0});r===-1&&(this.currentPartition.styles.length>=this.maxRulesPerPartition&&this.createNewPartition(),r=this.partitions.length-1);let i=this.partitions[r];try{return this.insertSorted(i,e,n),this.setRuleCache(e,r),!0}catch(n){return(0,t.isDebug)()&&console.warn(`[StylePartitionManager] Failed to insert rule: ${e}`,n),!1}}addCategoryRule(e,n){if(this.hasCategoryRule(e,n))return!1;let r=this.getCategoryPartition(n);r||=this.createNewCategoryPartition(n);try{this.insertSorted(r,e,(0,t.ruleSortKey)(e))}catch(r){return(0,t.isDebug)()&&console.warn(`[StylePartitionManager] Failed to insert rule in category: ${n} ${e}`,r),!1}return this.setCategoryRuleCache(e,n),!0}addRootRules(e){let n=this.getCategoryPartition(`root`);n||=this.createNewCategoryPartition(`root`);try{let t=n.styleElement.sheet;for(let r of e)t?t.insertRule(this.escapeCssRule(r),t.cssRules.length):n.styleElement.textContent+=r+`
|
|
4
|
+
`}catch(n){return(0,t.isDebug)()&&console.warn(`[StylePartitionManager] Failed to insert rule in category: root ${e.join(`
|
|
5
|
+
`)}`,n),{success:0,failed:e.length}}return{success:e.length,failed:0}}addRules(e){let t=0,n=0;for(let r of e){let e=this.getCategory(r.cls);if(e)for(let t of r.cssList)this.addCategoryRule(t,e);else for(let e of r.cssList)this.addRule(e)?t++:n++}return{success:t,failed:n}}removeRule(e,t){let n;if(t){if(this.classToCategoryPartitionMap.get(e)!==t)return!1;n=this.categoryPartitions.get(t)}else{let t=this.classToPartitionMap.get(e);n=t===void 0?void 0:this.partitions[t]}if(!n)return!1;let r=n.styles.indexOf(e);if(r===-1)return!1;let i=n.styleElement.sheet,a=!!i&&i.cssRules.length===n.styles.length;return n.styles.splice(r,1),n.keys?.splice(r,1),a&&i?i.deleteRule(r):n.styleElement.textContent=n.styles.length?n.styles.join(`
|
|
6
6
|
`)+`
|
|
7
|
-
`:"",e?this.classToCategoryPartitionMap.delete(t):this.classToPartitionMap.delete(t),!0}get ruleCount(){return this.classToPartitionMap.size+this.classToCategoryPartitionMap.size}findRulePartition(t){const e=this.classToPartitionMap.get(t);if(e!==void 0&&this.partitions[e])return this.partitions[e];const s=this.classToCategoryPartitionMap.get(t);return s!==void 0&&this.categoryPartitions.get(s)||null}updateRuleContent(t,e,s=!1){const i=this.getCategoryPartition(t);if(i)i.styleElement.textContent=e;else{const r=this.createNewCategoryPartition(t,s);console.log(`[StylePartitionManager] Created new partition for category: ${t}`),r.styleElement.textContent=e}}cleanup(){this.partitions.forEach(t=>{t.styleElement.parentNode&&t.styleElement.parentNode.removeChild(t.styleElement)}),this.categoryPartitions.forEach(t=>{t.styleElement.parentNode&&t.styleElement.parentNode.removeChild(t.styleElement)}),this.partitions=[],this.categoryPartitions.clear(),this.partitionCounter=0,this.classToPartitionMap.clear(),this.classToCategoryPartitionMap.clear()}}function C(a){return a?typeof a=="object"&&typeof a.baseVal=="string"?a.baseVal:a.toString():""}function d(a){return a?C(a).split(/\s+/).filter(Boolean):[]}class P{constructor(t,e,s=i=>h.parseClassName(i).utility?.category){this.observer=null,this.gc=null,this.incrementalParser=t,this.BrowserRuntime=e,this.getCategory=s}setGc(t){this.gc=t}setParser(t){this.incrementalParser=t}observe(t=document.body,e){return typeof window>"u"?new MutationObserver(()=>{}):(this.observer&&this.observer.disconnect(),this.gc?.setRoot(t),this.observer=new MutationObserver(s=>{const i=new Set,r=this.gc;if(s.forEach(n=>{if(r&&(n.type==="attributes"?r.reconcile(n.target):n.type==="childList"&&(n.removedNodes.forEach(o=>r.reconcileTree(o)),n.addedNodes.forEach(o=>r.reconcileTree(o)))),n.type==="attributes"&&n.attributeName==="class"&&t.contains(n.target)){const o=n.target;o.className&&d(o.className).forEach(u=>{this.incrementalParser.isProcessed(u)||i.add(u)})}n.type==="childList"&&n.addedNodes.forEach(o=>{if(o.nodeType===Node.ELEMENT_NODE&&t.contains(o)){const c=o;this.processElement(c,i),c.querySelectorAll("[class]").forEach(u=>{this.processElement(u,i)})}})}),i.size>0){const n=Array.from(i),o=this.incrementalParser.processClasses(n);this.BrowserRuntime?.applyParseResults(o)}else this.BrowserRuntime?.applyParseResults([]);r?.afterBatch()}),this.observer.observe(t,{attributes:!0,subtree:!0,attributeFilter:["class"],childList:!0}),e?.scan&&this.scanExistingClasses(t,e),this.observer)}scanExistingClasses(t,e){const s=new Set;t.className&&d(t.className).forEach(n=>{this.incrementalParser.isProcessed(n)||s.add(n)});const i=t.querySelectorAll("[class]");for(const r of i)if(r.className){const n=d(r.className);for(const o of n)this.incrementalParser.isProcessed(o)||s.add(o)}if(s.size>0){const r=Array.from(s),n=this.incrementalParser.processClasses(r),o=n.filter(u=>this.getCategory(u.cls)==="layout"),c=n.filter(u=>this.getCategory(u.cls)!=="layout");this.BrowserRuntime?.applyParseResults(o),e?.onReady?.(),this.BrowserRuntime?.applyParseResults(c)}else e?.onReady?.()}processElement(t,e){t.className&&d(t.className).forEach(i=>{this.incrementalParser.isProcessed(i)||e.add(i)})}disconnect(){this.observer&&(this.observer.disconnect(),this.observer=null)}}function M(a){return a.replace(/\\([0-9a-fA-F]{1,6})\s?|\\(.)/g,(t,e,s)=>e?String.fromCodePoint(parseInt(e,16)):s)}const v=/^\s*(?::(?:where|is)\(\s*)?\.((?:\\[0-9a-fA-F]{1,6}\s?|\\.|[\w-]|[^\x00-\x7F])+)/;function A(a){const t=[];let e=0,s=0;for(let i=0;i<a.length;i++){const r=a[i];r==="\\"?i++:r==="("||r==="["?e++:r===")"||r==="]"?e--:r===","&&e===0&&(t.push(a.slice(s,i)),s=i+1)}return t.push(a.slice(s)),t}function R(a,t=new Set){for(const e of Array.from(a)){if(e.type===7&&typeof e.name=="string"){t.add(e.name);continue}const s=e.cssRules;s&&s.length&&R(s,t)}return t}function y(a,t=new Set){for(const e of Array.from(a)){const s=e.selectorText;if(typeof s=="string")for(const r of A(s)){const n=v.exec(r);n&&t.add(M(n[1]))}const i=e.cssRules;i&&i.length&&y(i,t)}return t}class T{constructor(t,e,s,i=()=>Date.now()){this.host=t,this.graceMs=e,this.maxRules=s,this.now=i,this.counts=new Map,this.counted=new WeakMap,this.candidates=new Map,this.timer=null,this.root=null}setRoot(t){this.counts.clear(),this.counted=new WeakMap,this.candidates.clear(),this.cancel(),this.root=t,this.reconcileTree(t)}count(t){return this.counts.get(t)??0}reconcileTree(t){if(t.nodeType!==1)return;const e=t;this.reconcile(e),e.querySelectorAll("[class]").forEach(s=>this.reconcile(s))}reconcile(t){const e=this.root,i=!!e&&e.contains(t)?Array.from(new Set(d(t.getAttribute("class")))):[],r=this.counted.get(t);if(!r&&i.length===0)return;const n=new Set(r??[]),o=new Set(i);for(const c of o){if(n.has(c))continue;const u=(this.counts.get(c)??0)+1;this.counts.set(c,u),this.candidates.delete(c)}for(const c of n){if(o.has(c))continue;const u=(this.counts.get(c)??0)-1;u>0?this.counts.set(c,u):(this.counts.delete(c),this.candidates.delete(c),this.candidates.set(c,this.now()))}i.length?this.counted.set(t,i):this.counted.delete(t)}afterBatch(){this.candidates.size!==0&&(this.host.cachedCount()>this.maxRules?this.schedule(0):this.schedule(this.graceMs))}schedule(t){if(this.timer!==null){if(t>0)return;clearTimeout(this.timer)}this.timer=setTimeout(()=>{this.timer=null,this.sweep()},t)}sweep(){const t=this.now(),e=Math.max(0,this.host.cachedCount()-this.maxRules),s=[];let i=0;for(const[r,n]of this.candidates){const o=t-n>=this.graceMs;!o&&i>=e||(this.candidates.delete(r),!(this.count(r)>0||this.inDom(r)||this.host.isPermanent(r))&&(s.push(r),o||i++))}s.length&&this.host.reclaim(s),this.candidates.size&&this.schedule(this.graceMs)}inDom(t){const e=this.root;if(!e)return!1;const s=e.ownerDocument??document;return e.classList.contains(t)||s.documentElement.classList.contains(t)||s.getElementsByClassName(t).length>0}cancel(){this.timer!==null&&clearTimeout(this.timer),this.timer=null}stats(){return{trackedClasses:this.counts.size,candidates:this.candidates.size}}}const S="style[data-barocss-ssr]",b="@layer theme, base, components, utilities;";class w{constructor(t={}){this.cache=new Map,this.rootCache=new Set,this.isDestroyed=!1,this.existing=null,this.existingKeyframes=new Set,this.existingSheetCount=-1,this.pinned=new Set,this.gc=null,this.reclaimedCount=0,this.ssrRules=[],this.ssrClasses=new Set,this.observedOnce=!1,this.getCategory=s=>h.parseClassName(s,this.context).utility?.category;const e={};this.options={config:t.config||e,styleId:t.styleId||"barocss-runtime",insertionPoint:t.insertionPoint||"head",maxRulesPerPartition:t.maxRulesPerPartition||50,skipExisting:t.skipExisting??!1,gc:t.gc??!0,gcGraceMs:t.gcGraceMs??3e3,maxRules:t.maxRules??1/0},this.context=h.createContext(this.options.config),this.incrementalParser=new h.IncrementalParser(this.context),this.changeDetector=new P(this.incrementalParser,this,this.getCategory),this.stylePartitionManager=new g(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.options.gc&&(this.gc=new T({reclaim:s=>this.reclaim(s),isPermanent:s=>this.isPermanent(s),cachedCount:()=>this.cache.size},this.options.gcGraceMs,this.options.maxRules),this.changeDetector.setGc(this.gc)),this.init()}debugLog(t,e,s){(console[t]||console.log)(`[BrowserRuntime:${t.toUpperCase()}] ${e}`,s||"")}init(){console.log("[BrowserRuntime] init"),this.injectPreflightCSS(),this.ensureCssVars(),this.adoptSsrSheets()}adoptSsrSheets(){if(typeof document>"u")return;const t=[];if(document.head){for(const e of Array.from(document.head.querySelectorAll(`${S}:not([data-barocss-adopted])`))){const s=e.sheet;if(!s)continue;e.setAttribute("data-barocss-adopted","");const i=[];for(let r=s.cssRules.length-1;r>=0;r--){const n=s.cssRules[r],o=y([n]);o.size!==0&&(o.forEach(c=>this.ssrClasses.add(c)),i.unshift({css:n.cssText,cls:o.values().next().value}),s.deleteRule(r))}t.push(...i)}t.length!==0&&(this.ssrRules.push(...t),this.insertSsrRules(t))}}insertSsrRules(t){for(const{css:e,cls:s}of t){const i=this.getCategory(s);i?this.stylePartitionManager.addCategoryRule(e,i):this.stylePartitionManager.addRule(e)}}injectPreflightCSS(){const t=this.options.config.preflight??!0;if(t){const e=this.context.getPreflightCSS(t);this.stylePartitionManager.updateRuleContent("preflight",`${b}
|
|
8
|
-
@layer base {
|
|
9
|
-
${e}
|
|
10
|
-
}`,!0)}}ensureCssVars(){if(this.isDestroyed)return;const t=this.context.themeToCssVars?this.context.themeToCssVars():":root { /* CSS Variables will be generated here */ }";this.stylePartitionManager.updateRuleContent("css-vars",t)}getInsertionPoint(){if(typeof this.options.insertionPoint!="string")return this.options.insertionPoint;switch(this.options.insertionPoint){case"body":return document.body||document.head;case"head":default:return document.head}}addClass(t){if(this.isDestroyed)return;const e=this.normalizeClasses(t).filter(Boolean);e.forEach(s=>this.pinned.add(s)),this.processClasses(e)}processClasses(t){this.processClassesIncremental(t)}processClassesIncremental(t){const e=typeof window<"u",s=this.incrementalParser.processClasses(t);console.log("[BrowserRuntime] results",s,...t),this.applyParseResults(s,{isBrowser:e})}applyParseResults(t,e){if(this.isDestroyed)return;if(this.getInsertionPoint().isConnected&&this.stylePartitionManager.hasDetachedPartitions()){const n=Array.from(this.cache.values());this.reset(),t=[...n,...t],t.forEach(o=>this.incrementalParser.markProcessed(o.cls))}if(this.ssrClasses.size>0&&(t=t.filter(n=>!this.ssrClasses.has(n.cls))),this.options.skipExisting&&t.length>0&&typeof document<"u"){const n=this.getExistingClasses();t=t.filter(o=>!n.has(o.cls))}if(t.length===0)return;const s=[],i=[],r=this.options.skipExisting&&typeof document<"u"?(this.getExistingClasses(),this.existingKeyframes):null;for(const n of t)if(n.css&&Array.isArray(n.cssList)&&(s.push(n),this.cache.set(n.cls,n)),n.rootCss&&Array.isArray(n.rootCssList))for(const o of n.rootCssList){if(r?.size){const c=/^\s*@keyframes\s+([^\s{]+)/.exec(o)?.[1];if(c&&r.has(c))continue}this.rootCache.has(o)||(this.rootCache.add(o),i.push(o))}i.length>0&&this.stylePartitionManager.addRootRules(i.filter(Boolean)),s.length>0&&this.stylePartitionManager.addRules(s),this.debugLog("info",`Applied ${t.length} parser results`,{cssRuleCount:s.length,rootCssCount:i.length})}isPermanent(t){return this.pinned.has(t)||this.ssrClasses.has(t)||typeof document>"u"?!0:this.getExistingClasses().has(t)}reclaim(t){if(this.isDestroyed)return;const e=t.filter(r=>this.cache.has(r));if(e.length===0)return;const s=e.map(r=>this.cache.get(r));e.forEach(r=>{this.cache.delete(r),this.incrementalParser.unmarkProcessed(r)});const i=new Set;for(const r of this.cache.values())r.cssList.forEach(n=>i.add(n));for(const r of s){const n=this.getCategory(r.cls);for(const o of r.cssList)i.has(o)||this.stylePartitionManager.removeRule(o,n)}this.reclaimedCount+=e.length}getExistingClasses(){const t=new Set(Array.from(document.querySelectorAll("style[data-barocss]"),r=>r.sheet)),e=Array.from(document.styleSheets).filter(r=>{if(t.has(r))return!1;const n=r.ownerNode;return!(n&&typeof n.hasAttribute=="function"&&(n.hasAttribute("data-barocss")||(n.id||"").startsWith(this.options.styleId)))});if(this.existing&&e.length===this.existingSheetCount)return this.existing;const s=new Set,i=new Set;for(const r of e){let n;try{n=r.cssRules}catch{continue}y(n,s),R(n,i)}return this.existingKeyframes=i,this.existing=s,this.existingSheetCount=e.length,s}observe(t=document.body,e){return this.observedOnce||(this.observedOnce=!0,this.adoptSsrSheets()),this.changeDetector.observe(t,e)}normalizeClasses(t){return Array.isArray(t)?t.flatMap(e=>e.split(/\s+/)):t.split(/\s+/)}has(t){return this.cache.has(t)}getCss(t){return this.cache.get(t)?.cssList.join(`
|
|
7
|
+
`:``,t?this.classToCategoryPartitionMap.delete(e):this.classToPartitionMap.delete(e),!0}get ruleCount(){return this.classToPartitionMap.size+this.classToCategoryPartitionMap.size}findRulePartition(e){let t=this.classToPartitionMap.get(e);if(t!==void 0&&this.partitions[t])return this.partitions[t];let n=this.classToCategoryPartitionMap.get(e);return n===void 0?null:this.categoryPartitions.get(n)||null}updateRuleContent(e,n,r=!1){let i=this.getCategoryPartition(e);if(i)i.styleElement.textContent=n;else{let i=this.createNewCategoryPartition(e,r);(0,t.isDebug)()&&console.log(`[StylePartitionManager] Created new partition for category: ${e}`),i.styleElement.textContent=n}}cleanup(){this.partitions.forEach(e=>{e.styleElement.parentNode&&e.styleElement.parentNode.removeChild(e.styleElement)}),this.categoryPartitions.forEach(e=>{e.styleElement.parentNode&&e.styleElement.parentNode.removeChild(e.styleElement)}),this.partitions=[],this.categoryPartitions.clear(),this.partitionCounter=0,this.classToPartitionMap.clear(),this.classToCategoryPartitionMap.clear()}};function r(e){return e?typeof e==`object`&&typeof e.baseVal==`string`?e.baseVal:e.toString():``}function i(e){return e?r(e).split(/\s+/).filter(Boolean):[]}var a=class{constructor(e,n,r=e=>(0,t.parseClassName)(e).utility?.category){this.observer=null,this.gc=null,this.incrementalParser=e,this.BrowserRuntime=n,this.getCategory=r}setGc(e){this.gc=e}setParser(e){this.incrementalParser=e}observe(e=document.body,t){return typeof window>`u`?new MutationObserver(()=>{}):(this.observer&&this.observer.disconnect(),this.gc?.setRoot(e),this.observer=new MutationObserver(t=>{let n=new Set,r=this.gc;if(t.forEach(t=>{if(r&&(t.type===`attributes`?r.reconcile(t.target):t.type===`childList`&&(t.removedNodes.forEach(e=>r.reconcileTree(e)),t.addedNodes.forEach(e=>r.reconcileTree(e)))),t.type===`attributes`&&t.attributeName===`class`&&e.contains(t.target)){let e=t.target;e.className&&i(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||n.add(e)})}t.type===`childList`&&t.addedNodes.forEach(t=>{if(t.nodeType===Node.ELEMENT_NODE&&e.contains(t)){let e=t;this.processElement(e,n),e.querySelectorAll(`[class]`).forEach(e=>{this.processElement(e,n)})}})}),n.size>0){let e=Array.from(n),t=this.incrementalParser.processClasses(e);this.BrowserRuntime?.applyParseResults(t)}else this.BrowserRuntime?.applyParseResults([]);r?.afterBatch()}),this.observer.observe(e,{attributes:!0,subtree:!0,attributeFilter:[`class`],childList:!0}),t?.scan&&this.scanExistingClasses(e,t),this.observer)}scanExistingClasses(e,t){let n=new Set;e.className&&i(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||n.add(e)});let r=e.querySelectorAll(`[class]`);for(let e of r)if(e.className){let t=i(e.className);for(let e of t)this.incrementalParser.isProcessed(e)||n.add(e)}if(n.size>0){let e=Array.from(n),r=this.incrementalParser.processClasses(e),i=r.filter(e=>this.getCategory(e.cls)===`layout`),a=r.filter(e=>this.getCategory(e.cls)!==`layout`);this.BrowserRuntime?.applyParseResults(i),t?.onReady?.(),this.BrowserRuntime?.applyParseResults(a)}else t?.onReady?.()}processElement(e,t){e.className&&i(e.className).forEach(e=>{this.incrementalParser.isProcessed(e)||t.add(e)})}disconnect(){this.observer&&=(this.observer.disconnect(),null)}};function o(e){return e.replace(/\\([0-9a-fA-F]{1,6})\s?|\\(.)/g,(e,t,n)=>t?String.fromCodePoint(parseInt(t,16)):n)}var s=/^\s*(?::(?:where|is)\(\s*)?\.((?:\\[0-9a-fA-F]{1,6}\s?|\\.|[\w-]|[^\x00-\x7F])+)/;function c(e){let t=[],n=0,r=0;for(let i=0;i<e.length;i++){let a=e[i];a===`\\`?i++:a===`(`||a===`[`?n++:a===`)`||a===`]`?n--:a===`,`&&n===0&&(t.push(e.slice(r,i)),r=i+1)}return t.push(e.slice(r)),t}function l(e,t=new Set){for(let n of Array.from(e)){if(n.type===7&&typeof n.name==`string`){t.add(n.name);continue}let e=n.cssRules;e&&e.length&&l(e,t)}return t}function u(e,t=new Set){for(let n of Array.from(e)){let e=n.selectorText;if(typeof e==`string`)for(let n of c(e)){let e=s.exec(n);e&&t.add(o(e[1]))}let r=n.cssRules;r&&r.length&&u(r,t)}return t}var d=class{constructor(e,t,n,r=()=>Date.now()){this.host=e,this.graceMs=t,this.maxRules=n,this.now=r,this.counts=new Map,this.counted=new WeakMap,this.candidates=new Map,this.timer=null,this.root=null}setRoot(e){this.counts.clear(),this.counted=new WeakMap,this.candidates.clear(),this.cancel(),this.root=e,this.reconcileTree(e)}count(e){return this.counts.get(e)??0}reconcileTree(e){if(e.nodeType!==1)return;let t=e;this.reconcile(t),t.querySelectorAll(`[class]`).forEach(e=>this.reconcile(e))}reconcile(e){let t=this.root,n=t&&t.contains(e)?Array.from(new Set(i(e.getAttribute(`class`)))):[],r=this.counted.get(e);if(!r&&n.length===0)return;let a=new Set(r??[]),o=new Set(n);for(let e of o){if(a.has(e))continue;let t=(this.counts.get(e)??0)+1;this.counts.set(e,t),this.candidates.delete(e)}for(let e of a){if(o.has(e))continue;let t=(this.counts.get(e)??0)-1;t>0?this.counts.set(e,t):(this.counts.delete(e),this.candidates.delete(e),this.candidates.set(e,this.now()))}n.length?this.counted.set(e,n):this.counted.delete(e)}afterBatch(){this.candidates.size!==0&&(this.host.cachedCount()>this.maxRules?this.schedule(0):this.schedule(this.graceMs))}schedule(e){if(this.timer!==null){if(e>0)return;clearTimeout(this.timer)}this.timer=setTimeout(()=>{this.timer=null,this.sweep()},e)}sweep(){let e=this.now(),t=Math.max(0,this.host.cachedCount()-this.maxRules),n=[],r=0;for(let[i,a]of this.candidates){let o=e-a>=this.graceMs;!o&&r>=t||(this.candidates.delete(i),!(this.count(i)>0||this.inDom(i)||this.host.isPermanent(i))&&(n.push(i),o||r++))}n.length&&this.host.reclaim(n),this.candidates.size&&this.schedule(this.graceMs)}inDom(e){let t=this.root;if(!t)return!1;let n=t.ownerDocument??document;return t.classList.contains(e)||n.documentElement.classList.contains(e)||n.getElementsByClassName(e).length>0}cancel(){this.timer!==null&&clearTimeout(this.timer),this.timer=null}stats(){return{trackedClasses:this.counts.size,candidates:this.candidates.size}}},f=`style[data-barocss-ssr]`,p=`@layer theme, base, components, utilities;`,m=class{constructor(e={}){this.cache=new Map,this.rootCache=new Set,this.isDestroyed=!1,this.existing=null,this.existingKeyframes=new Set,this.existingSheetCount=-1,this.pinned=new Set,this.gc=null,this.reclaimedCount=0,this.ssrRules=[],this.ssrClasses=new Set,this.observedOnce=!1,this.getCategory=e=>(0,t.parseClassName)(e,this.context).utility?.category;let r={};this.options={config:e.config||r,styleId:e.styleId||`barocss-runtime`,insertionPoint:e.insertionPoint||`head`,maxRulesPerPartition:e.maxRulesPerPartition||50,skipExisting:e.skipExisting??!1,gc:e.gc??!0,gcGraceMs:e.gcGraceMs??3e3,maxRules:e.maxRules??1/0},this.context=(0,t.createContext)(this.options.config),this.incrementalParser=new t.IncrementalParser(this.context),this.changeDetector=new a(this.incrementalParser,this,this.getCategory),this.stylePartitionManager=new n(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.options.gc&&(this.gc=new d({reclaim:e=>this.reclaim(e),isPermanent:e=>this.isPermanent(e),cachedCount:()=>this.cache.size},this.options.gcGraceMs,this.options.maxRules),this.changeDetector.setGc(this.gc)),this.init()}debugLog(e,n,r){(0,t.isDebug)()&&(console[e]||console.log)(`[BrowserRuntime:${e.toUpperCase()}] ${n}`,r||``)}init(){this.debugLog(`debug`,`init`),this.injectPreflightCSS(),this.ensureCssVars(),this.adoptSsrSheets()}adoptSsrSheets(){if(typeof document>`u`)return;let e=[];if(document.head){for(let t of Array.from(document.head.querySelectorAll(`${f}:not([data-barocss-adopted])`))){let n=t.sheet;if(!n)continue;t.setAttribute(`data-barocss-adopted`,``);let r=[];for(let e=n.cssRules.length-1;e>=0;e--){let t=n.cssRules[e],i=u([t]);i.size!==0&&(i.forEach(e=>this.ssrClasses.add(e)),r.unshift({css:t.cssText,cls:i.values().next().value}),n.deleteRule(e))}e.push(...r)}e.length!==0&&(this.ssrRules.push(...e),this.insertSsrRules(e))}}insertSsrRules(e){for(let{css:t,cls:n}of e){let e=this.getCategory(n);e?this.stylePartitionManager.addCategoryRule(t,e):this.stylePartitionManager.addRule(t)}}injectPreflightCSS(){let e=this.options.config.preflight??!0;if(e){let t=this.context.getPreflightCSS(e);this.stylePartitionManager.updateRuleContent(`preflight`,`${p}\n@layer base {\n${t}\n}`,!0)}}ensureCssVars(){if(this.isDestroyed)return;let e=this.context.themeToCssVars?this.context.themeToCssVars():`:root { /* CSS Variables will be generated here */ }`;this.stylePartitionManager.updateRuleContent(`css-vars`,e)}getInsertionPoint(){if(typeof this.options.insertionPoint!=`string`)return this.options.insertionPoint;switch(this.options.insertionPoint){case`body`:return document.body||document.head;default:return document.head}}addClass(e){if(this.isDestroyed)return;let t=this.normalizeClasses(e).filter(Boolean);t.forEach(e=>this.pinned.add(e)),this.processClasses(t)}processClasses(e){this.processClassesIncremental(e)}processClassesIncremental(e){let t=typeof window<`u`,n=this.incrementalParser.processClasses(e);this.debugLog(`debug`,`results`,{results:n,classList:e}),this.applyParseResults(n,{isBrowser:t})}applyParseResults(e,t){if(this.isDestroyed)return;if(this.getInsertionPoint().isConnected&&this.stylePartitionManager.hasDetachedPartitions()){let t=Array.from(this.cache.values());this.reset(),e=[...t,...e],e.forEach(e=>this.incrementalParser.markProcessed(e.cls))}if(this.ssrClasses.size>0&&(e=e.filter(e=>!this.ssrClasses.has(e.cls))),this.options.skipExisting&&e.length>0&&typeof document<`u`){let t=this.getExistingClasses();e=e.filter(e=>!t.has(e.cls))}if(e.length===0)return;let n=[],r=[],i=this.options.skipExisting&&typeof document<`u`?(this.getExistingClasses(),this.existingKeyframes):null;for(let t of e)if(t.css&&Array.isArray(t.cssList)&&(n.push(t),this.cache.set(t.cls,t)),t.rootCss&&Array.isArray(t.rootCssList))for(let e of t.rootCssList){if(i?.size){let t=/^\s*@keyframes\s+([^\s{]+)/.exec(e)?.[1];if(t&&i.has(t))continue}this.rootCache.has(e)||(this.rootCache.add(e),r.push(e))}r.length>0&&this.stylePartitionManager.addRootRules(r.filter(Boolean)),n.length>0&&this.stylePartitionManager.addRules(n),this.debugLog(`info`,`Applied ${e.length} parser results`,{cssRuleCount:n.length,rootCssCount:r.length})}isPermanent(e){return this.pinned.has(e)||this.ssrClasses.has(e)||typeof document>`u`?!0:this.getExistingClasses().has(e)}reclaim(e){if(this.isDestroyed)return;let t=e.filter(e=>this.cache.has(e));if(t.length===0)return;let n=t.map(e=>this.cache.get(e));t.forEach(e=>{this.cache.delete(e),this.incrementalParser.unmarkProcessed(e)});let r=new Set;for(let e of this.cache.values())e.cssList.forEach(e=>r.add(e));for(let e of n){let t=this.getCategory(e.cls);for(let n of e.cssList)r.has(n)||this.stylePartitionManager.removeRule(n,t)}this.reclaimedCount+=t.length}getExistingClasses(){let e=new Set(Array.from(document.querySelectorAll(`style[data-barocss]`),e=>e.sheet)),t=Array.from(document.styleSheets).filter(t=>{if(e.has(t))return!1;let n=t.ownerNode;return!(n&&typeof n.hasAttribute==`function`&&(n.hasAttribute(`data-barocss`)||(n.id||``).startsWith(this.options.styleId)))});if(this.existing&&t.length===this.existingSheetCount)return this.existing;let n=new Set,r=new Set;for(let e of t){let t;try{t=e.cssRules}catch{continue}u(t,n),l(t,r)}return this.existingKeyframes=r,this.existing=n,this.existingSheetCount=t.length,n}observe(e=document.body,t){return this.observedOnce||(this.observedOnce=!0,this.adoptSsrSheets()),this.changeDetector.observe(e,t)}normalizeClasses(e){return Array.isArray(e)?e.flatMap(e=>e.split(/\s+/)):e.split(/\s+/)}has(e){return this.cache.has(e)}getCss(e){return this.cache.get(e)?.cssList.join(`
|
|
11
8
|
`)}getAllCss(){return[...this.rootCache,...Array.from(this.cache.values()).flatMap(e=>e.cssList)].join(`
|
|
12
|
-
`)}getClasses(){return Array.from(this.cache.keys())}getCacheStats(){
|
|
9
|
+
`)}getClasses(){return Array.from(this.cache.keys())}getCacheStats(){let e=this.incrementalParser.getStats();return{runtime:{cachedClasses:this.cache.size,rootCacheSize:this.rootCache.size,ruleCount:this.stylePartitionManager.ruleCount,reclaimedClasses:this.reclaimedCount,gc:this.gc?.stats()??null},ast:e.cacheStats.ast,incremental:e}}clearCaches(){this.isDestroyed||(this.cache.clear(),this.rootCache.clear(),(0,t.clearAstCache)(this.context),this.incrementalParser.clearProcessed(),this.stylePartitionManager.cleanup(),this.stylePartitionManager=new n(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.injectPreflightCSS(),this.ensureCssVars(),this.insertSsrRules(this.ssrRules))}reset(){this.isDestroyed||(this.cache.clear(),this.rootCache.clear(),this.incrementalParser.clearProcessed(),this.stylePartitionManager.cleanup(),this.stylePartitionManager=new n(this.getInsertionPoint(),this.options.maxRulesPerPartition,`${this.options.styleId}-partition`,this.getCategory),this.injectPreflightCSS(),this.ensureCssVars(),this.insertSsrRules(this.ssrRules))}updateConfig(e){if(this.isDestroyed)return;let n=Array.from(this.cache.keys());this.options.config=e,this.context=(0,t.createContext)(e),this.incrementalParser=new t.IncrementalParser(this.context),this.changeDetector.setParser(this.incrementalParser),this.reset(),n.length>0&&this.addClass(n)}removeClass(e){if(this.isDestroyed)return;let t=new Set(this.normalizeClasses(e)),n=Array.from(this.cache.values()).filter(e=>!t.has(e.cls));n.length!==this.cache.size&&(this.reset(),n.forEach(e=>this.incrementalParser.markProcessed(e.cls)),this.applyParseResults(n))}destroy(){this.isDestroyed||=(this.changeDetector.disconnect(),this.gc?.cancel(),this.stylePartitionManager.cleanup(),this.cache.clear(),this.rootCache.clear(),!0)}getStats(){return{cachedClasses:this.cache.size,styleElementId:this.options.styleId,isDestroyed:this.isDestroyed,config:this.options.config,cacheStats:this.getCacheStats()}}},h=null,g;function _(e={}){return!h||h.getStats().isDestroyed?(h=new m(e),g=e.config):e.config&&e.config!==g&&(h.updateConfig(e.config),g=e.config),h}function v({loadingClassName:e=`baro-boot`,...t}={}){if(!document.body){document.addEventListener(`DOMContentLoaded`,()=>v({loadingClassName:e,...t}),{once:!0});return}let n=`${e}-doing`,r=`${e}-done`;try{document.body.classList.add(n),_(t).observe(document.body,{scan:!0,onReady:()=>{document.body.classList.remove(n),document.body.classList.add(r)}})}catch(e){document.body?.classList.remove(n),console.error(`BaroCSS boot failed:`,e)}}var y=v;function b(e){if(!e||typeof e!=`object`||Array.isArray(e))return[];let t=e.elements;if(!t||typeof t!=`object`||Array.isArray(t))return[];let n=new Set;for(let e of Object.keys(t)){let r=t[e];if(!r||typeof r!=`object`||Array.isArray(r))continue;let i=r.props;if(!i||typeof i!=`object`||Array.isArray(i))continue;let a=i.className;if(typeof a==`string`)for(let e of a.split(/\s+/))e&&n.add(e)}return Array.from(n)}function x(e,t){let n=b(e);n.length>0&&t.addClass(n)}var S={colors:Object.fromEntries(`background.foreground.card.card-foreground.popover.popover-foreground.primary.primary-foreground.secondary.secondary-foreground.muted.muted-foreground.accent.accent-foreground.destructive.border.input.ring.chart-1.chart-2.chart-3.chart-4.chart-5.sidebar.sidebar-foreground.sidebar-primary.sidebar-primary-foreground.sidebar-accent.sidebar-accent-foreground.sidebar-border.sidebar-ring`.split(`.`).map(e=>[e,`var(--${e})`])),borderRadius:{sm:`calc(var(--radius) - 4px)`,md:`calc(var(--radius) - 2px)`,lg:`var(--radius)`,xl:`calc(var(--radius) + 4px)`}};e.BrowserRuntime=m,e.ChangeDetector=a,e.LAYER_ORDER=p,e.SSR_STYLE_SELECTOR=f,e.StylePartitionManager=n,e.baroBoot=v,e.baroStart=y,e.collectJsonRenderClassNames=b,e.getRuntime=_,e.normalizeClassName=r,e.normalizeClassNameList=i,e.preloadJsonRenderClasses=x,e.shadcnTheme=S});
|
package/package.json
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barocss/browser",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/barocss/barocss.git",
|
|
7
7
|
"directory": "packages/barocss-browser"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
|
-
"main": "./dist/index.
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"
|
|
15
|
-
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.es.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.cts",
|
|
20
|
+
"default": "./dist/index.cjs"
|
|
21
|
+
}
|
|
16
22
|
}
|
|
17
23
|
},
|
|
18
24
|
"files": [
|
|
@@ -24,11 +30,11 @@
|
|
|
24
30
|
"access": "public"
|
|
25
31
|
},
|
|
26
32
|
"dependencies": {
|
|
27
|
-
"@barocss/kit": "0.8.
|
|
33
|
+
"@barocss/kit": "0.8.2"
|
|
28
34
|
},
|
|
29
35
|
"devDependencies": {
|
|
30
36
|
"jsdom": "^26.1.0",
|
|
31
|
-
"vite": "^
|
|
37
|
+
"vite": "^8.3.1",
|
|
32
38
|
"vite-plugin-dts": "^4.5.4",
|
|
33
39
|
"vitest": "^5.0.2"
|
|
34
40
|
},
|