@teacss/directives 0.10.3 → 0.10.5

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/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { MagicStringLike, SourceCodeTransformer, StringifiedUtil, TeacssGenerator } from "@teacss/core";
2
+ import { CssDialect } from "@teacss/config";
2
3
  //#region src/apply.d.ts
3
4
  /**
4
5
  * The rules written after an enclosing rule so far, and the at-rule heads still
@@ -158,13 +159,14 @@ export declare function generateEntry(entryCss: string, options: GenerateEntryOp
158
159
  *
159
160
  * - `@teacss;` → replaced by `layers`
160
161
  * - `@presets` / `@source` / `@shortcut` / `@safelist` / `@custom {}` → stripped (consumed)
161
- * - native `@charset` / `@import`, and the `@layer` statements leading the file → hoisted in CSS-required order
162
+ * - native `@charset` / `@import`, and the `@layer` statements leading the file → hoisted in CSS-required order;
163
+ * a final `@import` without its `;` gains one
162
164
  * - everything else (plain rules, comments, `@media`, …) → kept in place
163
165
  *
164
166
  * Pure, css-tree-free: exact top-level at-rule ranges are scanned structurally
165
167
  * and spliced out of the original.
166
168
  */
167
- export declare function spliceEntry(entryCss: string, layers: string): string;
169
+ export declare function spliceEntry(entryCss: string, layers: string, dialect?: CssDialect): string;
168
170
  //#endregion
169
171
  //#region src/transform.d.ts
170
172
  export declare function transformDirectives(code: MagicStringLike, generator: TeacssGenerator, options: TransformerDirectivesOptions, filename?: string): Promise<void>;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import{C as e,S as t,_ as n,a as r,b as i,c as a,d as o,f as s,g as c,h as l,i as u,l as d,m as f,n as p,o as m,p as h,r as g,s as _,t as v,u as y,v as b,x,y as S}from"./screen-core-FmDUvprA.js";import{createGenerator as C,expandClassGroups as ee,expandVariantGroup as te,isCssId as w,splitClassTokens as T,toArray as E}from"@teacss/core";import{hasIconFn as D,hasThemeFn as O,transformThemeFn as ne,transformThemeString as k}from"@teacss/rule-helpers";import{ROLE_DIRECTIVES as A,resolveEntryConfig as re,scanTopLevelAtRules as j}from"@teacss/config";import{generateSources as ie}from"@teacss/runtime/source-generation";var ae=class e{moved;head;outer;depth;constructor(e,t,n,r){this.moved=e,this.head=t,this.outer=n,this.depth=r}static root(t){return new e({end:t,css:``,open:[]},``,void 0,0)}within(t){return new e(this.moved,t,this,this.depth+1)}place(e){let{moved:t}=this,n=[],r=this;for(;r.depth>0&&t.open[r.depth-1]!==r;)n.push(r),r=r.outer;t.css+=`}`.repeat(t.open.length-r.depth),t.open.length=r.depth;for(let e of n.reverse())t.css+=`${e.head}{`,t.open.push(e);t.css+=e}},oe=class e{hosts;inPlace;plain;nesting;constructor(e,t,n,r){this.hosts=e,this.inPlace=t,this.plain=n,this.nesting=r}static root(t,n,r){return new e([{condition:ae.root(n),selector:t,element:``}],void 0,!1,r)}within(t){let n=this.hosts.map(e=>({...e,condition:e.condition.within(t)}));return new e(n,this.inPlace===void 0?void 0:`&`,this.plain,this.nesting)}nested(t,r){let i=n(t);if(!i)return;let{taking:a,others:o}=i,s,c=this.plain;if(this.hosts.every(e=>e.nested===void 0)){let e=this.hosts[0],{groups:t,plain:n}=f(e.selector);c=!r&&n.length>0,r&&n.length&&t.push({bases:n,element:``}),s=t.map(({bases:t,element:n})=>{let i=b(a,this.anchored(t,a),r);return{condition:e.condition,selector:S(i,n),nested:d(i),element:n}})}else{let e=d(a);s=this.hosts.map(t=>({condition:t.condition.within(t.nested??``),selector:S(a,t.element),nested:e,element:t.element}))}let l=c?t.trim():o.length?d(o):void 0;return new e(s,l,c,this.nesting)}anchored(e,t){return this.nesting===`style-rule`?_(e,t,!1):this.nesting===`scope`?_(e,t,!0):e}standaloneHosts(){let e=this.hosts.map((e,t)=>[t,e.selector]);return this.inPlace===void 0?e:[...e,[void 0,this.inPlace]]}};function se(e){return e.kind===`rule`?e.rule.block:e.block}function ce(e){return e.enclosing?e.enclosing.standaloneHosts():[[void 0,e.kind===`rule`?e.selector:e.host]]}function M(e,t){return Error(`[@teacss] @apply: "${e}" is outside a style rule or @scope${t?` in ${t}`:``}`)}function le(e,t){return Error(`[@teacss] @apply: "${e}" needs a selector or condition, which a @keyframes block cannot hold${t?` in ${t}`:``}`)}async function ue(e,t,n){let r=[],i=[],a=!1,[o,s]=se(t);for(let c of x(e.source,o,s,e.preprocessor)){if(c.kind===`rule`){await n(c);continue}let o=await pe(e,t,c);o&&(r.push(o.consumedRange),i.push(...o.standalone),a||=o.hasInlineUtility)}if(t.kind===`rule`&&!a&&r.length>0){let n=o;r.every(([t,r])=>{let i=!e.source.slice(n,t).trim();return n=r,i})&&!e.source.slice(n,s).trim()&&e.code.remove(t.rule.start,t.rule.end)}return i}function de({code:e},t,n,r){let i=t.kind===`rule`?t.rule.end:t.block[1];if(t.kind===`rule`&&!t.enclosing&&n){let{moved:t}=n.hosts[0].condition;t.css&&(e.appendLeft(t.end,t.css+`}`.repeat(t.open.length)),t.css=``,t.open=[])}for(let n of r)n.moved!==void 0&&t.enclosing?t.enclosing.hosts[n.moved].condition.place(n.css):e.appendLeft(i,n.css)}const fe=e=>e===`style-rule`||e===`keyframes`;async function pe(e,t,n){let{code:i,source:u,generator:d,applyVariable:f,options:p,filename:m,applyUtilities:g,hoisted:_,nesting:v}=e,y,b;if(n.kind===`at-rule`&&n.name===`apply`)b=n.prelude[1],y=P(u.slice(...n.prelude));else if(n.kind===`declaration`&&f.includes(n.name))b=u[n.end-1]===`;`?n.end-1:n.end,y=T(P(u.slice(n.value[0],b).trim())).map(e=>P(e)).join(` `);else return;if(!y)return;y=me(y),y=ne(y,d.config.theme,p.throwOnMissing??!0,r(d));let x=T(te(ee(y))),S=await c(x,d),C=o(S);if(C.length&&(p.throwOnMissing??!0))throw Error(`${a(C)}${m?` in ${m}`:``}`);let w=h(S,d.parentOrders,d.config.layers,e=>d.sheetOrder(e));g.set(n.start,S.flatMap(([,e])=>e??[]));let E=[n.start,u[n.end-1]===`;`?n.end:b];if(!w.length)return i.remove(...E),{consumedRange:E,hasInlineUtility:!1,standalone:[]};let D,O=!1,k=[];for(let e of w)if(e.kind===`top-level`)v===`top-level`||v===`scope`?k.push({css:e.body}):N(_,e.body);else if(e.kind===`standalone`){D??=ce(t).map(([e,t])=>[e,s(t)]);let n=e.selector?.startsWith(`@`)??!1;for(let[t,r]of D){let i=l(r,e,m);if(i){if(v===`keyframes`&&!n)throw le(y,m);n&&fe(v)?N(_,i):k.push({css:i,moved:t})}}}else O=!0,i.appendLeft(n.start,e.body);return i.remove(...E),{consumedRange:E,hasInlineUtility:O,standalone:k}}function N(e,t){for(let n of x(t,0,t.length,!1)){let r=t.slice(n.start,n.end);e.includes(r)||e.push(r)}}function P(e){return e.replace(/^(['"])(.*)\1$/,`$2`)}function me(e){return e.replace(/\/\*[\s\S]*?\*\//g,``)}async function F({code:e,source:t,generator:n,options:r,applyUtilities:i},a,o,s){let c=await u(n,a,(e,n)=>t.slice(e,n),r.throwOnMissing??!0,s);if(c!==void 0)return e.overwrite(a.loc.start.offset,a.loc.end.offset,c),o!==void 0&&c.includes(`var(`)&&i.set(a.loc.start.offset,[[0,void 0,`${o}:${c};`,void 0,void 0,void 0,void 0]]),c}function he({code:e,source:t,generator:n},r){let i=v(t.slice(...r.prelude),n.config.theme);i&&e.overwrite(r.start,r.block?r.block[0]-1:r.prelude[1],`@media ${i}`)}const ge=new Set([`presets`,`source`,`shortcut`,`safelist`,...A]);function _e(e,t){let n=new Set;if(!t.includes(`@`))return n;let r=j(t);if(r.some(e=>e.name===`teacss`&&e.prelude===``&&e.terminator!==`block`))return n;for(let t of r)t.prelude.startsWith(`:`)||(ge.has(t.name)?t.terminator===`;`:t.name===`custom`&&(t.terminator===`block`||t.terminator===`;`))&&(e.remove(t.start,t.end),n.add(t.start));return n}function ve(e){return A.some(t=>e.includes(`@${t}`))||e.includes(`@preset`)||e.includes(`@source`)||e.includes(`@shortcut`)||e.includes(`@safelist`)||e.includes(`@custom`)}function ye(e){return/^(?:property|font-face|font-palette-values|counter-style)$/i.test(e)}function be(e){return e?.split(`?`).flatMap(e=>/\.(scss|sass|less|styl|stylus)$/.exec(e)?.[1]??[])[0]}function xe(e,t){let n=[];for(let r=t+1;r<e.length;r++){let i=e[r];if(i===`\\`)r++;else if(i===e[t])return{close:r,lineBreaks:n};else(i===`
2
- `||i===`\r`||i===`\f`)&&n.push(r)}}function Se(n,r){let a=``,o=0,s=0,c=0,l=0,u=!1;for(let d=0;d<n.length;d++){let f=n[d];if(f===`/`&&n[d+1]===`*`){let e=n.indexOf(`*/`,d+2);d=e===-1?n.length:e+1}else if(r&&f===`/`&&n[d+1]===`/`){let e=n.indexOf(`
3
- `,d),t=e===-1?n.length:e;a+=n.slice(o,d)+` `.repeat(t-d),o=t,d=t-1}else if(f===`\\`)d++;else if(f===`"`||f===`'`){let e=xe(n,d);if(e){for(let t of e.lineBreaks)a+=`${n.slice(o,t)} `,o=t+1;d=e.close}else d=t(n,d,n.length)-1}else if(i(n,d)){let t=e(n,d,n.length,r);u=t===-1,d=(u?n.length:t)-1}else if(f===`(`)l++;else if(f===`)`&&l)l--;else if(l&&!r)continue;else f===`}`?s=Math.max(0,s-1):f===`{`&&(c=Math.max(c,++s))}return{source:o?a+n.slice(o):n,blockDepth:c,unclosedUrl:u}}function Ce(e,t){return e.startsWith(t)&&!/[\w-]/.test(e[t.length]??``)}function I(e,t,n,r){return x(e,t,n,!0).some(t=>{let n=t.kind===`rule`||t.kind===`at-rule`?t.block:void 0;return U(e.slice(t.start,n?.[0]??t.end)).split(`
4
- `).slice(1).some(e=>r.some(t=>Ce(e.trimStart(),t)))||n!==void 0&&I(e,n[0],n[1],r)})}async function L(e,t,n,r){let i=be(r),{source:a,blockDepth:o,unclosedUrl:s}=Se(e.original,i!==void 0),c=E((n.applyVariable??[`--at-apply`])||[]),l=a.includes(`@apply`)||c.some(e=>a.includes(e)),u=a.includes(`@screen`),d=O(a)||D(a),f=ve(a)?_e(e,a):new Set;if(!l&&!d&&!u)return;if(i===`sass`)throw Error(`TeaCSS directives need braces; indented Sass is not supported, write it as SCSS${r?` in ${r}`:``}`);if(o>512)throw Error(`[@teacss] CSS blocks nest deeper than 512 levels${r?` in ${r}`:``}`);if(s)throw Error(`[@teacss] url() is not closed${r?` in ${r}`:``}`);if((i===`styl`||i===`stylus`)&&I(a,0,a.length,[`@apply`,`@screen`,...c]))throw Error(`TeaCSS directives need braces; indented Stylus is not supported, write it with braces${r?` in ${r}`:``}`);let p=new Map,h=[],g={options:n,applyVariable:c,generator:t,code:e,source:a,filename:r,preprocessor:i!==void 0,applyUtilities:p,hoisted:h,nesting:`top-level`,literalColors:!1,hasApply:l,hasScreen:u,hasFn:d,stripped:f};await B(g,x(a,0,a.length,g.preprocessor),void 0),h.length&&e.appendLeft(e.original.length,`\n${h.join(``)}`);let _=[...p].sort(([e],[t])=>e-t).flatMap(([,e])=>e),v=await m(t,_,r);v&&e.appendLeft(e.original.length,`\n${v}`)}const we=({source:e,applyVariable:t},[n,r])=>{let i=e.slice(n,r);return i.includes(`@apply`)||t.some(e=>i.includes(e))},R=e=>e===`top-level`||e===`scope`?`style-rule`:e;async function z(e,t,n){let{nesting:r,enclosing:i,literalColors:a}=e;Object.assign(e,t);try{await n()}finally{Object.assign(e,{nesting:r,enclosing:i,literalColors:a})}}async function B(e,t,n){for(let r of t)if(!e.stripped.has(r.start)){if(r.kind===`rule`)await Z(e,r);else if(r.kind===`at-rule`){if(r.name===`apply`)throw M(e.source.slice(...r.prelude),e.filename);await Y(e,r,n)}else if(r.kind===`declaration`){if(n===void 0&&e.hasApply&&e.applyVariable.includes(r.name))throw M(e.source.slice(...r.value),e.filename);await $(e,r)}else await V(e,r)}}async function V(e,{start:t,end:n,rulesets:r}){await J(e,[t,r[0]?.[0]??n]);for(let t of r)await W(e,t)}function Te({source:e},{name:t,prelude:n}){return t===`mixin`||t===`include`||e.slice(...n)===`:`}function H(e,n,r,i){let a=0;for(let o=n;o<e.length;o++){let n=e[o];if(n===`\\`)o++;else if(n===`"`||n===`'`)o=t(e,o,e.length)-1;else if(n===r)a++;else if(n===i&&--a===0)return o+1}return e.length}function U(e){let n=``,r=0;for(let i=0;i<e.length;){let a=e[i],o;if(a===`\\`){i+=2;continue}if(a===`"`||a===`'`)o=t(e,i,e.length);else if(a===`[`)o=H(e,i,`[`,`]`);else if((a===`#`||a===`@`)&&e[i+1]===`{`)o=H(e,i+1,`{`,`}`);else if(e.startsWith(`/*`,i)){let t=e.indexOf(`*/`,i+2);o=t===-1?e.length:t+2}else{i++;continue}n+=e.slice(r,i)+` `.repeat(o-i),r=o,i=o}return n+e.slice(r)}function Ee(e){let t=U(e);return/^[.#]?[\w-]+\s*\(/.test(t)||/[\s)]when[\s(]/.test(t)||t.includes(`:extend(`)}function W(e,t){return z(e,{nesting:R(e.nesting),enclosing:void 0},()=>X(e,t,`&`))}function De({generator:e},t,n){if(/^screen$/i.test(t)){let t=v(n,e.config.theme);return t===void 0?void 0:`@media ${t}`}return/^(?:media|supports|container|layer|starting-style)$/i.test(t)?`@${t} ${n}`:void 0}function Oe({generator:e},t){let n=/^theme\(\s*(?:"([^"]*)"|'([^']*)')\s*[,)]/.exec(t);return n!==null&&k(n[1]??n[2],e.config.theme,!1)!==void 0&&g(t)}function G(e,[t,n]){let{source:r,hasFn:i,preprocessor:a}=e;return!i||!O(r.slice(t,n))?[]:p(r.slice(t,n),t,(t,n)=>n!==`.`&&n!==`#`&&(a?Oe(e,t):/^theme\(\s*["']/.test(t)))}function K(e,t){if(G(e,t).length)throw Error(`[@teacss] theme() cannot be resolved in "${e.source.slice(...t).trim()}"; use it in a declaration value, an at-rule condition or a preprocessor value${e.filename?` in ${e.filename}`:``}`)}async function q(e,t,n,r){let i=e.source.slice(...t);for(let a of G(e,t).reverse()){let o=await F(e,a,n,r);o!==void 0&&(i=`${i.slice(0,a.loc.start.offset-t[0])}${o}${i.slice(a.loc.end.offset-t[0])}`)}return i}async function J(e,t){return e.preprocessor?q(e,t,`value`,e.literalColors):(K(e,t),e.source.slice(...t))}async function ke(e,t){let[n,r]=t.prelude,i=/^(?:include|mixin|function)$/.test(t.name)?/^[\w.-]+/.exec(e.source.slice(n,r))?.[0].length??0:0,a=/^(?:media|supports|container)$/i.test(t.name);return!a&&!e.preprocessor?(K(e,[n+i,r]),e.source.slice(n,r)):e.source.slice(n,n+i)+await q(e,[n+i,r],a?void 0:t.name,a||e.literalColors)}async function Y(e,t,n){let r=await ke(e,t);e.hasScreen&&t.name===`screen`&&he(e,t);let{block:i}=t;if(!i)return;if(e.preprocessor&&Te(e,t))return W(e,i);if(/keyframes$/i.test(t.name))return z(e,{nesting:`keyframes`,enclosing:e.enclosing},()=>X(e,i,void 0));let a=t.name===`scope`,o=a?`:scope`:n,s;if(e.enclosing&&o===`&`){let n=De(e,t.name,r);n!==void 0&&(s=e.enclosing.within(n))}await z(e,{nesting:a&&!e.preprocessor&&e.nesting===`top-level`?`scope`:e.nesting,enclosing:s,literalColors:e.literalColors||ye(t.name)},()=>X(e,i,o))}async function X(e,t,n){if(n!==void 0&&e.hasApply&&we(e,t))return Q(e,{kind:`nested`,block:t,host:n,enclosing:e.enclosing});await B(e,x(e.source,t[0],t[1],e.preprocessor),n)}async function Z(e,t){let n=await J(e,t.prelude);if(e.preprocessor&&Ee(n))return W(e,t.block);if(e.hasApply&&we(e,[t.start,t.end]))return Q(e,{kind:`rule`,rule:t,selector:n,enclosing:e.enclosing?.nested(n,e.preprocessor)});await z(e,{nesting:R(e.nesting),enclosing:e.enclosing},()=>B(e,x(e.source,t.block[0],t.block[1],e.preprocessor),`&`))}function Ae({nesting:e},t){return t.enclosing||t.kind!==`rule`?t.enclosing:y(t.selector)?oe.root(t.selector,t.rule.end,e):void 0}async function Q(e,t){let n=Ae(e,t),r=n!==void 0,i=t.kind===`rule`?R(e.nesting):e.nesting,a=await ue(e,t,t=>r?Promise.resolve():z(e,{nesting:i,enclosing:void 0},()=>Z(e,t))),o=t.kind===`rule`?t.rule.block:t.block;await z(e,{nesting:i,enclosing:n},async()=>{for(let n of x(e.source,o[0],o[1],e.preprocessor))n.kind===`declaration`?e.applyVariable.includes(n.name)||await $(e,n):n.kind===`at-rule`?n.name!==`apply`&&await Y(e,n,t.kind===`rule`?`&`:t.host):n.kind===`call`?await V(e,n):r&&await Z(e,n)}),de(e,t,n,a)}async function $(e,{name:t,value:n}){if(!e.hasFn)return;let r=e.source.slice(...n);if(O(r)||D(r))for(let i of p(r,n[0]))await F(e,i,t,e.literalColors)}async function je(e,t){let{base:n,entryFile:r,readSources:i,contents:a}=t,o=await re(e,n,r),s=await C(o.config),c=[],l=[];for(let e of o.config.content?.filesystem??[])e.startsWith(`!`)?l.push(e.slice(1)):c.push(e);let{files:u,sources:d}=a?{files:[],sources:a}:i?await i(c,l):{files:[],sources:[]},f=await ie(s,u,d);return{layers:f.getLayers()||``,files:u,unknown:o.unknown,matched:f.matched.size,hasTeacss:o.hasTeacss,hasPreset:!!o.config.presets?.length}}const Me=/\/\*[\s\S]*?\*\//g;function Ne(e,t){let n=[],r=[],i=[],a=!0,o=0;for(let s of j(e))a&&=!e.slice(o,s.start).replace(Me,``).trim(),o=s.end,s.name===`charset`&&s.terminator===`;`?(n.push(e.slice(s.start,s.end)),i.push({start:s.start,end:s.end,replacement:``})):(s.name===`import`||s.name===`layer`&&a)&&s.terminator===`;`?(r.push(e.slice(s.start,s.end)),i.push({start:s.start,end:s.end,replacement:``})):(A.includes(s.name)||s.name===`presets`||s.name===`source`||s.name===`shortcut`||s.name===`safelist`)&&s.terminator===`;`||s.name===`custom`&&(s.terminator===`block`||s.terminator===`;`)?i.push({start:s.start,end:s.end,replacement:``}):(s.name===`teacss`&&s.prelude===``&&s.terminator!==`block`&&i.push({start:s.start,end:s.end,replacement:t}),a=!1);i.sort((e,t)=>e.start-t.start);let s=``;o=0;for(let t of i)s+=e.slice(o,t.start)+t.replacement,o=t.end;s+=e.slice(o);let c=s.startsWith(``)?``:``;c&&(s=s.slice(1));let l=[...n,...r];return c+(l.length?`${l.join(`
5
- `)}\n`:``)+s}function Pe(e={}){return{name:`@teacss/directives`,enforce:e.enforce,idFilter:w,transform:(t,n,r)=>L(t,r.generator,e,n)}}export{Pe as default,je as generateEntry,Ne as spliceEntry,L as transformDirectives};
1
+ import{C as e,S as t,_ as n,a as r,b as i,c as a,d as o,f as s,g as c,h as l,i as u,l as d,m as f,n as p,o as m,p as h,r as g,s as _,t as v,u as y,v as b,x,y as S}from"./screen-core-FmDUvprA.js";import{createGenerator as C,expandClassGroups as ee,expandVariantGroup as te,isCssId as w,splitClassTokens as T,toArray as E}from"@teacss/core";import{hasIconFn as D,hasThemeFn as O,transformThemeFn as ne,transformThemeString as k}from"@teacss/rule-helpers";import{ROLE_DIRECTIVES as A,blankLineComments as re,cssDialectOf as j,customPropertyValueEnd as ie,declarationStartsAfter as ae,resolveEntryConfig as oe,scanTopLevelAtRules as M}from"@teacss/config";import{generateSources as se}from"@teacss/runtime/source-generation";var ce=class e{moved;head;outer;depth;constructor(e,t,n,r){this.moved=e,this.head=t,this.outer=n,this.depth=r}static root(t){return new e({end:t,css:``,open:[]},``,void 0,0)}within(t){return new e(this.moved,t,this,this.depth+1)}place(e){let{moved:t}=this,n=[],r=this;for(;r.depth>0&&t.open[r.depth-1]!==r;)n.push(r),r=r.outer;t.css+=`}`.repeat(t.open.length-r.depth),t.open.length=r.depth;for(let e of n.reverse())t.css+=`${e.head}{`,t.open.push(e);t.css+=e}},le=class e{hosts;inPlace;plain;nesting;constructor(e,t,n,r){this.hosts=e,this.inPlace=t,this.plain=n,this.nesting=r}static root(t,n,r){return new e([{condition:ce.root(n),selector:t,element:``}],void 0,!1,r)}within(t){let n=this.hosts.map(e=>({...e,condition:e.condition.within(t)}));return new e(n,this.inPlace===void 0?void 0:`&`,this.plain,this.nesting)}nested(t,r){let i=n(t);if(!i)return;let{taking:a,others:o}=i,s,c=this.plain;if(this.hosts.every(e=>e.nested===void 0)){let e=this.hosts[0],{groups:t,plain:n}=f(e.selector);c=!r&&n.length>0,r&&n.length&&t.push({bases:n,element:``}),s=t.map(({bases:t,element:n})=>{let i=b(a,this.anchored(t,a),r);return{condition:e.condition,selector:S(i,n),nested:d(i),element:n}})}else{let e=d(a);s=this.hosts.map(t=>({condition:t.condition.within(t.nested??``),selector:S(a,t.element),nested:e,element:t.element}))}let l=c?t.trim():o.length?d(o):void 0;return new e(s,l,c,this.nesting)}anchored(e,t){return this.nesting===`style-rule`?_(e,t,!1):this.nesting===`scope`?_(e,t,!0):e}standaloneHosts(){let e=this.hosts.map((e,t)=>[t,e.selector]);return this.inPlace===void 0?e:[...e,[void 0,this.inPlace]]}};function ue(e){return e.kind===`rule`?e.rule.block:e.block}function de(e){return e.enclosing?e.enclosing.standaloneHosts():[[void 0,e.kind===`rule`?e.selector:e.host]]}function N(e,t){return Error(`[@teacss] @apply: "${e}" is outside a style rule or @scope${t?` in ${t}`:``}`)}function fe(e,t){return Error(`[@teacss] @apply: "${e}" needs a selector or condition, which a @keyframes block cannot hold${t?` in ${t}`:``}`)}async function pe(e,t,n){let r=[],i=[],a=!1,[o,s]=ue(t);for(let c of x(e.source,o,s,e.preprocessor)){if(c.kind===`rule`){await n(c);continue}let o=await ge(e,t,c);o&&(r.push(o.consumedRange),i.push(...o.standalone),a||=o.hasInlineUtility)}if(t.kind===`rule`&&!a&&r.length>0){let n=o;r.every(([t,r])=>{let i=!e.source.slice(n,t).trim();return n=r,i})&&!e.source.slice(n,s).trim()&&e.code.remove(t.rule.start,t.rule.end)}return i}function me({code:e},t,n,r){let i=t.kind===`rule`?t.rule.end:t.block[1];if(t.kind===`rule`&&!t.enclosing&&n){let{moved:t}=n.hosts[0].condition;t.css&&(e.appendLeft(t.end,t.css+`}`.repeat(t.open.length)),t.css=``,t.open=[])}for(let n of r)n.moved!==void 0&&t.enclosing?t.enclosing.hosts[n.moved].condition.place(n.css):e.appendLeft(i,n.css)}const he=e=>e===`style-rule`||e===`keyframes`;async function ge(e,t,n){let{code:i,source:u,generator:d,applyVariable:f,options:p,filename:m,applyUtilities:g,hoisted:_,nesting:v}=e,y,b;if(n.kind===`at-rule`&&n.name===`apply`)b=n.prelude[1],y=F(u.slice(...n.prelude));else if(n.kind===`declaration`&&f.includes(n.name))b=u[n.end-1]===`;`?n.end-1:n.end,y=T(F(u.slice(n.value[0],b).trim())).map(e=>F(e)).join(` `);else return;if(!y)return;y=_e(y),y=ne(y,d.config.theme,p.throwOnMissing??!0,r(d));let x=T(te(ee(y))),S=await c(x,d),C=o(S);if(C.length&&(p.throwOnMissing??!0))throw Error(`${a(C)}${m?` in ${m}`:``}`);let w=h(S,d.parentOrders,d.config.layers,e=>d.sheetOrder(e));g.set(n.start,S.flatMap(([,e])=>e??[]));let E=[n.start,u[n.end-1]===`;`?n.end:b];if(!w.length)return i.remove(...E),{consumedRange:E,hasInlineUtility:!1,standalone:[]};let D,O=!1,k=[];for(let e of w)if(e.kind===`top-level`)v===`top-level`||v===`scope`?k.push({css:e.body}):P(_,e.body);else if(e.kind===`standalone`){D??=de(t).map(([e,t])=>[e,s(t)]);let n=e.selector?.startsWith(`@`)??!1;for(let[t,r]of D){let i=l(r,e,m);if(i){if(v===`keyframes`&&!n)throw fe(y,m);n&&he(v)?P(_,i):k.push({css:i,moved:t})}}}else O=!0,i.appendLeft(n.start,e.body);return i.remove(...E),{consumedRange:E,hasInlineUtility:O,standalone:k}}function P(e,t){for(let n of x(t,0,t.length,!1)){let r=t.slice(n.start,n.end);e.includes(r)||e.push(r)}}function F(e){return e.replace(/^(['"])(.*)\1$/,`$2`)}function _e(e){return e.replace(/\/\*[\s\S]*?\*\//g,``)}async function I({code:e,source:t,generator:n,options:r,applyUtilities:i},a,o,s){let c=await u(n,a,(e,n)=>t.slice(e,n),r.throwOnMissing??!0,s);if(c!==void 0)return e.overwrite(a.loc.start.offset,a.loc.end.offset,c),o!==void 0&&c.includes(`var(`)&&i.set(a.loc.start.offset,[[0,void 0,`${o}:${c};`,void 0,void 0,void 0,void 0]]),c}function ve({code:e,source:t,generator:n},r){let i=v(t.slice(...r.prelude),n.config.theme);i&&e.overwrite(r.start,r.block?r.block[0]-1:r.prelude[1],`@media ${i}`)}const ye=new Set([`presets`,`source`,`shortcut`,`safelist`,...A]);function be(e,t,n=`css`){let r=new Set;if(!t.includes(`@`))return r;let i=M(t,!1,n);if(i.some(e=>e.name===`teacss`&&e.prelude===``&&e.terminator!==`block`))return r;for(let t of i)(ye.has(t.name)?t.terminator===`;`:t.name===`custom`&&(t.terminator===`block`||t.terminator===`;`))&&(e.remove(t.start,t.end),r.add(t.start));return r}function xe(e){return A.some(t=>e.includes(`@${t}`))||e.includes(`@preset`)||e.includes(`@source`)||e.includes(`@shortcut`)||e.includes(`@safelist`)||e.includes(`@custom`)}function Se(e){return/^(?:property|font-face|font-palette-values|counter-style)$/i.test(e)}function Ce(e){return e?.split(`?`).flatMap(e=>/\.(scss|sass|less|styl|stylus)$/.exec(e)?.[1]??[])[0]}function we(e,t){let n=[];for(let r=t+1;r<e.length;r++){let i=e[r];if(i===`\\`)r++;else if(i===e[t])return{close:r,lineBreaks:n};else(i===`
2
+ `||i===`\r`||i===`\f`)&&n.push(r)}}function Te(n,r,a){let o=r!==`css`,s=``,c=0,l=0,u=0,d=0,f=!1,p=!0;for(let m=0;m<n.length;m++){let h=n[m];if(h===`/`&&n[m+1]===`*`){let e=n.indexOf(`*/`,m+2);m=e===-1?n.length:e+1;continue}if(o&&h===`/`&&n[m+1]===`/`){let e=m;for(;e<n.length&&n[e]!==`
3
+ `&&n[e]!==`\r`&&n[e]!==`\f`;)e++;s+=n.slice(c,m)+` `.repeat(e-m),c=e,m=e-1;continue}if(o&&p&&ie(n,m,r)>m&&a(/^--[\w-]*/.exec(n.slice(m,m+256))[0])){m=ie(n,m,r)-1,p=!1;continue}if(p=ae(h,p,r),h===`\\`)m++;else if(h===`"`||h===`'`){let e=we(n,m);if(e){for(let t of e.lineBreaks)s+=`${n.slice(c,t)} `,c=t+1;m=e.close}else m=t(n,m,n.length)-1}else if(i(n,m)){let t=e(n,m,n.length,o);f=t===-1,m=(f?n.length:t)-1}else if(h===`(`)d++;else if(h===`)`&&d)d--;else if(d&&!o)continue;else h===`}`?l=Math.max(0,l-1):h===`{`&&(u=Math.max(u,++l))}return{source:c?s+n.slice(c):n,blockDepth:u,unclosedUrl:f}}function Ee(e,t){return e.startsWith(t)&&!/[\w-]/.test(e[t.length]??``)}function L(e,t,n,r){return x(e,t,n,!0).some(t=>{let n=t.kind===`rule`||t.kind===`at-rule`?t.block:void 0;return G(e.slice(t.start,n?.[0]??t.end)).split(`
4
+ `).slice(1).some(e=>r.some(t=>Ee(e.trimStart(),t)))||n!==void 0&&L(e,n[0],n[1],r)})}async function R(e,t,n,r){let i=Ce(r),a=E((n.applyVariable??[`--at-apply`])||[]),{source:o,blockDepth:s,unclosedUrl:c}=Te(e.original,j(r),e=>!a.includes(e)),l=o.includes(`@apply`)||a.some(e=>o.includes(e)),u=o.includes(`@screen`),d=O(o)||D(o),f=xe(o)?be(e,o,j(r)):new Set;if(!l&&!d&&!u)return;if(i===`sass`)throw Error(`TeaCSS directives need braces; indented Sass is not supported, write it as SCSS${r?` in ${r}`:``}`);if(s>512)throw Error(`[@teacss] CSS blocks nest deeper than 512 levels${r?` in ${r}`:``}`);if(c)throw Error(`[@teacss] url() is not closed${r?` in ${r}`:``}`);if((i===`styl`||i===`stylus`)&&L(o,0,o.length,[`@apply`,`@screen`,...a]))throw Error(`TeaCSS directives need braces; indented Stylus is not supported, write it with braces${r?` in ${r}`:``}`);let p=new Map,h=[],g={options:n,applyVariable:a,generator:t,code:e,source:o,filename:r,preprocessor:i!==void 0,applyUtilities:p,hoisted:h,nesting:`top-level`,literalColors:!1,hasApply:l,hasScreen:u,hasFn:d,stripped:f};await H(g,x(o,0,o.length,g.preprocessor),void 0),h.length&&e.appendLeft(e.original.length,`\n${h.join(``)}`);let _=[...p].sort(([e],[t])=>e-t).flatMap(([,e])=>e),v=await m(t,_,r);v&&e.appendLeft(e.original.length,`\n${v}`)}const z=({source:e,applyVariable:t},[n,r])=>{let i=e.slice(n,r);return i.includes(`@apply`)||t.some(e=>i.includes(e))},B=e=>e===`top-level`||e===`scope`?`style-rule`:e;async function V(e,t,n){let{nesting:r,enclosing:i,literalColors:a}=e;Object.assign(e,t);try{await n()}finally{Object.assign(e,{nesting:r,enclosing:i,literalColors:a})}}async function H(e,t,n){for(let r of t)if(!e.stripped.has(r.start)){if(r.kind===`rule`)await $(e,r);else if(r.kind===`at-rule`){if(r.name===`apply`)throw N(e.source.slice(...r.prelude),e.filename);await Me(e,r,n)}else if(r.kind===`declaration`){if(n===void 0&&e.hasApply&&e.applyVariable.includes(r.name))throw N(e.source.slice(...r.value),e.filename);await Fe(e,r)}else await U(e,r)}}async function U(e,{start:t,end:n,rulesets:r}){await Z(e,[t,r[0]?.[0]??n]);for(let t of r)await K(e,t)}function De({source:e},{name:t,prelude:n}){return t===`mixin`||t===`include`||e.slice(...n)===`:`}function W(e,n,r,i){let a=0;for(let o=n;o<e.length;o++){let n=e[o];if(n===`\\`)o++;else if(n===`"`||n===`'`)o=t(e,o,e.length)-1;else if(n===r)a++;else if(n===i&&--a===0)return o+1}return e.length}function G(e){let n=``,r=0;for(let i=0;i<e.length;){let a=e[i],o;if(a===`\\`){i+=2;continue}if(a===`"`||a===`'`)o=t(e,i,e.length);else if(a===`[`)o=W(e,i,`[`,`]`);else if((a===`#`||a===`@`)&&e[i+1]===`{`)o=W(e,i+1,`{`,`}`);else if(e.startsWith(`/*`,i)){let t=e.indexOf(`*/`,i+2);o=t===-1?e.length:t+2}else{i++;continue}n+=e.slice(r,i)+` `.repeat(o-i),r=o,i=o}return n+e.slice(r)}function Oe(e){let t=G(e);return/^[.#]?[\w-]+\s*\(/.test(t)||/[\s)]when[\s(]/.test(t)||t.includes(`:extend(`)}function K(e,t){return V(e,{nesting:B(e.nesting),enclosing:void 0},()=>Q(e,t,`&`))}function ke({generator:e},t,n){if(/^screen$/i.test(t)){let t=v(n,e.config.theme);return t===void 0?void 0:`@media ${t}`}return/^(?:media|supports|container|layer|starting-style)$/i.test(t)?`@${t} ${n}`:void 0}function Ae({generator:e},t){let n=/^theme\(\s*(?:"([^"]*)"|'([^']*)')\s*[,)]/.exec(t);return n!==null&&k(n[1]??n[2],e.config.theme,!1)!==void 0&&g(t)}function q(e,[t,n]){let{source:r,hasFn:i,preprocessor:a}=e;return!i||!O(r.slice(t,n))?[]:p(r.slice(t,n),t,(t,n)=>n!==`.`&&n!==`#`&&(a?Ae(e,t):/^theme\(\s*["']/.test(t)))}function J(e,t){if(q(e,t).length)throw Error(`[@teacss] theme() cannot be resolved in "${e.source.slice(...t).trim()}"; use it in a declaration value, an at-rule condition or a preprocessor value${e.filename?` in ${e.filename}`:``}`)}function Y({source:e,hasFn:t,filename:n},[r,i]){let a=e.slice(r,i);if(t&&D(a)&&p(a,r,(e,t)=>t!==`.`&&t!==`#`&&/^icon\(\s*["']/.test(e)).length)throw Error(`[@teacss] icon() cannot be resolved in "${a.trim()}"; use it in a declaration value${n?` in ${n}`:``}`)}async function X(e,t,n,r){let i=e.source.slice(...t);for(let a of q(e,t).reverse()){let o=await I(e,a,n,r);o!==void 0&&(i=`${i.slice(0,a.loc.start.offset-t[0])}${o}${i.slice(a.loc.end.offset-t[0])}`)}return i}async function Z(e,t){return e.preprocessor?X(e,t,`value`,e.literalColors):(J(e,t),Y(e,t),e.source.slice(...t))}async function je(e,t){let[n,r]=t.prelude,i=/^(?:include|mixin|function)$/.test(t.name)?/^(?:\s|\/\*[\s\S]*?\*\/)*[\w.-]+/.exec(e.source.slice(n,r))?.[0].length??0:0,a=/^(?:media|supports|container|custom-media|import)$/i.test(t.name);return e.preprocessor||Y(e,[n+i,r]),!a&&!e.preprocessor?(J(e,[n+i,r]),e.source.slice(n,r)):e.source.slice(n,n+i)+await X(e,[n+i,r],a?void 0:t.name,a||e.literalColors)}async function Me(e,t,n){let r=await je(e,t);e.hasScreen&&t.name===`screen`&&ve(e,t);let{block:i}=t;if(!i)return;if(e.preprocessor&&De(e,t))return K(e,i);if(/keyframes$/i.test(t.name))return V(e,{nesting:`keyframes`,enclosing:e.enclosing},()=>Q(e,i,void 0));let a=t.name===`scope`,o=a?`:scope`:n,s;if(e.enclosing&&o===`&`){let n=ke(e,t.name,r);n!==void 0&&(s=e.enclosing.within(n))}await V(e,{nesting:a&&!e.preprocessor&&e.nesting===`top-level`?`scope`:e.nesting,enclosing:s,literalColors:e.literalColors||Se(t.name)},()=>Q(e,i,o))}async function Q(e,t,n){if(n!==void 0&&e.hasApply&&z(e,t))return Pe(e,{kind:`nested`,block:t,host:n,enclosing:e.enclosing});await H(e,x(e.source,t[0],t[1],e.preprocessor),n)}async function $(e,t){let n=await Z(e,t.prelude);if(e.preprocessor&&Oe(n))return K(e,t.block);if(e.hasApply&&z(e,[t.start,t.end]))return Pe(e,{kind:`rule`,rule:t,selector:n,enclosing:e.enclosing?.nested(n,e.preprocessor)});await V(e,{nesting:B(e.nesting),enclosing:e.enclosing},()=>H(e,x(e.source,t.block[0],t.block[1],e.preprocessor),`&`))}function Ne({nesting:e},t){return t.enclosing||t.kind!==`rule`?t.enclosing:y(t.selector)?le.root(t.selector,t.rule.end,e):void 0}async function Pe(e,t){let n=Ne(e,t),r=n!==void 0,i=t.kind===`rule`?B(e.nesting):e.nesting,a=await pe(e,t,t=>r?Promise.resolve():V(e,{nesting:i,enclosing:void 0},()=>$(e,t))),o=t.kind===`rule`?t.rule.block:t.block;await V(e,{nesting:i,enclosing:n},async()=>{for(let n of x(e.source,o[0],o[1],e.preprocessor))n.kind===`declaration`?e.applyVariable.includes(n.name)||await Fe(e,n):n.kind===`at-rule`?n.name!==`apply`&&await Me(e,n,t.kind===`rule`?`&`:t.host):n.kind===`call`?await U(e,n):r&&await $(e,n)}),me(e,t,n,a)}async function Fe(e,{name:t,value:n}){if(!e.hasFn)return;let r=e.source.slice(...n);if(O(r)||D(r))for(let i of p(r,n[0]))await I(e,i,t,e.literalColors)}async function Ie(e,t){let{base:n,entryFile:r,readSources:i,contents:a}=t,o=await oe(e,n,r),s=await C(o.config),c=[],l=[];for(let e of o.config.content?.filesystem??[])e.startsWith(`!`)?l.push(e.slice(1)):c.push(e);let{files:u,sources:d}=a?{files:[],sources:a}:i?await i(c,l):{files:[],sources:[]},f=await se(s,u,d);return{layers:f.getLayers()||``,files:u,unknown:o.unknown,matched:f.matched.size,hasTeacss:o.hasTeacss,hasPreset:!!o.config.presets?.length}}const Le=/\/\*[\s\S]*?\*\//g;function Re(n){let r=0;for(let a=0;a<n.length;a++){let o=n[a];if(o===`\\`){if(a===n.length-1)return!1;a++}else if(n.startsWith(`/*`,a)){let e=n.indexOf(`*/`,a+2);if(e<0)return!1;a=e+1}else if(o===`"`||o===`'`){let e=t(n,a,n.length);if(e-1===a||n[e-1]!==o)return!1;a=e-1}else if(i(n,a)){let t=e(n,a,n.length,!1);if(t<0)return!1;a=t-1}else if(o===`(`)r++;else if(o===`)`&&--r<0)return!1}return r===0}function ze(e,t,n=`css`){let r=[],i=[],a=[],o=!0,s=0,c=n===`css`?e:re(e,n);for(let l of M(e,!1,n)){o&&=!e.slice(s,l.start).replace(Le,``).trim(),s=l.end;let n=l.name.toLowerCase();if(n===`charset`&&l.terminator===`;`)r.push(e.slice(l.start,l.end)),a.push({start:l.start,end:l.end,replacement:``});else if((n===`import`||n===`layer`&&o)&&l.terminator===`;`)i.push(e.slice(l.start,l.end)),a.push({start:l.start,end:l.end,replacement:``});else if(n===`import`&&l.terminator===`eof`&&Re(c.slice(l.start,l.end).trimEnd())){let t=l.start+c.slice(l.start,l.end).trimEnd().length;i.push(`${e.slice(l.start,t)};${e.slice(t,l.end).trimEnd()}`),a.push({start:l.start,end:l.end,replacement:``})}else(A.includes(l.name)||l.name===`presets`||l.name===`source`||l.name===`shortcut`||l.name===`safelist`)&&l.terminator===`;`||l.name===`custom`&&(l.terminator===`block`||l.terminator===`;`)?a.push({start:l.start,end:l.end,replacement:``}):(l.name===`teacss`&&l.prelude===``&&l.terminator!==`block`&&a.push({start:l.start,end:l.end,replacement:t}),o=!1)}a.sort((e,t)=>e.start-t.start);let l=``;s=0;for(let t of a)l+=e.slice(s,t.start)+t.replacement,s=t.end;l+=e.slice(s);let u=l.startsWith(``)?``:``;u&&(l=l.slice(1));let d=[...r,...i];return u+(d.length?`${d.join(`
5
+ `)}\n`:``)+l}function Be(e={}){return{name:`@teacss/directives`,enforce:e.enforce,idFilter:w,transform:(t,n,r)=>R(t,r.generator,e,n)}}export{Be as default,Ie as generateEntry,ze as spliceEntry,R as transformDirectives};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teacss/directives",
3
- "version": "0.10.3",
3
+ "version": "0.10.5",
4
4
  "description": "Build-time CSS directive transformer for TeaCSS — @apply, theme(), icon(), and @screen.",
5
5
  "homepage": "https://css.teasim.com",
6
6
  "funding": "https://github.com/sponsors/billgo",
@@ -24,17 +24,17 @@
24
24
  "dev": "tsdown --watch"
25
25
  },
26
26
  "dependencies": {
27
- "@teacss/config": "0.10.3",
28
- "@teacss/core": "0.10.3",
29
- "@teacss/rule-helpers": "0.10.3",
30
- "@teacss/runtime": "0.10.3",
27
+ "@teacss/config": "0.10.5",
28
+ "@teacss/core": "0.10.5",
29
+ "@teacss/rule-helpers": "0.10.5",
30
+ "@teacss/runtime": "0.10.5",
31
31
  "@types/css-tree": "^3.2.0",
32
32
  "css-tree": "^3.2.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@teacss/preset-icons": "0.10.3",
36
- "@teacss/preset-official": "0.10.3",
37
- "@teacss/preset-standard": "0.10.3",
35
+ "@teacss/preset-icons": "0.10.5",
36
+ "@teacss/preset-official": "0.10.5",
37
+ "@teacss/preset-standard": "0.10.5",
38
38
  "magic-string": "^1.2.3"
39
39
  },
40
40
  "engines": {