svelte-shaker 0.4.0 → 0.5.0

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.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("svelte/compiler"),t=require("zimmerframe"),n=require("magic-string");function o(t,n){return e.parse(t,{modern:!0,filename:n})}function r(e,t,n){if(!n)return o(t,e);const r=n.get(e);if(r&&r.code===t)return r.ast;const s=o(t,e);return n.set(e,{code:t,ast:s}),s}function s(e,n,o){t.walk(e,n,o)}const a={known:!1};function i(e,t){if(!e)return a;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:a}case"UnaryExpression":{const n=i(e.argument,t);if(!n.known)return a;const o=n.value;switch(e.operator){case"!":return{known:!0,value:!o};case"-":return{known:!0,value:-o};case"+":return{known:!0,value:+o};case"typeof":return{known:!0,value:typeof o};case"void":return{known:!0,value:void 0};default:return a}}case"LogicalExpression":{const n=i(e.left,t);if(!n.known)return a;switch(e.operator){case"&&":return n.value?i(e.right,t):n;case"||":return n.value?n:i(e.right,t);case"??":return null===n.value||void 0===n.value?i(e.right,t):n;default:return a}}case"BinaryExpression":{const n=i(e.left,t),o=i(e.right,t);if(!n.known||!o.known)return a;const r=n.value,s=o.value;switch(e.operator){case"===":return{known:!0,value:r===s};case"!==":return{known:!0,value:r!==s};case"==":return{known:!0,value:r==s};case"!=":return{known:!0,value:r!=s};case"<":return{known:!0,value:r<s};case">":return{known:!0,value:r>s};case"<=":return{known:!0,value:r<=s};case">=":return{known:!0,value:r>=s};case"+":return{known:!0,value:r+s};case"-":return{known:!0,value:r-s};case"*":return{known:!0,value:r*s};case"/":return{known:!0,value:r/s};case"%":return{known:!0,value:r%s};default:return a}}default:return a}}function c(e,t,n){const o=i(e,t);if(o.known)return o;const r=u(e,t,n);return"unknown"===r?a:{known:!0,value:r}}function u(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?d(u(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=u(e.left,t,n),a=()=>u(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,t){return!0===e||!0===t||(!1!==e||!1!==t)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=f(e,o),a=i(t,n);if(s&&a.known)return l(s,a.value,r);const c=f(t,o),u=i(e,n);return c&&u.known?l(c,u.value,r):u.known&&a.known?r?u.value==a.value:u.value===a.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?d(s):s}return"unknown"}default:return"unknown"}var o,r}function f(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function l(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function d(e){return!0!==e&&(!1===e||"unknown")}function p(e,t,n){const{arms:o,elseFrag:r}=function(e){const t=[];let n,o=e;for(;o;){t.push({block:o,test:o.test,consequent:o.consequent});const e=o.alternate,r="Fragment"===e?.type&&1===e.nodes?.length&&"IfBlock"===e.nodes[0]?.type&&!0===e.nodes[0].elseif?e.nodes[0]:void 0;r?o=r:("Fragment"===e?.type&&(n=e),o=void 0)}return n?{arms:t,elseFrag:n}:{arms:t}}(e),s=[e.start,e.end],a=o.map((e=>c(e.test,t,n))),i=e=>e.known&&Boolean(e.value),u=e=>e.known&&!e.value;let f=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&f){const t=o[e].consequent;return{span:s,kept:t,removed:m(s,h(t)),recurse:!1}}u(t)||(f=!1)}const l=a.findIndex((e=>!u(e)));if(-1===l)return r?{span:s,kept:r,removed:m(s,h(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:v(o,a,l),recurse:!0};const d=o[l].block;return{span:s,kept:void 0,removed:[[s[0],d.start],...v(o,a,l)],recurse:!1,headerRewrite:{from:d.start,to:d.test.start,text:"{#if "}}}function m(e,t){if(!t)return[e];const n=[];return e[0]<t[0]&&n.push([e[0],t[0]]),t[1]<e[1]&&n.push([t[1],e[1]]),n}function h(e){const t=e?.nodes??[];return 0===t.length?null:[t[0].start,t[t.length-1].end]}function v(e,t,n){const o=[];for(let r=n+1;r<e.length;r++){const n=t[r];if(!n.known||n.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:w(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function w(e,t){const n=e?.nodes??[];return n.length?n[n.length-1].end:t}function g(e,t){return t.some((([t,n])=>e.start>=t&&e.end<=n))}function y(e,t,n){if(0===t.size&&0===n.size)return[];const o=[];return s(e,null,{IfBlock(e,{next:r}){if(e.elseif||g(e,o))return;const s=p(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const k=e=>e.endsWith(".svelte"),x=10,S="escapes as value (e.g. <svelte:component this={X}>)";async function b(e,t,n){return I(await M(e,t,n))}function I(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,z(r,n.get(r.id)??[],t));return o}(e,t),o=new Set;for(const e of n.values())for(const t of e.escapedComponents)o.add(t);for(const e of o){const t=n.get(e);t&&!t.bailReasons.includes(S)&&t.bailReasons.push(S)}let r=E(n,A(n,new Map));for(let e=0;e<x;e++){const e=E(n,A(n,P(n,r)));if(C(r,e)){r=e;break}r=e}return{models:n,plans:r}}async function M(e,t,n,o){const s=Array.isArray(e)?[...e]:[e],a=[],i=[],c=[...s],u=new Set(c);for(;c.length>0;){const e=c.shift(),s=await n(e);a.push({id:e,code:s});const f=r(e,s,o),l=f.instance;if(!l)continue;const d=new Map,p=new Map,m=[];for(const o of U(l)){if("*"===o.imported){p.set(o.local,o.value);continue}if("default"===o.imported&&k(o.value)){const n=await t(o.value,e);n&&(i.push({from:e,local:o.local,to:n,kind:"default-svelte"}),m.push(n));continue}const r=await H(o.value,o.imported,e,t,n);r&&(i.push({from:e,local:o.local,to:r,kind:"barrel"}),d.set(o.local,r))}const h=[];if(p.size>0)for(const o of L(f)){const r=o.indexOf("."),s=p.get(o.slice(0,r));if(null==s)continue;const a=await H(s,o.slice(r+1),e,t,n);a&&(i.push({from:e,local:o,to:a,kind:"namespace"}),h.push(a))}const v=R(f,d);for(const e of[...m,...v,...h])u.has(e)||(u.add(e),c.push(e))}return{files:a,edges:i,entries:s}}function A(e,t){const n=new Map,o=e=>{let t=n.get(e);return t||(t={sites:[]},n.set(e,t)),t};for(const n of e.values()){const e=t.get(n.id)??[];for(const t of n.childCalls)e.length>0&&g(t.node,e)||o(t.childId).sites.push(T(t.node))}return n}function E(e,t){const n=new Map;for(const o of e.values())n.set(o.id,q(o,t.get(o.id)));return n}function C(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!F(o.constFold,e.constFold))return!1;if(!D(o.narrow,e.narrow))return!1}return!0}function F(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e)if(!t.has(n)||!Object.is(t.get(n),o))return!1;return!0}function D(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e||o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!Object.is(o[t],e[t]))return!1}return!0}function P(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=y(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function z(e,t,n){const{id:o,code:a}=e,i=r(o,a,n),c=new Map;for(const e of t)c.set(e.local,e.to);const u=[];s(i.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||u.push(`<svelte:options ${t.name}>`);t()}});let f,l,d=null,p=!1;const m=new Set,h=new Set,v=i.instance;if(v){for(const e of U(v))m.add(e.local),"*"===e.imported&&h.add(e.local);const e=function(e){const t=e.content;for(const e of t?.body??[])if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("CallExpression"===n?.type&&"Identifier"===n.callee?.type&&"$props"===n.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(e){f=e.declaration,l=e.pattern,e.sharesStatement&&u.push("$props() shares a multi-declarator statement"),d=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){p=!0;continue}if("Property"!==t.type)continue;const e=t.key;if("Identifier"!==e?.type||!e.name)continue;const n=t.value,o="AssignmentPattern"===n?.type?n.right:void 0;d.push({name:e.name,property:t,defaultExpr:o})}}}const w=function(e,t){const n=[];return s(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.push({childId:r,node:e}),o()}}),n}(i,c),{shadowedNames:g,debugNames:y}=function(e,t,n){const o=new Set,r=new Set;t&&s(t,null,{_(e,{next:t}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===n||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const t of e.params??[])$(t,o);t()}});return s(e.fragment,null,{EachBlock(e,{next:t}){$(e.context,o),"string"==typeof e.index&&o.add(e.index),t()},SnippetBlock(e,{next:t}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const t of e.parameters??[])$(t,o);t()},AwaitBlock(e,{next:t}){$(e.value,o),$(e.error,o),t()},LetDirective(e,{next:t}){e.name&&o.add(e.name),t()},ConstTag(e,{next:t}){for(const t of e.declaration?.declarations??[])$(t.id,o);t()},DebugTag(e,{next:t}){for(const t of e.identifiers??[])"Identifier"===t.type&&t.name&&r.add(t.name);t()}}),{shadowedNames:o,debugNames:r}}(i,v,f),k=function(e,t,n,o){const r=new Set,a=e=>{if(!e)return;const n=t.get(e);if(n&&r.add(n),o.has(e))for(const[n,o]of t)n.startsWith(`${e}.`)&&r.add(o)};s(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&N(e,t.parent)&&a(e.name),o({parent:e})}}),e.instance&&s(e.instance,{parent:null},{_(e,{state:n,next:r}){"Identifier"===e.type&&e.name&&(t.has(e.name)||o.has(e.name))&&N(e,n.parent)&&!O(n.parent)&&a(e.name),r({parent:e})}});return r}(i,c,m,h);return{id:o,code:a,ast:i,imports:c,props:d,propsDeclaration:f,propsPattern:l,hasRestProp:p,childCalls:w,shadowedNames:g,debugNames:y,escapedComponents:k,bailReasons:u}}function $(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?$(n.argument,t):"Property"===n.type&&$(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])$(n,t);return;case"AssignmentPattern":return void $(e.left,t);case"RestElement":return void $(e.argument,t);default:return}}function N(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand)&&!O(t)))}function O(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function R(e,t){const n=new Set;return 0===t.size||s(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.add(r),o()}}),n}function L(e){const t=new Set;return s(e.fragment,null,{Component(e,{next:n}){"string"==typeof e.name&&e.name.includes(".")&&t.add(e.name),n()}}),t}function T(e){const t=e.attributes??[];let n=-1;for(let e=0;e<t.length;e++)"SpreadAttribute"===t[e].type&&(n=e);const o=new Map;for(let e=0;e<t.length;e++){const r=t[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,B(e,n));continue}if("Attribute"!==r.type||!s)continue;const a=_(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>n}:B(e,n))}for(const r of function(e){const t=e.fragment?.nodes??[],n=[];let o=!1;for(const e of t)if("SnippetBlock"!==e.type){if("Comment"!==e.type){if("Text"===e.type){if(""===(e.data??e.raw??"").trim())continue}o=!0}}else"Identifier"===e.expression?.type&&e.expression.name&&n.push(e.expression.name);o&&n.push("children");return n}(e))o.set(r,B(t.length,n));return{hadSpread:n>=0,explicit:o}}function B(e,t){return{value:void 0,dynamic:!0,afterLastSpread:e>t}}function _(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const t=Array.isArray(e)?e:[e];if(1===t.length){const e=t[0];return"Text"===e.type?{known:!0,value:e.data??e.raw??""}:"ExpressionTag"===e.type&&"Literal"===e.expression?.type?{known:!0,value:e.expression.value}:{known:!1}}let n="";for(const e of t){if("Text"!==e.type)return{known:!1};n+=e.data??e.raw??""}return{known:!0,value:n}}function j(e,t){return e.shadowedNames.has(t)||e.debugNames.has(t)}function q(e,t){const n={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return n.bail=!0,n.reasons.push(...e.bailReasons),n;if(!e.props||0===e.props.length)return n;const o=t?.sites??[];if(0===o.length)return n;for(const t of e.props){if(j(e,t.name))continue;const r=W(t,o);n.valueSets.set(t.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&n.narrow.set(t.name,r.values):n.constFold.set(t.name,r.values[0]))}return n}function W(e,t){const n=[];let o=!1,r=!1;const s=e=>{n.some((t=>Object.is(t,e)))||n.push(e)};for(const n of t){const t=n.explicit.get(e.name);if(t?.afterLastSpread){t.dynamic?o=!0:s(t.value);continue}if(n.hadSpread){r=!0;continue}const a=V(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function V(e){return e?"Literal"===e.type?{known:!0,value:e.value}:"Identifier"===e.type&&"undefined"===e.name?{known:!0,value:void 0}:{known:!1}:{known:!0,value:void 0}}function*U(e){const t=e.content;for(const e of t?.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t)for(const n of e.specifiers??[]){const e=n.local?.name;e&&("ImportDefaultSpecifier"===n.type?yield{value:t,local:e,imported:"default"}:"ImportNamespaceSpecifier"===n.type?yield{value:t,local:e,imported:"*"}:"ImportSpecifier"===n.type&&(yield{value:t,local:e,imported:J(n)??e}))}}}function J(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"Literal"===t?.type&&"string"==typeof t.value?t.value:void 0}function X(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const G=8;async function H(e,t,n,r,s,a=0){if(a>G)return null;const i=await r(e,n);if(!i)return null;if(k(e)||k(i))return"default"===t||"*"===t?i:null;let c;try{c=await s(i)}catch{return null}const u=function(e,t){try{const n=o(`<script module lang="ts">\n${e}\n<\/script>`,t);return n.module?.content?.body??null}catch{return null}}(c,i);if(!u)return null;for(const e of u)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(X(n.exported)===t)return H(String(e.source.value),X(n.local)??"default",i,r,s,a+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const n=await H(String(e.source.value),t,i,r,s,a+1);if(n)return n}}else for(const n of e.specifiers??[]){if(X(n.exported)!==t)continue;const e=X(n.local);if(!e)continue;const o=K(u,e);return o?H(o.value,o.imported,i,r,s,a+1):null}return null}function K(e,t){for(const n of e){if("ImportDeclaration"!==n.type)continue;const e=n.source?.value;if("string"==typeof e)for(const o of n.specifiers??[])if(o.local?.name===t){if("ImportDefaultSpecifier"===o.type)return{value:e,imported:"default"};if("ImportNamespaceSpecifier"===o.type)return{value:e,imported:"*"};if("ImportSpecifier"===o.type)return{value:e,imported:J(o)??t}}}return null}const Q=64;const Y=Symbol("unbounded-class-source");function Z(e,t,n){if(!0===e)return Y;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=ee(e,t,n);if(o===Y)return Y;const s=[];for(const e of r)for(const t of o)if(s.push(e+t),s.length>Q)return Y;r=s}const s=new Set;for(const e of r)for(const t of e.split(/\s+/))t&&s.add(t);return s}function ee(e,t,n){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,t,n){if(!e)return Y;if("Identifier"===e.type&&e.name&&n.has(e.name)){const t=new Set;for(const o of n.get(e.name))t.add(te(o));return t}const o=i(e,t);return o.known?new Set([te(o.value)]):Y}(e.expression,t,n):Y}function te(e){return String(e)}function ne(e,t,n){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,a=t.narrow;return s(e.ast.fragment,null,{_(e,{next:t}){if("RegularElement"===(s=e.type)||"SvelteElement"===s||"Component"===s||"SvelteComponent"===s||"SvelteSelf"===s){var s;for(const t of e.attributes??[]){if("SpreadAttribute"===t.type){o=!0;continue}if("ClassDirective"===t.type){t.name&&n.add(t.name);continue}if("Attribute"!==t.type||"class"!==t.name)continue;const e=Z(t.value,r,a);if(e===Y)o=!0;else for(const t of e)n.add(t)}t()}else t()}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let a=0;for(const t of o.children)"Rule"===t.type&&oe(t,r.classes)&&(re(e.code,t,o.children,n),a+=1);return a}function oe(e,t){if(function(e){let t=!1;return s(e,null,{_(e,{next:n}){"PseudoClassSelector"===e.type&&"global"===e.name&&(t=!0),n()}}),t}(e))return!1;const n=e.prelude?.children??[];return 0!==n.length&&n.every((e=>function(e,t){let n=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!t.has(e.name)&&(n=!0);return n}(e,t)))}function re(e,t,n,o){const r=n.indexOf(t),s=n[r-1];let a=t.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,t.end)}function se(e,t){return ie(e,ae(e,t))}function ae(e,t){const o=new Map,r=new Map,s=new Map;for(const a of e.values()){const e=new n(a.code);o.set(a.id,e);const i=t.get(a.id);if(i.bail){r.set(a.id,new Set);continue}const c=de(a,i,e);r.set(a.id,c.dropped),s.set(a.id,c.dead)}for(const t of e.values())ge(t,r,o.get(t.id),s.get(t.id)??[]);return o}function ie(e,t){const n={};for(const o of e.values())n[o.id]=t.get(o.id).toString();return n}function ce(e,t,n,o){const r=ae(e,t);return function(e,t,n,o){const r=new Map;for(const e of t){const t=r.get(e.owner);t?t.push(e):r.set(e.owner,[e])}for(const[t,s]of r){const r=e.get(t),a=o.get(t);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const t=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${t}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:n(e.variantId)})),ue(r.code,e.node,o,e.foldedProps,a)}c.length>0&&le(r,c,a)}}(e,n,o,r),ie(e,r)}function ue(e,t,n,o,r){const s=t.name;if(!s)return;const a=t.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,n);const i=`</${s}`,c=e.lastIndexOf(i,t.end);if(c>=t.start){const e=c+2;r.overwrite(e,e+s.length,n)}for(const n of t.attributes??[])"Attribute"===n.type&&n.name&&o.has(n.name)&&fe(e,n,r)}function fe(e,t,n){let o=t.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),n.remove(o,t.end)}function le(e,t,n){const o=t.map((e=>` import ${e.local} from ${JSON.stringify(e.spec)};`)).join("\n"),r=e.ast.instance,s=r?.content?.body??[];if(r&&s.length>0){const e=s[s.length-1];n.appendLeft(e.end,`\n${o}`)}else r&&r.content?n.appendLeft(r.content.start,`\n${o}\n`):n.prepend(`<script>\n${o}\n<\/script>\n`)}function de(e,t,n){const o=[];return{dropped:pe(e,t.constFold,t.narrow,t,n,o),dead:o}}function pe(e,t,n,o,r,a){if(0===t.size&&0===n.size)return new Set;const c=e.code,u=[];!function(e,t,n,o,r,a){s(e,null,{IfBlock(e,{next:s}){if(e.elseif||g(e,a))return;const i=p(e,t,n);if(function(e,t,n,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,t,n){const o=e?.nodes??[];return 0===o.length?"":me(o[0].start,o[o.length-1].end,o,t,n)}(e.kept,t,n));for(const[t,n]of e.removed)o.remove(t,n);if(e.headerRewrite){const{from:t,to:n,text:r}=e.headerRewrite;o.overwrite(t,n,r)}}(i,t,o,r),i.kept)a.push(i.span);else for(const e of i.removed)a.push(e);i.recurse&&s()}})}(e.ast.fragment,t,n,c,r,u),function(e,t,n,o,r){if(0===t.size)return;s(e,null,{ConditionalExpression(e,{next:s}){if(g(e,r))return;const a=i(e.test,t);if(!a.known)return void s();const c=a.value?e.consequent:e.alternate;c?(o.overwrite(e.start,e.end,me(c.start,c.end,[c],t,n)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,t,c,r,u);const f=function(e,t,n){const o=new Map,r=r=>{r&&he(r,t,e.code,((t,r,s)=>{g(s,n)||s===e.propsPattern||(o.get(t)??function(e,t){const n=[];return e.set(t,n),n}(o,t)).push(r)}))};return r(e.ast.instance),r(e.ast.fragment),o}(e,t,u);for(const[e,n]of t){const t=ke(n);for(const n of f.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const l=new Set(t.keys());!function(e,t,n){if(!e.props||0===t.size)return;const o=e.props.filter((e=>!t.has(e.name)));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,t,n){let o=t.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=t.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,t.start),a=e.slice(t.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?n.remove(o,r<e.length?r+1:r):n.remove(t.start,";"===e[t.end]?t.end+1:t.end)}(e.code,e.propsDeclaration,n);const r=e.propsPattern?.properties??[],s=new Set(e.props.filter((e=>t.has(e.name))).map((e=>e.property)));let a=0;for(;a<r.length;){if(!s.has(r[a])){a++;continue}let t=a;for(;t+1<r.length&&s.has(r[t+1]);)t++;ve(e.code,r,a,t,n),a=t+1}if(e.propsPattern)for(const o of e.props)t.has(o.name)&&we(e.propsPattern,o.name,n)}(e,l,r);return ne(e,{...o,constFold:t,narrow:n},r),a&&a.push(...u),l}function me(e,t,n,o,r){if(0===o.size)return r.slice(e,t);const s=[];for(const e of n)he(e,o,r,((e,t)=>s.push({...t,name:e})));if(0===s.length)return r.slice(e,t);s.sort(((e,t)=>e.start-t.start));let a="",i=e;for(const e of s)a+=r.slice(i,e.start),a+=e.head+ke(o.get(e.name))+e.tail,i=e.end;return a+=r.slice(i,t),a}function he(e,t,n,o){s(e,{parent:null,grandparent:null},{_(e,{state:r,next:s}){if("StyleDirective"===e.type&&!0===e.value&&e.name&&t.has(e.name)){let t=e.end;for(;t>e.start&&(" "===(a=n[t-1])||"\t"===a||"\n"===a||"\r"===a);)t-=1;const r=n.slice(e.start,t);o(e.name,{start:e.start,end:t,head:`${r}={`,tail:"}"},e)}else"Identifier"===e.type&&e.name&&t.has(e.name)&&!function(e,t){return!!t&&("MemberExpression"===t.type&&t.property===e&&!t.computed||("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand||("ImportSpecifier"===t.type||"ImportDefaultSpecifier"===t.type||"ImportNamespaceSpecifier"===t.type||"ExportSpecifier"===t.type)))}(e,r.parent)&&o(e.name,function(e,t,n,o){if("ClassDirective"===t?.type&&t.expression===e&&":"===o[e.start-1]){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}={`,tail:"}"}}if("ExpressionTag"===t?.type&&"Attribute"===n?.type&&n.name&&"{"===o[n.start])return{start:n.start,end:n.end,head:`${n.name}={`,tail:"}"};if("Property"===t?.type&&!0===t.shorthand&&t.value===e){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}: `,tail:""}}return{start:e.start,end:e.end,head:"",tail:""}}(e,r.parent,r.grandparent,n),e);var a;s({parent:e,grandparent:r.parent})}})}function ve(e,t,n,o,r){const s=t[n],a=t[o],i=t[o+1];if(i)return void r.remove(s.start,i.start);let c=a.end,u=c;for(;u<e.length&&/\s/.test(e[u]);)u++;","===e[u]&&(c=u+1);const f=t[n-1];r.remove(f?f.end:s.start,c)}function we(e,t,n){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex((e=>"Identifier"===e.key?.type&&e.key.name===t));if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?n.remove(s.start,a.start):i?n.remove(i.end,s.end):n.remove(s.start,s.end)}function ge(e,t,n,o){s(e.ast.fragment,null,{Component(r,{next:s}){if(o.length>0&&g(r,o))return;const a=r.name?e.imports.get(r.name):void 0,i=a?t.get(a):void 0;if(i&&i.size>0)for(const t of r.attributes??[])"Attribute"===t.type&&t.name&&i.has(t.name)&&ye(t.value)&&fe(e.code,t,n);s()}})}function ye(e){if(!0===e||null==e)return!0;return(Array.isArray(e)?e:[e]).every((e=>"Text"===e.type||"ExpressionTag"===e.type&&"Literal"===e.expression?.type))}function ke(e){return void 0===e?"undefined":JSON.stringify(e)}const xe={enabled:!1,maxVariants:8,minSavings:0};function Se(t,n,o=xe,r=[]){const s=new Map,a=[];if(!o.enabled)return{variants:s,bindings:a};const i=P(t,n),c=new Map,u=new Set;for(const e of t.values()){const o=i.get(e.id)??[];for(const r of e.childCalls){if(o.length>0&&g(r.node,o))continue;const s=t.get(r.childId),a=n.get(r.childId);if(!s||!a)continue;if(a.bail||!s.props||0===s.props.length){u.add(r.childId);continue}const i=Me(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const f=Ae(s,a,i);if(f===Ce(s,a)){u.add(r.childId);continue}const l=c.get(r.childId);l?l.push({owner:e.id,node:r.node,shape:i,code:f}):c.set(r.childId,[{owner:e.id,node:r.node,shape:i,code:f}])}}const f=function(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);n.set(o.id,e.bail?o.code:Ce(o,e))}return n}(t,n),l=new Map;for(const e of t.values())l.set(e.id,Ie(f.get(e.id),e));const d=new Set;for(const e of l.values())for(const t of e)d.add(t);const p=(Array.isArray(r)?r:[r]).filter((e=>t.has(e))).filter((e=>!d.has(e))),m=new Map,h=(t,n)=>{const o=m.get(n);if(void 0!==o)return o;let r;try{const{js:o}=e.compile(n,{generate:"client",dev:!1,filename:t});r=o.code.length}catch{r=null}return null!==r&&m.set(n,r),r},v=new Set;for(const e of c.keys())u.has(e)||v.add(e);for(const[e,n]of c){if(u.has(e))continue;if(n.some((t=>t.owner!==e&&v.has(t.owner))))continue;const r=new Map,i=[];let c=!1;for(const t of n){if(r.has(t.code))continue;if(i.length>=o.maxVariants){c=!0;break}const n=`${e}::v${i.length}`;r.set(t.code,n),i.push({id:n,code:t.code})}if(!c&&be(e,i,t,f,l,p,h,o.minSavings)){for(const t of i){const o=n.find((e=>e.code===t.code));s.set(t.id,{id:t.id,childId:e,code:t.code,foldedProps:o.shape})}for(const t of n)a.push({owner:t.owner,childId:e,node:t.node,variantId:r.get(t.code),foldedProps:t.shape})}}return{variants:s,bindings:a}}function be(e,t,n,o,r,s,a,i){const c=n.get(e),u=new Map;for(const e of t)u.set(e.id,Ie(e.code,c));const f=new Set,l=[...s];for(;l.length>0;){const e=l.pop();if(!f.has(e)){f.add(e);for(const t of r.get(e)??[])l.push(t)}}let d=0;for(const e of f){const t=a(e,o.get(e));if(null===t)return!1;d+=t}const p=t.map((e=>e.id)),m=new Set,h=new Set,v=t=>t===e?{comps:[],vars:p}:{comps:[t],vars:[]},w=[],g=[];for(const e of s){const t=v(e);w.push(...t.comps),g.push(...t.vars)}for(;w.length>0||g.length>0;){if(w.length>0){const e=w.pop();if(m.has(e))continue;m.add(e);for(const t of r.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!h.has(e)){h.add(e);for(const t of u.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const t=a(e,o.get(e));if(null===t)return!1;y+=t}for(const e of h){const n=t.find((t=>t.id===e)).code,o=a(e,n);if(null===o)return!1;y+=o}return y<d*(1-i)}function Ie(e,t){let n;try{n=o(e,t.id)}catch{return[]}const r=[];return s(n.fragment,null,{Component(e,{next:n}){const o=e.name?t.imports.get(e.name):void 0;o&&r.push(o),n()}}),r}function Me(e,t,n){const o=T(e),r=new Map;for(const e of t.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(n.constFold.has(e)||j(t,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Ae(e,t,o){const r=new Map(t.constFold);for(const[e,t]of o)r.set(e,t);const s=new Map;for(const[e,n]of t.narrow)r.has(e)||s.set(e,n);const a=new n(e.code);return pe(e,r,s,t,a),a.toString()}const Ee=new WeakMap;function Ce(e,t){const o=Ee.get(e);if(void 0!==o)return o;const r=new n(e.code);pe(e,t.constFold,t.narrow,t,r);const s=r.toString();return Ee.set(e,s),s}function Fe(e,t){for(const[n,r]of Object.entries(t)){const s=e.get(n);if(s&&r!==s.code)try{o(r,n)}catch{t[n]=s.code}}return t}exports.DEFAULT_MONO_OPTIONS=xe,exports.DevShaker=class{entries=new Set;resolve;readFile;mode;parseCache=new Map;codeCache=new Map;output={};constructor(e,t,n,o="incremental"){for(const t of Array.isArray(e)?e:[e])this.entries.add(t);this.resolve=t,this.readFile=n,this.mode=o}async init(){return this.output=await this.shake(),this.output}get(e){return this.output[e]}snapshot(){return{...this.output}}async update(e){const t="incremental"===this.mode;for(const t of e.removed??[])this.entries.delete(t),this.codeCache.delete(t),this.parseCache.delete(t);for(const n of e.added??[])this.entries.add(n),t&&this.codeCache.set(n,await this.readFile(n));for(const n of e.changed??[])t&&this.codeCache.set(n,await this.readFile(n));const n=this.output,o=await this.shake();this.output=o;const r={};for(const e of Object.keys(o))n[e]!==o[e]&&(r[e]=o[e]);return{changed:r,removed:Object.keys(n).filter((e=>!(e in o)))}}async shake(){const e="incremental"===this.mode,t=e?this.cachedReadFile:this.readFile,n=e?this.parseCache:void 0,o=await M([...this.entries],this.resolve,t,n),{models:r,plans:s}=I(o,n);return se(r,s)}cachedReadFile=async e=>{const t=this.codeCache.get(e);if(void 0!==t)return t;const n=await this.readFile(e);return this.codeCache.set(e,n),n}},exports.analyze=b,exports.analyzeInput=I,exports.buildAnalyzeInput=M,exports.monomorphize=Se,exports.svelteShaker=async function(e,t,n){const{models:o,plans:r}=await b(e,t,n);return Fe(o,se(o,r))},exports.svelteShakerWithMono=async function(e,t,n,o=xe,r=(e=>e)){const{models:s,plans:a}=await b(e,t,n),i=Se(s,a,o,e);return{files:Fe(s,0===i.bindings.length?se(s,a):ce(s,a,i.bindings.map((e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps}))),r)),mono:i}},exports.transformAll=se,exports.transformAllWithMono=ce;
1
+ "use strict";var e=require("svelte/compiler"),t=require("zimmerframe"),n=require("magic-string");function o(t,n){return e.parse(t,{modern:!0,filename:n})}function r(e,t,n,r=o){if(!n)return r(t,e);const s=n.get(e);if(s&&s.code===t)return s.ast;const a=r(t,e);return n.set(e,{code:t,ast:a}),a}function s(e,n,o){t.walk(e,n,o)}const a={known:!1};function i(e,t){if(!e)return a;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:a}case"UnaryExpression":{const n=i(e.argument,t);if(!n.known)return a;const o=n.value;switch(e.operator){case"!":return{known:!0,value:!o};case"-":return{known:!0,value:-o};case"+":return{known:!0,value:+o};case"typeof":return{known:!0,value:typeof o};case"void":return{known:!0,value:void 0};default:return a}}case"LogicalExpression":{const n=i(e.left,t);if(!n.known)return a;switch(e.operator){case"&&":return n.value?i(e.right,t):n;case"||":return n.value?n:i(e.right,t);case"??":return null===n.value||void 0===n.value?i(e.right,t):n;default:return a}}case"BinaryExpression":{const n=i(e.left,t),o=i(e.right,t);if(!n.known||!o.known)return a;const r=n.value,s=o.value;switch(e.operator){case"===":return{known:!0,value:r===s};case"!==":return{known:!0,value:r!==s};case"==":return{known:!0,value:r==s};case"!=":return{known:!0,value:r!=s};case"<":return{known:!0,value:r<s};case">":return{known:!0,value:r>s};case"<=":return{known:!0,value:r<=s};case">=":return{known:!0,value:r>=s};case"+":return{known:!0,value:r+s};case"-":return{known:!0,value:r-s};case"*":return{known:!0,value:r*s};case"/":return{known:!0,value:r/s};case"%":return{known:!0,value:r%s};default:return a}}default:return a}}function c(e,t,n){const o=i(e,t);if(o.known)return o;const r=u(e,t,n);return"unknown"===r?a:{known:!0,value:r}}function u(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?d(u(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=u(e.left,t,n),a=()=>u(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,t){return!0===e||!0===t||(!1!==e||!1!==t)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=f(e,o),a=i(t,n);if(s&&a.known)return l(s,a.value,r);const c=f(t,o),u=i(e,n);return c&&u.known?l(c,u.value,r):u.known&&a.known?r?u.value==a.value:u.value===a.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?d(s):s}return"unknown"}default:return"unknown"}var o,r}function f(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function l(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function d(e){return!0!==e&&(!1===e||"unknown")}function p(e,t,n){const{arms:o,elseFrag:r}=function(e){const t=[];let n,o=e;for(;o;){t.push({block:o,test:o.test,consequent:o.consequent});const e=o.alternate,r="Fragment"===e?.type&&1===e.nodes?.length&&"IfBlock"===e.nodes[0]?.type&&!0===e.nodes[0].elseif?e.nodes[0]:void 0;r?o=r:("Fragment"===e?.type&&(n=e),o=void 0)}return n?{arms:t,elseFrag:n}:{arms:t}}(e),s=[e.start,e.end],a=o.map(e=>c(e.test,t,n)),i=e=>e.known&&Boolean(e.value),u=e=>e.known&&!e.value;let f=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&f){const t=o[e].consequent;return{span:s,kept:t,removed:m(s,h(t)),recurse:!1}}u(t)||(f=!1)}const l=a.findIndex(e=>!u(e));if(-1===l)return r?{span:s,kept:r,removed:m(s,h(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:v(o,a,l),recurse:!0};const d=o[l].block;return{span:s,kept:void 0,removed:[[s[0],d.start],...v(o,a,l)],recurse:!1,headerRewrite:{from:d.start,to:d.test.start,text:"{#if "}}}function m(e,t){if(!t)return[e];const n=[];return e[0]<t[0]&&n.push([e[0],t[0]]),t[1]<e[1]&&n.push([t[1],e[1]]),n}function h(e){const t=e?.nodes??[];return 0===t.length?null:[t[0].start,t[t.length-1].end]}function v(e,t,n){const o=[];for(let r=n+1;r<e.length;r++){const n=t[r];if(!n.known||n.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:w(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function w(e,t){const n=e?.nodes??[];return n.length?n[n.length-1].end:t}function g(e,t){return t.some(([t,n])=>e.start>=t&&e.end<=n)}function y(e,t,n){if(0===t.size&&0===n.size)return[];const o=[];return s(e,null,{IfBlock(e,{next:r}){if(e.elseif||g(e,o))return;const s=p(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const k=e=>e.endsWith(".svelte"),x="escapes as value (e.g. <svelte:component this={X}>)";async function S(e,t,n){return b(await I(e,t,n))}function b(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,D(r,n.get(r.id)??[],t));return o}(e,t),o=new Set;for(const e of n.values())for(const t of e.escapedComponents)o.add(t);for(const e of o){const t=n.get(e);t&&!t.bailReasons.includes(x)&&t.bailReasons.push(x)}let r=A(n,M(n,new Map));for(let e=0;e<10;e++){const e=A(n,M(n,P(n,r)));if(E(r,e)){r=e;break}r=e}return{models:n,plans:r}}async function I(e,t,n,o,s){const a=Array.isArray(e)?[...e]:[e],i=[],c=[],u=[...a],f=new Set(u);for(;u.length>0;){const e=u.shift(),a=await n(e);i.push({id:e,code:a});const l=r(e,a,o,s),d=l.instance;if(!d)continue;const p=new Map,m=new Map,h=[];for(const o of V(d)){if("*"===o.imported){m.set(o.local,o.value);continue}if("default"===o.imported&&k(o.value)){const n=await t(o.value,e);n&&(c.push({from:e,local:o.local,to:n,kind:"default-svelte"}),h.push(n));continue}const r=await G(o.value,o.imported,e,t,n);r&&(c.push({from:e,local:o.local,to:r,kind:"barrel"}),p.set(o.local,r))}const v=[];if(m.size>0)for(const o of R(l)){const r=o.indexOf("."),s=m.get(o.slice(0,r));if(null==s)continue;const a=await G(s,o.slice(r+1),e,t,n);a&&(c.push({from:e,local:o,to:a,kind:"namespace"}),v.push(a))}const w=O(l,p);for(const e of[...h,...w,...v])f.has(e)||(f.add(e),u.push(e))}return{files:i,edges:c,entries:a}}function M(e,t){const n=new Map,o=e=>{let t=n.get(e);return t||(t={sites:[]},n.set(e,t)),t};for(const n of e.values()){const e=t.get(n.id)??[];for(const t of n.childCalls)e.length>0&&g(t.node,e)||o(t.childId).sites.push(L(t.node))}return n}function A(e,t){const n=new Map;for(const o of e.values())n.set(o.id,j(o,t.get(o.id)));return n}function E(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!C(o.constFold,e.constFold))return!1;if(!F(o.narrow,e.narrow))return!1}return!0}function C(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e)if(!t.has(n)||!Object.is(t.get(n),o))return!1;return!0}function F(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e||o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!Object.is(o[t],e[t]))return!1}return!0}function P(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=y(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function D(e,t,n){const{id:o,code:a}=e,i=r(o,a,n),c=new Map;for(const e of t)c.set(e.local,e.to);const u=[];s(i.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||u.push(`<svelte:options ${t.name}>`);t()}});let f,l,d=null,p=!1;const m=new Set,h=new Set,v=i.instance;if(v){for(const e of V(v))m.add(e.local),"*"===e.imported&&h.add(e.local);const e=function(e){const t=e.content;for(const e of t?.body??[])if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("CallExpression"===n?.type&&"Identifier"===n.callee?.type&&"$props"===n.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(e){f=e.declaration,l=e.pattern,e.sharesStatement&&u.push("$props() shares a multi-declarator statement"),d=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){p=!0;continue}if("Property"!==t.type)continue;const e=t.key;if("Identifier"!==e?.type||!e.name)continue;const n=t.value,o="AssignmentPattern"===n?.type?n.right:void 0;d.push({name:e.name,property:t,defaultExpr:o})}}}const w=function(e,t){const n=[];return s(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.push({childId:r,node:e}),o()}}),n}(i,c),{shadowedNames:g,debugNames:y}=function(e,t,n){const o=new Set,r=new Set;t&&s(t,null,{_(e,{next:t}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===n||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const t of e.params??[])z(t,o);t()}});return s(e.fragment,null,{EachBlock(e,{next:t}){z(e.context,o),"string"==typeof e.index&&o.add(e.index),t()},SnippetBlock(e,{next:t}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const t of e.parameters??[])z(t,o);t()},AwaitBlock(e,{next:t}){z(e.value,o),z(e.error,o),t()},LetDirective(e,{next:t}){e.name&&o.add(e.name),t()},ConstTag(e,{next:t}){for(const t of e.declaration?.declarations??[])z(t.id,o);t()},DebugTag(e,{next:t}){for(const t of e.identifiers??[])"Identifier"===t.type&&t.name&&r.add(t.name);t()}}),{shadowedNames:o,debugNames:r}}(i,v,f),k=function(e,t,n,o){const r=new Set,a=e=>{if(!e)return;const n=t.get(e);if(n&&r.add(n),o.has(e))for(const[n,o]of t)n.startsWith(`${e}.`)&&r.add(o)};s(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&$(e,t.parent)&&a(e.name),o({parent:e})}}),e.instance&&s(e.instance,{parent:null},{_(e,{state:n,next:r}){"Identifier"===e.type&&e.name&&(t.has(e.name)||o.has(e.name))&&$(e,n.parent)&&!N(n.parent)&&a(e.name),r({parent:e})}});return r}(i,c,m,h);return{id:o,code:a,ast:i,imports:c,props:d,propsDeclaration:f,propsPattern:l,hasRestProp:p,childCalls:w,shadowedNames:g,debugNames:y,escapedComponents:k,bailReasons:u}}function z(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?z(n.argument,t):"Property"===n.type&&z(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])z(n,t);return;case"AssignmentPattern":return void z(e.left,t);case"RestElement":return void z(e.argument,t);default:return}}function $(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand)&&!N(t)))}function N(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function O(e,t){const n=new Set;return 0===t.size||s(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.add(r),o()}}),n}function R(e){const t=new Set;return s(e.fragment,null,{Component(e,{next:n}){"string"==typeof e.name&&e.name.includes(".")&&t.add(e.name),n()}}),t}function L(e){const t=e.attributes??[];let n=-1;for(let e=0;e<t.length;e++)"SpreadAttribute"===t[e].type&&(n=e);const o=new Map;for(let e=0;e<t.length;e++){const r=t[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,T(e,n));continue}if("Attribute"!==r.type||!s)continue;const a=B(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>n}:T(e,n))}for(const r of function(e){const t=e.fragment?.nodes??[],n=[];let o=!1;for(const e of t)if("SnippetBlock"!==e.type){if("Comment"!==e.type){if("Text"===e.type){if(""===(e.data??e.raw??"").trim())continue}o=!0}}else"Identifier"===e.expression?.type&&e.expression.name&&n.push(e.expression.name);o&&n.push("children");return n}(e))o.set(r,T(t.length,n));return{hadSpread:n>=0,explicit:o}}function T(e,t){return{value:void 0,dynamic:!0,afterLastSpread:e>t}}function B(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const t=Array.isArray(e)?e:[e];if(1===t.length){const e=t[0];return"Text"===e.type?{known:!0,value:e.data??e.raw??""}:"ExpressionTag"===e.type&&"Literal"===e.expression?.type?{known:!0,value:e.expression.value}:{known:!1}}let n="";for(const e of t){if("Text"!==e.type)return{known:!1};n+=e.data??e.raw??""}return{known:!0,value:n}}function _(e,t){return e.shadowedNames.has(t)||e.debugNames.has(t)}function j(e,t){const n={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return n.bail=!0,n.reasons.push(...e.bailReasons),n;if(!e.props||0===e.props.length)return n;const o=t?.sites??[];if(0===o.length)return n;for(const t of e.props){if(_(e,t.name))continue;const r=q(t,o);n.valueSets.set(t.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&n.narrow.set(t.name,r.values):n.constFold.set(t.name,r.values[0]))}return n}function q(e,t){const n=[];let o=!1,r=!1;const s=e=>{n.some(t=>Object.is(t,e))||n.push(e)};for(const n of t){const t=n.explicit.get(e.name);if(t?.afterLastSpread){t.dynamic?o=!0:s(t.value);continue}if(n.hadSpread){r=!0;continue}const a=W(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function W(e){return e?"Literal"===e.type?{known:!0,value:e.value}:"Identifier"===e.type&&"undefined"===e.name?{known:!0,value:void 0}:{known:!1}:{known:!0,value:void 0}}function*V(e){const t=e.content;for(const e of t?.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t)for(const n of e.specifiers??[]){const e=n.local?.name;e&&("ImportDefaultSpecifier"===n.type?yield{value:t,local:e,imported:"default"}:"ImportNamespaceSpecifier"===n.type?yield{value:t,local:e,imported:"*"}:"ImportSpecifier"===n.type&&(yield{value:t,local:e,imported:U(n)??e}))}}}function U(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"Literal"===t?.type&&"string"==typeof t.value?t.value:void 0}function J(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const X=8;async function G(e,t,n,r,s,a=0){if(a>X)return null;const i=await r(e,n);if(!i)return null;if(k(e)||k(i))return"default"===t||"*"===t?i:null;let c;try{c=await s(i)}catch{return null}const u=function(e,t){try{const n=o(`<script module lang="ts">\n${e}\n<\/script>`,t);return n.module?.content?.body??null}catch{return null}}(c,i);if(!u)return null;for(const e of u)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(J(n.exported)===t)return G(String(e.source.value),J(n.local)??"default",i,r,s,a+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const n=await G(String(e.source.value),t,i,r,s,a+1);if(n)return n}}else for(const n of e.specifiers??[]){if(J(n.exported)!==t)continue;const e=J(n.local);if(!e)continue;const o=H(u,e);return o?G(o.value,o.imported,i,r,s,a+1):null}return null}function H(e,t){for(const n of e){if("ImportDeclaration"!==n.type)continue;const e=n.source?.value;if("string"==typeof e)for(const o of n.specifiers??[])if(o.local?.name===t){if("ImportDefaultSpecifier"===o.type)return{value:e,imported:"default"};if("ImportNamespaceSpecifier"===o.type)return{value:e,imported:"*"};if("ImportSpecifier"===o.type)return{value:e,imported:U(o)??t}}}return null}const K=Symbol("unbounded-class-source");function Q(e,t,n){if(!0===e)return K;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=Y(e,t,n);if(o===K)return K;const s=[];for(const e of r)for(const t of o)if(s.push(e+t),s.length>64)return K;r=s}const s=new Set;for(const e of r)for(const t of e.split(/\s+/))t&&s.add(t);return s}function Y(e,t,n){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,t,n){if(!e)return K;if("Identifier"===e.type&&e.name&&n.has(e.name)){const t=new Set;for(const o of n.get(e.name))t.add(Z(o));return t}const o=i(e,t);return o.known?new Set([Z(o.value)]):K}(e.expression,t,n):K}function Z(e){return String(e)}function ee(e,t,n){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,a=t.narrow;return s(e.ast.fragment,null,{_(e,{next:t}){if("RegularElement"===(s=e.type)||"SvelteElement"===s||"Component"===s||"SvelteComponent"===s||"SvelteSelf"===s){var s;for(const t of e.attributes??[]){if("SpreadAttribute"===t.type){o=!0;continue}if("ClassDirective"===t.type){t.name&&n.add(t.name);continue}if("Attribute"!==t.type||"class"!==t.name)continue;const e=Q(t.value,r,a);if(e===K)o=!0;else for(const t of e)n.add(t)}t()}else t()}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let a=0;for(const t of o.children)"Rule"===t.type&&te(t,r.classes)&&(ne(e.code,t,o.children,n),a+=1);return a}function te(e,t){if(function(e){let t=!1;return s(e,null,{_(e,{next:n}){"PseudoClassSelector"===e.type&&"global"===e.name&&(t=!0),n()}}),t}(e))return!1;const n=e.prelude?.children??[];return 0!==n.length&&n.every(e=>function(e,t){let n=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!t.has(e.name)&&(n=!0);return n}(e,t))}function ne(e,t,n,o){const r=n.indexOf(t),s=n[r-1];let a=t.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,t.end)}function oe(e,t){return se(e,re(e,t))}function re(e,t){const o=new Map,r=new Map,s=new Map;for(const a of e.values()){const e=new n(a.code);o.set(a.id,e);const i=t.get(a.id);if(i.bail){r.set(a.id,new Set);continue}const c=fe(a,i,e);r.set(a.id,c.dropped),s.set(a.id,c.dead)}for(const t of e.values())ve(t,r,o.get(t.id),s.get(t.id)??[]);return o}function se(e,t){const n={};for(const o of e.values())n[o.id]=t.get(o.id).toString();return n}function ae(e,t,n,o){const r=re(e,t);return function(e,t,n,o){const r=new Map;for(const e of t){const t=r.get(e.owner);t?t.push(e):r.set(e.owner,[e])}for(const[t,s]of r){const r=e.get(t),a=o.get(t);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const t=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${t}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:n(e.variantId)})),ie(r.code,e.node,o,e.foldedProps,a)}c.length>0&&ue(r,c,a)}}(e,n,o,r),se(e,r)}function ie(e,t,n,o,r){const s=t.name;if(!s)return;const a=t.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,n);const i=`</${s}`,c=e.lastIndexOf(i,t.end);if(c>=t.start){const e=c+2;r.overwrite(e,e+s.length,n)}for(const n of t.attributes??[])"Attribute"===n.type&&n.name&&o.has(n.name)&&ce(e,n,r)}function ce(e,t,n){let o=t.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),n.remove(o,t.end)}function ue(e,t,n){const o=t.map(e=>` import ${e.local} from ${JSON.stringify(e.spec)};`).join("\n"),r=e.ast.instance,s=r?.content?.body??[];if(r&&s.length>0){const e=s[s.length-1];return void n.appendLeft(e.end,`\n${o}`)}r&&r.content?n.appendLeft(r.content.start,`\n${o}\n`):n.prepend(`<script>\n${o}\n<\/script>\n`)}function fe(e,t,n){const o=[];return{dropped:le(e,t.constFold,t.narrow,t,n,o),dead:o}}function le(e,t,n,o,r,a){if(0===t.size&&0===n.size)return new Set;const c=e.code,u=[];!function(e,t,n,o,r,a){s(e,null,{IfBlock(e,{next:s}){if(e.elseif||g(e,a))return;const i=p(e,t,n);if(function(e,t,n,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,t,n){const o=e?.nodes??[];return 0===o.length?"":de(o[0].start,o[o.length-1].end,o,t,n)}(e.kept,t,n));for(const[t,n]of e.removed)o.remove(t,n);if(e.headerRewrite){const{from:t,to:n,text:r}=e.headerRewrite;o.overwrite(t,n,r)}}(i,t,o,r),i.kept)a.push(i.span);else for(const e of i.removed)a.push(e);i.recurse&&s()}})}(e.ast.fragment,t,n,c,r,u),function(e,t,n,o,r){if(0===t.size)return;s(e,null,{ConditionalExpression(e,{next:s}){if(g(e,r))return;const a=i(e.test,t);if(!a.known)return void s();const c=a.value?e.consequent:e.alternate;c?(o.overwrite(e.start,e.end,de(c.start,c.end,[c],t,n)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,t,c,r,u);const f=function(e,t,n){const o=new Map,r=r=>{r&&pe(r,t,e.code,(t,r,s)=>{g(s,n)||s===e.propsPattern||(o.get(t)??function(e,t){const n=[];return e.set(t,n),n}(o,t)).push(r)})};return r(e.ast.instance),r(e.ast.fragment),o}(e,t,u);for(const[e,n]of t){const t=ye(n);for(const n of f.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const l=new Set(t.keys());!function(e,t,n){if(!e.props||0===t.size)return;const o=e.props.filter(e=>!t.has(e.name));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,t,n){let o=t.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=t.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,t.start),a=e.slice(t.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?n.remove(o,r<e.length?r+1:r):n.remove(t.start,";"===e[t.end]?t.end+1:t.end)}(e.code,e.propsDeclaration,n);const r=e.propsPattern?.properties??[],s=new Set(e.props.filter(e=>t.has(e.name)).map(e=>e.property));let a=0;for(;a<r.length;){if(!s.has(r[a])){a++;continue}let t=a;for(;t+1<r.length&&s.has(r[t+1]);)t++;me(e.code,r,a,t,n),a=t+1}if(e.propsPattern)for(const o of e.props)t.has(o.name)&&he(e.propsPattern,o.name,n)}(e,l,r);return ee(e,{...o,constFold:t,narrow:n},r),a&&a.push(...u),l}function de(e,t,n,o,r){if(0===o.size)return r.slice(e,t);const s=[];for(const e of n)pe(e,o,r,(e,t)=>s.push({...t,name:e}));if(0===s.length)return r.slice(e,t);s.sort((e,t)=>e.start-t.start);let a="",i=e;for(const e of s)a+=r.slice(i,e.start),a+=e.head+ye(o.get(e.name))+e.tail,i=e.end;return a+=r.slice(i,t),a}function pe(e,t,n,o){s(e,{parent:null,grandparent:null},{_(e,{state:r,next:s}){if("StyleDirective"===e.type&&!0===e.value&&e.name&&t.has(e.name)){let t=e.end;for(;t>e.start&&ge(n[t-1]);)t-=1;const r=n.slice(e.start,t);o(e.name,{start:e.start,end:t,head:`${r}={`,tail:"}"},e)}else"Identifier"===e.type&&e.name&&t.has(e.name)&&!function(e,t){return!!t&&("MemberExpression"===t.type&&t.property===e&&!t.computed||("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand||(!("TSPropertySignature"!==t.type&&"TSMethodSignature"!==t.type||t.key!==e||t.computed)||("ImportSpecifier"===t.type||"ImportDefaultSpecifier"===t.type||"ImportNamespaceSpecifier"===t.type||"ExportSpecifier"===t.type))))}(e,r.parent)&&o(e.name,function(e,t,n,o){if("ClassDirective"===t?.type&&t.expression===e&&":"===o[e.start-1]){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}={`,tail:"}"}}if("ExpressionTag"===t?.type&&"Attribute"===n?.type&&n.name&&"{"===o[n.start])return{start:n.start,end:n.end,head:`${n.name}={`,tail:"}"};if("Property"===t?.type&&!0===t.shorthand&&t.value===e){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}: `,tail:""}}return{start:e.start,end:e.end,head:"",tail:""}}(e,r.parent,r.grandparent,n),e);s({parent:e,grandparent:r.parent})}})}function me(e,t,n,o,r){const s=t[n],a=t[o],i=t[o+1];if(i)return void r.remove(s.start,i.start);let c=a.end,u=c;for(;u<e.length&&/\s/.test(e[u]);)u++;","===e[u]&&(c=u+1);const f=t[n-1];r.remove(f?f.end:s.start,c)}function he(e,t,n){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?n.remove(s.start,a.start):i?n.remove(i.end,s.end):n.remove(s.start,s.end)}function ve(e,t,n,o){s(e.ast.fragment,null,{Component(r,{next:s}){if(o.length>0&&g(r,o))return;const a=r.name?e.imports.get(r.name):void 0,i=a?t.get(a):void 0;if(i&&i.size>0)for(const t of r.attributes??[])"Attribute"===t.type&&t.name&&i.has(t.name)&&we(t.value)&&ce(e.code,t,n);s()}})}function we(e){if(!0===e||null==e)return!0;return(Array.isArray(e)?e:[e]).every(e=>"Text"===e.type||"ExpressionTag"===e.type&&"Literal"===e.expression?.type)}function ge(e){return" "===e||"\t"===e||"\n"===e||"\r"===e}function ye(e){return void 0===e?"undefined":JSON.stringify(e)}const ke={enabled:!1,maxVariants:8,minSavings:0};function xe(t,n,o=ke,r=[]){const s=new Map,a=[];if(!o.enabled)return{variants:s,bindings:a};const i=P(t,n),c=new Map,u=new Set;for(const e of t.values()){const o=i.get(e.id)??[];for(const r of e.childCalls){if(o.length>0&&g(r.node,o))continue;const s=t.get(r.childId),a=n.get(r.childId);if(!s||!a)continue;if(a.bail||!s.props||0===s.props.length){u.add(r.childId);continue}const i=Ie(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const f=Me(s,a,i);if(f===Ee(s,a)){u.add(r.childId);continue}const l=c.get(r.childId);l?l.push({owner:e.id,node:r.node,shape:i,code:f}):c.set(r.childId,[{owner:e.id,node:r.node,shape:i,code:f}])}}const f=function(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);n.set(o.id,e.bail?o.code:Ee(o,e))}return n}(t,n),l=new Map;for(const e of t.values())l.set(e.id,be(f.get(e.id),e));const d=new Set;for(const e of l.values())for(const t of e)d.add(t);const p=(Array.isArray(r)?r:[r]).filter(e=>t.has(e)).filter(e=>!d.has(e)),m=new Map,h=(t,n)=>{const o=m.get(n);if(void 0!==o)return o;let r;try{const{js:o}=e.compile(n,{generate:"client",dev:!1,filename:t});r=o.code.length}catch{r=null}return null!==r&&m.set(n,r),r},v=new Set;for(const e of c.keys())u.has(e)||v.add(e);for(const[e,n]of c){if(u.has(e))continue;if(n.some(t=>t.owner!==e&&v.has(t.owner)))continue;const r=new Map,i=[];let c=!1;for(const t of n){if(r.has(t.code))continue;if(i.length>=o.maxVariants){c=!0;break}const n=`${e}::v${i.length}`;r.set(t.code,n),i.push({id:n,code:t.code})}if(!c&&Se(e,i,t,f,l,p,h,o.minSavings)){for(const t of i){const o=n.find(e=>e.code===t.code);s.set(t.id,{id:t.id,childId:e,code:t.code,foldedProps:o.shape})}for(const t of n)a.push({owner:t.owner,childId:e,node:t.node,variantId:r.get(t.code),foldedProps:t.shape})}}return{variants:s,bindings:a}}function Se(e,t,n,o,r,s,a,i){const c=n.get(e),u=new Map;for(const e of t)u.set(e.id,be(e.code,c));const f=new Set,l=[...s];for(;l.length>0;){const e=l.pop();if(!f.has(e)){f.add(e);for(const t of r.get(e)??[])l.push(t)}}let d=0;for(const e of f){const t=a(e,o.get(e));if(null===t)return!1;d+=t}const p=t.map(e=>e.id),m=new Set,h=new Set,v=t=>t===e?{comps:[],vars:p}:{comps:[t],vars:[]},w=[],g=[];for(const e of s){const t=v(e);w.push(...t.comps),g.push(...t.vars)}for(;w.length>0||g.length>0;){if(w.length>0){const e=w.pop();if(m.has(e))continue;m.add(e);for(const t of r.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!h.has(e)){h.add(e);for(const t of u.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const t=a(e,o.get(e));if(null===t)return!1;y+=t}for(const e of h){const n=t.find(t=>t.id===e).code,o=a(e,n);if(null===o)return!1;y+=o}return y<d*(1-i)}function be(e,t){let n;try{n=o(e,t.id)}catch{return[]}const r=[];return s(n.fragment,null,{Component(e,{next:n}){const o=e.name?t.imports.get(e.name):void 0;o&&r.push(o),n()}}),r}function Ie(e,t,n){const o=L(e),r=new Map;for(const e of t.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(n.constFold.has(e)||_(t,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Me(e,t,o){const r=new Map(t.constFold);for(const[e,t]of o)r.set(e,t);const s=new Map;for(const[e,n]of t.narrow)r.has(e)||s.set(e,n);const a=new n(e.code);return le(e,r,s,t,a),a.toString()}const Ae=new WeakMap;function Ee(e,t){const o=Ae.get(e);if(void 0!==o)return o;const r=new n(e.code);le(e,t.constFold,t.narrow,t,r);const s=r.toString();return Ae.set(e,s),s}async function Ce(e,t,n,o){if(!o)return S(e,t,n);const r=new Map;return b(await I(e,t,n,r,o),r)}function Fe(e,t){for(const[n,r]of Object.entries(t)){const s=e.get(n);if(s&&r!==s.code)try{o(r,n)}catch{t[n]=s.code}}return t}exports.DEFAULT_MONO_OPTIONS=ke,exports.DevShaker=class{entries=new Set;resolve;readFile;mode;parse;parseCache=new Map;codeCache=new Map;output={};constructor(e,t,n,o="incremental",r){for(const t of Array.isArray(e)?e:[e])this.entries.add(t);this.resolve=t,this.readFile=n,this.mode=o,this.parse=r}async init(){return this.output=await this.shake(),this.output}get(e){return this.output[e]}snapshot(){return{...this.output}}async update(e){const t="incremental"===this.mode;for(const t of e.removed??[])this.entries.delete(t),this.codeCache.delete(t),this.parseCache.delete(t);for(const n of e.added??[])this.entries.add(n),t&&this.codeCache.set(n,await this.readFile(n));for(const n of e.changed??[])t&&this.codeCache.set(n,await this.readFile(n));const n=this.output,o=await this.shake();this.output=o;const r={};for(const e of Object.keys(o))n[e]!==o[e]&&(r[e]=o[e]);return{changed:r,removed:Object.keys(n).filter(e=>!(e in o))}}async shake(){const e="incremental"===this.mode,t=e?this.cachedReadFile:this.readFile,n=e?this.parseCache:this.parse?new Map:void 0,o=await I([...this.entries],this.resolve,t,n,this.parse),{models:r,plans:s}=b(o,n);return oe(r,s)}cachedReadFile=async e=>{const t=this.codeCache.get(e);if(void 0!==t)return t;const n=await this.readFile(e);return this.codeCache.set(e,n),n}},exports.analyze=S,exports.analyzeInput=b,exports.buildAnalyzeInput=I,exports.monomorphize=xe,exports.svelteShaker=async function(e,t,n,o){const{models:r,plans:s}=await Ce(e,t,n,o);return Fe(r,oe(r,s))},exports.svelteShakerWithMono=async function(e,t,n,o=ke,r=e=>e,s){const{models:a,plans:i}=await Ce(e,t,n,s),c=xe(a,i,o,e);return{files:Fe(a,0===c.bindings.length?oe(a,i):ae(a,i,c.bindings.map(e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps})),r)),mono:c}},exports.transformAll=oe,exports.transformAllWithMono=ae;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{parse as e,compile as t}from"svelte/compiler";import{walk as n}from"zimmerframe";import o from"magic-string";function r(t,n){return e(t,{modern:!0,filename:n})}function s(e,t,n){if(!n)return r(t,e);const o=n.get(e);if(o&&o.code===t)return o.ast;const s=r(t,e);return n.set(e,{code:t,ast:s}),s}function a(e,t,o){n(e,t,o)}const i={known:!1};function c(e,t){if(!e)return i;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:i}case"UnaryExpression":{const n=c(e.argument,t);if(!n.known)return i;const o=n.value;switch(e.operator){case"!":return{known:!0,value:!o};case"-":return{known:!0,value:-o};case"+":return{known:!0,value:+o};case"typeof":return{known:!0,value:typeof o};case"void":return{known:!0,value:void 0};default:return i}}case"LogicalExpression":{const n=c(e.left,t);if(!n.known)return i;switch(e.operator){case"&&":return n.value?c(e.right,t):n;case"||":return n.value?n:c(e.right,t);case"??":return null===n.value||void 0===n.value?c(e.right,t):n;default:return i}}case"BinaryExpression":{const n=c(e.left,t),o=c(e.right,t);if(!n.known||!o.known)return i;const r=n.value,s=o.value;switch(e.operator){case"===":return{known:!0,value:r===s};case"!==":return{known:!0,value:r!==s};case"==":return{known:!0,value:r==s};case"!=":return{known:!0,value:r!=s};case"<":return{known:!0,value:r<s};case">":return{known:!0,value:r>s};case"<=":return{known:!0,value:r<=s};case">=":return{known:!0,value:r>=s};case"+":return{known:!0,value:r+s};case"-":return{known:!0,value:r-s};case"*":return{known:!0,value:r*s};case"/":return{known:!0,value:r/s};case"%":return{known:!0,value:r%s};default:return i}}default:return i}}function u(e,t,n){const o=c(e,t);if(o.known)return o;const r=f(e,t,n);return"unknown"===r?i:{known:!0,value:r}}function f(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?p(f(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=f(e.left,t,n),a=()=>f(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,t){return!0===e||!0===t||(!1!==e||!1!==t)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=l(e,o),a=c(t,n);if(s&&a.known)return d(s,a.value,r);const i=l(t,o),u=c(e,n);return i&&u.known?d(i,u.value,r):u.known&&a.known?r?u.value==a.value:u.value===a.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?p(s):s}return"unknown"}default:return"unknown"}var o,r}function l(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function d(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function p(e){return!0!==e&&(!1===e||"unknown")}function m(e,t,n){const{arms:o,elseFrag:r}=function(e){const t=[];let n,o=e;for(;o;){t.push({block:o,test:o.test,consequent:o.consequent});const e=o.alternate,r="Fragment"===e?.type&&1===e.nodes?.length&&"IfBlock"===e.nodes[0]?.type&&!0===e.nodes[0].elseif?e.nodes[0]:void 0;r?o=r:("Fragment"===e?.type&&(n=e),o=void 0)}return n?{arms:t,elseFrag:n}:{arms:t}}(e),s=[e.start,e.end],a=o.map((e=>u(e.test,t,n))),i=e=>e.known&&Boolean(e.value),c=e=>e.known&&!e.value;let f=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&f){const t=o[e].consequent;return{span:s,kept:t,removed:h(s,v(t)),recurse:!1}}c(t)||(f=!1)}const l=a.findIndex((e=>!c(e)));if(-1===l)return r?{span:s,kept:r,removed:h(s,v(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:w(o,a,l),recurse:!0};const d=o[l].block;return{span:s,kept:void 0,removed:[[s[0],d.start],...w(o,a,l)],recurse:!1,headerRewrite:{from:d.start,to:d.test.start,text:"{#if "}}}function h(e,t){if(!t)return[e];const n=[];return e[0]<t[0]&&n.push([e[0],t[0]]),t[1]<e[1]&&n.push([t[1],e[1]]),n}function v(e){const t=e?.nodes??[];return 0===t.length?null:[t[0].start,t[t.length-1].end]}function w(e,t,n){const o=[];for(let r=n+1;r<e.length;r++){const n=t[r];if(!n.known||n.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:g(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function g(e,t){const n=e?.nodes??[];return n.length?n[n.length-1].end:t}function y(e,t){return t.some((([t,n])=>e.start>=t&&e.end<=n))}function k(e,t,n){if(0===t.size&&0===n.size)return[];const o=[];return a(e,null,{IfBlock(e,{next:r}){if(e.elseif||y(e,o))return;const s=m(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const x=e=>e.endsWith(".svelte"),S=10,b="escapes as value (e.g. <svelte:component this={X}>)";async function I(e,t,n){return M(await C(e,t,n))}function M(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,z(r,n.get(r.id)??[],t));return o}(e,t),o=new Set;for(const e of n.values())for(const t of e.escapedComponents)o.add(t);for(const e of o){const t=n.get(e);t&&!t.bailReasons.includes(b)&&t.bailReasons.push(b)}let r=A(n,E(n,new Map));for(let e=0;e<S;e++){const e=A(n,E(n,$(n,r)));if(F(r,e)){r=e;break}r=e}return{models:n,plans:r}}async function C(e,t,n,o){const r=Array.isArray(e)?[...e]:[e],a=[],i=[],c=[...r],u=new Set(c);for(;c.length>0;){const e=c.shift(),r=await n(e);a.push({id:e,code:r});const f=s(e,r,o),l=f.instance;if(!l)continue;const d=new Map,p=new Map,m=[];for(const o of U(l)){if("*"===o.imported){p.set(o.local,o.value);continue}if("default"===o.imported&&x(o.value)){const n=await t(o.value,e);n&&(i.push({from:e,local:o.local,to:n,kind:"default-svelte"}),m.push(n));continue}const r=await K(o.value,o.imported,e,t,n);r&&(i.push({from:e,local:o.local,to:r,kind:"barrel"}),d.set(o.local,r))}const h=[];if(p.size>0)for(const o of B(f)){const r=o.indexOf("."),s=p.get(o.slice(0,r));if(null==s)continue;const a=await K(s,o.slice(r+1),e,t,n);a&&(i.push({from:e,local:o,to:a,kind:"namespace"}),h.push(a))}const v=O(f,d);for(const e of[...m,...v,...h])u.has(e)||(u.add(e),c.push(e))}return{files:a,edges:i,entries:r}}function E(e,t){const n=new Map,o=e=>{let t=n.get(e);return t||(t={sites:[]},n.set(e,t)),t};for(const n of e.values()){const e=t.get(n.id)??[];for(const t of n.childCalls)e.length>0&&y(t.node,e)||o(t.childId).sites.push(T(t.node))}return n}function A(e,t){const n=new Map;for(const o of e.values())n.set(o.id,V(o,t.get(o.id)));return n}function F(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!P(o.constFold,e.constFold))return!1;if(!D(o.narrow,e.narrow))return!1}return!0}function P(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e)if(!t.has(n)||!Object.is(t.get(n),o))return!1;return!0}function D(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e||o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!Object.is(o[t],e[t]))return!1}return!0}function $(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=k(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function z(e,t,n){const{id:o,code:r}=e,i=s(o,r,n),c=new Map;for(const e of t)c.set(e.local,e.to);const u=[];a(i.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||u.push(`<svelte:options ${t.name}>`);t()}});let f,l,d=null,p=!1;const m=new Set,h=new Set,v=i.instance;if(v){for(const e of U(v))m.add(e.local),"*"===e.imported&&h.add(e.local);const e=function(e){const t=e.content;for(const e of t?.body??[])if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("CallExpression"===n?.type&&"Identifier"===n.callee?.type&&"$props"===n.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(e){f=e.declaration,l=e.pattern,e.sharesStatement&&u.push("$props() shares a multi-declarator statement"),d=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){p=!0;continue}if("Property"!==t.type)continue;const e=t.key;if("Identifier"!==e?.type||!e.name)continue;const n=t.value,o="AssignmentPattern"===n?.type?n.right:void 0;d.push({name:e.name,property:t,defaultExpr:o})}}}const w=function(e,t){const n=[];return a(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.push({childId:r,node:e}),o()}}),n}(i,c),{shadowedNames:g,debugNames:y}=function(e,t,n){const o=new Set,r=new Set;t&&a(t,null,{_(e,{next:t}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===n||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const t of e.params??[])R(t,o);t()}});return a(e.fragment,null,{EachBlock(e,{next:t}){R(e.context,o),"string"==typeof e.index&&o.add(e.index),t()},SnippetBlock(e,{next:t}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const t of e.parameters??[])R(t,o);t()},AwaitBlock(e,{next:t}){R(e.value,o),R(e.error,o),t()},LetDirective(e,{next:t}){e.name&&o.add(e.name),t()},ConstTag(e,{next:t}){for(const t of e.declaration?.declarations??[])R(t.id,o);t()},DebugTag(e,{next:t}){for(const t of e.identifiers??[])"Identifier"===t.type&&t.name&&r.add(t.name);t()}}),{shadowedNames:o,debugNames:r}}(i,v,f),k=function(e,t,n,o){const r=new Set,s=e=>{if(!e)return;const n=t.get(e);if(n&&r.add(n),o.has(e))for(const[n,o]of t)n.startsWith(`${e}.`)&&r.add(o)};a(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&N(e,t.parent)&&s(e.name),o({parent:e})}}),e.instance&&a(e.instance,{parent:null},{_(e,{state:n,next:r}){"Identifier"===e.type&&e.name&&(t.has(e.name)||o.has(e.name))&&N(e,n.parent)&&!L(n.parent)&&s(e.name),r({parent:e})}});return r}(i,c,m,h);return{id:o,code:r,ast:i,imports:c,props:d,propsDeclaration:f,propsPattern:l,hasRestProp:p,childCalls:w,shadowedNames:g,debugNames:y,escapedComponents:k,bailReasons:u}}function R(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?R(n.argument,t):"Property"===n.type&&R(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])R(n,t);return;case"AssignmentPattern":return void R(e.left,t);case"RestElement":return void R(e.argument,t);default:return}}function N(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand)&&!L(t)))}function L(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function O(e,t){const n=new Set;return 0===t.size||a(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.add(r),o()}}),n}function B(e){const t=new Set;return a(e.fragment,null,{Component(e,{next:n}){"string"==typeof e.name&&e.name.includes(".")&&t.add(e.name),n()}}),t}function T(e){const t=e.attributes??[];let n=-1;for(let e=0;e<t.length;e++)"SpreadAttribute"===t[e].type&&(n=e);const o=new Map;for(let e=0;e<t.length;e++){const r=t[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,j(e,n));continue}if("Attribute"!==r.type||!s)continue;const a=_(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>n}:j(e,n))}for(const r of function(e){const t=e.fragment?.nodes??[],n=[];let o=!1;for(const e of t)if("SnippetBlock"!==e.type){if("Comment"!==e.type){if("Text"===e.type){if(""===(e.data??e.raw??"").trim())continue}o=!0}}else"Identifier"===e.expression?.type&&e.expression.name&&n.push(e.expression.name);o&&n.push("children");return n}(e))o.set(r,j(t.length,n));return{hadSpread:n>=0,explicit:o}}function j(e,t){return{value:void 0,dynamic:!0,afterLastSpread:e>t}}function _(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const t=Array.isArray(e)?e:[e];if(1===t.length){const e=t[0];return"Text"===e.type?{known:!0,value:e.data??e.raw??""}:"ExpressionTag"===e.type&&"Literal"===e.expression?.type?{known:!0,value:e.expression.value}:{known:!1}}let n="";for(const e of t){if("Text"!==e.type)return{known:!1};n+=e.data??e.raw??""}return{known:!0,value:n}}function q(e,t){return e.shadowedNames.has(t)||e.debugNames.has(t)}function V(e,t){const n={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return n.bail=!0,n.reasons.push(...e.bailReasons),n;if(!e.props||0===e.props.length)return n;const o=t?.sites??[];if(0===o.length)return n;for(const t of e.props){if(q(e,t.name))continue;const r=W(t,o);n.valueSets.set(t.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&n.narrow.set(t.name,r.values):n.constFold.set(t.name,r.values[0]))}return n}function W(e,t){const n=[];let o=!1,r=!1;const s=e=>{n.some((t=>Object.is(t,e)))||n.push(e)};for(const n of t){const t=n.explicit.get(e.name);if(t?.afterLastSpread){t.dynamic?o=!0:s(t.value);continue}if(n.hadSpread){r=!0;continue}const a=J(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function J(e){return e?"Literal"===e.type?{known:!0,value:e.value}:"Identifier"===e.type&&"undefined"===e.name?{known:!0,value:void 0}:{known:!1}:{known:!0,value:void 0}}function*U(e){const t=e.content;for(const e of t?.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t)for(const n of e.specifiers??[]){const e=n.local?.name;e&&("ImportDefaultSpecifier"===n.type?yield{value:t,local:e,imported:"default"}:"ImportNamespaceSpecifier"===n.type?yield{value:t,local:e,imported:"*"}:"ImportSpecifier"===n.type&&(yield{value:t,local:e,imported:X(n)??e}))}}}function X(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"Literal"===t?.type&&"string"==typeof t.value?t.value:void 0}function G(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const H=8;async function K(e,t,n,o,s,a=0){if(a>H)return null;const i=await o(e,n);if(!i)return null;if(x(e)||x(i))return"default"===t||"*"===t?i:null;let c;try{c=await s(i)}catch{return null}const u=function(e,t){try{const n=r(`<script module lang="ts">\n${e}\n<\/script>`,t);return n.module?.content?.body??null}catch{return null}}(c,i);if(!u)return null;for(const e of u)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(G(n.exported)===t)return K(String(e.source.value),G(n.local)??"default",i,o,s,a+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const n=await K(String(e.source.value),t,i,o,s,a+1);if(n)return n}}else for(const n of e.specifiers??[]){if(G(n.exported)!==t)continue;const e=G(n.local);if(!e)continue;const r=Q(u,e);return r?K(r.value,r.imported,i,o,s,a+1):null}return null}function Q(e,t){for(const n of e){if("ImportDeclaration"!==n.type)continue;const e=n.source?.value;if("string"==typeof e)for(const o of n.specifiers??[])if(o.local?.name===t){if("ImportDefaultSpecifier"===o.type)return{value:e,imported:"default"};if("ImportNamespaceSpecifier"===o.type)return{value:e,imported:"*"};if("ImportSpecifier"===o.type)return{value:e,imported:X(o)??t}}}return null}const Y=64;const Z=Symbol("unbounded-class-source");function ee(e,t,n){if(!0===e)return Z;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=te(e,t,n);if(o===Z)return Z;const s=[];for(const e of r)for(const t of o)if(s.push(e+t),s.length>Y)return Z;r=s}const s=new Set;for(const e of r)for(const t of e.split(/\s+/))t&&s.add(t);return s}function te(e,t,n){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,t,n){if(!e)return Z;if("Identifier"===e.type&&e.name&&n.has(e.name)){const t=new Set;for(const o of n.get(e.name))t.add(ne(o));return t}const o=c(e,t);return o.known?new Set([ne(o.value)]):Z}(e.expression,t,n):Z}function ne(e){return String(e)}function oe(e,t,n){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,s=t.narrow;return a(e.ast.fragment,null,{_(e,{next:t}){if("RegularElement"===(a=e.type)||"SvelteElement"===a||"Component"===a||"SvelteComponent"===a||"SvelteSelf"===a){var a;for(const t of e.attributes??[]){if("SpreadAttribute"===t.type){o=!0;continue}if("ClassDirective"===t.type){t.name&&n.add(t.name);continue}if("Attribute"!==t.type||"class"!==t.name)continue;const e=ee(t.value,r,s);if(e===Z)o=!0;else for(const t of e)n.add(t)}t()}else t()}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let s=0;for(const t of o.children)"Rule"===t.type&&re(t,r.classes)&&(se(e.code,t,o.children,n),s+=1);return s}function re(e,t){if(function(e){let t=!1;return a(e,null,{_(e,{next:n}){"PseudoClassSelector"===e.type&&"global"===e.name&&(t=!0),n()}}),t}(e))return!1;const n=e.prelude?.children??[];return 0!==n.length&&n.every((e=>function(e,t){let n=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!t.has(e.name)&&(n=!0);return n}(e,t)))}function se(e,t,n,o){const r=n.indexOf(t),s=n[r-1];let a=t.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,t.end)}function ae(e,t){return ce(e,ie(e,t))}function ie(e,t){const n=new Map,r=new Map,s=new Map;for(const a of e.values()){const e=new o(a.code);n.set(a.id,e);const i=t.get(a.id);if(i.bail){r.set(a.id,new Set);continue}const c=pe(a,i,e);r.set(a.id,c.dropped),s.set(a.id,c.dead)}for(const t of e.values())ye(t,r,n.get(t.id),s.get(t.id)??[]);return n}function ce(e,t){const n={};for(const o of e.values())n[o.id]=t.get(o.id).toString();return n}function ue(e,t,n,o){const r=ie(e,t);return function(e,t,n,o){const r=new Map;for(const e of t){const t=r.get(e.owner);t?t.push(e):r.set(e.owner,[e])}for(const[t,s]of r){const r=e.get(t),a=o.get(t);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const t=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${t}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:n(e.variantId)})),fe(r.code,e.node,o,e.foldedProps,a)}c.length>0&&de(r,c,a)}}(e,n,o,r),ce(e,r)}function fe(e,t,n,o,r){const s=t.name;if(!s)return;const a=t.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,n);const i=`</${s}`,c=e.lastIndexOf(i,t.end);if(c>=t.start){const e=c+2;r.overwrite(e,e+s.length,n)}for(const n of t.attributes??[])"Attribute"===n.type&&n.name&&o.has(n.name)&&le(e,n,r)}function le(e,t,n){let o=t.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),n.remove(o,t.end)}function de(e,t,n){const o=t.map((e=>` import ${e.local} from ${JSON.stringify(e.spec)};`)).join("\n"),r=e.ast.instance,s=r?.content?.body??[];if(r&&s.length>0){const e=s[s.length-1];n.appendLeft(e.end,`\n${o}`)}else r&&r.content?n.appendLeft(r.content.start,`\n${o}\n`):n.prepend(`<script>\n${o}\n<\/script>\n`)}function pe(e,t,n){const o=[];return{dropped:me(e,t.constFold,t.narrow,t,n,o),dead:o}}function me(e,t,n,o,r,s){if(0===t.size&&0===n.size)return new Set;const i=e.code,u=[];!function(e,t,n,o,r,s){a(e,null,{IfBlock(e,{next:a}){if(e.elseif||y(e,s))return;const i=m(e,t,n);if(function(e,t,n,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,t,n){const o=e?.nodes??[];return 0===o.length?"":he(o[0].start,o[o.length-1].end,o,t,n)}(e.kept,t,n));for(const[t,n]of e.removed)o.remove(t,n);if(e.headerRewrite){const{from:t,to:n,text:r}=e.headerRewrite;o.overwrite(t,n,r)}}(i,t,o,r),i.kept)s.push(i.span);else for(const e of i.removed)s.push(e);i.recurse&&a()}})}(e.ast.fragment,t,n,i,r,u),function(e,t,n,o,r){if(0===t.size)return;a(e,null,{ConditionalExpression(e,{next:s}){if(y(e,r))return;const a=c(e.test,t);if(!a.known)return void s();const i=a.value?e.consequent:e.alternate;i?(o.overwrite(e.start,e.end,he(i.start,i.end,[i],t,n)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,t,i,r,u);const f=function(e,t,n){const o=new Map,r=r=>{r&&ve(r,t,e.code,((t,r,s)=>{y(s,n)||s===e.propsPattern||(o.get(t)??function(e,t){const n=[];return e.set(t,n),n}(o,t)).push(r)}))};return r(e.ast.instance),r(e.ast.fragment),o}(e,t,u);for(const[e,n]of t){const t=xe(n);for(const n of f.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const l=new Set(t.keys());!function(e,t,n){if(!e.props||0===t.size)return;const o=e.props.filter((e=>!t.has(e.name)));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,t,n){let o=t.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=t.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,t.start),a=e.slice(t.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?n.remove(o,r<e.length?r+1:r):n.remove(t.start,";"===e[t.end]?t.end+1:t.end)}(e.code,e.propsDeclaration,n);const r=e.propsPattern?.properties??[],s=new Set(e.props.filter((e=>t.has(e.name))).map((e=>e.property)));let a=0;for(;a<r.length;){if(!s.has(r[a])){a++;continue}let t=a;for(;t+1<r.length&&s.has(r[t+1]);)t++;we(e.code,r,a,t,n),a=t+1}if(e.propsPattern)for(const o of e.props)t.has(o.name)&&ge(e.propsPattern,o.name,n)}(e,l,r);return oe(e,{...o,constFold:t,narrow:n},r),s&&s.push(...u),l}function he(e,t,n,o,r){if(0===o.size)return r.slice(e,t);const s=[];for(const e of n)ve(e,o,r,((e,t)=>s.push({...t,name:e})));if(0===s.length)return r.slice(e,t);s.sort(((e,t)=>e.start-t.start));let a="",i=e;for(const e of s)a+=r.slice(i,e.start),a+=e.head+xe(o.get(e.name))+e.tail,i=e.end;return a+=r.slice(i,t),a}function ve(e,t,n,o){a(e,{parent:null,grandparent:null},{_(e,{state:r,next:s}){if("StyleDirective"===e.type&&!0===e.value&&e.name&&t.has(e.name)){let t=e.end;for(;t>e.start&&(" "===(a=n[t-1])||"\t"===a||"\n"===a||"\r"===a);)t-=1;const r=n.slice(e.start,t);o(e.name,{start:e.start,end:t,head:`${r}={`,tail:"}"},e)}else"Identifier"===e.type&&e.name&&t.has(e.name)&&!function(e,t){return!!t&&("MemberExpression"===t.type&&t.property===e&&!t.computed||("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand||("ImportSpecifier"===t.type||"ImportDefaultSpecifier"===t.type||"ImportNamespaceSpecifier"===t.type||"ExportSpecifier"===t.type)))}(e,r.parent)&&o(e.name,function(e,t,n,o){if("ClassDirective"===t?.type&&t.expression===e&&":"===o[e.start-1]){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}={`,tail:"}"}}if("ExpressionTag"===t?.type&&"Attribute"===n?.type&&n.name&&"{"===o[n.start])return{start:n.start,end:n.end,head:`${n.name}={`,tail:"}"};if("Property"===t?.type&&!0===t.shorthand&&t.value===e){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}: `,tail:""}}return{start:e.start,end:e.end,head:"",tail:""}}(e,r.parent,r.grandparent,n),e);var a;s({parent:e,grandparent:r.parent})}})}function we(e,t,n,o,r){const s=t[n],a=t[o],i=t[o+1];if(i)return void r.remove(s.start,i.start);let c=a.end,u=c;for(;u<e.length&&/\s/.test(e[u]);)u++;","===e[u]&&(c=u+1);const f=t[n-1];r.remove(f?f.end:s.start,c)}function ge(e,t,n){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex((e=>"Identifier"===e.key?.type&&e.key.name===t));if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?n.remove(s.start,a.start):i?n.remove(i.end,s.end):n.remove(s.start,s.end)}function ye(e,t,n,o){a(e.ast.fragment,null,{Component(r,{next:s}){if(o.length>0&&y(r,o))return;const a=r.name?e.imports.get(r.name):void 0,i=a?t.get(a):void 0;if(i&&i.size>0)for(const t of r.attributes??[])"Attribute"===t.type&&t.name&&i.has(t.name)&&ke(t.value)&&le(e.code,t,n);s()}})}function ke(e){if(!0===e||null==e)return!0;return(Array.isArray(e)?e:[e]).every((e=>"Text"===e.type||"ExpressionTag"===e.type&&"Literal"===e.expression?.type))}function xe(e){return void 0===e?"undefined":JSON.stringify(e)}const Se={enabled:!1,maxVariants:8,minSavings:0};function be(e,n,o=Se,r=[]){const s=new Map,a=[];if(!o.enabled)return{variants:s,bindings:a};const i=$(e,n),c=new Map,u=new Set;for(const t of e.values()){const o=i.get(t.id)??[];for(const r of t.childCalls){if(o.length>0&&y(r.node,o))continue;const s=e.get(r.childId),a=n.get(r.childId);if(!s||!a)continue;if(a.bail||!s.props||0===s.props.length){u.add(r.childId);continue}const i=Ce(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const f=Ee(s,a,i);if(f===Fe(s,a)){u.add(r.childId);continue}const l=c.get(r.childId);l?l.push({owner:t.id,node:r.node,shape:i,code:f}):c.set(r.childId,[{owner:t.id,node:r.node,shape:i,code:f}])}}const f=function(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);n.set(o.id,e.bail?o.code:Fe(o,e))}return n}(e,n),l=new Map;for(const t of e.values())l.set(t.id,Me(f.get(t.id),t));const d=new Set;for(const e of l.values())for(const t of e)d.add(t);const p=(Array.isArray(r)?r:[r]).filter((t=>e.has(t))).filter((e=>!d.has(e))),m=new Map,h=(e,n)=>{const o=m.get(n);if(void 0!==o)return o;let r;try{const{js:o}=t(n,{generate:"client",dev:!1,filename:e});r=o.code.length}catch{r=null}return null!==r&&m.set(n,r),r},v=new Set;for(const e of c.keys())u.has(e)||v.add(e);for(const[t,n]of c){if(u.has(t))continue;if(n.some((e=>e.owner!==t&&v.has(e.owner))))continue;const r=new Map,i=[];let c=!1;for(const e of n){if(r.has(e.code))continue;if(i.length>=o.maxVariants){c=!0;break}const n=`${t}::v${i.length}`;r.set(e.code,n),i.push({id:n,code:e.code})}if(!c&&Ie(t,i,e,f,l,p,h,o.minSavings)){for(const e of i){const o=n.find((t=>t.code===e.code));s.set(e.id,{id:e.id,childId:t,code:e.code,foldedProps:o.shape})}for(const e of n)a.push({owner:e.owner,childId:t,node:e.node,variantId:r.get(e.code),foldedProps:e.shape})}}return{variants:s,bindings:a}}function Ie(e,t,n,o,r,s,a,i){const c=n.get(e),u=new Map;for(const e of t)u.set(e.id,Me(e.code,c));const f=new Set,l=[...s];for(;l.length>0;){const e=l.pop();if(!f.has(e)){f.add(e);for(const t of r.get(e)??[])l.push(t)}}let d=0;for(const e of f){const t=a(e,o.get(e));if(null===t)return!1;d+=t}const p=t.map((e=>e.id)),m=new Set,h=new Set,v=t=>t===e?{comps:[],vars:p}:{comps:[t],vars:[]},w=[],g=[];for(const e of s){const t=v(e);w.push(...t.comps),g.push(...t.vars)}for(;w.length>0||g.length>0;){if(w.length>0){const e=w.pop();if(m.has(e))continue;m.add(e);for(const t of r.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!h.has(e)){h.add(e);for(const t of u.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const t=a(e,o.get(e));if(null===t)return!1;y+=t}for(const e of h){const n=t.find((t=>t.id===e)).code,o=a(e,n);if(null===o)return!1;y+=o}return y<d*(1-i)}function Me(e,t){let n;try{n=r(e,t.id)}catch{return[]}const o=[];return a(n.fragment,null,{Component(e,{next:n}){const r=e.name?t.imports.get(e.name):void 0;r&&o.push(r),n()}}),o}function Ce(e,t,n){const o=T(e),r=new Map;for(const e of t.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(n.constFold.has(e)||q(t,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Ee(e,t,n){const r=new Map(t.constFold);for(const[e,t]of n)r.set(e,t);const s=new Map;for(const[e,n]of t.narrow)r.has(e)||s.set(e,n);const a=new o(e.code);return me(e,r,s,t,a),a.toString()}const Ae=new WeakMap;function Fe(e,t){const n=Ae.get(e);if(void 0!==n)return n;const r=new o(e.code);me(e,t.constFold,t.narrow,t,r);const s=r.toString();return Ae.set(e,s),s}class Pe{entries=new Set;resolve;readFile;mode;parseCache=new Map;codeCache=new Map;output={};constructor(e,t,n,o="incremental"){for(const t of Array.isArray(e)?e:[e])this.entries.add(t);this.resolve=t,this.readFile=n,this.mode=o}async init(){return this.output=await this.shake(),this.output}get(e){return this.output[e]}snapshot(){return{...this.output}}async update(e){const t="incremental"===this.mode;for(const t of e.removed??[])this.entries.delete(t),this.codeCache.delete(t),this.parseCache.delete(t);for(const n of e.added??[])this.entries.add(n),t&&this.codeCache.set(n,await this.readFile(n));for(const n of e.changed??[])t&&this.codeCache.set(n,await this.readFile(n));const n=this.output,o=await this.shake();this.output=o;const r={};for(const e of Object.keys(o))n[e]!==o[e]&&(r[e]=o[e]);return{changed:r,removed:Object.keys(n).filter((e=>!(e in o)))}}async shake(){const e="incremental"===this.mode,t=e?this.cachedReadFile:this.readFile,n=e?this.parseCache:void 0,o=await C([...this.entries],this.resolve,t,n),{models:r,plans:s}=M(o,n);return ae(r,s)}cachedReadFile=async e=>{const t=this.codeCache.get(e);if(void 0!==t)return t;const n=await this.readFile(e);return this.codeCache.set(e,n),n}}async function De(e,t,n){const{models:o,plans:r}=await I(e,t,n);return $e(o,ae(o,r))}function $e(e,t){for(const[n,o]of Object.entries(t)){const s=e.get(n);if(s&&o!==s.code)try{r(o,n)}catch{t[n]=s.code}}return t}async function ze(e,t,n,o=Se,r=(e=>e)){const{models:s,plans:a}=await I(e,t,n),i=be(s,a,o,e);return{files:$e(s,0===i.bindings.length?ae(s,a):ue(s,a,i.bindings.map((e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps}))),r)),mono:i}}export{Se as DEFAULT_MONO_OPTIONS,Pe as DevShaker,I as analyze,M as analyzeInput,C as buildAnalyzeInput,be as monomorphize,De as svelteShaker,ze as svelteShakerWithMono,ae as transformAll,ue as transformAllWithMono};
1
+ import{parse as e,compile as t}from"svelte/compiler";import{walk as n}from"zimmerframe";import o from"magic-string";function r(t,n){return e(t,{modern:!0,filename:n})}function s(e,t,n,o=r){if(!n)return o(t,e);const s=n.get(e);if(s&&s.code===t)return s.ast;const a=o(t,e);return n.set(e,{code:t,ast:a}),a}function a(e,t,o){n(e,t,o)}const i={known:!1};function c(e,t){if(!e)return i;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:i}case"UnaryExpression":{const n=c(e.argument,t);if(!n.known)return i;const o=n.value;switch(e.operator){case"!":return{known:!0,value:!o};case"-":return{known:!0,value:-o};case"+":return{known:!0,value:+o};case"typeof":return{known:!0,value:typeof o};case"void":return{known:!0,value:void 0};default:return i}}case"LogicalExpression":{const n=c(e.left,t);if(!n.known)return i;switch(e.operator){case"&&":return n.value?c(e.right,t):n;case"||":return n.value?n:c(e.right,t);case"??":return null===n.value||void 0===n.value?c(e.right,t):n;default:return i}}case"BinaryExpression":{const n=c(e.left,t),o=c(e.right,t);if(!n.known||!o.known)return i;const r=n.value,s=o.value;switch(e.operator){case"===":return{known:!0,value:r===s};case"!==":return{known:!0,value:r!==s};case"==":return{known:!0,value:r==s};case"!=":return{known:!0,value:r!=s};case"<":return{known:!0,value:r<s};case">":return{known:!0,value:r>s};case"<=":return{known:!0,value:r<=s};case">=":return{known:!0,value:r>=s};case"+":return{known:!0,value:r+s};case"-":return{known:!0,value:r-s};case"*":return{known:!0,value:r*s};case"/":return{known:!0,value:r/s};case"%":return{known:!0,value:r%s};default:return i}}default:return i}}function u(e,t,n){const o=c(e,t);if(o.known)return o;const r=f(e,t,n);return"unknown"===r?i:{known:!0,value:r}}function f(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?p(f(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=f(e.left,t,n),a=()=>f(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,t){return!0===e||!0===t||(!1!==e||!1!==t)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=l(e,o),a=c(t,n);if(s&&a.known)return d(s,a.value,r);const i=l(t,o),u=c(e,n);return i&&u.known?d(i,u.value,r):u.known&&a.known?r?u.value==a.value:u.value===a.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?p(s):s}return"unknown"}default:return"unknown"}var o,r}function l(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function d(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function p(e){return!0!==e&&(!1===e||"unknown")}function m(e,t,n){const{arms:o,elseFrag:r}=function(e){const t=[];let n,o=e;for(;o;){t.push({block:o,test:o.test,consequent:o.consequent});const e=o.alternate,r="Fragment"===e?.type&&1===e.nodes?.length&&"IfBlock"===e.nodes[0]?.type&&!0===e.nodes[0].elseif?e.nodes[0]:void 0;r?o=r:("Fragment"===e?.type&&(n=e),o=void 0)}return n?{arms:t,elseFrag:n}:{arms:t}}(e),s=[e.start,e.end],a=o.map(e=>u(e.test,t,n)),i=e=>e.known&&Boolean(e.value),c=e=>e.known&&!e.value;let f=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&f){const t=o[e].consequent;return{span:s,kept:t,removed:h(s,v(t)),recurse:!1}}c(t)||(f=!1)}const l=a.findIndex(e=>!c(e));if(-1===l)return r?{span:s,kept:r,removed:h(s,v(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:w(o,a,l),recurse:!0};const d=o[l].block;return{span:s,kept:void 0,removed:[[s[0],d.start],...w(o,a,l)],recurse:!1,headerRewrite:{from:d.start,to:d.test.start,text:"{#if "}}}function h(e,t){if(!t)return[e];const n=[];return e[0]<t[0]&&n.push([e[0],t[0]]),t[1]<e[1]&&n.push([t[1],e[1]]),n}function v(e){const t=e?.nodes??[];return 0===t.length?null:[t[0].start,t[t.length-1].end]}function w(e,t,n){const o=[];for(let r=n+1;r<e.length;r++){const n=t[r];if(!n.known||n.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:g(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function g(e,t){const n=e?.nodes??[];return n.length?n[n.length-1].end:t}function y(e,t){return t.some(([t,n])=>e.start>=t&&e.end<=n)}function k(e,t,n){if(0===t.size&&0===n.size)return[];const o=[];return a(e,null,{IfBlock(e,{next:r}){if(e.elseif||y(e,o))return;const s=m(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const x=e=>e.endsWith(".svelte"),S="escapes as value (e.g. <svelte:component this={X}>)";async function b(e,t,n){return I(await M(e,t,n))}function I(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,$(r,n.get(r.id)??[],t));return o}(e,t),o=new Set;for(const e of n.values())for(const t of e.escapedComponents)o.add(t);for(const e of o){const t=n.get(e);t&&!t.bailReasons.includes(S)&&t.bailReasons.push(S)}let r=E(n,C(n,new Map));for(let e=0;e<10;e++){const e=E(n,C(n,D(n,r)));if(A(r,e)){r=e;break}r=e}return{models:n,plans:r}}async function M(e,t,n,o,r){const a=Array.isArray(e)?[...e]:[e],i=[],c=[],u=[...a],f=new Set(u);for(;u.length>0;){const e=u.shift(),a=await n(e);i.push({id:e,code:a});const l=s(e,a,o,r),d=l.instance;if(!d)continue;const p=new Map,m=new Map,h=[];for(const o of J(d)){if("*"===o.imported){m.set(o.local,o.value);continue}if("default"===o.imported&&x(o.value)){const n=await t(o.value,e);n&&(c.push({from:e,local:o.local,to:n,kind:"default-svelte"}),h.push(n));continue}const r=await H(o.value,o.imported,e,t,n);r&&(c.push({from:e,local:o.local,to:r,kind:"barrel"}),p.set(o.local,r))}const v=[];if(m.size>0)for(const o of O(l)){const r=o.indexOf("."),s=m.get(o.slice(0,r));if(null==s)continue;const a=await H(s,o.slice(r+1),e,t,n);a&&(c.push({from:e,local:o,to:a,kind:"namespace"}),v.push(a))}const w=L(l,p);for(const e of[...h,...w,...v])f.has(e)||(f.add(e),u.push(e))}return{files:i,edges:c,entries:a}}function C(e,t){const n=new Map,o=e=>{let t=n.get(e);return t||(t={sites:[]},n.set(e,t)),t};for(const n of e.values()){const e=t.get(n.id)??[];for(const t of n.childCalls)e.length>0&&y(t.node,e)||o(t.childId).sites.push(T(t.node))}return n}function E(e,t){const n=new Map;for(const o of e.values())n.set(o.id,q(o,t.get(o.id)));return n}function A(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!F(o.constFold,e.constFold))return!1;if(!P(o.narrow,e.narrow))return!1}return!0}function F(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e)if(!t.has(n)||!Object.is(t.get(n),o))return!1;return!0}function P(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e||o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!Object.is(o[t],e[t]))return!1}return!0}function D(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=k(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function $(e,t,n){const{id:o,code:r}=e,i=s(o,r,n),c=new Map;for(const e of t)c.set(e.local,e.to);const u=[];a(i.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||u.push(`<svelte:options ${t.name}>`);t()}});let f,l,d=null,p=!1;const m=new Set,h=new Set,v=i.instance;if(v){for(const e of J(v))m.add(e.local),"*"===e.imported&&h.add(e.local);const e=function(e){const t=e.content;for(const e of t?.body??[])if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("CallExpression"===n?.type&&"Identifier"===n.callee?.type&&"$props"===n.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(e){f=e.declaration,l=e.pattern,e.sharesStatement&&u.push("$props() shares a multi-declarator statement"),d=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){p=!0;continue}if("Property"!==t.type)continue;const e=t.key;if("Identifier"!==e?.type||!e.name)continue;const n=t.value,o="AssignmentPattern"===n?.type?n.right:void 0;d.push({name:e.name,property:t,defaultExpr:o})}}}const w=function(e,t){const n=[];return a(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.push({childId:r,node:e}),o()}}),n}(i,c),{shadowedNames:g,debugNames:y}=function(e,t,n){const o=new Set,r=new Set;t&&a(t,null,{_(e,{next:t}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===n||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const t of e.params??[])z(t,o);t()}});return a(e.fragment,null,{EachBlock(e,{next:t}){z(e.context,o),"string"==typeof e.index&&o.add(e.index),t()},SnippetBlock(e,{next:t}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const t of e.parameters??[])z(t,o);t()},AwaitBlock(e,{next:t}){z(e.value,o),z(e.error,o),t()},LetDirective(e,{next:t}){e.name&&o.add(e.name),t()},ConstTag(e,{next:t}){for(const t of e.declaration?.declarations??[])z(t.id,o);t()},DebugTag(e,{next:t}){for(const t of e.identifiers??[])"Identifier"===t.type&&t.name&&r.add(t.name);t()}}),{shadowedNames:o,debugNames:r}}(i,v,f),k=function(e,t,n,o){const r=new Set,s=e=>{if(!e)return;const n=t.get(e);if(n&&r.add(n),o.has(e))for(const[n,o]of t)n.startsWith(`${e}.`)&&r.add(o)};a(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&R(e,t.parent)&&s(e.name),o({parent:e})}}),e.instance&&a(e.instance,{parent:null},{_(e,{state:n,next:r}){"Identifier"===e.type&&e.name&&(t.has(e.name)||o.has(e.name))&&R(e,n.parent)&&!N(n.parent)&&s(e.name),r({parent:e})}});return r}(i,c,m,h);return{id:o,code:r,ast:i,imports:c,props:d,propsDeclaration:f,propsPattern:l,hasRestProp:p,childCalls:w,shadowedNames:g,debugNames:y,escapedComponents:k,bailReasons:u}}function z(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?z(n.argument,t):"Property"===n.type&&z(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])z(n,t);return;case"AssignmentPattern":return void z(e.left,t);case"RestElement":return void z(e.argument,t);default:return}}function R(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand)&&!N(t)))}function N(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function L(e,t){const n=new Set;return 0===t.size||a(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.add(r),o()}}),n}function O(e){const t=new Set;return a(e.fragment,null,{Component(e,{next:n}){"string"==typeof e.name&&e.name.includes(".")&&t.add(e.name),n()}}),t}function T(e){const t=e.attributes??[];let n=-1;for(let e=0;e<t.length;e++)"SpreadAttribute"===t[e].type&&(n=e);const o=new Map;for(let e=0;e<t.length;e++){const r=t[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,B(e,n));continue}if("Attribute"!==r.type||!s)continue;const a=j(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>n}:B(e,n))}for(const r of function(e){const t=e.fragment?.nodes??[],n=[];let o=!1;for(const e of t)if("SnippetBlock"!==e.type){if("Comment"!==e.type){if("Text"===e.type){if(""===(e.data??e.raw??"").trim())continue}o=!0}}else"Identifier"===e.expression?.type&&e.expression.name&&n.push(e.expression.name);o&&n.push("children");return n}(e))o.set(r,B(t.length,n));return{hadSpread:n>=0,explicit:o}}function B(e,t){return{value:void 0,dynamic:!0,afterLastSpread:e>t}}function j(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const t=Array.isArray(e)?e:[e];if(1===t.length){const e=t[0];return"Text"===e.type?{known:!0,value:e.data??e.raw??""}:"ExpressionTag"===e.type&&"Literal"===e.expression?.type?{known:!0,value:e.expression.value}:{known:!1}}let n="";for(const e of t){if("Text"!==e.type)return{known:!1};n+=e.data??e.raw??""}return{known:!0,value:n}}function _(e,t){return e.shadowedNames.has(t)||e.debugNames.has(t)}function q(e,t){const n={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return n.bail=!0,n.reasons.push(...e.bailReasons),n;if(!e.props||0===e.props.length)return n;const o=t?.sites??[];if(0===o.length)return n;for(const t of e.props){if(_(e,t.name))continue;const r=V(t,o);n.valueSets.set(t.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&n.narrow.set(t.name,r.values):n.constFold.set(t.name,r.values[0]))}return n}function V(e,t){const n=[];let o=!1,r=!1;const s=e=>{n.some(t=>Object.is(t,e))||n.push(e)};for(const n of t){const t=n.explicit.get(e.name);if(t?.afterLastSpread){t.dynamic?o=!0:s(t.value);continue}if(n.hadSpread){r=!0;continue}const a=W(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function W(e){return e?"Literal"===e.type?{known:!0,value:e.value}:"Identifier"===e.type&&"undefined"===e.name?{known:!0,value:void 0}:{known:!1}:{known:!0,value:void 0}}function*J(e){const t=e.content;for(const e of t?.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t)for(const n of e.specifiers??[]){const e=n.local?.name;e&&("ImportDefaultSpecifier"===n.type?yield{value:t,local:e,imported:"default"}:"ImportNamespaceSpecifier"===n.type?yield{value:t,local:e,imported:"*"}:"ImportSpecifier"===n.type&&(yield{value:t,local:e,imported:U(n)??e}))}}}function U(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"Literal"===t?.type&&"string"==typeof t.value?t.value:void 0}function X(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const G=8;async function H(e,t,n,o,s,a=0){if(a>G)return null;const i=await o(e,n);if(!i)return null;if(x(e)||x(i))return"default"===t||"*"===t?i:null;let c;try{c=await s(i)}catch{return null}const u=function(e,t){try{const n=r(`<script module lang="ts">\n${e}\n<\/script>`,t);return n.module?.content?.body??null}catch{return null}}(c,i);if(!u)return null;for(const e of u)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(X(n.exported)===t)return H(String(e.source.value),X(n.local)??"default",i,o,s,a+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const n=await H(String(e.source.value),t,i,o,s,a+1);if(n)return n}}else for(const n of e.specifiers??[]){if(X(n.exported)!==t)continue;const e=X(n.local);if(!e)continue;const r=K(u,e);return r?H(r.value,r.imported,i,o,s,a+1):null}return null}function K(e,t){for(const n of e){if("ImportDeclaration"!==n.type)continue;const e=n.source?.value;if("string"==typeof e)for(const o of n.specifiers??[])if(o.local?.name===t){if("ImportDefaultSpecifier"===o.type)return{value:e,imported:"default"};if("ImportNamespaceSpecifier"===o.type)return{value:e,imported:"*"};if("ImportSpecifier"===o.type)return{value:e,imported:U(o)??t}}}return null}const Q=Symbol("unbounded-class-source");function Y(e,t,n){if(!0===e)return Q;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=Z(e,t,n);if(o===Q)return Q;const s=[];for(const e of r)for(const t of o)if(s.push(e+t),s.length>64)return Q;r=s}const s=new Set;for(const e of r)for(const t of e.split(/\s+/))t&&s.add(t);return s}function Z(e,t,n){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,t,n){if(!e)return Q;if("Identifier"===e.type&&e.name&&n.has(e.name)){const t=new Set;for(const o of n.get(e.name))t.add(ee(o));return t}const o=c(e,t);return o.known?new Set([ee(o.value)]):Q}(e.expression,t,n):Q}function ee(e){return String(e)}function te(e,t,n){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,s=t.narrow;return a(e.ast.fragment,null,{_(e,{next:t}){if("RegularElement"===(a=e.type)||"SvelteElement"===a||"Component"===a||"SvelteComponent"===a||"SvelteSelf"===a){var a;for(const t of e.attributes??[]){if("SpreadAttribute"===t.type){o=!0;continue}if("ClassDirective"===t.type){t.name&&n.add(t.name);continue}if("Attribute"!==t.type||"class"!==t.name)continue;const e=Y(t.value,r,s);if(e===Q)o=!0;else for(const t of e)n.add(t)}t()}else t()}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let s=0;for(const t of o.children)"Rule"===t.type&&ne(t,r.classes)&&(oe(e.code,t,o.children,n),s+=1);return s}function ne(e,t){if(function(e){let t=!1;return a(e,null,{_(e,{next:n}){"PseudoClassSelector"===e.type&&"global"===e.name&&(t=!0),n()}}),t}(e))return!1;const n=e.prelude?.children??[];return 0!==n.length&&n.every(e=>function(e,t){let n=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!t.has(e.name)&&(n=!0);return n}(e,t))}function oe(e,t,n,o){const r=n.indexOf(t),s=n[r-1];let a=t.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,t.end)}function re(e,t){return ae(e,se(e,t))}function se(e,t){const n=new Map,r=new Map,s=new Map;for(const a of e.values()){const e=new o(a.code);n.set(a.id,e);const i=t.get(a.id);if(i.bail){r.set(a.id,new Set);continue}const c=le(a,i,e);r.set(a.id,c.dropped),s.set(a.id,c.dead)}for(const t of e.values())we(t,r,n.get(t.id),s.get(t.id)??[]);return n}function ae(e,t){const n={};for(const o of e.values())n[o.id]=t.get(o.id).toString();return n}function ie(e,t,n,o){const r=se(e,t);return function(e,t,n,o){const r=new Map;for(const e of t){const t=r.get(e.owner);t?t.push(e):r.set(e.owner,[e])}for(const[t,s]of r){const r=e.get(t),a=o.get(t);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const t=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${t}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:n(e.variantId)})),ce(r.code,e.node,o,e.foldedProps,a)}c.length>0&&fe(r,c,a)}}(e,n,o,r),ae(e,r)}function ce(e,t,n,o,r){const s=t.name;if(!s)return;const a=t.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,n);const i=`</${s}`,c=e.lastIndexOf(i,t.end);if(c>=t.start){const e=c+2;r.overwrite(e,e+s.length,n)}for(const n of t.attributes??[])"Attribute"===n.type&&n.name&&o.has(n.name)&&ue(e,n,r)}function ue(e,t,n){let o=t.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),n.remove(o,t.end)}function fe(e,t,n){const o=t.map(e=>` import ${e.local} from ${JSON.stringify(e.spec)};`).join("\n"),r=e.ast.instance,s=r?.content?.body??[];if(r&&s.length>0){const e=s[s.length-1];return void n.appendLeft(e.end,`\n${o}`)}r&&r.content?n.appendLeft(r.content.start,`\n${o}\n`):n.prepend(`<script>\n${o}\n<\/script>\n`)}function le(e,t,n){const o=[];return{dropped:de(e,t.constFold,t.narrow,t,n,o),dead:o}}function de(e,t,n,o,r,s){if(0===t.size&&0===n.size)return new Set;const i=e.code,u=[];!function(e,t,n,o,r,s){a(e,null,{IfBlock(e,{next:a}){if(e.elseif||y(e,s))return;const i=m(e,t,n);if(function(e,t,n,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,t,n){const o=e?.nodes??[];return 0===o.length?"":pe(o[0].start,o[o.length-1].end,o,t,n)}(e.kept,t,n));for(const[t,n]of e.removed)o.remove(t,n);if(e.headerRewrite){const{from:t,to:n,text:r}=e.headerRewrite;o.overwrite(t,n,r)}}(i,t,o,r),i.kept)s.push(i.span);else for(const e of i.removed)s.push(e);i.recurse&&a()}})}(e.ast.fragment,t,n,i,r,u),function(e,t,n,o,r){if(0===t.size)return;a(e,null,{ConditionalExpression(e,{next:s}){if(y(e,r))return;const a=c(e.test,t);if(!a.known)return void s();const i=a.value?e.consequent:e.alternate;i?(o.overwrite(e.start,e.end,pe(i.start,i.end,[i],t,n)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,t,i,r,u);const f=function(e,t,n){const o=new Map,r=r=>{r&&me(r,t,e.code,(t,r,s)=>{y(s,n)||s===e.propsPattern||(o.get(t)??function(e,t){const n=[];return e.set(t,n),n}(o,t)).push(r)})};return r(e.ast.instance),r(e.ast.fragment),o}(e,t,u);for(const[e,n]of t){const t=ke(n);for(const n of f.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const l=new Set(t.keys());!function(e,t,n){if(!e.props||0===t.size)return;const o=e.props.filter(e=>!t.has(e.name));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,t,n){let o=t.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=t.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,t.start),a=e.slice(t.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?n.remove(o,r<e.length?r+1:r):n.remove(t.start,";"===e[t.end]?t.end+1:t.end)}(e.code,e.propsDeclaration,n);const r=e.propsPattern?.properties??[],s=new Set(e.props.filter(e=>t.has(e.name)).map(e=>e.property));let a=0;for(;a<r.length;){if(!s.has(r[a])){a++;continue}let t=a;for(;t+1<r.length&&s.has(r[t+1]);)t++;he(e.code,r,a,t,n),a=t+1}if(e.propsPattern)for(const o of e.props)t.has(o.name)&&ve(e.propsPattern,o.name,n)}(e,l,r);return te(e,{...o,constFold:t,narrow:n},r),s&&s.push(...u),l}function pe(e,t,n,o,r){if(0===o.size)return r.slice(e,t);const s=[];for(const e of n)me(e,o,r,(e,t)=>s.push({...t,name:e}));if(0===s.length)return r.slice(e,t);s.sort((e,t)=>e.start-t.start);let a="",i=e;for(const e of s)a+=r.slice(i,e.start),a+=e.head+ke(o.get(e.name))+e.tail,i=e.end;return a+=r.slice(i,t),a}function me(e,t,n,o){a(e,{parent:null,grandparent:null},{_(e,{state:r,next:s}){if("StyleDirective"===e.type&&!0===e.value&&e.name&&t.has(e.name)){let t=e.end;for(;t>e.start&&ye(n[t-1]);)t-=1;const r=n.slice(e.start,t);o(e.name,{start:e.start,end:t,head:`${r}={`,tail:"}"},e)}else"Identifier"===e.type&&e.name&&t.has(e.name)&&!function(e,t){return!!t&&("MemberExpression"===t.type&&t.property===e&&!t.computed||("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand||(!("TSPropertySignature"!==t.type&&"TSMethodSignature"!==t.type||t.key!==e||t.computed)||("ImportSpecifier"===t.type||"ImportDefaultSpecifier"===t.type||"ImportNamespaceSpecifier"===t.type||"ExportSpecifier"===t.type))))}(e,r.parent)&&o(e.name,function(e,t,n,o){if("ClassDirective"===t?.type&&t.expression===e&&":"===o[e.start-1]){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}={`,tail:"}"}}if("ExpressionTag"===t?.type&&"Attribute"===n?.type&&n.name&&"{"===o[n.start])return{start:n.start,end:n.end,head:`${n.name}={`,tail:"}"};if("Property"===t?.type&&!0===t.shorthand&&t.value===e){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}: `,tail:""}}return{start:e.start,end:e.end,head:"",tail:""}}(e,r.parent,r.grandparent,n),e);s({parent:e,grandparent:r.parent})}})}function he(e,t,n,o,r){const s=t[n],a=t[o],i=t[o+1];if(i)return void r.remove(s.start,i.start);let c=a.end,u=c;for(;u<e.length&&/\s/.test(e[u]);)u++;","===e[u]&&(c=u+1);const f=t[n-1];r.remove(f?f.end:s.start,c)}function ve(e,t,n){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?n.remove(s.start,a.start):i?n.remove(i.end,s.end):n.remove(s.start,s.end)}function we(e,t,n,o){a(e.ast.fragment,null,{Component(r,{next:s}){if(o.length>0&&y(r,o))return;const a=r.name?e.imports.get(r.name):void 0,i=a?t.get(a):void 0;if(i&&i.size>0)for(const t of r.attributes??[])"Attribute"===t.type&&t.name&&i.has(t.name)&&ge(t.value)&&ue(e.code,t,n);s()}})}function ge(e){if(!0===e||null==e)return!0;return(Array.isArray(e)?e:[e]).every(e=>"Text"===e.type||"ExpressionTag"===e.type&&"Literal"===e.expression?.type)}function ye(e){return" "===e||"\t"===e||"\n"===e||"\r"===e}function ke(e){return void 0===e?"undefined":JSON.stringify(e)}const xe={enabled:!1,maxVariants:8,minSavings:0};function Se(e,n,o=xe,r=[]){const s=new Map,a=[];if(!o.enabled)return{variants:s,bindings:a};const i=D(e,n),c=new Map,u=new Set;for(const t of e.values()){const o=i.get(t.id)??[];for(const r of t.childCalls){if(o.length>0&&y(r.node,o))continue;const s=e.get(r.childId),a=n.get(r.childId);if(!s||!a)continue;if(a.bail||!s.props||0===s.props.length){u.add(r.childId);continue}const i=Me(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const f=Ce(s,a,i);if(f===Ae(s,a)){u.add(r.childId);continue}const l=c.get(r.childId);l?l.push({owner:t.id,node:r.node,shape:i,code:f}):c.set(r.childId,[{owner:t.id,node:r.node,shape:i,code:f}])}}const f=function(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);n.set(o.id,e.bail?o.code:Ae(o,e))}return n}(e,n),l=new Map;for(const t of e.values())l.set(t.id,Ie(f.get(t.id),t));const d=new Set;for(const e of l.values())for(const t of e)d.add(t);const p=(Array.isArray(r)?r:[r]).filter(t=>e.has(t)).filter(e=>!d.has(e)),m=new Map,h=(e,n)=>{const o=m.get(n);if(void 0!==o)return o;let r;try{const{js:o}=t(n,{generate:"client",dev:!1,filename:e});r=o.code.length}catch{r=null}return null!==r&&m.set(n,r),r},v=new Set;for(const e of c.keys())u.has(e)||v.add(e);for(const[t,n]of c){if(u.has(t))continue;if(n.some(e=>e.owner!==t&&v.has(e.owner)))continue;const r=new Map,i=[];let c=!1;for(const e of n){if(r.has(e.code))continue;if(i.length>=o.maxVariants){c=!0;break}const n=`${t}::v${i.length}`;r.set(e.code,n),i.push({id:n,code:e.code})}if(!c&&be(t,i,e,f,l,p,h,o.minSavings)){for(const e of i){const o=n.find(t=>t.code===e.code);s.set(e.id,{id:e.id,childId:t,code:e.code,foldedProps:o.shape})}for(const e of n)a.push({owner:e.owner,childId:t,node:e.node,variantId:r.get(e.code),foldedProps:e.shape})}}return{variants:s,bindings:a}}function be(e,t,n,o,r,s,a,i){const c=n.get(e),u=new Map;for(const e of t)u.set(e.id,Ie(e.code,c));const f=new Set,l=[...s];for(;l.length>0;){const e=l.pop();if(!f.has(e)){f.add(e);for(const t of r.get(e)??[])l.push(t)}}let d=0;for(const e of f){const t=a(e,o.get(e));if(null===t)return!1;d+=t}const p=t.map(e=>e.id),m=new Set,h=new Set,v=t=>t===e?{comps:[],vars:p}:{comps:[t],vars:[]},w=[],g=[];for(const e of s){const t=v(e);w.push(...t.comps),g.push(...t.vars)}for(;w.length>0||g.length>0;){if(w.length>0){const e=w.pop();if(m.has(e))continue;m.add(e);for(const t of r.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!h.has(e)){h.add(e);for(const t of u.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const t=a(e,o.get(e));if(null===t)return!1;y+=t}for(const e of h){const n=t.find(t=>t.id===e).code,o=a(e,n);if(null===o)return!1;y+=o}return y<d*(1-i)}function Ie(e,t){let n;try{n=r(e,t.id)}catch{return[]}const o=[];return a(n.fragment,null,{Component(e,{next:n}){const r=e.name?t.imports.get(e.name):void 0;r&&o.push(r),n()}}),o}function Me(e,t,n){const o=T(e),r=new Map;for(const e of t.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(n.constFold.has(e)||_(t,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Ce(e,t,n){const r=new Map(t.constFold);for(const[e,t]of n)r.set(e,t);const s=new Map;for(const[e,n]of t.narrow)r.has(e)||s.set(e,n);const a=new o(e.code);return de(e,r,s,t,a),a.toString()}const Ee=new WeakMap;function Ae(e,t){const n=Ee.get(e);if(void 0!==n)return n;const r=new o(e.code);de(e,t.constFold,t.narrow,t,r);const s=r.toString();return Ee.set(e,s),s}class Fe{entries=new Set;resolve;readFile;mode;parse;parseCache=new Map;codeCache=new Map;output={};constructor(e,t,n,o="incremental",r){for(const t of Array.isArray(e)?e:[e])this.entries.add(t);this.resolve=t,this.readFile=n,this.mode=o,this.parse=r}async init(){return this.output=await this.shake(),this.output}get(e){return this.output[e]}snapshot(){return{...this.output}}async update(e){const t="incremental"===this.mode;for(const t of e.removed??[])this.entries.delete(t),this.codeCache.delete(t),this.parseCache.delete(t);for(const n of e.added??[])this.entries.add(n),t&&this.codeCache.set(n,await this.readFile(n));for(const n of e.changed??[])t&&this.codeCache.set(n,await this.readFile(n));const n=this.output,o=await this.shake();this.output=o;const r={};for(const e of Object.keys(o))n[e]!==o[e]&&(r[e]=o[e]);return{changed:r,removed:Object.keys(n).filter(e=>!(e in o))}}async shake(){const e="incremental"===this.mode,t=e?this.cachedReadFile:this.readFile,n=e?this.parseCache:this.parse?new Map:void 0,o=await M([...this.entries],this.resolve,t,n,this.parse),{models:r,plans:s}=I(o,n);return re(r,s)}cachedReadFile=async e=>{const t=this.codeCache.get(e);if(void 0!==t)return t;const n=await this.readFile(e);return this.codeCache.set(e,n),n}}async function Pe(e,t,n,o){const{models:r,plans:s}=await De(e,t,n,o);return $e(r,re(r,s))}async function De(e,t,n,o){if(!o)return b(e,t,n);const r=new Map;return I(await M(e,t,n,r,o),r)}function $e(e,t){for(const[n,o]of Object.entries(t)){const s=e.get(n);if(s&&o!==s.code)try{r(o,n)}catch{t[n]=s.code}}return t}async function ze(e,t,n,o=xe,r=e=>e,s){const{models:a,plans:i}=await De(e,t,n,s),c=Se(a,i,o,e);return{files:$e(a,0===c.bindings.length?re(a,i):ie(a,i,c.bindings.map(e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps})),r)),mono:c}}export{xe as DEFAULT_MONO_OPTIONS,Fe as DevShaker,b as analyze,I as analyzeInput,M as buildAnalyzeInput,Se as monomorphize,Pe as svelteShaker,ze as svelteShakerWithMono,re as transformAll,ie as transformAllWithMono};
@@ -1,4 +1,4 @@
1
- import { type AnyNode, type ParseCache, type Root } from './parse';
1
+ import { type AnyNode, type Parse, type ParseCache, type Root } from './parse';
2
2
  import { type AnalyzeInput, type ComponentId, type ComponentPlan, type Literal } from './ir';
3
3
  import { type Span } from './dead';
4
4
  export type Resolve = (source: string, importer: ComponentId) => Promise<ComponentId | null> | ComponentId | null;
@@ -126,7 +126,7 @@ export declare function analyzeInput(input: AnalyzeInput, parseCache?: ParseCach
126
126
  * import is never crawled — its `<Comp/>` site cannot exist, so it cannot taint a
127
127
  * value set), keeping the produced model set — and thus the output — identical.
128
128
  */
129
- export declare function buildAnalyzeInput(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, parseCache?: ParseCache): Promise<AnalyzeInput>;
129
+ export declare function buildAnalyzeInput(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, parseCache?: ParseCache, parse?: Parse): Promise<AnalyzeInput>;
130
130
  /**
131
131
  * Dead `{#if}` spans per component implied by `plans`, via the SAME shared
132
132
  * predicate the transform uses ({@link computeDeadSpans}). A bailed component
@@ -1,4 +1,5 @@
1
1
  import { type ReadFile, type Resolve } from './analyze';
2
+ import type { Parse } from './parse';
2
3
  import type { ComponentId, EditResult } from './ir';
3
4
  /**
4
5
  * dev shake granularity (docs/RUST-MIGRATION.md §3 M2 / ARCHITECTURE §6.2):
@@ -39,13 +40,15 @@ export declare class DevShaker {
39
40
  private readonly resolve;
40
41
  private readonly readFile;
41
42
  private readonly mode;
43
+ /** Non-default parser (e.g. rsvelte), or `undefined` for svelte/compiler. */
44
+ private readonly parse;
42
45
  /** Content-keyed AST cache — only changed files re-parse (§2.2). */
43
46
  private readonly parseCache;
44
47
  /** Last-seen source per file, so an update re-reads only what changed. */
45
48
  private readonly codeCache;
46
49
  /** Current slimmed output per `.svelte` id (the live shake result). */
47
50
  private output;
48
- constructor(files: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, mode?: DevMode);
51
+ constructor(files: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, mode?: DevMode, parse?: Parse);
49
52
  /** Full initial shake of the program. Returns the slimmed source per file. */
50
53
  init(): Promise<Record<ComponentId, string>>;
51
54
  /** The current slimmed source for a file, or `undefined` if not in the program. */
@@ -1,8 +1,10 @@
1
1
  import { type ReadFile, type Resolve } from './analyze';
2
+ import { type Parse } from './parse';
2
3
  import { type MonomorphizeOptions, type MonomorphizeResult } from './mono';
3
4
  import type { ComponentId } from './ir';
4
5
  export type { ComponentId, AnalyzeInput, InputFile, ResolvedEdge, EdgeKind, EditResult, } from './ir';
5
6
  export type { Resolve, ReadFile } from './analyze';
7
+ export type { Parse, Root } from './parse';
6
8
  export { analyze, analyzeInput, buildAnalyzeInput } from './analyze';
7
9
  export { DevShaker, type DevMode, type DevShakerChange } from './engine';
8
10
  export { transformAll, transformAllWithMono } from './transform';
@@ -17,7 +19,7 @@ export { monomorphize, DEFAULT_MONO_OPTIONS, type MonomorphizeOptions, type Mono
17
19
  * Node callers use `fsResolve` / `fs.readFileSync` from `svelte-shaker/node`.
18
20
  * See docs/ARCHITECTURE.md §5 — this is the Engine; the Shell owns resolution.
19
21
  */
20
- export declare function svelteShaker(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile): Promise<Record<ComponentId, string>>;
22
+ export declare function svelteShaker(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, parse?: Parse): Promise<Record<ComponentId, string>>;
21
23
  /** The full output of a shake including L2 specialization. */
22
24
  export interface ShakeResult {
23
25
  /**
@@ -42,4 +44,4 @@ export type VariantSpecifier = (variantId: string) => string;
42
44
  * specialized and `files` equals the L0/L1/L1.5 output exactly — a strict
43
45
  * superset of the default behavior, so existing consumers are unaffected.
44
46
  */
45
- export declare function svelteShakerWithMono(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, mono?: MonomorphizeOptions, variantSpecifier?: VariantSpecifier): Promise<ShakeResult>;
47
+ export declare function svelteShakerWithMono(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, mono?: MonomorphizeOptions, variantSpecifier?: VariantSpecifier, parse?: Parse): Promise<ShakeResult>;
@@ -85,7 +85,15 @@ export type ParseCache = Map<string, {
85
85
  code: string;
86
86
  ast: Root;
87
87
  }>;
88
- export declare function parseCached(filename: string, code: string, cache?: ParseCache): Root;
88
+ /**
89
+ * A `.svelte` -> modern-AST parser, swappable for the default {@link parseSvelte}
90
+ * (svelte/compiler). The engine reads only the AST it returns, so any parser that
91
+ * emits svelte/compiler's modern shape (UTF-16 `start`/`end`) can drive it — the
92
+ * Vite plugin's `parser: 'rsvelte'` option supplies rsvelte's native parser here
93
+ * (docs/RUST-MIGRATION.md §6).
94
+ */
95
+ export type Parse = (code: string, filename: string) => Root;
96
+ export declare function parseCached(filename: string, code: string, cache?: ParseCache, parse?: Parse): Root;
89
97
  export interface WalkCtx<S> {
90
98
  state: S;
91
99
  next: (state?: S) => void;
@@ -0,0 +1,14 @@
1
+ import type { Parse } from './parse';
2
+ /**
3
+ * Build a {@link Parse} backed by rsvelte's native parser
4
+ * (`@rsvelte/vite-plugin-svelte-native`), or `null` if that OPTIONAL peer package
5
+ * can't be loaded (not installed / unsupported platform) — the caller then falls
6
+ * back to svelte/compiler.
7
+ *
8
+ * `skipExpressionLoc: true` is REQUIRED, not cosmetic: the per-expression `loc`
9
+ * blocks roughly double the AST and make the engine's walk the dominant cost
10
+ * (full-pipeline 0.72x WITH them vs 1.46x WITHOUT) — and the engine reads only
11
+ * UTF-16 `start`/`end`, never `loc`, so dropping them leaves the output identical
12
+ * (docs/RUST-MIGRATION.md §6).
13
+ */
14
+ export declare function tryLoadRsvelteParser(): Parse | null;
@@ -32,6 +32,19 @@ export interface ShakerOptions {
32
32
  * L2 is NOT applied in dev (L0/L1/L1.5 only — docs §5 risks).
33
33
  */
34
34
  dev?: false | DevMode;
35
+ /**
36
+ * Which parser feeds the engine (docs/RUST-MIGRATION.md §6). Default
37
+ * `'svelte'` — svelte/compiler, byte-for-byte the established behavior.
38
+ * `'rsvelte'` uses rsvelte's native parser (the OPTIONAL peer
39
+ * `@rsvelte/vite-plugin-svelte-native`), which parses ~2.2x faster (full
40
+ * pipeline ~1.46x) and shakes a sound superset. The engine reads only UTF-16
41
+ * `start`/`end`, so the choice never affects soundness — only speed and
42
+ * (occasionally) how much is shaken. If `'rsvelte'` is requested but the native
43
+ * package can't be loaded (not installed, or no binary for this platform) the
44
+ * plugin THROWS rather than silently falling back, so the output stays
45
+ * deterministic across machines (install the peer on every build platform).
46
+ */
47
+ parser?: 'svelte' | 'rsvelte';
35
48
  }
36
49
  /**
37
50
  * Source-level Svelte tree-shaking as a Vite plugin (docs/ARCHITECTURE.md §6).
package/dist/vite.js CHANGED
@@ -1 +1 @@
1
- import*as e from"node:fs";import*as t from"node:path";import{parse as n,compile as o}from"svelte/compiler";import{walk as r}from"zimmerframe";import s from"magic-string";function a(e,t){return n(e,{modern:!0,filename:t})}function i(e,t,n){if(!n)return a(t,e);const o=n.get(e);if(o&&o.code===t)return o.ast;const r=a(t,e);return n.set(e,{code:t,ast:r}),r}function c(e,t,n){r(e,t,n)}const u={known:!1};function l(e,t){if(!e)return u;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:u}case"UnaryExpression":{const n=l(e.argument,t);if(!n.known)return u;const o=n.value;switch(e.operator){case"!":return{known:!0,value:!o};case"-":return{known:!0,value:-o};case"+":return{known:!0,value:+o};case"typeof":return{known:!0,value:typeof o};case"void":return{known:!0,value:void 0};default:return u}}case"LogicalExpression":{const n=l(e.left,t);if(!n.known)return u;switch(e.operator){case"&&":return n.value?l(e.right,t):n;case"||":return n.value?n:l(e.right,t);case"??":return null===n.value||void 0===n.value?l(e.right,t):n;default:return u}}case"BinaryExpression":{const n=l(e.left,t),o=l(e.right,t);if(!n.known||!o.known)return u;const r=n.value,s=o.value;switch(e.operator){case"===":return{known:!0,value:r===s};case"!==":return{known:!0,value:r!==s};case"==":return{known:!0,value:r==s};case"!=":return{known:!0,value:r!=s};case"<":return{known:!0,value:r<s};case">":return{known:!0,value:r>s};case"<=":return{known:!0,value:r<=s};case">=":return{known:!0,value:r>=s};case"+":return{known:!0,value:r+s};case"-":return{known:!0,value:r-s};case"*":return{known:!0,value:r*s};case"/":return{known:!0,value:r/s};case"%":return{known:!0,value:r%s};default:return u}}default:return u}}function f(e,t,n){const o=l(e,t);if(o.known)return o;const r=d(e,t,n);return"unknown"===r?u:{known:!0,value:r}}function d(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?h(d(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=d(e.left,t,n),a=()=>d(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,t){return!0===e||!0===t||(!1!==e||!1!==t)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=p(e,o),a=l(t,n);if(s&&a.known)return m(s,a.value,r);const i=p(t,o),c=l(e,n);return i&&c.known?m(i,c.value,r):c.known&&a.known?r?c.value==a.value:c.value===a.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?h(s):s}return"unknown"}default:return"unknown"}var o,r}function p(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function m(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function h(e){return!0!==e&&(!1===e||"unknown")}function v(e,t,n){const{arms:o,elseFrag:r}=function(e){const t=[];let n,o=e;for(;o;){t.push({block:o,test:o.test,consequent:o.consequent});const e=o.alternate,r="Fragment"===e?.type&&1===e.nodes?.length&&"IfBlock"===e.nodes[0]?.type&&!0===e.nodes[0].elseif?e.nodes[0]:void 0;r?o=r:("Fragment"===e?.type&&(n=e),o=void 0)}return n?{arms:t,elseFrag:n}:{arms:t}}(e),s=[e.start,e.end],a=o.map((e=>f(e.test,t,n))),i=e=>e.known&&Boolean(e.value),c=e=>e.known&&!e.value;let u=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&u){const t=o[e].consequent;return{span:s,kept:t,removed:w(s,g(t)),recurse:!1}}c(t)||(u=!1)}const l=a.findIndex((e=>!c(e)));if(-1===l)return r?{span:s,kept:r,removed:w(s,g(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:y(o,a,l),recurse:!0};const d=o[l].block;return{span:s,kept:void 0,removed:[[s[0],d.start],...y(o,a,l)],recurse:!1,headerRewrite:{from:d.start,to:d.test.start,text:"{#if "}}}function w(e,t){if(!t)return[e];const n=[];return e[0]<t[0]&&n.push([e[0],t[0]]),t[1]<e[1]&&n.push([t[1],e[1]]),n}function g(e){const t=e?.nodes??[];return 0===t.length?null:[t[0].start,t[t.length-1].end]}function y(e,t,n){const o=[];for(let r=n+1;r<e.length;r++){const n=t[r];if(!n.known||n.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:k(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function k(e,t){const n=e?.nodes??[];return n.length?n[n.length-1].end:t}function x(e,t){return t.some((([t,n])=>e.start>=t&&e.end<=n))}function S(e,t,n){if(0===t.size&&0===n.size)return[];const o=[];return c(e,null,{IfBlock(e,{next:r}){if(e.elseif||x(e,o))return;const s=v(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const b=e=>e.endsWith(".svelte"),I=10,M="escapes as value (e.g. <svelte:component this={X}>)";async function F(e,t,n){return A(await C(e,t,n))}function A(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,R(r,n.get(r.id)??[],t));return o}(e,t),o=new Set;for(const e of n.values())for(const t of e.escapedComponents)o.add(t);for(const e of o){const t=n.get(e);t&&!t.bailReasons.includes(M)&&t.bailReasons.push(M)}let r=$(n,E(n,new Map));for(let e=0;e<I;e++){const e=$(n,E(n,O(n,r)));if(D(r,e)){r=e;break}r=e}return{models:n,plans:r}}async function C(e,t,n,o){const r=Array.isArray(e)?[...e]:[e],s=[],a=[],c=[...r],u=new Set(c);for(;c.length>0;){const e=c.shift(),r=await n(e);s.push({id:e,code:r});const l=i(e,r,o),f=l.instance;if(!f)continue;const d=new Map,p=new Map,m=[];for(const o of H(f)){if("*"===o.imported){p.set(o.local,o.value);continue}if("default"===o.imported&&b(o.value)){const n=await t(o.value,e);n&&(a.push({from:e,local:o.local,to:n,kind:"default-svelte"}),m.push(n));continue}const r=await Y(o.value,o.imported,e,t,n);r&&(a.push({from:e,local:o.local,to:r,kind:"barrel"}),d.set(o.local,r))}const h=[];if(p.size>0)for(const o of B(l)){const r=o.indexOf("."),s=p.get(o.slice(0,r));if(null==s)continue;const i=await Y(s,o.slice(r+1),e,t,n);i&&(a.push({from:e,local:o,to:i,kind:"namespace"}),h.push(i))}const v=W(l,d);for(const e of[...m,...v,...h])u.has(e)||(u.add(e),c.push(e))}return{files:s,edges:a,entries:r}}function E(e,t){const n=new Map,o=e=>{let t=n.get(e);return t||(t={sites:[]},n.set(e,t)),t};for(const n of e.values()){const e=t.get(n.id)??[];for(const t of n.childCalls)e.length>0&&x(t.node,e)||o(t.childId).sites.push(T(t.node))}return n}function $(e,t){const n=new Map;for(const o of e.values())n.set(o.id,G(o,t.get(o.id)));return n}function D(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!P(o.constFold,e.constFold))return!1;if(!z(o.narrow,e.narrow))return!1}return!0}function P(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e)if(!t.has(n)||!Object.is(t.get(n),o))return!1;return!0}function z(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e||o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!Object.is(o[t],e[t]))return!1}return!0}function O(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=S(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function R(e,t,n){const{id:o,code:r}=e,s=i(o,r,n),a=new Map;for(const e of t)a.set(e.local,e.to);const u=[];c(s.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||u.push(`<svelte:options ${t.name}>`);t()}});let l,f,d=null,p=!1;const m=new Set,h=new Set,v=s.instance;if(v){for(const e of H(v))m.add(e.local),"*"===e.imported&&h.add(e.local);const e=function(e){const t=e.content;for(const e of t?.body??[])if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("CallExpression"===n?.type&&"Identifier"===n.callee?.type&&"$props"===n.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(e){l=e.declaration,f=e.pattern,e.sharesStatement&&u.push("$props() shares a multi-declarator statement"),d=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){p=!0;continue}if("Property"!==t.type)continue;const e=t.key;if("Identifier"!==e?.type||!e.name)continue;const n=t.value,o="AssignmentPattern"===n?.type?n.right:void 0;d.push({name:e.name,property:t,defaultExpr:o})}}}const w=function(e,t){const n=[];return c(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.push({childId:r,node:e}),o()}}),n}(s,a),{shadowedNames:g,debugNames:y}=function(e,t,n){const o=new Set,r=new Set;t&&c(t,null,{_(e,{next:t}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===n||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const t of e.params??[])N(t,o);t()}});return c(e.fragment,null,{EachBlock(e,{next:t}){N(e.context,o),"string"==typeof e.index&&o.add(e.index),t()},SnippetBlock(e,{next:t}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const t of e.parameters??[])N(t,o);t()},AwaitBlock(e,{next:t}){N(e.value,o),N(e.error,o),t()},LetDirective(e,{next:t}){e.name&&o.add(e.name),t()},ConstTag(e,{next:t}){for(const t of e.declaration?.declarations??[])N(t.id,o);t()},DebugTag(e,{next:t}){for(const t of e.identifiers??[])"Identifier"===t.type&&t.name&&r.add(t.name);t()}}),{shadowedNames:o,debugNames:r}}(s,v,l),k=function(e,t,n,o){const r=new Set,s=e=>{if(!e)return;const n=t.get(e);if(n&&r.add(n),o.has(e))for(const[n,o]of t)n.startsWith(`${e}.`)&&r.add(o)};c(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&j(e,t.parent)&&s(e.name),o({parent:e})}}),e.instance&&c(e.instance,{parent:null},{_(e,{state:n,next:r}){"Identifier"===e.type&&e.name&&(t.has(e.name)||o.has(e.name))&&j(e,n.parent)&&!L(n.parent)&&s(e.name),r({parent:e})}});return r}(s,a,m,h);return{id:o,code:r,ast:s,imports:a,props:d,propsDeclaration:l,propsPattern:f,hasRestProp:p,childCalls:w,shadowedNames:g,debugNames:y,escapedComponents:k,bailReasons:u}}function N(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?N(n.argument,t):"Property"===n.type&&N(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])N(n,t);return;case"AssignmentPattern":return void N(e.left,t);case"RestElement":return void N(e.argument,t);default:return}}function j(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand)&&!L(t)))}function L(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function W(e,t){const n=new Set;return 0===t.size||c(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.add(r),o()}}),n}function B(e){const t=new Set;return c(e.fragment,null,{Component(e,{next:n}){"string"==typeof e.name&&e.name.includes(".")&&t.add(e.name),n()}}),t}function T(e){const t=e.attributes??[];let n=-1;for(let e=0;e<t.length;e++)"SpreadAttribute"===t[e].type&&(n=e);const o=new Map;for(let e=0;e<t.length;e++){const r=t[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,_(e,n));continue}if("Attribute"!==r.type||!s)continue;const a=q(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>n}:_(e,n))}for(const r of function(e){const t=e.fragment?.nodes??[],n=[];let o=!1;for(const e of t)if("SnippetBlock"!==e.type){if("Comment"!==e.type){if("Text"===e.type){if(""===(e.data??e.raw??"").trim())continue}o=!0}}else"Identifier"===e.expression?.type&&e.expression.name&&n.push(e.expression.name);o&&n.push("children");return n}(e))o.set(r,_(t.length,n));return{hadSpread:n>=0,explicit:o}}function _(e,t){return{value:void 0,dynamic:!0,afterLastSpread:e>t}}function q(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const t=Array.isArray(e)?e:[e];if(1===t.length){const e=t[0];return"Text"===e.type?{known:!0,value:e.data??e.raw??""}:"ExpressionTag"===e.type&&"Literal"===e.expression?.type?{known:!0,value:e.expression.value}:{known:!1}}let n="";for(const e of t){if("Text"!==e.type)return{known:!1};n+=e.data??e.raw??""}return{known:!0,value:n}}function V(e,t){return e.shadowedNames.has(t)||e.debugNames.has(t)}function G(e,t){const n={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return n.bail=!0,n.reasons.push(...e.bailReasons),n;if(!e.props||0===e.props.length)return n;const o=t?.sites??[];if(0===o.length)return n;for(const t of e.props){if(V(e,t.name))continue;const r=U(t,o);n.valueSets.set(t.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&n.narrow.set(t.name,r.values):n.constFold.set(t.name,r.values[0]))}return n}function U(e,t){const n=[];let o=!1,r=!1;const s=e=>{n.some((t=>Object.is(t,e)))||n.push(e)};for(const n of t){const t=n.explicit.get(e.name);if(t?.afterLastSpread){t.dynamic?o=!0:s(t.value);continue}if(n.hadSpread){r=!0;continue}const a=J(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function J(e){return e?"Literal"===e.type?{known:!0,value:e.value}:"Identifier"===e.type&&"undefined"===e.name?{known:!0,value:void 0}:{known:!1}:{known:!0,value:void 0}}function*H(e){const t=e.content;for(const e of t?.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t)for(const n of e.specifiers??[]){const e=n.local?.name;e&&("ImportDefaultSpecifier"===n.type?yield{value:t,local:e,imported:"default"}:"ImportNamespaceSpecifier"===n.type?yield{value:t,local:e,imported:"*"}:"ImportSpecifier"===n.type&&(yield{value:t,local:e,imported:X(n)??e}))}}}function X(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"Literal"===t?.type&&"string"==typeof t.value?t.value:void 0}function K(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const Q=8;async function Y(e,t,n,o,r,s=0){if(s>Q)return null;const i=await o(e,n);if(!i)return null;if(b(e)||b(i))return"default"===t||"*"===t?i:null;let c;try{c=await r(i)}catch{return null}const u=function(e,t){try{const n=a(`<script module lang="ts">\n${e}\n<\/script>`,t);return n.module?.content?.body??null}catch{return null}}(c,i);if(!u)return null;for(const e of u)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(K(n.exported)===t)return Y(String(e.source.value),K(n.local)??"default",i,o,r,s+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const n=await Y(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(K(n.exported)!==t)continue;const e=K(n.local);if(!e)continue;const a=Z(u,e);return a?Y(a.value,a.imported,i,o,r,s+1):null}return null}function Z(e,t){for(const n of e){if("ImportDeclaration"!==n.type)continue;const e=n.source?.value;if("string"==typeof e)for(const o of n.specifiers??[])if(o.local?.name===t){if("ImportDefaultSpecifier"===o.type)return{value:e,imported:"default"};if("ImportNamespaceSpecifier"===o.type)return{value:e,imported:"*"};if("ImportSpecifier"===o.type)return{value:e,imported:X(o)??t}}}return null}const ee=64;const te=Symbol("unbounded-class-source");function ne(e,t,n){if(!0===e)return te;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=oe(e,t,n);if(o===te)return te;const s=[];for(const e of r)for(const t of o)if(s.push(e+t),s.length>ee)return te;r=s}const s=new Set;for(const e of r)for(const t of e.split(/\s+/))t&&s.add(t);return s}function oe(e,t,n){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,t,n){if(!e)return te;if("Identifier"===e.type&&e.name&&n.has(e.name)){const t=new Set;for(const o of n.get(e.name))t.add(re(o));return t}const o=l(e,t);return o.known?new Set([re(o.value)]):te}(e.expression,t,n):te}function re(e){return String(e)}function se(e,t,n){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,s=t.narrow;return c(e.ast.fragment,null,{_(e,{next:t}){if("RegularElement"===(a=e.type)||"SvelteElement"===a||"Component"===a||"SvelteComponent"===a||"SvelteSelf"===a){var a;for(const t of e.attributes??[]){if("SpreadAttribute"===t.type){o=!0;continue}if("ClassDirective"===t.type){t.name&&n.add(t.name);continue}if("Attribute"!==t.type||"class"!==t.name)continue;const e=ne(t.value,r,s);if(e===te)o=!0;else for(const t of e)n.add(t)}t()}else t()}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let s=0;for(const t of o.children)"Rule"===t.type&&ae(t,r.classes)&&(ie(e.code,t,o.children,n),s+=1);return s}function ae(e,t){if(function(e){let t=!1;return c(e,null,{_(e,{next:n}){"PseudoClassSelector"===e.type&&"global"===e.name&&(t=!0),n()}}),t}(e))return!1;const n=e.prelude?.children??[];return 0!==n.length&&n.every((e=>function(e,t){let n=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!t.has(e.name)&&(n=!0);return n}(e,t)))}function ie(e,t,n,o){const r=n.indexOf(t),s=n[r-1];let a=t.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,t.end)}function ce(e,t){return le(e,ue(e,t))}function ue(e,t){const n=new Map,o=new Map,r=new Map;for(const a of e.values()){const e=new s(a.code);n.set(a.id,e);const i=t.get(a.id);if(i.bail){o.set(a.id,new Set);continue}const c=he(a,i,e);o.set(a.id,c.dropped),r.set(a.id,c.dead)}for(const t of e.values())xe(t,o,n.get(t.id),r.get(t.id)??[]);return n}function le(e,t){const n={};for(const o of e.values())n[o.id]=t.get(o.id).toString();return n}function fe(e,t,n,o){const r=ue(e,t);return function(e,t,n,o){const r=new Map;for(const e of t){const t=r.get(e.owner);t?t.push(e):r.set(e.owner,[e])}for(const[t,s]of r){const r=e.get(t),a=o.get(t);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const t=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${t}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:n(e.variantId)})),de(r.code,e.node,o,e.foldedProps,a)}c.length>0&&me(r,c,a)}}(e,n,o,r),le(e,r)}function de(e,t,n,o,r){const s=t.name;if(!s)return;const a=t.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,n);const i=`</${s}`,c=e.lastIndexOf(i,t.end);if(c>=t.start){const e=c+2;r.overwrite(e,e+s.length,n)}for(const n of t.attributes??[])"Attribute"===n.type&&n.name&&o.has(n.name)&&pe(e,n,r)}function pe(e,t,n){let o=t.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),n.remove(o,t.end)}function me(e,t,n){const o=t.map((e=>` import ${e.local} from ${JSON.stringify(e.spec)};`)).join("\n"),r=e.ast.instance,s=r?.content?.body??[];if(r&&s.length>0){const e=s[s.length-1];n.appendLeft(e.end,`\n${o}`)}else r&&r.content?n.appendLeft(r.content.start,`\n${o}\n`):n.prepend(`<script>\n${o}\n<\/script>\n`)}function he(e,t,n){const o=[];return{dropped:ve(e,t.constFold,t.narrow,t,n,o),dead:o}}function ve(e,t,n,o,r,s){if(0===t.size&&0===n.size)return new Set;const a=e.code,i=[];!function(e,t,n,o,r,s){c(e,null,{IfBlock(e,{next:a}){if(e.elseif||x(e,s))return;const i=v(e,t,n);if(function(e,t,n,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,t,n){const o=e?.nodes??[];return 0===o.length?"":we(o[0].start,o[o.length-1].end,o,t,n)}(e.kept,t,n));for(const[t,n]of e.removed)o.remove(t,n);if(e.headerRewrite){const{from:t,to:n,text:r}=e.headerRewrite;o.overwrite(t,n,r)}}(i,t,o,r),i.kept)s.push(i.span);else for(const e of i.removed)s.push(e);i.recurse&&a()}})}(e.ast.fragment,t,n,a,r,i),function(e,t,n,o,r){if(0===t.size)return;c(e,null,{ConditionalExpression(e,{next:s}){if(x(e,r))return;const a=l(e.test,t);if(!a.known)return void s();const i=a.value?e.consequent:e.alternate;i?(o.overwrite(e.start,e.end,we(i.start,i.end,[i],t,n)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,t,a,r,i);const u=function(e,t,n){const o=new Map,r=r=>{r&&ge(r,t,e.code,((t,r,s)=>{x(s,n)||s===e.propsPattern||(o.get(t)??function(e,t){const n=[];return e.set(t,n),n}(o,t)).push(r)}))};return r(e.ast.instance),r(e.ast.fragment),o}(e,t,i);for(const[e,n]of t){const t=be(n);for(const n of u.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const f=new Set(t.keys());!function(e,t,n){if(!e.props||0===t.size)return;const o=e.props.filter((e=>!t.has(e.name)));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,t,n){let o=t.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=t.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,t.start),a=e.slice(t.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?n.remove(o,r<e.length?r+1:r):n.remove(t.start,";"===e[t.end]?t.end+1:t.end)}(e.code,e.propsDeclaration,n);const r=e.propsPattern?.properties??[],s=new Set(e.props.filter((e=>t.has(e.name))).map((e=>e.property)));let a=0;for(;a<r.length;){if(!s.has(r[a])){a++;continue}let t=a;for(;t+1<r.length&&s.has(r[t+1]);)t++;ye(e.code,r,a,t,n),a=t+1}if(e.propsPattern)for(const o of e.props)t.has(o.name)&&ke(e.propsPattern,o.name,n)}(e,f,r);return se(e,{...o,constFold:t,narrow:n},r),s&&s.push(...i),f}function we(e,t,n,o,r){if(0===o.size)return r.slice(e,t);const s=[];for(const e of n)ge(e,o,r,((e,t)=>s.push({...t,name:e})));if(0===s.length)return r.slice(e,t);s.sort(((e,t)=>e.start-t.start));let a="",i=e;for(const e of s)a+=r.slice(i,e.start),a+=e.head+be(o.get(e.name))+e.tail,i=e.end;return a+=r.slice(i,t),a}function ge(e,t,n,o){c(e,{parent:null,grandparent:null},{_(e,{state:r,next:s}){if("StyleDirective"===e.type&&!0===e.value&&e.name&&t.has(e.name)){let t=e.end;for(;t>e.start&&(" "===(a=n[t-1])||"\t"===a||"\n"===a||"\r"===a);)t-=1;const r=n.slice(e.start,t);o(e.name,{start:e.start,end:t,head:`${r}={`,tail:"}"},e)}else"Identifier"===e.type&&e.name&&t.has(e.name)&&!function(e,t){return!!t&&("MemberExpression"===t.type&&t.property===e&&!t.computed||("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand||("ImportSpecifier"===t.type||"ImportDefaultSpecifier"===t.type||"ImportNamespaceSpecifier"===t.type||"ExportSpecifier"===t.type)))}(e,r.parent)&&o(e.name,function(e,t,n,o){if("ClassDirective"===t?.type&&t.expression===e&&":"===o[e.start-1]){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}={`,tail:"}"}}if("ExpressionTag"===t?.type&&"Attribute"===n?.type&&n.name&&"{"===o[n.start])return{start:n.start,end:n.end,head:`${n.name}={`,tail:"}"};if("Property"===t?.type&&!0===t.shorthand&&t.value===e){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}: `,tail:""}}return{start:e.start,end:e.end,head:"",tail:""}}(e,r.parent,r.grandparent,n),e);var a;s({parent:e,grandparent:r.parent})}})}function ye(e,t,n,o,r){const s=t[n],a=t[o],i=t[o+1];if(i)return void r.remove(s.start,i.start);let c=a.end,u=c;for(;u<e.length&&/\s/.test(e[u]);)u++;","===e[u]&&(c=u+1);const l=t[n-1];r.remove(l?l.end:s.start,c)}function ke(e,t,n){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex((e=>"Identifier"===e.key?.type&&e.key.name===t));if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?n.remove(s.start,a.start):i?n.remove(i.end,s.end):n.remove(s.start,s.end)}function xe(e,t,n,o){c(e.ast.fragment,null,{Component(r,{next:s}){if(o.length>0&&x(r,o))return;const a=r.name?e.imports.get(r.name):void 0,i=a?t.get(a):void 0;if(i&&i.size>0)for(const t of r.attributes??[])"Attribute"===t.type&&t.name&&i.has(t.name)&&Se(t.value)&&pe(e.code,t,n);s()}})}function Se(e){if(!0===e||null==e)return!0;return(Array.isArray(e)?e:[e]).every((e=>"Text"===e.type||"ExpressionTag"===e.type&&"Literal"===e.expression?.type))}function be(e){return void 0===e?"undefined":JSON.stringify(e)}const Ie={enabled:!1,maxVariants:8,minSavings:0};function Me(e,t,n=Ie,r=[]){const s=new Map,a=[];if(!n.enabled)return{variants:s,bindings:a};const i=O(e,t),c=new Map,u=new Set;for(const n of e.values()){const o=i.get(n.id)??[];for(const r of n.childCalls){if(o.length>0&&x(r.node,o))continue;const s=e.get(r.childId),a=t.get(r.childId);if(!s||!a)continue;if(a.bail||!s.props||0===s.props.length){u.add(r.childId);continue}const i=Ce(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const l=Ee(s,a,i);if(l===De(s,a)){u.add(r.childId);continue}const f=c.get(r.childId);f?f.push({owner:n.id,node:r.node,shape:i,code:l}):c.set(r.childId,[{owner:n.id,node:r.node,shape:i,code:l}])}}const l=function(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);n.set(o.id,e.bail?o.code:De(o,e))}return n}(e,t),f=new Map;for(const t of e.values())f.set(t.id,Ae(l.get(t.id),t));const d=new Set;for(const e of f.values())for(const t of e)d.add(t);const p=(Array.isArray(r)?r:[r]).filter((t=>e.has(t))).filter((e=>!d.has(e))),m=new Map,h=(e,t)=>{const n=m.get(t);if(void 0!==n)return n;let r;try{const{js:n}=o(t,{generate:"client",dev:!1,filename:e});r=n.code.length}catch{r=null}return null!==r&&m.set(t,r),r},v=new Set;for(const e of c.keys())u.has(e)||v.add(e);for(const[t,o]of c){if(u.has(t))continue;if(o.some((e=>e.owner!==t&&v.has(e.owner))))continue;const r=new Map,i=[];let c=!1;for(const e of o){if(r.has(e.code))continue;if(i.length>=n.maxVariants){c=!0;break}const o=`${t}::v${i.length}`;r.set(e.code,o),i.push({id:o,code:e.code})}if(!c&&Fe(t,i,e,l,f,p,h,n.minSavings)){for(const e of i){const n=o.find((t=>t.code===e.code));s.set(e.id,{id:e.id,childId:t,code:e.code,foldedProps:n.shape})}for(const e of o)a.push({owner:e.owner,childId:t,node:e.node,variantId:r.get(e.code),foldedProps:e.shape})}}return{variants:s,bindings:a}}function Fe(e,t,n,o,r,s,a,i){const c=n.get(e),u=new Map;for(const e of t)u.set(e.id,Ae(e.code,c));const l=new Set,f=[...s];for(;f.length>0;){const e=f.pop();if(!l.has(e)){l.add(e);for(const t of r.get(e)??[])f.push(t)}}let d=0;for(const e of l){const t=a(e,o.get(e));if(null===t)return!1;d+=t}const p=t.map((e=>e.id)),m=new Set,h=new Set,v=t=>t===e?{comps:[],vars:p}:{comps:[t],vars:[]},w=[],g=[];for(const e of s){const t=v(e);w.push(...t.comps),g.push(...t.vars)}for(;w.length>0||g.length>0;){if(w.length>0){const e=w.pop();if(m.has(e))continue;m.add(e);for(const t of r.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!h.has(e)){h.add(e);for(const t of u.get(e)??[]){const e=v(t);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const t=a(e,o.get(e));if(null===t)return!1;y+=t}for(const e of h){const n=t.find((t=>t.id===e)).code,o=a(e,n);if(null===o)return!1;y+=o}return y<d*(1-i)}function Ae(e,t){let n;try{n=a(e,t.id)}catch{return[]}const o=[];return c(n.fragment,null,{Component(e,{next:n}){const r=e.name?t.imports.get(e.name):void 0;r&&o.push(r),n()}}),o}function Ce(e,t,n){const o=T(e),r=new Map;for(const e of t.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(n.constFold.has(e)||V(t,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Ee(e,t,n){const o=new Map(t.constFold);for(const[e,t]of n)o.set(e,t);const r=new Map;for(const[e,n]of t.narrow)o.has(e)||r.set(e,n);const a=new s(e.code);return ve(e,o,r,t,a),a.toString()}const $e=new WeakMap;function De(e,t){const n=$e.get(e);if(void 0!==n)return n;const o=new s(e.code);ve(e,t.constFold,t.narrow,t,o);const r=o.toString();return $e.set(e,r),r}class Pe{entries=new Set;resolve;readFile;mode;parseCache=new Map;codeCache=new Map;output={};constructor(e,t,n,o="incremental"){for(const t of Array.isArray(e)?e:[e])this.entries.add(t);this.resolve=t,this.readFile=n,this.mode=o}async init(){return this.output=await this.shake(),this.output}get(e){return this.output[e]}snapshot(){return{...this.output}}async update(e){const t="incremental"===this.mode;for(const t of e.removed??[])this.entries.delete(t),this.codeCache.delete(t),this.parseCache.delete(t);for(const n of e.added??[])this.entries.add(n),t&&this.codeCache.set(n,await this.readFile(n));for(const n of e.changed??[])t&&this.codeCache.set(n,await this.readFile(n));const n=this.output,o=await this.shake();this.output=o;const r={};for(const e of Object.keys(o))n[e]!==o[e]&&(r[e]=o[e]);return{changed:r,removed:Object.keys(n).filter((e=>!(e in o)))}}async shake(){const e="incremental"===this.mode,t=e?this.cachedReadFile:this.readFile,n=e?this.parseCache:void 0,o=await C([...this.entries],this.resolve,t,n),{models:r,plans:s}=A(o,n);return ce(r,s)}cachedReadFile=async e=>{const t=this.codeCache.get(e);if(void 0!==t)return t;const n=await this.readFile(e);return this.codeCache.set(e,n),n}}function ze(e,t){for(const[n,o]of Object.entries(t)){const r=e.get(n);if(r&&o!==r.code)try{a(o,n)}catch{t[n]=r.code}}return t}const Oe=(e,n)=>e.startsWith(".")?t.resolve(t.dirname(n),e):null;function Re(n){const o=[];let r;try{r=e.readdirSync(n,{withFileTypes:!0})}catch{return o}for(const e of r){if("node_modules"===e.name||e.name.startsWith("."))continue;const r=t.join(n,e.name);e.isDirectory()?o.push(...Re(r)):e.isFile()&&e.name.endsWith(".svelte")&&o.push(r)}return o}const Ne="shaker_variant";function je(n={}){const o=function(e){if(2!==e.level||!e.monomorphize)return Ie;const t="object"==typeof e.monomorphize?e.monomorphize:{};return{...Ie,enabled:!0,...t}}(n);let r={},s=new Map,a=process.cwd();const i="coarse"===n.dev||"incremental"===n.dev?n.dev:null;let c=null;const u=e=>{const t=e.lastIndexOf("::v"),n=e.slice(0,t),o=e.slice(t+3);return`${n}?${Ne}=${o}`};return{name:"vite-plugin-svelte-shaker",enforce:"pre",apply:(e,t)=>null!=i||"build"===t.command,configResolved(e){a=e.root},async configureServer(o){if(!i)return;const s=(n.include??["."]).map((e=>t.resolve(a,e))),u=s.flatMap(Re);c=new Pe(u,Oe,(t=>e.readFileSync(t,"utf-8")),i),r=await c.init();const l=async(e,n)=>{if(!c||!(e=>e.endsWith(".svelte")&&s.some((n=>e===n||e.startsWith(n+t.sep))))(e))return;const a=await c.update({[n]:[e]});for(const[e,t]of Object.entries(a.changed))r[e]=t;for(const e of a.removed)delete r[e];o.moduleGraph.invalidateAll(),o.ws.send({type:"full-reload"})};o.watcher.on("add",(e=>{l(e,"added")})),o.watcher.on("unlink",(e=>{l(e,"removed")}))},async handleHotUpdate(e){if(!c||!e.file.endsWith(".svelte"))return;const t=await c.update({changed:[e.file]});for(const[e,n]of Object.entries(t.changed))r[e]=n;for(const e of t.removed)delete r[e];const n=new Set(e.modules);for(const o of Object.keys(t.changed)){const t=e.server.moduleGraph.getModulesByFile(o);if(t)for(const o of t)e.server.moduleGraph.invalidateModule(o),n.add(o)}return[...n]},async buildStart(){if(c)return;const i=(n.include??["."]).map((e=>t.resolve(a,e))).flatMap(Re);if(0===i.length)return r={},void(s=new Map);const l=t=>e.readFileSync(t,"utf-8"),f=async(e,n)=>{if(e.startsWith(".")||t.isAbsolute(e))return Oe(e,n);let o;try{o=await this.resolve(e,n)}catch{return null}return!o||o.external?null:o.id.split("?")[0]};if(!o.enabled)return r=await async function(e,t,n){const{models:o,plans:r}=await F(e,t,n);return ze(o,ce(o,r))}(i,f,l),void(s=new Map);const d=await async function(e,t,n,o=Ie,r=(e=>e)){const{models:s,plans:a}=await F(e,t,n),i=Me(s,a,o,e);return{files:ze(s,0===i.bindings.length?ce(s,a):fe(s,a,i.bindings.map((e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps}))),r)),mono:i}}(i,f,l,o,u);r=d.files,s=new Map;for(const e of d.mono.variants.values())s.set(u(e.id),e.code)},resolveId(e,n){if(!e.includes(`${Ne}=`))return null;const[o,r]=e.split("?");if(!o||!o.endsWith(".svelte"))return null;const s=n?t.dirname(n.split("?")[0]):a;return`${o.startsWith(".")?t.resolve(s,o):o}?${r}`},load:e=>e.includes(`${Ne}=`)?s.get(e)??null:null,transform(e,t){if(t.includes(`${Ne}=`))return null;if(t.includes("svelte&type="))return null;const n=t.split("?")[0];if(!n.endsWith(".svelte"))return null;const o=r[n];return null==o||o===e?null:o}}}export{je as shaker};
1
+ import*as e from"node:fs";import*as t from"node:path";import{parse as n,compile as o}from"svelte/compiler";import{walk as r}from"zimmerframe";import s from"magic-string";import{createRequire as a}from"node:module";function i(e,t){return n(e,{modern:!0,filename:t})}function c(e,t,n,o=i){if(!n)return o(t,e);const r=n.get(e);if(r&&r.code===t)return r.ast;const s=o(t,e);return n.set(e,{code:t,ast:s}),s}function u(e,t,n){r(e,t,n)}const l={known:!1};function f(e,t){if(!e)return l;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:l}case"UnaryExpression":{const n=f(e.argument,t);if(!n.known)return l;const o=n.value;switch(e.operator){case"!":return{known:!0,value:!o};case"-":return{known:!0,value:-o};case"+":return{known:!0,value:+o};case"typeof":return{known:!0,value:typeof o};case"void":return{known:!0,value:void 0};default:return l}}case"LogicalExpression":{const n=f(e.left,t);if(!n.known)return l;switch(e.operator){case"&&":return n.value?f(e.right,t):n;case"||":return n.value?n:f(e.right,t);case"??":return null===n.value||void 0===n.value?f(e.right,t):n;default:return l}}case"BinaryExpression":{const n=f(e.left,t),o=f(e.right,t);if(!n.known||!o.known)return l;const r=n.value,s=o.value;switch(e.operator){case"===":return{known:!0,value:r===s};case"!==":return{known:!0,value:r!==s};case"==":return{known:!0,value:r==s};case"!=":return{known:!0,value:r!=s};case"<":return{known:!0,value:r<s};case">":return{known:!0,value:r>s};case"<=":return{known:!0,value:r<=s};case">=":return{known:!0,value:r>=s};case"+":return{known:!0,value:r+s};case"-":return{known:!0,value:r-s};case"*":return{known:!0,value:r*s};case"/":return{known:!0,value:r/s};case"%":return{known:!0,value:r%s};default:return l}}default:return l}}function d(e,t,n){const o=f(e,t);if(o.known)return o;const r=p(e,t,n);return"unknown"===r?l:{known:!0,value:r}}function p(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?v(p(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=p(e.left,t,n),a=()=>p(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,t){return!0===e||!0===t||(!1!==e||!1!==t)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=m(e,o),a=f(t,n);if(s&&a.known)return h(s,a.value,r);const i=m(t,o),c=f(e,n);return i&&c.known?h(i,c.value,r):c.known&&a.known?r?c.value==a.value:c.value===a.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?v(s):s}return"unknown"}default:return"unknown"}var o,r}function m(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function h(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function v(e){return!0!==e&&(!1===e||"unknown")}function w(e,t,n){const{arms:o,elseFrag:r}=function(e){const t=[];let n,o=e;for(;o;){t.push({block:o,test:o.test,consequent:o.consequent});const e=o.alternate,r="Fragment"===e?.type&&1===e.nodes?.length&&"IfBlock"===e.nodes[0]?.type&&!0===e.nodes[0].elseif?e.nodes[0]:void 0;r?o=r:("Fragment"===e?.type&&(n=e),o=void 0)}return n?{arms:t,elseFrag:n}:{arms:t}}(e),s=[e.start,e.end],a=o.map(e=>d(e.test,t,n)),i=e=>e.known&&Boolean(e.value),c=e=>e.known&&!e.value;let u=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&u){const t=o[e].consequent;return{span:s,kept:t,removed:y(s,g(t)),recurse:!1}}c(t)||(u=!1)}const l=a.findIndex(e=>!c(e));if(-1===l)return r?{span:s,kept:r,removed:y(s,g(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:k(o,a,l),recurse:!0};const f=o[l].block;return{span:s,kept:void 0,removed:[[s[0],f.start],...k(o,a,l)],recurse:!1,headerRewrite:{from:f.start,to:f.test.start,text:"{#if "}}}function y(e,t){if(!t)return[e];const n=[];return e[0]<t[0]&&n.push([e[0],t[0]]),t[1]<e[1]&&n.push([t[1],e[1]]),n}function g(e){const t=e?.nodes??[];return 0===t.length?null:[t[0].start,t[t.length-1].end]}function k(e,t,n){const o=[];for(let r=n+1;r<e.length;r++){const n=t[r];if(!n.known||n.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:S(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function S(e,t){const n=e?.nodes??[];return n.length?n[n.length-1].end:t}function b(e,t){return t.some(([t,n])=>e.start>=t&&e.end<=n)}function x(e,t,n){if(0===t.size&&0===n.size)return[];const o=[];return u(e,null,{IfBlock(e,{next:r}){if(e.elseif||b(e,o))return;const s=w(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const I=e=>e.endsWith(".svelte"),M="escapes as value (e.g. <svelte:component this={X}>)";function E(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,O(r,n.get(r.id)??[],t));return o}(e,t),o=new Set;for(const e of n.values())for(const t of e.escapedComponents)o.add(t);for(const e of o){const t=n.get(e);t&&!t.bailReasons.includes(M)&&t.bailReasons.push(M)}let r=C(n,A(n,new Map));for(let e=0;e<10;e++){const e=C(n,A(n,z(n,r)));if($(r,e)){r=e;break}r=e}return{models:n,plans:r}}async function F(e,t,n,o,r){const s=Array.isArray(e)?[...e]:[e],a=[],i=[],u=[...s],l=new Set(u);for(;u.length>0;){const e=u.shift(),s=await n(e);a.push({id:e,code:s});const f=c(e,s,o,r),d=f.instance;if(!d)continue;const p=new Map,m=new Map,h=[];for(const o of U(d)){if("*"===o.imported){m.set(o.local,o.value);continue}if("default"===o.imported&&I(o.value)){const n=await t(o.value,e);n&&(i.push({from:e,local:o.local,to:n,kind:"default-svelte"}),h.push(n));continue}const r=await Q(o.value,o.imported,e,t,n);r&&(i.push({from:e,local:o.local,to:r,kind:"barrel"}),p.set(o.local,r))}const v=[];if(m.size>0)for(const o of T(f)){const r=o.indexOf("."),s=m.get(o.slice(0,r));if(null==s)continue;const a=await Q(s,o.slice(r+1),e,t,n);a&&(i.push({from:e,local:o,to:a,kind:"namespace"}),v.push(a))}const w=j(f,p);for(const e of[...h,...w,...v])l.has(e)||(l.add(e),u.push(e))}return{files:a,edges:i,entries:s}}function A(e,t){const n=new Map,o=e=>{let t=n.get(e);return t||(t={sites:[]},n.set(e,t)),t};for(const n of e.values()){const e=t.get(n.id)??[];for(const t of n.childCalls)e.length>0&&b(t.node,e)||o(t.childId).sites.push(W(t.node))}return n}function C(e,t){const n=new Map;for(const o of e.values())n.set(o.id,V(o,t.get(o.id)));return n}function $(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!P(o.constFold,e.constFold))return!1;if(!D(o.narrow,e.narrow))return!1}return!0}function P(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e)if(!t.has(n)||!Object.is(t.get(n),o))return!1;return!0}function D(e,t){if(e.size!==t.size)return!1;for(const[n,o]of e){const e=t.get(n);if(!e||o.length!==e.length)return!1;for(let t=0;t<o.length;t++)if(!Object.is(o[t],e[t]))return!1}return!0}function z(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=x(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function O(e,t,n){const{id:o,code:r}=e,s=c(o,r,n),a=new Map;for(const e of t)a.set(e.local,e.to);const i=[];u(s.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||i.push(`<svelte:options ${t.name}>`);t()}});let l,f,d=null,p=!1;const m=new Set,h=new Set,v=s.instance;if(v){for(const e of U(v))m.add(e.local),"*"===e.imported&&h.add(e.local);const e=function(e){const t=e.content;for(const e of t?.body??[])if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("CallExpression"===n?.type&&"Identifier"===n.callee?.type&&"$props"===n.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(e){l=e.declaration,f=e.pattern,e.sharesStatement&&i.push("$props() shares a multi-declarator statement"),d=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){p=!0;continue}if("Property"!==t.type)continue;const e=t.key;if("Identifier"!==e?.type||!e.name)continue;const n=t.value,o="AssignmentPattern"===n?.type?n.right:void 0;d.push({name:e.name,property:t,defaultExpr:o})}}}const w=function(e,t){const n=[];return u(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.push({childId:r,node:e}),o()}}),n}(s,a),{shadowedNames:y,debugNames:g}=function(e,t,n){const o=new Set,r=new Set;t&&u(t,null,{_(e,{next:t}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===n||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const t of e.params??[])R(t,o);t()}});return u(e.fragment,null,{EachBlock(e,{next:t}){R(e.context,o),"string"==typeof e.index&&o.add(e.index),t()},SnippetBlock(e,{next:t}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const t of e.parameters??[])R(t,o);t()},AwaitBlock(e,{next:t}){R(e.value,o),R(e.error,o),t()},LetDirective(e,{next:t}){e.name&&o.add(e.name),t()},ConstTag(e,{next:t}){for(const t of e.declaration?.declarations??[])R(t.id,o);t()},DebugTag(e,{next:t}){for(const t of e.identifiers??[])"Identifier"===t.type&&t.name&&r.add(t.name);t()}}),{shadowedNames:o,debugNames:r}}(s,v,l),k=function(e,t,n,o){const r=new Set,s=e=>{if(!e)return;const n=t.get(e);if(n&&r.add(n),o.has(e))for(const[n,o]of t)n.startsWith(`${e}.`)&&r.add(o)};u(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&N(e,t.parent)&&s(e.name),o({parent:e})}}),e.instance&&u(e.instance,{parent:null},{_(e,{state:n,next:r}){"Identifier"===e.type&&e.name&&(t.has(e.name)||o.has(e.name))&&N(e,n.parent)&&!L(n.parent)&&s(e.name),r({parent:e})}});return r}(s,a,m,h);return{id:o,code:r,ast:s,imports:a,props:d,propsDeclaration:l,propsPattern:f,hasRestProp:p,childCalls:w,shadowedNames:y,debugNames:g,escapedComponents:k,bailReasons:i}}function R(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?R(n.argument,t):"Property"===n.type&&R(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])R(n,t);return;case"AssignmentPattern":return void R(e.left,t);case"RestElement":return void R(e.argument,t);default:return}}function N(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand)&&!L(t)))}function L(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function j(e,t){const n=new Set;return 0===t.size||u(e.fragment,null,{Component(e,{next:o}){const r=e.name?t.get(e.name):void 0;r&&n.add(r),o()}}),n}function T(e){const t=new Set;return u(e.fragment,null,{Component(e,{next:n}){"string"==typeof e.name&&e.name.includes(".")&&t.add(e.name),n()}}),t}function W(e){const t=e.attributes??[];let n=-1;for(let e=0;e<t.length;e++)"SpreadAttribute"===t[e].type&&(n=e);const o=new Map;for(let e=0;e<t.length;e++){const r=t[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,B(e,n));continue}if("Attribute"!==r.type||!s)continue;const a=_(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>n}:B(e,n))}for(const r of function(e){const t=e.fragment?.nodes??[],n=[];let o=!1;for(const e of t)if("SnippetBlock"!==e.type){if("Comment"!==e.type){if("Text"===e.type){if(""===(e.data??e.raw??"").trim())continue}o=!0}}else"Identifier"===e.expression?.type&&e.expression.name&&n.push(e.expression.name);o&&n.push("children");return n}(e))o.set(r,B(t.length,n));return{hadSpread:n>=0,explicit:o}}function B(e,t){return{value:void 0,dynamic:!0,afterLastSpread:e>t}}function _(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const t=Array.isArray(e)?e:[e];if(1===t.length){const e=t[0];return"Text"===e.type?{known:!0,value:e.data??e.raw??""}:"ExpressionTag"===e.type&&"Literal"===e.expression?.type?{known:!0,value:e.expression.value}:{known:!1}}let n="";for(const e of t){if("Text"!==e.type)return{known:!1};n+=e.data??e.raw??""}return{known:!0,value:n}}function q(e,t){return e.shadowedNames.has(t)||e.debugNames.has(t)}function V(e,t){const n={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return n.bail=!0,n.reasons.push(...e.bailReasons),n;if(!e.props||0===e.props.length)return n;const o=t?.sites??[];if(0===o.length)return n;for(const t of e.props){if(q(e,t.name))continue;const r=G(t,o);n.valueSets.set(t.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&n.narrow.set(t.name,r.values):n.constFold.set(t.name,r.values[0]))}return n}function G(e,t){const n=[];let o=!1,r=!1;const s=e=>{n.some(t=>Object.is(t,e))||n.push(e)};for(const n of t){const t=n.explicit.get(e.name);if(t?.afterLastSpread){t.dynamic?o=!0:s(t.value);continue}if(n.hadSpread){r=!0;continue}const a=J(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function J(e){return e?"Literal"===e.type?{known:!0,value:e.value}:"Identifier"===e.type&&"undefined"===e.name?{known:!0,value:void 0}:{known:!1}:{known:!0,value:void 0}}function*U(e){const t=e.content;for(const e of t?.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t)for(const n of e.specifiers??[]){const e=n.local?.name;e&&("ImportDefaultSpecifier"===n.type?yield{value:t,local:e,imported:"default"}:"ImportNamespaceSpecifier"===n.type?yield{value:t,local:e,imported:"*"}:"ImportSpecifier"===n.type&&(yield{value:t,local:e,imported:H(n)??e}))}}}function H(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"Literal"===t?.type&&"string"==typeof t.value?t.value:void 0}function X(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const K=8;async function Q(e,t,n,o,r,s=0){if(s>K)return null;const a=await o(e,n);if(!a)return null;if(I(e)||I(a))return"default"===t||"*"===t?a:null;let c;try{c=await r(a)}catch{return null}const u=function(e,t){try{const n=i(`<script module lang="ts">\n${e}\n<\/script>`,t);return n.module?.content?.body??null}catch{return null}}(c,a);if(!u)return null;for(const e of u)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(X(n.exported)===t)return Q(String(e.source.value),X(n.local)??"default",a,o,r,s+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const n=await Q(String(e.source.value),t,a,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(X(n.exported)!==t)continue;const e=X(n.local);if(!e)continue;const i=Y(u,e);return i?Q(i.value,i.imported,a,o,r,s+1):null}return null}function Y(e,t){for(const n of e){if("ImportDeclaration"!==n.type)continue;const e=n.source?.value;if("string"==typeof e)for(const o of n.specifiers??[])if(o.local?.name===t){if("ImportDefaultSpecifier"===o.type)return{value:e,imported:"default"};if("ImportNamespaceSpecifier"===o.type)return{value:e,imported:"*"};if("ImportSpecifier"===o.type)return{value:e,imported:H(o)??t}}}return null}const Z=Symbol("unbounded-class-source");function ee(e,t,n){if(!0===e)return Z;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=te(e,t,n);if(o===Z)return Z;const s=[];for(const e of r)for(const t of o)if(s.push(e+t),s.length>64)return Z;r=s}const s=new Set;for(const e of r)for(const t of e.split(/\s+/))t&&s.add(t);return s}function te(e,t,n){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,t,n){if(!e)return Z;if("Identifier"===e.type&&e.name&&n.has(e.name)){const t=new Set;for(const o of n.get(e.name))t.add(ne(o));return t}const o=f(e,t);return o.known?new Set([ne(o.value)]):Z}(e.expression,t,n):Z}function ne(e){return String(e)}function oe(e,t,n){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,s=t.narrow;return u(e.ast.fragment,null,{_(e,{next:t}){if("RegularElement"===(a=e.type)||"SvelteElement"===a||"Component"===a||"SvelteComponent"===a||"SvelteSelf"===a){var a;for(const t of e.attributes??[]){if("SpreadAttribute"===t.type){o=!0;continue}if("ClassDirective"===t.type){t.name&&n.add(t.name);continue}if("Attribute"!==t.type||"class"!==t.name)continue;const e=ee(t.value,r,s);if(e===Z)o=!0;else for(const t of e)n.add(t)}t()}else t()}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let s=0;for(const t of o.children)"Rule"===t.type&&re(t,r.classes)&&(se(e.code,t,o.children,n),s+=1);return s}function re(e,t){if(function(e){let t=!1;return u(e,null,{_(e,{next:n}){"PseudoClassSelector"===e.type&&"global"===e.name&&(t=!0),n()}}),t}(e))return!1;const n=e.prelude?.children??[];return 0!==n.length&&n.every(e=>function(e,t){let n=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!t.has(e.name)&&(n=!0);return n}(e,t))}function se(e,t,n,o){const r=n.indexOf(t),s=n[r-1];let a=t.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,t.end)}function ae(e,t){return ce(e,ie(e,t))}function ie(e,t){const n=new Map,o=new Map,r=new Map;for(const a of e.values()){const e=new s(a.code);n.set(a.id,e);const i=t.get(a.id);if(i.bail){o.set(a.id,new Set);continue}const c=pe(a,i,e);o.set(a.id,c.dropped),r.set(a.id,c.dead)}for(const t of e.values())ge(t,o,n.get(t.id),r.get(t.id)??[]);return n}function ce(e,t){const n={};for(const o of e.values())n[o.id]=t.get(o.id).toString();return n}function ue(e,t,n,o){const r=ie(e,t);return function(e,t,n,o){const r=new Map;for(const e of t){const t=r.get(e.owner);t?t.push(e):r.set(e.owner,[e])}for(const[t,s]of r){const r=e.get(t),a=o.get(t);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const t=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${t}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:n(e.variantId)})),le(r.code,e.node,o,e.foldedProps,a)}c.length>0&&de(r,c,a)}}(e,n,o,r),ce(e,r)}function le(e,t,n,o,r){const s=t.name;if(!s)return;const a=t.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,n);const i=`</${s}`,c=e.lastIndexOf(i,t.end);if(c>=t.start){const e=c+2;r.overwrite(e,e+s.length,n)}for(const n of t.attributes??[])"Attribute"===n.type&&n.name&&o.has(n.name)&&fe(e,n,r)}function fe(e,t,n){let o=t.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),n.remove(o,t.end)}function de(e,t,n){const o=t.map(e=>` import ${e.local} from ${JSON.stringify(e.spec)};`).join("\n"),r=e.ast.instance,s=r?.content?.body??[];if(r&&s.length>0){const e=s[s.length-1];return void n.appendLeft(e.end,`\n${o}`)}r&&r.content?n.appendLeft(r.content.start,`\n${o}\n`):n.prepend(`<script>\n${o}\n<\/script>\n`)}function pe(e,t,n){const o=[];return{dropped:me(e,t.constFold,t.narrow,t,n,o),dead:o}}function me(e,t,n,o,r,s){if(0===t.size&&0===n.size)return new Set;const a=e.code,i=[];!function(e,t,n,o,r,s){u(e,null,{IfBlock(e,{next:a}){if(e.elseif||b(e,s))return;const i=w(e,t,n);if(function(e,t,n,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,t,n){const o=e?.nodes??[];return 0===o.length?"":he(o[0].start,o[o.length-1].end,o,t,n)}(e.kept,t,n));for(const[t,n]of e.removed)o.remove(t,n);if(e.headerRewrite){const{from:t,to:n,text:r}=e.headerRewrite;o.overwrite(t,n,r)}}(i,t,o,r),i.kept)s.push(i.span);else for(const e of i.removed)s.push(e);i.recurse&&a()}})}(e.ast.fragment,t,n,a,r,i),function(e,t,n,o,r){if(0===t.size)return;u(e,null,{ConditionalExpression(e,{next:s}){if(b(e,r))return;const a=f(e.test,t);if(!a.known)return void s();const i=a.value?e.consequent:e.alternate;i?(o.overwrite(e.start,e.end,he(i.start,i.end,[i],t,n)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,t,a,r,i);const c=function(e,t,n){const o=new Map,r=r=>{r&&ve(r,t,e.code,(t,r,s)=>{b(s,n)||s===e.propsPattern||(o.get(t)??function(e,t){const n=[];return e.set(t,n),n}(o,t)).push(r)})};return r(e.ast.instance),r(e.ast.fragment),o}(e,t,i);for(const[e,n]of t){const t=be(n);for(const n of c.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const l=new Set(t.keys());!function(e,t,n){if(!e.props||0===t.size)return;const o=e.props.filter(e=>!t.has(e.name));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,t,n){let o=t.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=t.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,t.start),a=e.slice(t.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?n.remove(o,r<e.length?r+1:r):n.remove(t.start,";"===e[t.end]?t.end+1:t.end)}(e.code,e.propsDeclaration,n);const r=e.propsPattern?.properties??[],s=new Set(e.props.filter(e=>t.has(e.name)).map(e=>e.property));let a=0;for(;a<r.length;){if(!s.has(r[a])){a++;continue}let t=a;for(;t+1<r.length&&s.has(r[t+1]);)t++;we(e.code,r,a,t,n),a=t+1}if(e.propsPattern)for(const o of e.props)t.has(o.name)&&ye(e.propsPattern,o.name,n)}(e,l,r);return oe(e,{...o,constFold:t,narrow:n},r),s&&s.push(...i),l}function he(e,t,n,o,r){if(0===o.size)return r.slice(e,t);const s=[];for(const e of n)ve(e,o,r,(e,t)=>s.push({...t,name:e}));if(0===s.length)return r.slice(e,t);s.sort((e,t)=>e.start-t.start);let a="",i=e;for(const e of s)a+=r.slice(i,e.start),a+=e.head+be(o.get(e.name))+e.tail,i=e.end;return a+=r.slice(i,t),a}function ve(e,t,n,o){u(e,{parent:null,grandparent:null},{_(e,{state:r,next:s}){if("StyleDirective"===e.type&&!0===e.value&&e.name&&t.has(e.name)){let t=e.end;for(;t>e.start&&Se(n[t-1]);)t-=1;const r=n.slice(e.start,t);o(e.name,{start:e.start,end:t,head:`${r}={`,tail:"}"},e)}else"Identifier"===e.type&&e.name&&t.has(e.name)&&!function(e,t){return!!t&&("MemberExpression"===t.type&&t.property===e&&!t.computed||("Property"===t.type&&t.key===e&&!t.computed&&!0!==t.shorthand||(!("TSPropertySignature"!==t.type&&"TSMethodSignature"!==t.type||t.key!==e||t.computed)||("ImportSpecifier"===t.type||"ImportDefaultSpecifier"===t.type||"ImportNamespaceSpecifier"===t.type||"ExportSpecifier"===t.type))))}(e,r.parent)&&o(e.name,function(e,t,n,o){if("ClassDirective"===t?.type&&t.expression===e&&":"===o[e.start-1]){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}={`,tail:"}"}}if("ExpressionTag"===t?.type&&"Attribute"===n?.type&&n.name&&"{"===o[n.start])return{start:n.start,end:n.end,head:`${n.name}={`,tail:"}"};if("Property"===t?.type&&!0===t.shorthand&&t.value===e){const t=o.slice(e.start,e.end);return{start:e.start,end:e.end,head:`${t}: `,tail:""}}return{start:e.start,end:e.end,head:"",tail:""}}(e,r.parent,r.grandparent,n),e);s({parent:e,grandparent:r.parent})}})}function we(e,t,n,o,r){const s=t[n],a=t[o],i=t[o+1];if(i)return void r.remove(s.start,i.start);let c=a.end,u=c;for(;u<e.length&&/\s/.test(e[u]);)u++;","===e[u]&&(c=u+1);const l=t[n-1];r.remove(l?l.end:s.start,c)}function ye(e,t,n){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?n.remove(s.start,a.start):i?n.remove(i.end,s.end):n.remove(s.start,s.end)}function ge(e,t,n,o){u(e.ast.fragment,null,{Component(r,{next:s}){if(o.length>0&&b(r,o))return;const a=r.name?e.imports.get(r.name):void 0,i=a?t.get(a):void 0;if(i&&i.size>0)for(const t of r.attributes??[])"Attribute"===t.type&&t.name&&i.has(t.name)&&ke(t.value)&&fe(e.code,t,n);s()}})}function ke(e){if(!0===e||null==e)return!0;return(Array.isArray(e)?e:[e]).every(e=>"Text"===e.type||"ExpressionTag"===e.type&&"Literal"===e.expression?.type)}function Se(e){return" "===e||"\t"===e||"\n"===e||"\r"===e}function be(e){return void 0===e?"undefined":JSON.stringify(e)}const xe={enabled:!1,maxVariants:8,minSavings:0};function Ie(e,t,n=xe,r=[]){const s=new Map,a=[];if(!n.enabled)return{variants:s,bindings:a};const i=z(e,t),c=new Map,u=new Set;for(const n of e.values()){const o=i.get(n.id)??[];for(const r of n.childCalls){if(o.length>0&&b(r.node,o))continue;const s=e.get(r.childId),a=t.get(r.childId);if(!s||!a)continue;if(a.bail||!s.props||0===s.props.length){u.add(r.childId);continue}const i=Fe(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const l=Ae(s,a,i);if(l===$e(s,a)){u.add(r.childId);continue}const f=c.get(r.childId);f?f.push({owner:n.id,node:r.node,shape:i,code:l}):c.set(r.childId,[{owner:n.id,node:r.node,shape:i,code:l}])}}const l=function(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);n.set(o.id,e.bail?o.code:$e(o,e))}return n}(e,t),f=new Map;for(const t of e.values())f.set(t.id,Ee(l.get(t.id),t));const d=new Set;for(const e of f.values())for(const t of e)d.add(t);const p=(Array.isArray(r)?r:[r]).filter(t=>e.has(t)).filter(e=>!d.has(e)),m=new Map,h=(e,t)=>{const n=m.get(t);if(void 0!==n)return n;let r;try{const{js:n}=o(t,{generate:"client",dev:!1,filename:e});r=n.code.length}catch{r=null}return null!==r&&m.set(t,r),r},v=new Set;for(const e of c.keys())u.has(e)||v.add(e);for(const[t,o]of c){if(u.has(t))continue;if(o.some(e=>e.owner!==t&&v.has(e.owner)))continue;const r=new Map,i=[];let c=!1;for(const e of o){if(r.has(e.code))continue;if(i.length>=n.maxVariants){c=!0;break}const o=`${t}::v${i.length}`;r.set(e.code,o),i.push({id:o,code:e.code})}if(!c&&Me(t,i,e,l,f,p,h,n.minSavings)){for(const e of i){const n=o.find(t=>t.code===e.code);s.set(e.id,{id:e.id,childId:t,code:e.code,foldedProps:n.shape})}for(const e of o)a.push({owner:e.owner,childId:t,node:e.node,variantId:r.get(e.code),foldedProps:e.shape})}}return{variants:s,bindings:a}}function Me(e,t,n,o,r,s,a,i){const c=n.get(e),u=new Map;for(const e of t)u.set(e.id,Ee(e.code,c));const l=new Set,f=[...s];for(;f.length>0;){const e=f.pop();if(!l.has(e)){l.add(e);for(const t of r.get(e)??[])f.push(t)}}let d=0;for(const e of l){const t=a(e,o.get(e));if(null===t)return!1;d+=t}const p=t.map(e=>e.id),m=new Set,h=new Set,v=t=>t===e?{comps:[],vars:p}:{comps:[t],vars:[]},w=[],y=[];for(const e of s){const t=v(e);w.push(...t.comps),y.push(...t.vars)}for(;w.length>0||y.length>0;){if(w.length>0){const e=w.pop();if(m.has(e))continue;m.add(e);for(const t of r.get(e)??[]){const e=v(t);w.push(...e.comps),y.push(...e.vars)}continue}const e=y.pop();if(!h.has(e)){h.add(e);for(const t of u.get(e)??[]){const e=v(t);w.push(...e.comps),y.push(...e.vars)}}}let g=0;for(const e of m){const t=a(e,o.get(e));if(null===t)return!1;g+=t}for(const e of h){const n=t.find(t=>t.id===e).code,o=a(e,n);if(null===o)return!1;g+=o}return g<d*(1-i)}function Ee(e,t){let n;try{n=i(e,t.id)}catch{return[]}const o=[];return u(n.fragment,null,{Component(e,{next:n}){const r=e.name?t.imports.get(e.name):void 0;r&&o.push(r),n()}}),o}function Fe(e,t,n){const o=W(e),r=new Map;for(const e of t.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(n.constFold.has(e)||q(t,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Ae(e,t,n){const o=new Map(t.constFold);for(const[e,t]of n)o.set(e,t);const r=new Map;for(const[e,n]of t.narrow)o.has(e)||r.set(e,n);const a=new s(e.code);return me(e,o,r,t,a),a.toString()}const Ce=new WeakMap;function $e(e,t){const n=Ce.get(e);if(void 0!==n)return n;const o=new s(e.code);me(e,t.constFold,t.narrow,t,o);const r=o.toString();return Ce.set(e,r),r}class Pe{entries=new Set;resolve;readFile;mode;parse;parseCache=new Map;codeCache=new Map;output={};constructor(e,t,n,o="incremental",r){for(const t of Array.isArray(e)?e:[e])this.entries.add(t);this.resolve=t,this.readFile=n,this.mode=o,this.parse=r}async init(){return this.output=await this.shake(),this.output}get(e){return this.output[e]}snapshot(){return{...this.output}}async update(e){const t="incremental"===this.mode;for(const t of e.removed??[])this.entries.delete(t),this.codeCache.delete(t),this.parseCache.delete(t);for(const n of e.added??[])this.entries.add(n),t&&this.codeCache.set(n,await this.readFile(n));for(const n of e.changed??[])t&&this.codeCache.set(n,await this.readFile(n));const n=this.output,o=await this.shake();this.output=o;const r={};for(const e of Object.keys(o))n[e]!==o[e]&&(r[e]=o[e]);return{changed:r,removed:Object.keys(n).filter(e=>!(e in o))}}async shake(){const e="incremental"===this.mode,t=e?this.cachedReadFile:this.readFile,n=e?this.parseCache:this.parse?new Map:void 0,o=await F([...this.entries],this.resolve,t,n,this.parse),{models:r,plans:s}=E(o,n);return ae(r,s)}cachedReadFile=async e=>{const t=this.codeCache.get(e);if(void 0!==t)return t;const n=await this.readFile(e);return this.codeCache.set(e,n),n}}async function De(e,t,n,o){if(!o)return async function(e,t,n){return E(await F(e,t,n))}(e,t,n);const r=new Map;return E(await F(e,t,n,r,o),r)}function ze(e,t){for(const[n,o]of Object.entries(t)){const r=e.get(n);if(r&&o!==r.code)try{i(o,n)}catch{t[n]=r.code}}return t}const Oe=(e,n)=>e.startsWith(".")?t.resolve(t.dirname(n),e):null;function Re(n){const o=[];let r;try{r=e.readdirSync(n,{withFileTypes:!0})}catch{return o}for(const e of r){if("node_modules"===e.name||e.name.startsWith("."))continue;const r=t.join(n,e.name);e.isDirectory()?o.push(...Re(r)):e.isFile()&&e.name.endsWith(".svelte")&&o.push(r)}return o}const Ne=a(import.meta.url);const Le="shaker_variant";function je(n={}){const o=function(e){if(2!==e.level||!e.monomorphize)return xe;const t="object"==typeof e.monomorphize?e.monomorphize:{};return{...xe,enabled:!0,...t}}(n);let r={},s=new Map,a=process.cwd();const i="coarse"===n.dev||"incremental"===n.dev?n.dev:null;let c,u=null,l=!1;const f=()=>{if(l)return c;if(l=!0,"rsvelte"===n.parser&&(c=function(){let e;try{e=Ne("@rsvelte/vite-plugin-svelte-native")}catch{return null}const t=e.parse;return"function"!=typeof t?null:e=>JSON.parse(t(e,{skipExpressionLoc:!0}))}()??void 0,!c))throw new Error('[vite-plugin-svelte-shaker] parser: "rsvelte" was requested but the optional peer `@rsvelte/vite-plugin-svelte-native` could not be loaded (not installed, or no prebuilt binary for this platform). Install it on every build platform, or remove `parser: "rsvelte"` to use svelte/compiler.');return c},d=e=>{const t=e.lastIndexOf("::v"),n=e.slice(0,t),o=e.slice(t+3);return`${n}?${Le}=${o}`};return{name:"vite-plugin-svelte-shaker",enforce:"pre",apply:(e,t)=>null!=i||"build"===t.command,configResolved(e){a=e.root},async configureServer(o){if(!i)return;const s=(n.include??["."]).map(e=>t.resolve(a,e)),c=s.flatMap(Re);u=new Pe(c,Oe,t=>e.readFileSync(t,"utf-8"),i,f()),r=await u.init();const l=async(e,n)=>{if(!u||!(e=>e.endsWith(".svelte")&&s.some(n=>e===n||e.startsWith(n+t.sep)))(e))return;const a=await u.update({[n]:[e]});for(const[e,t]of Object.entries(a.changed))r[e]=t;for(const e of a.removed)delete r[e];o.moduleGraph.invalidateAll(),o.ws.send({type:"full-reload"})};o.watcher.on("add",e=>{l(e,"added")}),o.watcher.on("unlink",e=>{l(e,"removed")})},async handleHotUpdate(e){if(!u||!e.file.endsWith(".svelte"))return;const t=await u.update({changed:[e.file]});for(const[e,n]of Object.entries(t.changed))r[e]=n;for(const e of t.removed)delete r[e];const n=new Set(e.modules);for(const o of Object.keys(t.changed)){const t=e.server.moduleGraph.getModulesByFile(o);if(t)for(const o of t)e.server.moduleGraph.invalidateModule(o),n.add(o)}return[...n]},async buildStart(){if(u)return;const i=(n.include??["."]).map(e=>t.resolve(a,e)).flatMap(Re);if(0===i.length)return r={},void(s=new Map);const c=t=>e.readFileSync(t,"utf-8"),l=async(e,n)=>{if(e.startsWith(".")||t.isAbsolute(e))return Oe(e,n);let o;try{o=await this.resolve(e,n)}catch{return null}return!o||o.external?null:o.id.split("?")[0]};if(!o.enabled)return r=await async function(e,t,n,o){const{models:r,plans:s}=await De(e,t,n,o);return ze(r,ae(r,s))}(i,l,c,f()),void(s=new Map);const p=await async function(e,t,n,o=xe,r=e=>e,s){const{models:a,plans:i}=await De(e,t,n,s),c=Ie(a,i,o,e);return{files:ze(a,0===c.bindings.length?ae(a,i):ue(a,i,c.bindings.map(e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps})),r)),mono:c}}(i,l,c,o,d,f());r=p.files,s=new Map;for(const e of p.mono.variants.values())s.set(d(e.id),e.code)},resolveId(e,n){if(!e.includes(`${Le}=`))return null;const[o,r]=e.split("?");if(!o||!o.endsWith(".svelte"))return null;const s=n?t.dirname(n.split("?")[0]):a;return`${o.startsWith(".")?t.resolve(s,o):o}?${r}`},load:e=>e.includes(`${Le}=`)?s.get(e)??null:null,transform(e,t){if(t.includes(`${Le}=`))return null;if(t.includes("svelte&type="))return null;const n=t.split("?")[0];if(!n.endsWith(".svelte"))return null;const o=r[n];return null==o||o===e?null:o}}}export{je as shaker};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-shaker",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Tree shaking for Svelte components",
5
5
  "keywords": [
6
6
  "dead-code-elimination",
@@ -49,8 +49,8 @@
49
49
  "access": "public"
50
50
  },
51
51
  "dependencies": {
52
- "magic-string": "0.30.10",
53
- "zimmerframe": "1.1.2"
52
+ "magic-string": "0.30.21",
53
+ "zimmerframe": "1.1.4"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@rollup/plugin-commonjs": "25.0.8",
@@ -58,6 +58,7 @@
58
58
  "@rollup/plugin-terser": "0.4.4",
59
59
  "@rollup/plugin-typescript": "11.1.6",
60
60
  "@rsvelte/compiler": "0.6.1",
61
+ "@rsvelte/vite-plugin-svelte-native": "0.2.4",
61
62
  "@sveltejs/vite-plugin-svelte": "^4",
62
63
  "rollup": "4.18.0",
63
64
  "svelte": "^5",
@@ -65,8 +66,14 @@
65
66
  "vite": "^5"
66
67
  },
67
68
  "peerDependencies": {
69
+ "@rsvelte/vite-plugin-svelte-native": ">=0.2.4",
68
70
  "svelte": "^5"
69
71
  },
72
+ "peerDependenciesMeta": {
73
+ "@rsvelte/vite-plugin-svelte-native": {
74
+ "optional": true
75
+ }
76
+ },
70
77
  "engines": {
71
78
  "node": ">=22"
72
79
  },