Skip to content

[v4] Crash on unknown keywords #15085

@bep

Description

@bep
  • 4.0.0-beta.1
  • MacOS
  • @tailwindcss/cli

When I use an unknown keyword, e.g.:

body {
  @apply foo-bar;
}

The build fails with the less-than-useful error message below printed to stderr.

node_modules/tailwindcss/dist/lib.mjs:12
`);let t=[],n=[],e=null,o="",s;for(let a=0;a<r.length;a++){let g=r.charCodeAt(a);switch(g){case nt:case it:case ot:case lt:case at:case st:case ut:case ft:case ct:{if(o.length>0){let h=Pe(o);e?e.nodes.push(h):t.push(h),o=""}let m=a,f=a+1;for(;f<r.length&&(s=r.charCodeAt(f),!(s!==nt&&s!==it&&s!==ot&&s!==lt&&s!==at&&s!==st&&s!==ut&&s!==ft&&s!==ct));f++);a=f-1;let c=br(r.slice(m,f));e?e.nodes.push(c):t.push(c);break}case Cr:case xr:{let m=a;for(let f=a+1;f<r.length;f++)if(s=r.charCodeAt(f),s===wr)f+=1;else if(s===g){a=f;break}o+=r.slice(m,a+1);break}case Ar:{let m=yr(o,[]);o="",e?e.nodes.push(m):t.push(m),n.push(m),e=m;break}case kr:{let m=n.pop();if(o.length>0){let f=Pe(o);m.nodes.push(f),o=""}n.length>0?e=n[n.length-1]:e=null;break}default:o+=String.fromCharCode(g)}}return o.length>0&&t.push(Pe(o)),t}function H(r){if(r.indexOf("(")===-1)return de(r);let t=X(r);return Ue(t),r=W(t),r=He(r),r}function de(r){let t="";for(let n=0;n<r.length;n++){let e=r[n];e==="\\"&&r[n+1]==="_"?(t+="_",n+=1):e==="_"?t+=" ":t+=e}return t}function Ue(r){for(let t of r)switch(t.kind){case"function":{if(t.value==="url"||t.value.endsWith("_url")){t.value=de(t.value);break}if(t.value==="var"||t.value.endsWith("_var")||t.value==="theme"||t.value.endsWith("_theme")){t.value=de(t.value);for(let n=0;n<t.nodes.length;n++)n==0&&t.nodes[n].kind==="word"||Ue([t.nodes[n]]);break}t.value=de(t.value),Ue(t.nodes);break}case"separator":case"word":{t.value=de(t.value);break}default:$r(t)}}function $r(r){throw new Error(`Unexpected value: ${r}`)}var Nr=58,dt=45,pt=97,gt=122;function*mt(r,t){let n=_(r,":");if(t.theme.prefix){if(n.length===1||n[0]!==t.theme.prefix)return null;n.shift()}let e=n.pop(),o=[];for(let h=n.length-1;h>=0;--h){let d=t.parseVariant(n[h]);if(d===null)return;o.push(d)}let s=!1;e[e.length-1]==="!"?(s=!0,e=e.slice(0,-1)):e[0]==="!"&&(s=!0,e=e.slice(1)),t.utilities.has(e,"static")&&!e.includes("[")&&(yield{kind:"static",root:e,variants:o,important:s,raw:r});let[a,g=null,m]=_(e,"/");if(m)return;let f=g===null?null:Ie(g);if(g!==null&&f===null)return;if(a[0]==="["){if(a[a.length-1]!=="]")return;let h=a.charCodeAt(1);if(h!==dt&&!(h>=pt&&h<=gt))return;a=a.slice(1,-1);let d=a.indexOf(":");if(d===-1||d===0||d===a.length-1)return;let v=a.slice(0,d),y=H(a.slice(d+1));yield{kind:"arbitrary",property:v,value:y,modifier:f,variants:o,important:s,raw:r};return}let c;if(a[a.length-1]==="]"){let h=a.indexOf("-[");if(h===-1)return;let d=a.slice(0,h);if(!t.utilities.has(d,"functional"))return;let v=a.slice(h+1);c=[[d,v]]}else if(a[a.length-1]===")"){let h=a.indexOf("-(");if(h===-1)return;let d=a.slice(0,h);if(!t.utilities.has(d,"functional"))return;let v=a.slice(h+2,-1),y=_(v,":"),k=null;if(y.length===2&&(k=y[0],v=y[1]),v[0]!=="-"&&v[1]!=="-")return;c=[[d,k===null?`[var(${v})]`:`[${k}:var(${v})]`]]}else c=vt(a,h=>t.utilities.has(h,"functional"));for(let[h,d]of c){let v={kind:"functional",root:h,modifier:f,value:null,variants:o,important:s,raw:r};if(d===null){yield v;continue}{let y=d.indexOf("[");if(y!==-1){let $=H(d.slice(y+1,-1)),E="";for(let T=0;T<$.length;T++){let R=$.charCodeAt(T);if(R===Nr){E=$.slice(0,T),$=$.slice(T+1);break}if(!(R===dt||R>=pt&&R<=gt))break}if($.length===0||$.trim().length===0)continue;v.value={kind:"arbitrary",dataType:E||null,value:$}}else{let $=g===null||v.modifier?.kind==="arbitrary"?null:`${d}/${g}`;v.value={kind:"named",value:d,fraction:$}}}yield v}}function Ie(r){if(r[0]==="["&&r[r.length-1]==="]"){let t=H(r.slice(1,-1));return t.length===0||t.trim().length===0?null:{kind:"arbitrary",value:t}}if(r[0]==="("&&r[r.length-1]===")"){let t=H(r.slice(1,-1));return t.length===0||t.trim().length===0?null:{kind:"arbitrary",value:`var(${t})`}}return{kind:"named",value:r}}function ht(r,t){if(r[0]==="["&&r[r.length-1]==="]"){if(r[1]==="@"&&r.includes("&"))return null;let n=H(r.slice(1,-1));if(n.length===0||n.trim().length===0)return null;let e=n[0]===">"||n[0]==="+"||n[0]==="~";return!e&&n[0]!=="@"&&!n.includes("&")&&(n=`&:is(${n})`),{kind:"arbitrary",selector:n,relative:e}}{let[n,e=null,o]=_(r,"/");if(o)return null;let s=vt(n,a=>t.variants.has(a));for(let[a,g]of s)switch(t.variants.kind(a)){case"static":return g!==null||e!==null?null:{kind:"static",root:a};case"functional":{let m=e===null?null:Ie(e);if(e!==null&&m===null)return null;if(g===null)return{kind:"functional",root:a,modifier:m,value:null};if(g[0]==="["&&g[g.length-1]==="]"){let f=H(g.slice(1,-1));return f.length===0||f.trim().length===0?null:{kind:"functional",root:a,modifier:m,value:{kind:"arbitrary",value:f}}}if(g[0]==="("&&g[g.length-1]===")"){let f=H(g.slice(1,-1));return f.length===0||f.trim().length===0?null:{kind:"functional",root:a,modifier:m,value:{kind:"arbitrary",value:`var(${f})`}}}return{kind:"functional",root:a,modifier:m,value:{kind:"named",value:g}}}case"compound":{if(g===null)return null;let m=t.parseVariant(g);if(m===null||!t.variants.compoundsWith(a,m))return null;let f=e===null?null:Ie(e);return e!==null&&f===null?null:{kind:"compound",root:a,modifier:f,variant:m}}}}return null}function*vt(r,t){t(r)&&(yield[r,null]);let n=r.lastIndexOf("-");if(n===-1){r[0]==="@"&&t("@")&&(yield["@",r.slice(1)]);return}do{let e=r.slice(0,n);if(t(e)){let o=[e,r.slice(n+1)];if(o[1]==="")break;yield o}n=r.lastIndexOf("-",n-1)}while(n>0)}var yt="theme(";function oe(r,t){D(r,n=>{if(n.kind==="declaration"&&n.value?.includes(yt)){n.value=bt(n.value,t);return}n.kind==="at-rule"&&(n.name==="@media"||n.name==="@custom-media"||n.name==="@container"||n.name==="@supports")&&n.params.includes(yt)&&(n.params=bt(n.params,t))})}function bt(r,t){let n=X(r);return _e(n,(e,{replaceWith:o})=>{if(e.kind==="function"&&e.value==="theme"){if(e.nodes.length<1)throw new Error("Expected `theme()` function call to have a path. For example: `theme(--color-red-500)`.");e.nodes[0].kind==="separator"&&e.nodes[0].value.trim()===""&&e.nodes.shift();let s=e.nodes[0];if(s.kind!=="word")throw new Error(`Expected \`theme()\` function to start with a path, but instead found ${s.value}.`);let a=s.value,g=1;for(let f=g;f<e.nodes.length&&!e.nodes[f].value.includes(",");f++)a+=W([e.nodes[f]]),g=f+1;a=Tr(a);let m=e.nodes.slice(g+1);o(Vr(t,a,m))}}),W(n)}function Vr(r,t,n){let e=r(t);if(!e&&n.length>0)return n;if(!e)throw new Error(`Could not resolve value for theme function: \`theme(${t})\`. Consider checking if the path is correct or provide a fallback value to silence this error.`);return X(e)}function Tr(r){if(r[0]!=="'"&&r[0]!=='"')return r;let t="",n=r[0];for(let e=1;e<r.length-1;e++){let o=r[e],s=r[e+1];o==="\\"&&(s===n||s==="\\")?(t+=s,e++):t+=o}return t}function wt(r){let t=[];for(let n of r.utilities.keys("static"))t.push([n,{modifiers:[]}]);for(let n of r.utilities.keys("functional")){let e=r.utilities.getCompletions(n);for(let o of e)for(let s of o.values){let a=s===null?n:`${n}-${s}`;t.push([a,{modifiers:o.m

TRUNCATED...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions