svelte-shaker 0.1.1 → 0.2.1
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 +1 -1
- package/dist/index.js +1 -1
- package/dist/types/src/analyze.d.ts +25 -2
- package/dist/types/src/engine.d.ts +70 -0
- package/dist/types/src/index.d.ts +3 -2
- package/dist/types/src/ir.d.ts +37 -0
- package/dist/types/src/parse.d.ts +13 -0
- package/dist/types/src/vite.d.ts +18 -3
- package/dist/vite.js +1 -1
- package/package.json +3 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("svelte/compiler"),n=require("zimmerframe"),t=require("magic-string");function o(n,t){return e.parse(n,{modern:!0,filename:t})}function r(e,t,o){n.walk(e,t,o)}const s={known:!1};function a(e,n){if(!e)return s;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const t=e.name??"";return"undefined"===t?{known:!0,value:void 0}:n.has(t)?{known:!0,value:n.get(t)}:s}case"UnaryExpression":{const t=a(e.argument,n);if(!t.known)return s;const o=t.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 s}}case"LogicalExpression":{const t=a(e.left,n);if(!t.known)return s;switch(e.operator){case"&&":return t.value?a(e.right,n):t;case"||":return t.value?t:a(e.right,n);case"??":return null===t.value||void 0===t.value?a(e.right,n):t;default:return s}}case"BinaryExpression":{const t=a(e.left,n),o=a(e.right,n);if(!t.known||!o.known)return s;const r=t.value,i=o.value;switch(e.operator){case"===":return{known:!0,value:r===i};case"!==":return{known:!0,value:r!==i};case"==":return{known:!0,value:r==i};case"!=":return{known:!0,value:r!=i};case"<":return{known:!0,value:r<i};case">":return{known:!0,value:r>i};case"<=":return{known:!0,value:r<=i};case">=":return{known:!0,value:r>=i};case"+":return{known:!0,value:r+i};case"-":return{known:!0,value:r-i};case"*":return{known:!0,value:r*i};case"/":return{known:!0,value:r/i};case"%":return{known:!0,value:r%i};default:return s}}default:return s}}function i(e,n,t){const o=a(e,n);if(o.known)return o;const r=c(e,n,t);return"unknown"===r?s:{known:!0,value:r}}function c(e,n,t){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?l(c(e.argument,n,t)):"unknown";case"LogicalExpression":{const s=c(e.left,n,t),a=()=>c(e.right,n,t);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,n){return!0===e||!0===n||(!1!==e||!1!==n)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,n,t,o,r){const s=u(e,o),i=a(n,t);if(s&&i.known)return f(s,i.value,r);const c=u(n,o),l=a(e,t);return c&&l.known?f(c,l.value,r):l.known&&i.known?r?l.value==i.value:l.value===i.value:"unknown"}(e.left,e.right,n,t,r);return"!=="===o||"!="===o?l(s):s}return"unknown"}default:return"unknown"}var o,r}function u(e,n){return"Identifier"===e?.type&&e.name&&n.has(e.name)?n.get(e.name):null}function f(e,n,t){const o=t?e=>e==n:e=>e===n;return!!e.some(o)&&(!!e.every(o)||"unknown")}function l(e){return!0!==e&&(!1===e||"unknown")}function p(e,n,t){const{arms:o,elseFrag:r}=function(e){const n=[];let t,o=e;for(;o;){n.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&&(t=e),o=void 0)}return t?{arms:n,elseFrag:t}:{arms:n}}(e),s=[e.start,e.end],a=o.map((e=>i(e.test,n,t))),c=e=>e.known&&Boolean(e.value),u=e=>e.known&&!e.value;let f=!0;for(let e=0;e<o.length;e++){const n=a[e];if(c(n)&&f){const n=o[e].consequent;return{span:s,kept:n,removed:d(s,m(n)),recurse:!1}}u(n)||(f=!1)}const l=a.findIndex((e=>!u(e)));if(-1===l)return r?{span:s,kept:r,removed:d(s,m(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 p=o[l].block;return{span:s,kept:void 0,removed:[[s[0],p.start],...v(o,a,l)],recurse:!1,headerRewrite:{from:p.start,to:p.test.start,text:"{#if "}}}function d(e,n){if(!n)return[e];const t=[];return e[0]<n[0]&&t.push([e[0],n[0]]),n[1]<e[1]&&t.push([n[1],e[1]]),t}function m(e){const n=e?.nodes??[];return 0===n.length?null:[n[0].start,n[n.length-1].end]}function v(e,n,t){const o=[];for(let r=t+1;r<e.length;r++){const t=n[r];if(!t.known||t.value)continue;const s=e[r],a=e[r+1]?.block,i=a?a.start:h(s.consequent,s.block.end);o.push([s.block.start,i])}return o}function h(e,n){const t=e?.nodes??[];return t.length?t[t.length-1].end:n}function w(e,n){return n.some((([n,t])=>e.start>=n&&e.end<=t))}function g(e,n,t){if(0===n.size&&0===t.size)return[];const o=[];return r(e,null,{IfBlock(e,{next:r}){if(e.elseif||w(e,o))return;const s=p(e,n,t);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const y=e=>e.endsWith(".svelte"),k=10,x="escapes as value (e.g. <svelte:component this={X}>)",S="rendered through a barrel/named import (call sites unobservable)";async function b(e,n,t){const o=await async function(e,n,t){const o=new Map,r=Array.isArray(e)?[...e]:[e],s=new Set(r);for(;r.length>0;){const e=r.shift(),a=await t(e),i=await D(e,a,n,t);o.set(e,i);for(const e of[...i.imports.values(),...i.barrelChildIds])s.has(e)||(s.add(e),r.push(e))}return o}(e,n,t),r=new Set;for(const e of o.values())for(const n of e.escapedComponents)r.add(n);for(const e of r){const n=o.get(e);n&&!n.bailReasons.includes(x)&&n.bailReasons.push(x)}const s=new Set;for(const e of o.values())for(const n of e.barrelChildIds)s.add(n);for(const e of s){const n=o.get(e);n&&!n.bailReasons.includes(S)&&n.bailReasons.push(S)}let a=M(o,I(o,new Map));for(let e=0;e<k;e++){const e=M(o,I(o,C(o,a)));if(E(a,e)){a=e;break}a=e}return{models:o,plans:a}}function I(e,n){const t=new Map,o=e=>{let n=t.get(e);return n||(n={sites:[]},t.set(e,n)),n};for(const t of e.values()){const e=n.get(t.id)??[];for(const n of t.childCalls)e.length>0&&w(n.node,e)||o(n.childId).sites.push(R(n.node))}return t}function M(e,n){const t=new Map;for(const o of e.values())t.set(o.id,T(o,n.get(o.id)));return t}function E(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e){const e=n.get(t);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!A(o.constFold,e.constFold))return!1;if(!P(o.narrow,e.narrow))return!1}return!0}function A(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e)if(!n.has(t)||!Object.is(n.get(t),o))return!1;return!0}function P(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e){const e=n.get(t);if(!e||o.length!==e.length)return!1;for(let n=0;n<o.length;n++)if(!Object.is(o[n],e[n]))return!1}return!0}function C(e,n){const t=new Map;for(const o of e.values()){const e=n.get(o.id);if(e.bail)continue;const r=g(o.ast.fragment,e.constFold,e.narrow);r.length>0&&t.set(o.id,r)}return t}async function D(e,n,t,s){const a=o(n,e),i=new Map,c=new Map,u=[];r(a.fragment,null,{SvelteOptions(e,{next:n}){for(const n of e.attributes??[])"Attribute"!==n.type||"accessors"!==n.name&&"customElement"!==n.name||u.push(`<svelte:options ${n.name}>`);n()}});let f,l,p=null,d=!1;const m=new Set,v=a.instance;if(v){for(const n of function*(e){const n=e.content;for(const e of n?.body??[]){if("ImportDeclaration"!==e.type)continue;const n=e.source?.value;if("string"==typeof n)for(const t of e.specifiers??[]){const e=t.local?.name;e&&("ImportDefaultSpecifier"===t.type?yield{value:n,local:e,imported:"default"}:"ImportNamespaceSpecifier"===t.type?yield{value:n,local:e,imported:"*"}:"ImportSpecifier"===t.type&&(yield{value:n,local:e,imported:q(t)??e}))}}}(v)){if(m.add(n.local),"default"===n.imported&&y(n.value)){const o=await t(n.value,e);o&&i.set(n.local,o);continue}const o=await W(n.value,n.imported,e,t,s);o&&c.set(n.local,o)}const n=function(e){const n=e.content;for(const e of n?.body??[])if("VariableDeclaration"===e.type)for(const n of e.declarations??[]){const t=n.init,o=n.id;if("CallExpression"===t?.type&&"Identifier"===t.callee?.type&&"$props"===t.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(n){f=n.declaration,l=n.pattern,n.sharesStatement&&u.push("$props() shares a multi-declarator statement"),p=[];for(const e of n.pattern.properties??[]){if("RestElement"===e.type){d=!0;continue}if("Property"!==e.type)continue;const n=e.key;if("Identifier"!==n?.type||!n.name)continue;const t=e.value,o="AssignmentPattern"===t?.type?t.right:void 0;p.push({name:n.name,property:e,defaultExpr:o})}}}const h=function(e,n){const t=[];return r(e.fragment,null,{Component(e,{next:o}){const r=e.name?n.get(e.name):void 0;r&&t.push({childId:r,node:e}),o()}}),t}(a,i),w=function(e,n){const t=new Set;return 0===n.size||r(e.fragment,null,{Component(e,{next:o}){const r=e.name?n.get(e.name):void 0;r&&t.add(r),o()}}),t}(a,c),{shadowedNames:g,debugNames:k}=function(e,n,t){const o=new Set,s=new Set;n&&r(n,null,{_(e,{next:n}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===t||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])F(n,o);n()}});return r(e.fragment,null,{EachBlock(e,{next:n}){F(e.context,o),"string"==typeof e.index&&o.add(e.index),n()},SnippetBlock(e,{next:n}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const n of e.parameters??[])F(n,o);n()},AwaitBlock(e,{next:n}){F(e.value,o),F(e.error,o),n()},LetDirective(e,{next:n}){e.name&&o.add(e.name),n()},ConstTag(e,{next:n}){for(const n of e.declaration?.declarations??[])F(n.id,o);n()},DebugTag(e,{next:n}){for(const n of e.identifiers??[])"Identifier"===n.type&&n.name&&s.add(n.name);n()}}),{shadowedNames:o,debugNames:s}}(a,v,f),x=function(e,n,t){const o=new Set,s=e=>{if(!e)return;const t=n.get(e);t&&o.add(t)};r(e.fragment,{parent:null},{_(e,{state:n,next:o}){"Identifier"===e.type&&e.name&&t.has(e.name)&&z(e,n.parent)&&s(e.name),o({parent:e})}}),e.instance&&r(e.instance,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&z(e,t.parent)&&!N(t.parent)&&s(e.name),o({parent:e})}});return o}(a,i,m);return{id:e,code:n,ast:a,imports:i,props:p,propsDeclaration:f,propsPattern:l,hasRestProp:d,childCalls:h,shadowedNames:g,debugNames:k,escapedComponents:x,barrelChildIds:w,bailReasons:u}}function F(e,n){if(e)switch(e.type){case"Identifier":return void(e.name&&n.add(e.name));case"ObjectPattern":for(const t of e.properties??[])"RestElement"===t.type?F(t.argument,n):"Property"===t.type&&F(t.value??t.key,n);return;case"ArrayPattern":for(const t of e.elements??[])F(t,n);return;case"AssignmentPattern":return void F(e.left,n);case"RestElement":return void F(e.argument,n);default:return}}function z(e,n){return!!n&&(!("MemberExpression"===n.type&&n.property===e&&!n.computed)&&(!("Property"===n.type&&n.key===e&&!n.computed&&!0!==n.shorthand)&&!N(n)))}function N(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function R(e){const n=e.attributes??[];let t=-1;for(let e=0;e<n.length;e++)"SpreadAttribute"===n[e].type&&(t=e);const o=new Map;for(let e=0;e<n.length;e++){const r=n[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,L(e,t));continue}if("Attribute"!==r.type||!s)continue;const a=$(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>t}:L(e,t))}for(const r of function(e){const n=e.fragment?.nodes??[],t=[];let o=!1;for(const e of n)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&&t.push(e.expression.name);o&&t.push("children");return t}(e))o.set(r,L(n.length,t));return{hadSpread:t>=0,explicit:o}}function L(e,n){return{value:void 0,dynamic:!0,afterLastSpread:e>n}}function $(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const n=Array.isArray(e)?e:[e];if(1===n.length){const e=n[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 t="";for(const e of n){if("Text"!==e.type)return{known:!1};t+=e.data??e.raw??""}return{known:!0,value:t}}function O(e,n){return e.shadowedNames.has(n)||e.debugNames.has(n)}function T(e,n){const t={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return t.bail=!0,t.reasons.push(...e.bailReasons),t;if(!e.props||0===e.props.length)return t;const o=n?.sites??[];if(0===o.length)return t;for(const n of e.props){if(O(e,n.name))continue;const r=_(n,o);t.valueSets.set(n.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&t.narrow.set(n.name,r.values):t.constFold.set(n.name,r.values[0]))}return t}function _(e,n){const t=[];let o=!1,r=!1;const s=e=>{t.some((n=>Object.is(n,e)))||t.push(e)};for(const t of n){const n=t.explicit.get(e.name);if(n?.afterLastSpread){n.dynamic?o=!0:s(n.value);continue}if(t.hadSpread){r=!0;continue}const a=B(e.defaultExpr);a.known?s(a.value):o=!0}return{values:t,dynamic:o,top:r}}function B(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 q(e){const n=e.imported;return"Identifier"===n?.type&&n.name?n.name:"Literal"===n?.type&&"string"==typeof n.value?n.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 V=8;async function W(e,n,t,r,s,a=0){if(a>V)return null;const i=await r(e,t);if(!i)return null;if(y(e)||y(i))return"default"===n||"*"===n?i:null;let c;try{c=await s(i)}catch{return null}const u=function(e,n){try{const t=o(`<script module>\n${e}\n<\/script>`,n);return t.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 t of e.specifiers??[])if(j(t.exported)===n)return W(String(e.source.value),j(t.local)??"default",i,r,s,a+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const t=await W(String(e.source.value),n,i,r,s,a+1);if(t)return t}}else for(const t of e.specifiers??[]){if(j(t.exported)!==n)continue;const e=j(t.local);if(!e)continue;const o=U(u,e);return o?W(o.value,o.imported,i,r,s,a+1):null}return null}function U(e,n){for(const t of e){if("ImportDeclaration"!==t.type)continue;const e=t.source?.value;if("string"==typeof e)for(const o of t.specifiers??[])if(o.local?.name===n){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:q(o)??n}}}return null}const J=64;const X=Symbol("unbounded-class-source");function G(e,n,t){if(!0===e)return X;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=H(e,n,t);if(o===X)return X;const s=[];for(const e of r)for(const n of o)if(s.push(e+n),s.length>J)return X;r=s}const s=new Set;for(const e of r)for(const n of e.split(/\s+/))n&&s.add(n);return s}function H(e,n,t){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,n,t){if(!e)return X;if("Identifier"===e.type&&e.name&&t.has(e.name)){const n=new Set;for(const o of t.get(e.name))n.add(K(o));return n}const o=a(e,n);return o.known?new Set([K(o.value)]):X}(e.expression,n,t):X}function K(e){return String(e)}function Q(e,n,t){const o=e.ast.css;if(!o||!o.children)return 0;const s=function(e,n){const t=new Set;let o=!1;const s=n.constFold,a=n.narrow;return r(e.ast.fragment,null,{_(e,{next:n}){if("RegularElement"===(r=e.type)||"SvelteElement"===r||"Component"===r||"SvelteComponent"===r||"SvelteSelf"===r){var r;for(const n of e.attributes??[]){if("SpreadAttribute"===n.type){o=!0;continue}if("ClassDirective"===n.type){n.name&&t.add(n.name);continue}if("Attribute"!==n.type||"class"!==n.name)continue;const e=G(n.value,s,a);if(e===X)o=!0;else for(const n of e)t.add(n)}n()}else n()}}),{classes:t,unbounded:o}}(e,n);if(s.unbounded)return 0;let a=0;for(const n of o.children)"Rule"===n.type&&Y(n,s.classes)&&(Z(e.code,n,o.children,t),a+=1);return a}function Y(e,n){if(function(e){let n=!1;return r(e,null,{_(e,{next:t}){"PseudoClassSelector"===e.type&&"global"===e.name&&(n=!0),t()}}),n}(e))return!1;const t=e.prelude?.children??[];return 0!==t.length&&t.every((e=>function(e,n){let t=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!n.has(e.name)&&(t=!0);return t}(e,n)))}function Z(e,n,t,o){const r=t.indexOf(n),s=t[r-1];let a=n.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,n.end)}function ee(e,n){return te(e,ne(e,n))}function ne(e,n){const o=new Map,r=new Map;for(const s of e.values()){const e=new t(s.code);o.set(s.id,e);const a=n.get(s.id);r.set(s.id,a.bail?new Set:ie(s,a,e))}for(const n of e.values())de(n,r,o.get(n.id));return o}function te(e,n){const t={};for(const o of e.values())t[o.id]=n.get(o.id).toString();return t}function oe(e,n,t,o){const r=ne(e,n);return function(e,n,t,o){const r=new Map;for(const e of n){const n=r.get(e.owner);n?n.push(e):r.set(e.owner,[e])}for(const[n,s]of r){const r=e.get(n),a=o.get(n);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const n=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${n}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:t(e.variantId)})),re(r.code,e.node,o,e.foldedProps,a)}c.length>0&&ae(r,c,a)}}(e,t,o,r),te(e,r)}function re(e,n,t,o,r){const s=n.name;if(!s)return;const a=n.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,t);const i=`</${s}`,c=e.lastIndexOf(i,n.end);if(c>=n.start){const e=c+2;r.overwrite(e,e+s.length,t)}for(const t of n.attributes??[])"Attribute"===t.type&&t.name&&o.has(t.name)&&se(e,t,r)}function se(e,n,t){let o=n.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),t.remove(o,n.end)}function ae(e,n,t){const o=n.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];t.appendLeft(e.end,`\n${o}`)}else r&&r.content?t.appendLeft(r.content.start,`\n${o}\n`):t.prepend(`<script>\n${o}\n<\/script>\n`)}function ie(e,n,t){return ce(e,n.constFold,n.narrow,n,t)}function ce(e,n,t,o,s){if(0===n.size&&0===t.size)return new Set;const i=e.code,c=[];!function(e,n,t,o,s,a){r(e,null,{IfBlock(e,{next:r}){if(e.elseif||w(e,a))return;const i=p(e,n,t);if(function(e,n,t,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,n,t){const o=e?.nodes??[];return 0===o.length?"":ue(o[0].start,o[o.length-1].end,o,n,t)}(e.kept,n,t));for(const[n,t]of e.removed)o.remove(n,t);if(e.headerRewrite){const{from:n,to:t,text:r}=e.headerRewrite;o.overwrite(n,t,r)}}(i,n,o,s),i.kept)a.push(i.span);else for(const e of i.removed)a.push(e);i.recurse&&r()}})}(e.ast.fragment,n,t,i,s,c),function(e,n,t,o,s){if(0===n.size)return;r(e,null,{ConditionalExpression(e,{next:r}){if(w(e,s))return;const i=a(e.test,n);if(!i.known)return void r();const c=i.value?e.consequent:e.alternate;c?(o.overwrite(e.start,e.end,ue(c.start,c.end,[c],n,t)),s.push([e.start,e.end])):r()}})}(e.ast.fragment,n,i,s,c);const u=function(e,n,t){const o=new Map,s=s=>{s&&r(s,{parent:null},{_(r,{state:s,next:a}){"Identifier"===r.type&&r.name&&n.has(r.name)&&!w(r,t)&&r!==e.propsPattern&&!fe(r,s.parent)&&(o.get(r.name)??function(e,n){const t=[];return e.set(n,t),t}(o,r.name)).push([r.start,r.end]),a({parent:r})}})};return s(e.ast.instance),s(e.ast.fragment),o}(e,n,c);for(const[e,t]of n)for(const n of u.get(e)??[])s.overwrite(n[0],n[1],ve(t));const f=new Set(n.keys());!function(e,n,t){if(!e.props||0===n.size)return;const o=e.props.filter((e=>!n.has(e.name)));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,n,t){let o=n.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=n.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,n.start),a=e.slice(n.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?t.remove(o,r<e.length?r+1:r):t.remove(n.start,";"===e[n.end]?n.end+1:n.end)}(e.code,e.propsDeclaration,t);const r=e.propsPattern?.properties??[];for(const o of e.props)n.has(o.name)&&(le(r,o.property,t),e.propsPattern&&pe(e.propsPattern,o.name,t))}(e,f,s);return Q(e,{...o,constFold:n,narrow:t},s),f}function ue(e,n,t,o,s){if(0===o.size)return s.slice(e,n);const a=[];for(const e of t)r(e,{parent:null},{_(e,{state:n,next:t}){"Identifier"===e.type&&e.name&&o.has(e.name)&&!fe(e,n.parent)&&a.push({start:e.start,end:e.end,name:e.name}),t({parent:e})}});if(0===a.length)return s.slice(e,n);a.sort(((e,n)=>e.start-n.start));let i="",c=e;for(const e of a)i+=s.slice(c,e.start),i+=ve(o.get(e.name)),c=e.end;return i+=s.slice(c,n),i}function fe(e,n){return!!n&&("MemberExpression"===n.type&&n.property===e&&!n.computed||("Property"===n.type&&n.key===e&&!n.computed&&!0!==n.shorthand||("ImportSpecifier"===n.type||"ImportDefaultSpecifier"===n.type||"ImportNamespaceSpecifier"===n.type||"ExportSpecifier"===n.type)))}function le(e,n,t){const o=e.indexOf(n),r=e[o+1],s=e[o-1];r?t.remove(n.start,r.start):s?t.remove(s.end,n.end):t.remove(n.start,n.end)}function pe(e,n,t){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex((e=>"Identifier"===e.key?.type&&e.key.name===n));if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?t.remove(s.start,a.start):i?t.remove(i.end,s.end):t.remove(s.start,s.end)}function de(e,n,t){r(e.ast.fragment,null,{Component(o,{next:r}){const s=o.name?e.imports.get(o.name):void 0,a=s?n.get(s):void 0;if(a&&a.size>0)for(const n of o.attributes??[])"Attribute"===n.type&&n.name&&a.has(n.name)&&me(n.value)&&se(e.code,n,t);r()}})}function me(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 ve(e){return void 0===e?"undefined":JSON.stringify(e)}const he={enabled:!1,maxVariants:8,minSavings:0};function we(n,t,o=he,r=[]){const s=new Map,a=[];if(!o.enabled)return{variants:s,bindings:a};const i=C(n,t),c=new Map,u=new Set;for(const e of n.values()){const o=i.get(e.id)??[];for(const r of e.childCalls){if(o.length>0&&w(r.node,o))continue;const s=n.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=ke(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const f=xe(s,a,i);if(f===be(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,n){const t=new Map;for(const o of e.values()){const e=n.get(o.id);t.set(o.id,e.bail?o.code:be(o,e))}return t}(n,t),l=new Map;for(const e of n.values())l.set(e.id,ye(f.get(e.id),e));const p=new Set;for(const e of l.values())for(const n of e)p.add(n);const d=(Array.isArray(r)?r:[r]).filter((e=>n.has(e))).filter((e=>!p.has(e))),m=new Map,v=(n,t)=>{const o=m.get(t);if(void 0!==o)return o;let r;try{const{js:o}=e.compile(t,{generate:"client",dev:!1,filename:n});r=o.code.length}catch{r=null}return null!==r&&m.set(t,r),r},h=new Set;for(const e of c.keys())u.has(e)||h.add(e);for(const[e,t]of c){if(u.has(e))continue;if(t.some((n=>n.owner!==e&&h.has(n.owner))))continue;const r=new Map,i=[];let c=!1;for(const n of t){if(r.has(n.code))continue;if(i.length>=o.maxVariants){c=!0;break}const t=`${e}::v${i.length}`;r.set(n.code,t),i.push({id:t,code:n.code})}if(!c&&ge(e,i,n,f,l,d,v,o.minSavings)){for(const n of i){const o=t.find((e=>e.code===n.code));s.set(n.id,{id:n.id,childId:e,code:n.code,foldedProps:o.shape})}for(const n of t)a.push({owner:n.owner,childId:e,node:n.node,variantId:r.get(n.code),foldedProps:n.shape})}}return{variants:s,bindings:a}}function ge(e,n,t,o,r,s,a,i){const c=t.get(e),u=new Map;for(const e of n)u.set(e.id,ye(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 n of r.get(e)??[])l.push(n)}}let p=0;for(const e of f){const n=a(e,o.get(e));if(null===n)return!1;p+=n}const d=n.map((e=>e.id)),m=new Set,v=new Set,h=n=>n===e?{comps:[],vars:d}:{comps:[n],vars:[]},w=[],g=[];for(const e of s){const n=h(e);w.push(...n.comps),g.push(...n.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 n of r.get(e)??[]){const e=h(n);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!v.has(e)){v.add(e);for(const n of u.get(e)??[]){const e=h(n);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const n=a(e,o.get(e));if(null===n)return!1;y+=n}for(const e of v){const t=n.find((n=>n.id===e)).code,o=a(e,t);if(null===o)return!1;y+=o}return y<p*(1-i)}function ye(e,n){let t;try{t=o(e,n.id)}catch{return[]}const s=[];return r(t.fragment,null,{Component(e,{next:t}){const o=e.name?n.imports.get(e.name):void 0;o&&s.push(o),t()}}),s}function ke(e,n,t){const o=R(e),r=new Map;for(const e of n.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(t.constFold.has(e)||O(n,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function xe(e,n,o){const r=new Map(n.constFold);for(const[e,n]of o)r.set(e,n);const s=new Map;for(const[e,t]of n.narrow)r.has(e)||s.set(e,t);const a=new t(e.code);return ce(e,r,s,n,a),a.toString()}const Se=new WeakMap;function be(e,n){const o=Se.get(e);if(void 0!==o)return o;const r=new t(e.code);ce(e,n.constFold,n.narrow,n,r);const s=r.toString();return Se.set(e,s),s}exports.DEFAULT_MONO_OPTIONS=he,exports.analyze=b,exports.monomorphize=we,exports.svelteShaker=async function(e,n,t){const{models:o,plans:r}=await b(e,n,t);return ee(o,r)},exports.svelteShakerWithMono=async function(e,n,t,o=he,r=(e=>e)){const{models:s,plans:a}=await b(e,n,t),i=we(s,a,o,e);return{files:0===i.bindings.length?ee(s,a):oe(s,a,i.bindings.map((e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps}))),r),mono:i}},exports.transformAll=ee,exports.transformAllWithMono=oe;
|
|
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=l(e,o),a=i(t,n);if(s&&a.known)return f(s,a.value,r);const c=l(t,o),u=i(e,n);return c&&u.known?f(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 l(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function f(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 l=!0;for(let e=0;e<o.length;e++){const t=a[e];if(i(t)&&l){const t=o[e].consequent;return{span:s,kept:t,removed:m(s,h(t)),recurse:!1}}u(t)||(l=!1)}const f=a.findIndex((e=>!u(e)));if(-1===f)return r?{span:s,kept:r,removed:m(s,h(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===f)return{span:s,kept:void 0,removed:v(o,a,f),recurse:!0};const d=o[f].block;return{span:s,kept:void 0,removed:[[s[0],d.start],...v(o,a,f)],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}>)",b="rendered through a barrel/named import (call sites unobservable)";async function I(e,t,n){return M(await A(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,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(S)&&t.bailReasons.push(S)}const r=new Set;for(const e of n.values())for(const t of e.barrelChildIds)r.add(t);for(const e of r){const t=n.get(e);t&&!t.bailReasons.includes(b)&&t.bailReasons.push(b)}let s=E(n,C(n,new Map));for(let e=0;e<x;e++){const e=E(n,C(n,z(n,s)));if(F(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function A(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 l=r(e,s,o),f=l.instance;if(!f)continue;const d=new Map,p=[];for(const o of U(f)){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"}),p.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 m=L(l,d);for(const e of[...p,...m])u.has(e)||(u.add(e),c.push(e))}return{files:a,edges:i,entries:s}}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&&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 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(!D(o.constFold,e.constFold))return!1;if(!P(o.narrow,e.narrow))return!1}return!0}function D(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 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=y(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:a}=e,i=r(o,a,n),c=new Map,u=new Map;for(const e of t)"default-svelte"===e.kind?c.set(e.local,e.to):u.set(e.local,e.to);const l=[];s(i.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||l.push(`<svelte:options ${t.name}>`);t()}});let f,d,p=null,m=!1;const h=new Set,v=i.instance;if(v){for(const e of U(v))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,d=e.pattern,e.sharesStatement&&l.push("$props() shares a multi-declarator statement"),p=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){m=!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;p.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),g=L(i,u),{shadowedNames:y,debugNames:k}=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),x=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};s(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&N(e,t.parent)&&r(e.name),o({parent:e})}}),e.instance&&s(e.instance,{parent:null},{_(e,{state:n,next:o}){"Identifier"===e.type&&e.name&&t.has(e.name)&&N(e,n.parent)&&!O(n.parent)&&r(e.name),o({parent:e})}});return o}(i,c,h);return{id:o,code:a,ast:i,imports:c,props:p,propsDeclaration:f,propsPattern:d,hasRestProp:m,childCalls:w,shadowedNames:y,debugNames:k,escapedComponents:x,barrelChildIds:g,bailReasons:l}}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 L(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 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=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*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>\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;for(const s of e.values()){const e=new n(s.code);o.set(s.id,e);const a=t.get(s.id);r.set(s.id,a.bail?new Set:de(s,a,e))}for(const t of e.values())ge(t,r,o.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&&fe(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)&&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 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];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){return pe(e,t.constFold,t.narrow,t,n)}function pe(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;const a=e.code,c=[];!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,a,r,c),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,a,r,c);const u=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,c);for(const[e,n]of t){const t=ke(n);for(const n of u.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??[];for(const o of e.props)t.has(o.name)&&(ve(r,o.property,n),e.propsPattern&&we(e.propsPattern,o.name,n))}(e,l,r);return ne(e,{...o,constFold:t,narrow:n},r),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:"}"};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){const o=e.indexOf(t),r=e[o+1],s=e[o-1];r?n.remove(t.start,r.start):s?n.remove(s.end,t.end):n.remove(t.start,t.end)}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){s(e.ast.fragment,null,{Component(o,{next:r}){const s=o.name?e.imports.get(o.name):void 0,a=s?t.get(s):void 0;if(a&&a.size>0)for(const t of o.attributes??[])"Attribute"===t.type&&t.name&&a.has(t.name)&&ye(t.value)&&le(e.code,t,n);r()}})}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=z(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 l=Ae(s,a,i);if(l===Ee(s,a)){u.add(r.childId);continue}const f=c.get(r.childId);f?f.push({owner:e.id,node:r.node,shape:i,code:l}):c.set(r.childId,[{owner:e.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:Ee(o,e))}return n}(t,n),f=new Map;for(const e of t.values())f.set(e.id,Ie(l.get(e.id),e));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((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,l,f,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 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 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 Ce=new WeakMap;function Ee(e,t){const o=Ce.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 Ce.set(e,s),s}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 A([...this.entries],this.resolve,t,n),{models:r,plans:s}=M(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=I,exports.analyzeInput=M,exports.buildAnalyzeInput=A,exports.monomorphize=Se,exports.svelteShaker=async function(e,t,n){const{models:o,plans:r}=await I(e,t,n);return se(o,r)},exports.svelteShakerWithMono=async function(e,t,n,o=xe,r=(e=>e)){const{models:s,plans:a}=await I(e,t,n),i=Se(s,a,o,e);return{files: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;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parse as e,compile as n}from"svelte/compiler";import{walk as t}from"zimmerframe";import o from"magic-string";function r(n,t){return e(n,{modern:!0,filename:t})}function s(e,n,o){t(e,n,o)}const a={known:!1};function i(e,n){if(!e)return a;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const t=e.name??"";return"undefined"===t?{known:!0,value:void 0}:n.has(t)?{known:!0,value:n.get(t)}:a}case"UnaryExpression":{const t=i(e.argument,n);if(!t.known)return a;const o=t.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 t=i(e.left,n);if(!t.known)return a;switch(e.operator){case"&&":return t.value?i(e.right,n):t;case"||":return t.value?t:i(e.right,n);case"??":return null===t.value||void 0===t.value?i(e.right,n):t;default:return a}}case"BinaryExpression":{const t=i(e.left,n),o=i(e.right,n);if(!t.known||!o.known)return a;const r=t.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,n,t){const o=i(e,n);if(o.known)return o;const r=u(e,n,t);return"unknown"===r?a:{known:!0,value:r}}function u(e,n,t){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?p(u(e.argument,n,t)):"unknown";case"LogicalExpression":{const s=u(e.left,n,t),a=()=>u(e.right,n,t);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,n){return!0===e||!0===n||(!1!==e||!1!==n)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,n,t,o,r){const s=f(e,o),a=i(n,t);if(s&&a.known)return l(s,a.value,r);const c=f(n,o),u=i(e,t);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,n,t,r);return"!=="===o||"!="===o?p(s):s}return"unknown"}default:return"unknown"}var o,r}function f(e,n){return"Identifier"===e?.type&&e.name&&n.has(e.name)?n.get(e.name):null}function l(e,n,t){const o=t?e=>e==n:e=>e===n;return!!e.some(o)&&(!!e.every(o)||"unknown")}function p(e){return!0!==e&&(!1===e||"unknown")}function d(e,n,t){const{arms:o,elseFrag:r}=function(e){const n=[];let t,o=e;for(;o;){n.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&&(t=e),o=void 0)}return t?{arms:n,elseFrag:t}:{arms:n}}(e),s=[e.start,e.end],a=o.map((e=>c(e.test,n,t))),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 n=a[e];if(i(n)&&f){const n=o[e].consequent;return{span:s,kept:n,removed:m(s,v(n)),recurse:!1}}u(n)||(f=!1)}const l=a.findIndex((e=>!u(e)));if(-1===l)return r?{span:s,kept:r,removed:m(s,v(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===l)return{span:s,kept:void 0,removed:h(o,a,l),recurse:!0};const p=o[l].block;return{span:s,kept:void 0,removed:[[s[0],p.start],...h(o,a,l)],recurse:!1,headerRewrite:{from:p.start,to:p.test.start,text:"{#if "}}}function m(e,n){if(!n)return[e];const t=[];return e[0]<n[0]&&t.push([e[0],n[0]]),n[1]<e[1]&&t.push([n[1],e[1]]),t}function v(e){const n=e?.nodes??[];return 0===n.length?null:[n[0].start,n[n.length-1].end]}function h(e,n,t){const o=[];for(let r=t+1;r<e.length;r++){const t=n[r];if(!t.known||t.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,n){const t=e?.nodes??[];return t.length?t[t.length-1].end:n}function g(e,n){return n.some((([n,t])=>e.start>=n&&e.end<=t))}function y(e,n,t){if(0===n.size&&0===t.size)return[];const o=[];return s(e,null,{IfBlock(e,{next:r}){if(e.elseif||g(e,o))return;const s=d(e,n,t);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const k=e=>e.endsWith(".svelte"),x=10,b="escapes as value (e.g. <svelte:component this={X}>)",S="rendered through a barrel/named import (call sites unobservable)";async function I(e,n,t){const o=await async function(e,n,t){const o=new Map,r=Array.isArray(e)?[...e]:[e],s=new Set(r);for(;r.length>0;){const e=r.shift(),a=await t(e),i=await F(e,a,n,t);o.set(e,i);for(const e of[...i.imports.values(),...i.barrelChildIds])s.has(e)||(s.add(e),r.push(e))}return o}(e,n,t),r=new Set;for(const e of o.values())for(const n of e.escapedComponents)r.add(n);for(const e of r){const n=o.get(e);n&&!n.bailReasons.includes(b)&&n.bailReasons.push(b)}const s=new Set;for(const e of o.values())for(const n of e.barrelChildIds)s.add(n);for(const e of s){const n=o.get(e);n&&!n.bailReasons.includes(S)&&n.bailReasons.push(S)}let a=E(o,M(o,new Map));for(let e=0;e<x;e++){const e=E(o,M(o,D(o,a)));if(A(a,e)){a=e;break}a=e}return{models:o,plans:a}}function M(e,n){const t=new Map,o=e=>{let n=t.get(e);return n||(n={sites:[]},t.set(e,n)),n};for(const t of e.values()){const e=n.get(t.id)??[];for(const n of t.childCalls)e.length>0&&g(n.node,e)||o(n.childId).sites.push($(n.node))}return t}function E(e,n){const t=new Map;for(const o of e.values())t.set(o.id,T(o,n.get(o.id)));return t}function A(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e){const e=n.get(t);if(!e)return!1;if(o.bail!==e.bail)return!1;if(!C(o.constFold,e.constFold))return!1;if(!P(o.narrow,e.narrow))return!1}return!0}function C(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e)if(!n.has(t)||!Object.is(n.get(t),o))return!1;return!0}function P(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e){const e=n.get(t);if(!e||o.length!==e.length)return!1;for(let n=0;n<o.length;n++)if(!Object.is(o[n],e[n]))return!1}return!0}function D(e,n){const t=new Map;for(const o of e.values()){const e=n.get(o.id);if(e.bail)continue;const r=y(o.ast.fragment,e.constFold,e.narrow);r.length>0&&t.set(o.id,r)}return t}async function F(e,n,t,o){const a=r(n,e),i=new Map,c=new Map,u=[];s(a.fragment,null,{SvelteOptions(e,{next:n}){for(const n of e.attributes??[])"Attribute"!==n.type||"accessors"!==n.name&&"customElement"!==n.name||u.push(`<svelte:options ${n.name}>`);n()}});let f,l,p=null,d=!1;const m=new Set,v=a.instance;if(v){for(const n of function*(e){const n=e.content;for(const e of n?.body??[]){if("ImportDeclaration"!==e.type)continue;const n=e.source?.value;if("string"==typeof n)for(const t of e.specifiers??[]){const e=t.local?.name;e&&("ImportDefaultSpecifier"===t.type?yield{value:n,local:e,imported:"default"}:"ImportNamespaceSpecifier"===t.type?yield{value:n,local:e,imported:"*"}:"ImportSpecifier"===t.type&&(yield{value:n,local:e,imported:q(t)??e}))}}}(v)){if(m.add(n.local),"default"===n.imported&&k(n.value)){const o=await t(n.value,e);o&&i.set(n.local,o);continue}const r=await U(n.value,n.imported,e,t,o);r&&c.set(n.local,r)}const n=function(e){const n=e.content;for(const e of n?.body??[])if("VariableDeclaration"===e.type)for(const n of e.declarations??[]){const t=n.init,o=n.id;if("CallExpression"===t?.type&&"Identifier"===t.callee?.type&&"$props"===t.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(n){f=n.declaration,l=n.pattern,n.sharesStatement&&u.push("$props() shares a multi-declarator statement"),p=[];for(const e of n.pattern.properties??[]){if("RestElement"===e.type){d=!0;continue}if("Property"!==e.type)continue;const n=e.key;if("Identifier"!==n?.type||!n.name)continue;const t=e.value,o="AssignmentPattern"===t?.type?t.right:void 0;p.push({name:n.name,property:e,defaultExpr:o})}}}const h=function(e,n){const t=[];return s(e.fragment,null,{Component(e,{next:o}){const r=e.name?n.get(e.name):void 0;r&&t.push({childId:r,node:e}),o()}}),t}(a,i),w=function(e,n){const t=new Set;return 0===n.size||s(e.fragment,null,{Component(e,{next:o}){const r=e.name?n.get(e.name):void 0;r&&t.add(r),o()}}),t}(a,c),{shadowedNames:g,debugNames:y}=function(e,n,t){const o=new Set,r=new Set;n&&s(n,null,{_(e,{next:n}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===t||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])z(n,o);n()}});return s(e.fragment,null,{EachBlock(e,{next:n}){z(e.context,o),"string"==typeof e.index&&o.add(e.index),n()},SnippetBlock(e,{next:n}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const n of e.parameters??[])z(n,o);n()},AwaitBlock(e,{next:n}){z(e.value,o),z(e.error,o),n()},LetDirective(e,{next:n}){e.name&&o.add(e.name),n()},ConstTag(e,{next:n}){for(const n of e.declaration?.declarations??[])z(n.id,o);n()},DebugTag(e,{next:n}){for(const n of e.identifiers??[])"Identifier"===n.type&&n.name&&r.add(n.name);n()}}),{shadowedNames:o,debugNames:r}}(a,v,f),x=function(e,n,t){const o=new Set,r=e=>{if(!e)return;const t=n.get(e);t&&o.add(t)};s(e.fragment,{parent:null},{_(e,{state:n,next:o}){"Identifier"===e.type&&e.name&&t.has(e.name)&&R(e,n.parent)&&r(e.name),o({parent:e})}}),e.instance&&s(e.instance,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&R(e,t.parent)&&!N(t.parent)&&r(e.name),o({parent:e})}});return o}(a,i,m);return{id:e,code:n,ast:a,imports:i,props:p,propsDeclaration:f,propsPattern:l,hasRestProp:d,childCalls:h,shadowedNames:g,debugNames:y,escapedComponents:x,barrelChildIds:w,bailReasons:u}}function z(e,n){if(e)switch(e.type){case"Identifier":return void(e.name&&n.add(e.name));case"ObjectPattern":for(const t of e.properties??[])"RestElement"===t.type?z(t.argument,n):"Property"===t.type&&z(t.value??t.key,n);return;case"ArrayPattern":for(const t of e.elements??[])z(t,n);return;case"AssignmentPattern":return void z(e.left,n);case"RestElement":return void z(e.argument,n);default:return}}function R(e,n){return!!n&&(!("MemberExpression"===n.type&&n.property===e&&!n.computed)&&(!("Property"===n.type&&n.key===e&&!n.computed&&!0!==n.shorthand)&&!N(n)))}function N(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function $(e){const n=e.attributes??[];let t=-1;for(let e=0;e<n.length;e++)"SpreadAttribute"===n[e].type&&(t=e);const o=new Map;for(let e=0;e<n.length;e++){const r=n[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,L(e,t));continue}if("Attribute"!==r.type||!s)continue;const a=B(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>t}:L(e,t))}for(const r of function(e){const n=e.fragment?.nodes??[],t=[];let o=!1;for(const e of n)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&&t.push(e.expression.name);o&&t.push("children");return t}(e))o.set(r,L(n.length,t));return{hadSpread:t>=0,explicit:o}}function L(e,n){return{value:void 0,dynamic:!0,afterLastSpread:e>n}}function B(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const n=Array.isArray(e)?e:[e];if(1===n.length){const e=n[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 t="";for(const e of n){if("Text"!==e.type)return{known:!1};t+=e.data??e.raw??""}return{known:!0,value:t}}function O(e,n){return e.shadowedNames.has(n)||e.debugNames.has(n)}function T(e,n){const t={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return t.bail=!0,t.reasons.push(...e.bailReasons),t;if(!e.props||0===e.props.length)return t;const o=n?.sites??[];if(0===o.length)return t;for(const n of e.props){if(O(e,n.name))continue;const r=_(n,o);t.valueSets.set(n.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&t.narrow.set(n.name,r.values):t.constFold.set(n.name,r.values[0]))}return t}function _(e,n){const t=[];let o=!1,r=!1;const s=e=>{t.some((n=>Object.is(n,e)))||t.push(e)};for(const t of n){const n=t.explicit.get(e.name);if(n?.afterLastSpread){n.dynamic?o=!0:s(n.value);continue}if(t.hadSpread){r=!0;continue}const a=j(e.defaultExpr);a.known?s(a.value):o=!0}return{values:t,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 q(e){const n=e.imported;return"Identifier"===n?.type&&n.name?n.name:"Literal"===n?.type&&"string"==typeof n.value?n.value:void 0}function V(e){return"Identifier"===e?.type&&e.name?e.name:"Literal"===e?.type&&"string"==typeof e.value?e.value:void 0}const J=8;async function U(e,n,t,o,s,a=0){if(a>J)return null;const i=await o(e,t);if(!i)return null;if(k(e)||k(i))return"default"===n||"*"===n?i:null;let c;try{c=await s(i)}catch{return null}const u=function(e,n){try{const t=r(`<script module>\n${e}\n<\/script>`,n);return t.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 t of e.specifiers??[])if(V(t.exported)===n)return U(String(e.source.value),V(t.local)??"default",i,o,s,a+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const t=await U(String(e.source.value),n,i,o,s,a+1);if(t)return t}}else for(const t of e.specifiers??[]){if(V(t.exported)!==n)continue;const e=V(t.local);if(!e)continue;const r=W(u,e);return r?U(r.value,r.imported,i,o,s,a+1):null}return null}function W(e,n){for(const t of e){if("ImportDeclaration"!==t.type)continue;const e=t.source?.value;if("string"==typeof e)for(const o of t.specifiers??[])if(o.local?.name===n){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:q(o)??n}}}return null}const X=64;const G=Symbol("unbounded-class-source");function H(e,n,t){if(!0===e)return G;if(null==e)return new Set;const o=Array.isArray(e)?e:[e];let r=[""];for(const e of o){const o=K(e,n,t);if(o===G)return G;const s=[];for(const e of r)for(const n of o)if(s.push(e+n),s.length>X)return G;r=s}const s=new Set;for(const e of r)for(const n of e.split(/\s+/))n&&s.add(n);return s}function K(e,n,t){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,n,t){if(!e)return G;if("Identifier"===e.type&&e.name&&t.has(e.name)){const n=new Set;for(const o of t.get(e.name))n.add(Q(o));return n}const o=i(e,n);return o.known?new Set([Q(o.value)]):G}(e.expression,n,t):G}function Q(e){return String(e)}function Y(e,n,t){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,n){const t=new Set;let o=!1;const r=n.constFold,a=n.narrow;return s(e.ast.fragment,null,{_(e,{next:n}){if("RegularElement"===(s=e.type)||"SvelteElement"===s||"Component"===s||"SvelteComponent"===s||"SvelteSelf"===s){var s;for(const n of e.attributes??[]){if("SpreadAttribute"===n.type){o=!0;continue}if("ClassDirective"===n.type){n.name&&t.add(n.name);continue}if("Attribute"!==n.type||"class"!==n.name)continue;const e=H(n.value,r,a);if(e===G)o=!0;else for(const n of e)t.add(n)}n()}else n()}}),{classes:t,unbounded:o}}(e,n);if(r.unbounded)return 0;let a=0;for(const n of o.children)"Rule"===n.type&&Z(n,r.classes)&&(ee(e.code,n,o.children,t),a+=1);return a}function Z(e,n){if(function(e){let n=!1;return s(e,null,{_(e,{next:t}){"PseudoClassSelector"===e.type&&"global"===e.name&&(n=!0),t()}}),n}(e))return!1;const t=e.prelude?.children??[];return 0!==t.length&&t.every((e=>function(e,n){let t=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!n.has(e.name)&&(t=!0);return t}(e,n)))}function ee(e,n,t,o){const r=t.indexOf(n),s=t[r-1];let a=n.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,n.end)}function ne(e,n){return oe(e,te(e,n))}function te(e,n){const t=new Map,r=new Map;for(const s of e.values()){const e=new o(s.code);t.set(s.id,e);const a=n.get(s.id);r.set(s.id,a.bail?new Set:ce(s,a,e))}for(const n of e.values())me(n,r,t.get(n.id));return t}function oe(e,n){const t={};for(const o of e.values())t[o.id]=n.get(o.id).toString();return t}function re(e,n,t,o){const r=te(e,n);return function(e,n,t,o){const r=new Map;for(const e of n){const n=r.get(e.owner);n?n.push(e):r.set(e.owner,[e])}for(const[n,s]of r){const r=e.get(n),a=o.get(n);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const n=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${n}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:t(e.variantId)})),se(r.code,e.node,o,e.foldedProps,a)}c.length>0&&ie(r,c,a)}}(e,t,o,r),oe(e,r)}function se(e,n,t,o,r){const s=n.name;if(!s)return;const a=n.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,t);const i=`</${s}`,c=e.lastIndexOf(i,n.end);if(c>=n.start){const e=c+2;r.overwrite(e,e+s.length,t)}for(const t of n.attributes??[])"Attribute"===t.type&&t.name&&o.has(t.name)&&ae(e,t,r)}function ae(e,n,t){let o=n.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),t.remove(o,n.end)}function ie(e,n,t){const o=n.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];t.appendLeft(e.end,`\n${o}`)}else r&&r.content?t.appendLeft(r.content.start,`\n${o}\n`):t.prepend(`<script>\n${o}\n<\/script>\n`)}function ce(e,n,t){return ue(e,n.constFold,n.narrow,n,t)}function ue(e,n,t,o,r){if(0===n.size&&0===t.size)return new Set;const a=e.code,c=[];!function(e,n,t,o,r,a){s(e,null,{IfBlock(e,{next:s}){if(e.elseif||g(e,a))return;const i=d(e,n,t);if(function(e,n,t,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,n,t){const o=e?.nodes??[];return 0===o.length?"":fe(o[0].start,o[o.length-1].end,o,n,t)}(e.kept,n,t));for(const[n,t]of e.removed)o.remove(n,t);if(e.headerRewrite){const{from:n,to:t,text:r}=e.headerRewrite;o.overwrite(n,t,r)}}(i,n,o,r),i.kept)a.push(i.span);else for(const e of i.removed)a.push(e);i.recurse&&s()}})}(e.ast.fragment,n,t,a,r,c),function(e,n,t,o,r){if(0===n.size)return;s(e,null,{ConditionalExpression(e,{next:s}){if(g(e,r))return;const a=i(e.test,n);if(!a.known)return void s();const c=a.value?e.consequent:e.alternate;c?(o.overwrite(e.start,e.end,fe(c.start,c.end,[c],n,t)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,n,a,r,c);const u=function(e,n,t){const o=new Map,r=r=>{r&&s(r,{parent:null},{_(r,{state:s,next:a}){"Identifier"===r.type&&r.name&&n.has(r.name)&&!g(r,t)&&r!==e.propsPattern&&!le(r,s.parent)&&(o.get(r.name)??function(e,n){const t=[];return e.set(n,t),t}(o,r.name)).push([r.start,r.end]),a({parent:r})}})};return r(e.ast.instance),r(e.ast.fragment),o}(e,n,c);for(const[e,t]of n)for(const n of u.get(e)??[])r.overwrite(n[0],n[1],he(t));const f=new Set(n.keys());!function(e,n,t){if(!e.props||0===n.size)return;const o=e.props.filter((e=>!n.has(e.name)));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,n,t){let o=n.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=n.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,n.start),a=e.slice(n.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?t.remove(o,r<e.length?r+1:r):t.remove(n.start,";"===e[n.end]?n.end+1:n.end)}(e.code,e.propsDeclaration,t);const r=e.propsPattern?.properties??[];for(const o of e.props)n.has(o.name)&&(pe(r,o.property,t),e.propsPattern&&de(e.propsPattern,o.name,t))}(e,f,r);return Y(e,{...o,constFold:n,narrow:t},r),f}function fe(e,n,t,o,r){if(0===o.size)return r.slice(e,n);const a=[];for(const e of t)s(e,{parent:null},{_(e,{state:n,next:t}){"Identifier"===e.type&&e.name&&o.has(e.name)&&!le(e,n.parent)&&a.push({start:e.start,end:e.end,name:e.name}),t({parent:e})}});if(0===a.length)return r.slice(e,n);a.sort(((e,n)=>e.start-n.start));let i="",c=e;for(const e of a)i+=r.slice(c,e.start),i+=he(o.get(e.name)),c=e.end;return i+=r.slice(c,n),i}function le(e,n){return!!n&&("MemberExpression"===n.type&&n.property===e&&!n.computed||("Property"===n.type&&n.key===e&&!n.computed&&!0!==n.shorthand||("ImportSpecifier"===n.type||"ImportDefaultSpecifier"===n.type||"ImportNamespaceSpecifier"===n.type||"ExportSpecifier"===n.type)))}function pe(e,n,t){const o=e.indexOf(n),r=e[o+1],s=e[o-1];r?t.remove(n.start,r.start):s?t.remove(s.end,n.end):t.remove(n.start,n.end)}function de(e,n,t){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex((e=>"Identifier"===e.key?.type&&e.key.name===n));if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?t.remove(s.start,a.start):i?t.remove(i.end,s.end):t.remove(s.start,s.end)}function me(e,n,t){s(e.ast.fragment,null,{Component(o,{next:r}){const s=o.name?e.imports.get(o.name):void 0,a=s?n.get(s):void 0;if(a&&a.size>0)for(const n of o.attributes??[])"Attribute"===n.type&&n.name&&a.has(n.name)&&ve(n.value)&&ae(e.code,n,t);r()}})}function ve(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 he(e){return void 0===e?"undefined":JSON.stringify(e)}const we={enabled:!1,maxVariants:8,minSavings:0};function ge(e,t,o=we,r=[]){const s=new Map,a=[];if(!o.enabled)return{variants:s,bindings:a};const i=D(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&&g(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=xe(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const f=be(s,a,i);if(f===Ie(s,a)){u.add(r.childId);continue}const l=c.get(r.childId);l?l.push({owner:n.id,node:r.node,shape:i,code:f}):c.set(r.childId,[{owner:n.id,node:r.node,shape:i,code:f}])}}const f=function(e,n){const t=new Map;for(const o of e.values()){const e=n.get(o.id);t.set(o.id,e.bail?o.code:Ie(o,e))}return t}(e,t),l=new Map;for(const n of e.values())l.set(n.id,ke(f.get(n.id),n));const p=new Set;for(const e of l.values())for(const n of e)p.add(n);const d=(Array.isArray(r)?r:[r]).filter((n=>e.has(n))).filter((e=>!p.has(e))),m=new Map,v=(e,t)=>{const o=m.get(t);if(void 0!==o)return o;let r;try{const{js:o}=n(t,{generate:"client",dev:!1,filename:e});r=o.code.length}catch{r=null}return null!==r&&m.set(t,r),r},h=new Set;for(const e of c.keys())u.has(e)||h.add(e);for(const[n,t]of c){if(u.has(n))continue;if(t.some((e=>e.owner!==n&&h.has(e.owner))))continue;const r=new Map,i=[];let c=!1;for(const e of t){if(r.has(e.code))continue;if(i.length>=o.maxVariants){c=!0;break}const t=`${n}::v${i.length}`;r.set(e.code,t),i.push({id:t,code:e.code})}if(!c&&ye(n,i,e,f,l,d,v,o.minSavings)){for(const e of i){const o=t.find((n=>n.code===e.code));s.set(e.id,{id:e.id,childId:n,code:e.code,foldedProps:o.shape})}for(const e of t)a.push({owner:e.owner,childId:n,node:e.node,variantId:r.get(e.code),foldedProps:e.shape})}}return{variants:s,bindings:a}}function ye(e,n,t,o,r,s,a,i){const c=t.get(e),u=new Map;for(const e of n)u.set(e.id,ke(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 n of r.get(e)??[])l.push(n)}}let p=0;for(const e of f){const n=a(e,o.get(e));if(null===n)return!1;p+=n}const d=n.map((e=>e.id)),m=new Set,v=new Set,h=n=>n===e?{comps:[],vars:d}:{comps:[n],vars:[]},w=[],g=[];for(const e of s){const n=h(e);w.push(...n.comps),g.push(...n.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 n of r.get(e)??[]){const e=h(n);w.push(...e.comps),g.push(...e.vars)}continue}const e=g.pop();if(!v.has(e)){v.add(e);for(const n of u.get(e)??[]){const e=h(n);w.push(...e.comps),g.push(...e.vars)}}}let y=0;for(const e of m){const n=a(e,o.get(e));if(null===n)return!1;y+=n}for(const e of v){const t=n.find((n=>n.id===e)).code,o=a(e,t);if(null===o)return!1;y+=o}return y<p*(1-i)}function ke(e,n){let t;try{t=r(e,n.id)}catch{return[]}const o=[];return s(t.fragment,null,{Component(e,{next:t}){const r=e.name?n.imports.get(e.name):void 0;r&&o.push(r),t()}}),o}function xe(e,n,t){const o=$(e),r=new Map;for(const e of n.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(t.constFold.has(e)||O(n,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function be(e,n,t){const r=new Map(n.constFold);for(const[e,n]of t)r.set(e,n);const s=new Map;for(const[e,t]of n.narrow)r.has(e)||s.set(e,t);const a=new o(e.code);return ue(e,r,s,n,a),a.toString()}const Se=new WeakMap;function Ie(e,n){const t=Se.get(e);if(void 0!==t)return t;const r=new o(e.code);ue(e,n.constFold,n.narrow,n,r);const s=r.toString();return Se.set(e,s),s}async function Me(e,n,t){const{models:o,plans:r}=await I(e,n,t);return ne(o,r)}async function Ee(e,n,t,o=we,r=(e=>e)){const{models:s,plans:a}=await I(e,n,t),i=ge(s,a,o,e);return{files:0===i.bindings.length?ne(s,a):re(s,a,i.bindings.map((e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps}))),r),mono:i}}export{we as DEFAULT_MONO_OPTIONS,I as analyze,ge as monomorphize,Me as svelteShaker,Ee as svelteShakerWithMono,ne as transformAll,re 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){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"),b=10,S="escapes as value (e.g. <svelte:component this={X}>)",I="rendered through a barrel/named import (call sites unobservable)";async function M(e,t,n){return C(await E(e,t,n))}function C(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)}const r=new Set;for(const e of n.values())for(const t of e.barrelChildIds)r.add(t);for(const e of r){const t=n.get(e);t&&!t.bailReasons.includes(I)&&t.bailReasons.push(I)}let s=F(n,A(n,new Map));for(let e=0;e<b;e++){const e=F(n,A(n,$(n,s)));if(D(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function E(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=[];for(const o of W(l)){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"}),p.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 m=B(f,d);for(const e of[...p,...m])u.has(e)||(u.add(e),c.push(e))}return{files:a,edges:i,entries:r}}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&&y(t.node,e)||o(t.childId).sites.push(T(t.node))}return n}function F(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 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(!R(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 R(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,u=new Map;for(const e of t)"default-svelte"===e.kind?c.set(e.local,e.to):u.set(e.local,e.to);const f=[];a(i.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||f.push(`<svelte:options ${t.name}>`);t()}});let l,d,p=null,m=!1;const h=new Set,v=i.instance;if(v){for(const e of W(v))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,d=e.pattern,e.sharesStatement&&f.push("$props() shares a multi-declarator statement"),p=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){m=!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;p.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),g=B(i,u),{shadowedNames:y,debugNames:k}=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??[])N(t,o);t()}});return a(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}}(i,v,l),x=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};a(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&L(e,t.parent)&&r(e.name),o({parent:e})}}),e.instance&&a(e.instance,{parent:null},{_(e,{state:n,next:o}){"Identifier"===e.type&&e.name&&t.has(e.name)&&L(e,n.parent)&&!O(n.parent)&&r(e.name),o({parent:e})}});return o}(i,c,h);return{id:o,code:r,ast:i,imports:c,props:p,propsDeclaration:l,propsPattern:d,hasRestProp:m,childCalls:w,shadowedNames:y,debugNames:k,escapedComponents:x,barrelChildIds:g,bailReasons:f}}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 L(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 B(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 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=J(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 J(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=U(e.defaultExpr);a.known?s(a.value):o=!0}return{values:n,dynamic:o,top:r}}function U(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*W(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>\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;for(const s of e.values()){const e=new o(s.code);n.set(s.id,e);const a=t.get(s.id);r.set(s.id,a.bail?new Set:pe(s,a,e))}for(const t of e.values())ye(t,r,n.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){return me(e,t.constFold,t.narrow,t,n)}function me(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;const s=e.code,i=[];!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,s,r,i),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,s,r,i);const u=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,i);for(const[e,n]of t){const t=xe(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??[];for(const o of e.props)t.has(o.name)&&(we(r,o.property,n),e.propsPattern&&ge(e.propsPattern,o.name,n))}(e,f,r);return oe(e,{...o,constFold:t,narrow:n},r),f}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:"}"};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){const o=e.indexOf(t),r=e[o+1],s=e[o-1];r?n.remove(t.start,r.start):s?n.remove(s.end,t.end):n.remove(t.start,t.end)}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){a(e.ast.fragment,null,{Component(o,{next:r}){const s=o.name?e.imports.get(o.name):void 0,a=s?t.get(s):void 0;if(a&&a.size>0)for(const t of o.attributes??[])"Attribute"===t.type&&t.name&&a.has(t.name)&&ke(t.value)&&le(e.code,t,n);r()}})}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 be={enabled:!1,maxVariants:8,minSavings:0};function Se(e,n,o=be,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 De{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 E([...this.entries],this.resolve,t,n),{models:r,plans:s}=C(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 Pe(e,t,n){const{models:o,plans:r}=await M(e,t,n);return ae(o,r)}async function Re(e,t,n,o=be,r=(e=>e)){const{models:s,plans:a}=await M(e,t,n),i=Se(s,a,o,e);return{files: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{be as DEFAULT_MONO_OPTIONS,De as DevShaker,M as analyze,C as analyzeInput,E as buildAnalyzeInput,Se as monomorphize,Pe as svelteShaker,Re as svelteShakerWithMono,ae as transformAll,ue as transformAllWithMono};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type AnyNode, type Root } from './parse';
|
|
2
|
-
import { type ComponentId, type ComponentPlan, type Literal } from './ir';
|
|
1
|
+
import { type AnyNode, type ParseCache, type Root } from './parse';
|
|
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;
|
|
5
5
|
export type ReadFile = (id: ComponentId) => Promise<string> | string;
|
|
@@ -109,6 +109,29 @@ export interface AnalyzeResult {
|
|
|
109
109
|
* until the plans stop changing.
|
|
110
110
|
*/
|
|
111
111
|
export declare function analyze(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile): Promise<AnalyzeResult>;
|
|
112
|
+
/**
|
|
113
|
+
* The pure, environment-free engine entry (docs/RUST-MIGRATION.md §2): given a
|
|
114
|
+
* fully-resolved, batched {@link AnalyzeInput}, build every component's model and
|
|
115
|
+
* compute its plan to a whole-program fixpoint (docs §2.1). It does NO module
|
|
116
|
+
* resolution or file IO — that is the Shell-side resolution layer's job
|
|
117
|
+
* ({@link buildAnalyzeInput}) — so this is the half that ports to Rust unchanged:
|
|
118
|
+
* one batched call in, plans out, no per-edge callback across the boundary.
|
|
119
|
+
*/
|
|
120
|
+
export declare function analyzeInput(input: AnalyzeInput, parseCache?: ParseCache): AnalyzeResult;
|
|
121
|
+
/**
|
|
122
|
+
* The Shell-side resolution + IO layer (docs/RUST-MIGRATION.md §2.1): BFS-crawl
|
|
123
|
+
* the component graph from `entries`, resolving every import edge and reading
|
|
124
|
+
* every reachable `.svelte` file up front, into a batched {@link AnalyzeInput}.
|
|
125
|
+
*
|
|
126
|
+
* This is the half that STAYS in JS — it owns `this.resolve` / file IO for Vite
|
|
127
|
+
* ecosystem compat (docs ARCHITECTURE §5/§9) — so the engine ({@link
|
|
128
|
+
* analyzeInput}) consumes its output with no callback across the boundary. The
|
|
129
|
+
* traversal mirrors the old crawl exactly: direct default-`.svelte` children and
|
|
130
|
+
* the barrel children a file actually RENDERS are followed (an unrendered barrel
|
|
131
|
+
* import is never crawled — its `<Comp/>` site cannot exist, so it cannot taint a
|
|
132
|
+
* value set), keeping the produced model set — and thus the output — identical.
|
|
133
|
+
*/
|
|
134
|
+
export declare function buildAnalyzeInput(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, parseCache?: ParseCache): Promise<AnalyzeInput>;
|
|
112
135
|
/**
|
|
113
136
|
* Dead `{#if}` spans per component implied by `plans`, via the SAME shared
|
|
114
137
|
* predicate the transform uses ({@link computeDeadSpans}). A bailed component
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type ReadFile, type Resolve } from './analyze';
|
|
2
|
+
import type { ComponentId, EditResult } from './ir';
|
|
3
|
+
/**
|
|
4
|
+
* dev shake granularity (docs/RUST-MIGRATION.md §3 M2 / ARCHITECTURE §6.2):
|
|
5
|
+
* - `'coarse'` — every change re-reads, re-parses, and re-analyzes the whole
|
|
6
|
+
* program from scratch. Trivially correct; the safety valve.
|
|
7
|
+
* - `'incremental'`— re-reads and re-parses ONLY the changed/added files (the
|
|
8
|
+
* dominant cost), then re-runs the whole-program fixpoint and
|
|
9
|
+
* transform over the cached models. The fixpoint still reads
|
|
10
|
+
* EVERY file, so the inverted "all importers" dependency can
|
|
11
|
+
* never be missed — the result is identical to `'coarse'`,
|
|
12
|
+
* only faster. The differential oracle (tests/dev.test.ts)
|
|
13
|
+
* pins `incremental === coarse === full` for every edit.
|
|
14
|
+
*/
|
|
15
|
+
export type DevMode = 'coarse' | 'incremental';
|
|
16
|
+
/** Which files the Shell observed change since the last update. */
|
|
17
|
+
export interface DevShakerChange {
|
|
18
|
+
/** Existing files whose content changed (re-read from disk). */
|
|
19
|
+
changed?: ComponentId[];
|
|
20
|
+
/** Newly-created files now in the program. */
|
|
21
|
+
added?: ComponentId[];
|
|
22
|
+
/** Deleted files. */
|
|
23
|
+
removed?: ComponentId[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A long-lived, incremental whole-program shaker for `vite dev` (docs §2.1, §3
|
|
27
|
+
* M2). Construct it with the FULL file set (the Shell's FS scan — the call-site
|
|
28
|
+
* completeness boundary, ARCHITECTURE §6.2) and the same `resolve`/`readFile` the
|
|
29
|
+
* build path uses; call {@link init} once, then {@link update} on every file
|
|
30
|
+
* change. `update` returns the components whose SLIMMED OUTPUT changed, which the
|
|
31
|
+
* Shell turns into the widened HMR boundary.
|
|
32
|
+
*
|
|
33
|
+
* L2 monomorphization is intentionally NOT applied in dev (its net-win gate is a
|
|
34
|
+
* whole-program measurement that is expensive to keep incrementally correct); dev
|
|
35
|
+
* covers L0/L1/L1.5 only (docs §5 risks).
|
|
36
|
+
*/
|
|
37
|
+
export declare class DevShaker {
|
|
38
|
+
private readonly entries;
|
|
39
|
+
private readonly resolve;
|
|
40
|
+
private readonly readFile;
|
|
41
|
+
private readonly mode;
|
|
42
|
+
/** Content-keyed AST cache — only changed files re-parse (§2.2). */
|
|
43
|
+
private readonly parseCache;
|
|
44
|
+
/** Last-seen source per file, so an update re-reads only what changed. */
|
|
45
|
+
private readonly codeCache;
|
|
46
|
+
/** Current slimmed output per `.svelte` id (the live shake result). */
|
|
47
|
+
private output;
|
|
48
|
+
constructor(files: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, mode?: DevMode);
|
|
49
|
+
/** Full initial shake of the program. Returns the slimmed source per file. */
|
|
50
|
+
init(): Promise<Record<ComponentId, string>>;
|
|
51
|
+
/** The current slimmed source for a file, or `undefined` if not in the program. */
|
|
52
|
+
get(id: ComponentId): string | undefined;
|
|
53
|
+
/** A copy of the current whole-program output (every file's slimmed source). */
|
|
54
|
+
snapshot(): Record<ComponentId, string>;
|
|
55
|
+
/**
|
|
56
|
+
* Apply a batch of file changes and re-shake. Returns the delta: the
|
|
57
|
+
* components whose output changed (a superset of the edited files — a call-site
|
|
58
|
+
* edit can change a child's residual) and the ones no longer in the program.
|
|
59
|
+
*/
|
|
60
|
+
update(change: DevShakerChange): Promise<EditResult>;
|
|
61
|
+
/**
|
|
62
|
+
* Run the whole-program shake. In `'incremental'` mode it reads through the
|
|
63
|
+
* code/parse caches (so unchanged files are neither re-read nor re-parsed); in
|
|
64
|
+
* `'coarse'` mode it bypasses both for a from-scratch rebuild. Both produce
|
|
65
|
+
* identical output — only the work differs.
|
|
66
|
+
*/
|
|
67
|
+
private shake;
|
|
68
|
+
/** Read through the code cache, disk-reading and caching on a miss. */
|
|
69
|
+
private readonly cachedReadFile;
|
|
70
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type ReadFile, type Resolve } from './analyze';
|
|
2
2
|
import { type MonomorphizeOptions, type MonomorphizeResult } from './mono';
|
|
3
3
|
import type { ComponentId } from './ir';
|
|
4
|
-
export type { ComponentId } from './ir';
|
|
4
|
+
export type { ComponentId, AnalyzeInput, InputFile, ResolvedEdge, EdgeKind, EditResult, } from './ir';
|
|
5
5
|
export type { Resolve, ReadFile } from './analyze';
|
|
6
|
-
export { analyze } from './analyze';
|
|
6
|
+
export { analyze, analyzeInput, buildAnalyzeInput } from './analyze';
|
|
7
|
+
export { DevShaker, type DevMode, type DevShakerChange } from './engine';
|
|
7
8
|
export { transformAll, transformAllWithMono } from './transform';
|
|
8
9
|
export { monomorphize, DEFAULT_MONO_OPTIONS, type MonomorphizeOptions, type MonomorphizeResult, type Variant, type CallSiteBinding, } from './mono';
|
|
9
10
|
/**
|
package/dist/types/src/ir.d.ts
CHANGED
|
@@ -2,6 +2,43 @@
|
|
|
2
2
|
export type ComponentId = string;
|
|
3
3
|
/** A statically-known literal value a prop can take. */
|
|
4
4
|
export type Literal = string | number | boolean | null | undefined;
|
|
5
|
+
/** How an imported local name binds to a child `.svelte` component. */
|
|
6
|
+
export type EdgeKind = 'default-svelte' | 'barrel';
|
|
7
|
+
/** One reachable `.svelte` source the engine will model. */
|
|
8
|
+
export interface InputFile {
|
|
9
|
+
id: ComponentId;
|
|
10
|
+
code: string;
|
|
11
|
+
}
|
|
12
|
+
/** One resolved import edge: `from` binds `local` to the child `.svelte` `to`. */
|
|
13
|
+
export interface ResolvedEdge {
|
|
14
|
+
from: ComponentId;
|
|
15
|
+
local: string;
|
|
16
|
+
to: ComponentId;
|
|
17
|
+
kind: EdgeKind;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The fully-resolved, batched input to the engine (docs §2.1). `files` is every
|
|
21
|
+
* reachable `.svelte` (barrel `.js`/`.ts` are consumed during resolution and do
|
|
22
|
+
* not appear here); `edges` are already resolved to absolute ids; `entries` is
|
|
23
|
+
* the call-site-completeness set (the Shell's FS scan) and the L2 net-win roots.
|
|
24
|
+
*/
|
|
25
|
+
export interface AnalyzeInput {
|
|
26
|
+
files: InputFile[];
|
|
27
|
+
edges: ResolvedEdge[];
|
|
28
|
+
entries: ComponentId[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The delta the dev engine returns after applying file changes (docs §2.1, the
|
|
32
|
+
* `vite dev` incremental path). `changed` maps each component whose SLIMMED
|
|
33
|
+
* OUTPUT changed to its new source — a SUPERSET of the edited files, because a
|
|
34
|
+
* call-site edit can change a child's residual without the child being touched
|
|
35
|
+
* (the HMR module-graph divergence the Shell must widen for). `removed` lists
|
|
36
|
+
* components no longer in the program (deleted or now unreachable).
|
|
37
|
+
*/
|
|
38
|
+
export interface EditResult {
|
|
39
|
+
changed: Record<ComponentId, string>;
|
|
40
|
+
removed: ComponentId[];
|
|
41
|
+
}
|
|
5
42
|
/**
|
|
6
43
|
* The join, over every call site in the program, of the value passed to a
|
|
7
44
|
* single prop. See the lattice in docs/ARCHITECTURE.md §2.2.
|
|
@@ -73,6 +73,19 @@ export interface Root extends AnyNode {
|
|
|
73
73
|
fragment: AnyNode;
|
|
74
74
|
}
|
|
75
75
|
export declare function parseSvelte(code: string, filename: string): Root;
|
|
76
|
+
/**
|
|
77
|
+
* Content-keyed parse cache: a hit returns the IDENTICAL AST for unchanged
|
|
78
|
+
* source, so the dev engine re-parses only the files that actually changed
|
|
79
|
+
* (docs/RUST-MIGRATION.md §2.2 — `parse(id)` is the cached input query, the
|
|
80
|
+
* dominant cost an edit avoids). Keyed by content, so a stale entry can never
|
|
81
|
+
* return an AST whose byte offsets disagree with the source: a code mismatch
|
|
82
|
+
* forces a re-parse.
|
|
83
|
+
*/
|
|
84
|
+
export type ParseCache = Map<string, {
|
|
85
|
+
code: string;
|
|
86
|
+
ast: Root;
|
|
87
|
+
}>;
|
|
88
|
+
export declare function parseCached(filename: string, code: string, cache?: ParseCache): Root;
|
|
76
89
|
export interface WalkCtx<S> {
|
|
77
90
|
state: S;
|
|
78
91
|
next: (state?: S) => void;
|
package/dist/types/src/vite.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Plugin } from 'vite';
|
|
2
|
+
import { type DevMode } from './engine';
|
|
2
3
|
import { type MonomorphizeOptions } from './mono';
|
|
3
4
|
export interface ShakerOptions {
|
|
4
5
|
/**
|
|
@@ -20,13 +21,27 @@ export interface ShakerOptions {
|
|
|
20
21
|
* to OFF.
|
|
21
22
|
*/
|
|
22
23
|
monomorphize?: boolean | Partial<Omit<MonomorphizeOptions, 'enabled'>>;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to shake in `vite dev` too (docs/RUST-MIGRATION.md §3 M2,
|
|
26
|
+
* ARCHITECTURE §6.2). Default `false` — dev is a pass-through, which is always
|
|
27
|
+
* correct and keeps HMR simple. Opt in to incremental dev shaking with:
|
|
28
|
+
* - `'incremental'` — re-parses only changed files, re-runs the whole-program
|
|
29
|
+
* fixpoint over a long-lived {@link DevShaker} (fast, the intended mode);
|
|
30
|
+
* - `'coarse'` — re-analyzes the whole program on every change (the slow but
|
|
31
|
+
* trivially-correct safety valve).
|
|
32
|
+
* L2 is NOT applied in dev (L0/L1/L1.5 only — docs §5 risks).
|
|
33
|
+
*/
|
|
34
|
+
dev?: false | DevMode;
|
|
23
35
|
}
|
|
24
36
|
/**
|
|
25
37
|
* Source-level Svelte tree-shaking as a Vite plugin (docs/ARCHITECTURE.md §6).
|
|
26
38
|
*
|
|
27
|
-
* Build-only by
|
|
28
|
-
*
|
|
29
|
-
* `
|
|
39
|
+
* Build-only by default: dev is a pass-through unless `dev` is set (§6.2), so
|
|
40
|
+
* `apply` returns true in build always and in serve only when opted in. When
|
|
41
|
+
* `dev` is enabled, `configureServer` drives a long-lived incremental
|
|
42
|
+
* {@link DevShaker} and `handleHotUpdate` widens the HMR boundary to the children
|
|
43
|
+
* whose residual changed (docs/RUST-MIGRATION.md §3 M2). `enforce: 'pre'` runs
|
|
44
|
+
* us before `@sveltejs/vite-plugin-svelte` in both modes, so we hand it
|
|
30
45
|
* already-slimmed `.svelte` source and stay decoupled from Svelte's codegen.
|
|
31
46
|
*
|
|
32
47
|
* L2 wiring (opt-in, `level: 2`): a specialized variant is exposed as a request
|
package/dist/vite.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"node:fs";import*as n from"node:path";import{parse as t,compile as o}from"svelte/compiler";import{walk as r}from"zimmerframe";import s from"magic-string";function a(e,n){return t(e,{modern:!0,filename:n})}function i(e,n,t){r(e,n,t)}const c={known:!1};function u(e,n){if(!e)return c;switch(e.type){case"Literal":return{known:!0,value:e.value};case"Identifier":{const t=e.name??"";return"undefined"===t?{known:!0,value:void 0}:n.has(t)?{known:!0,value:n.get(t)}:c}case"UnaryExpression":{const t=u(e.argument,n);if(!t.known)return c;const o=t.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 c}}case"LogicalExpression":{const t=u(e.left,n);if(!t.known)return c;switch(e.operator){case"&&":return t.value?u(e.right,n):t;case"||":return t.value?t:u(e.right,n);case"??":return null===t.value||void 0===t.value?u(e.right,n):t;default:return c}}case"BinaryExpression":{const t=u(e.left,n),o=u(e.right,n);if(!t.known||!o.known)return c;const r=t.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 c}}default:return c}}function l(e,n,t){const o=u(e,n);if(o.known)return o;const r=f(e,n,t);return"unknown"===r?c:{known:!0,value:r}}function f(e,n,t){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?m(f(e.argument,n,t)):"unknown";case"LogicalExpression":{const s=f(e.left,n,t),a=()=>f(e.right,n,t);return"&&"===e.operator?!1!==s&&(o=s,r=a(),!1!==o&&!1!==r&&(!0===o&&!0===r||"unknown")):"||"===e.operator?!0===s||function(e,n){return!0===e||!0===n||(!1!==e||!1!==n)&&"unknown"}(s,a()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,n,t,o,r){const s=p(e,o),a=u(n,t);if(s&&a.known)return d(s,a.value,r);const i=p(n,o),c=u(e,t);return i&&c.known?d(i,c.value,r):c.known&&a.known?r?c.value==a.value:c.value===a.value:"unknown"}(e.left,e.right,n,t,r);return"!=="===o||"!="===o?m(s):s}return"unknown"}default:return"unknown"}var o,r}function p(e,n){return"Identifier"===e?.type&&e.name&&n.has(e.name)?n.get(e.name):null}function d(e,n,t){const o=t?e=>e==n:e=>e===n;return!!e.some(o)&&(!!e.every(o)||"unknown")}function m(e){return!0!==e&&(!1===e||"unknown")}function v(e,n,t){const{arms:o,elseFrag:r}=function(e){const n=[];let t,o=e;for(;o;){n.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&&(t=e),o=void 0)}return t?{arms:n,elseFrag:t}:{arms:n}}(e),s=[e.start,e.end],a=o.map((e=>l(e.test,n,t))),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 n=a[e];if(i(n)&&u){const n=o[e].consequent;return{span:s,kept:n,removed:h(s,w(n)),recurse:!1}}c(n)||(u=!1)}const f=a.findIndex((e=>!c(e)));if(-1===f)return r?{span:s,kept:r,removed:h(s,w(r)),recurse:!1}:{span:s,kept:void 0,removed:[s],recurse:!1};if(0===f)return{span:s,kept:void 0,removed:y(o,a,f),recurse:!0};const p=o[f].block;return{span:s,kept:void 0,removed:[[s[0],p.start],...y(o,a,f)],recurse:!1,headerRewrite:{from:p.start,to:p.test.start,text:"{#if "}}}function h(e,n){if(!n)return[e];const t=[];return e[0]<n[0]&&t.push([e[0],n[0]]),n[1]<e[1]&&t.push([n[1],e[1]]),t}function w(e){const n=e?.nodes??[];return 0===n.length?null:[n[0].start,n[n.length-1].end]}function y(e,n,t){const o=[];for(let r=t+1;r<e.length;r++){const t=n[r];if(!t.known||t.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,n){const t=e?.nodes??[];return t.length?t[t.length-1].end:n}function k(e,n){return n.some((([n,t])=>e.start>=n&&e.end<=t))}function x(e,n,t){if(0===n.size&&0===t.size)return[];const o=[];return i(e,null,{IfBlock(e,{next:r}){if(e.elseif||k(e,o))return;const s=v(e,n,t);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const b=e=>e.endsWith(".svelte"),S=10,I="escapes as value (e.g. <svelte:component this={X}>)",M="rendered through a barrel/named import (call sites unobservable)";async function E(e,n,t){const o=await async function(e,n,t){const o=new Map,r=Array.isArray(e)?[...e]:[e],s=new Set(r);for(;r.length>0;){const e=r.shift(),a=await t(e),i=await z(e,a,n,t);o.set(e,i);for(const e of[...i.imports.values(),...i.barrelChildIds])s.has(e)||(s.add(e),r.push(e))}return o}(e,n,t),r=new Set;for(const e of o.values())for(const n of e.escapedComponents)r.add(n);for(const e of r){const n=o.get(e);n&&!n.bailReasons.includes(I)&&n.bailReasons.push(I)}const s=new Set;for(const e of o.values())for(const n of e.barrelChildIds)s.add(n);for(const e of s){const n=o.get(e);n&&!n.bailReasons.includes(M)&&n.bailReasons.push(M)}let a=$(o,A(o,new Map));for(let e=0;e<S;e++){const e=$(o,A(o,D(o,a)));if(C(a,e)){a=e;break}a=e}return{models:o,plans:a}}function A(e,n){const t=new Map,o=e=>{let n=t.get(e);return n||(n={sites:[]},t.set(e,n)),n};for(const t of e.values()){const e=n.get(t.id)??[];for(const n of t.childCalls)e.length>0&&k(n.node,e)||o(n.childId).sites.push(O(n.node))}return t}function $(e,n){const t=new Map;for(const o of e.values())t.set(o.id,j(o,n.get(o.id)));return t}function C(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e){const e=n.get(t);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,n){if(e.size!==n.size)return!1;for(const[t,o]of e)if(!n.has(t)||!Object.is(n.get(t),o))return!1;return!0}function P(e,n){if(e.size!==n.size)return!1;for(const[t,o]of e){const e=n.get(t);if(!e||o.length!==e.length)return!1;for(let n=0;n<o.length;n++)if(!Object.is(o[n],e[n]))return!1}return!0}function D(e,n){const t=new Map;for(const o of e.values()){const e=n.get(o.id);if(e.bail)continue;const r=x(o.ast.fragment,e.constFold,e.narrow);r.length>0&&t.set(o.id,r)}return t}async function z(e,n,t,o){const r=a(n,e),s=new Map,c=new Map,u=[];i(r.fragment,null,{SvelteOptions(e,{next:n}){for(const n of e.attributes??[])"Attribute"!==n.type||"accessors"!==n.name&&"customElement"!==n.name||u.push(`<svelte:options ${n.name}>`);n()}});let l,f,p=null,d=!1;const m=new Set,v=r.instance;if(v){for(const n of function*(e){const n=e.content;for(const e of n?.body??[]){if("ImportDeclaration"!==e.type)continue;const n=e.source?.value;if("string"==typeof n)for(const t of e.specifiers??[]){const e=t.local?.name;e&&("ImportDefaultSpecifier"===t.type?yield{value:n,local:e,imported:"default"}:"ImportNamespaceSpecifier"===t.type?yield{value:n,local:e,imported:"*"}:"ImportSpecifier"===t.type&&(yield{value:n,local:e,imported:V(t)??e}))}}}(v)){if(m.add(n.local),"default"===n.imported&&b(n.value)){const o=await t(n.value,e);o&&s.set(n.local,o);continue}const r=await X(n.value,n.imported,e,t,o);r&&c.set(n.local,r)}const n=function(e){const n=e.content;for(const e of n?.body??[])if("VariableDeclaration"===e.type)for(const n of e.declarations??[]){const t=n.init,o=n.id;if("CallExpression"===t?.type&&"Identifier"===t.callee?.type&&"$props"===t.callee.name&&"ObjectPattern"===o?.type)return{declaration:e,pattern:o,sharesStatement:(e.declarations?.length??1)>1}}return null}(v);if(n){l=n.declaration,f=n.pattern,n.sharesStatement&&u.push("$props() shares a multi-declarator statement"),p=[];for(const e of n.pattern.properties??[]){if("RestElement"===e.type){d=!0;continue}if("Property"!==e.type)continue;const n=e.key;if("Identifier"!==n?.type||!n.name)continue;const t=e.value,o="AssignmentPattern"===t?.type?t.right:void 0;p.push({name:n.name,property:e,defaultExpr:o})}}}const h=function(e,n){const t=[];return i(e.fragment,null,{Component(e,{next:o}){const r=e.name?n.get(e.name):void 0;r&&t.push({childId:r,node:e}),o()}}),t}(r,s),w=function(e,n){const t=new Set;return 0===n.size||i(e.fragment,null,{Component(e,{next:o}){const r=e.name?n.get(e.name):void 0;r&&t.add(r),o()}}),t}(r,c),{shadowedNames:y,debugNames:g}=function(e,n,t){const o=new Set,r=new Set;n&&i(n,null,{_(e,{next:n}){if("VariableDeclarator"!==e.type&&"FunctionDeclaration"!==e.type||e===t||"Identifier"!==e.id?.type||!e.id.name||o.add(e.id.name),"FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])R(n,o);n()}});return i(e.fragment,null,{EachBlock(e,{next:n}){R(e.context,o),"string"==typeof e.index&&o.add(e.index),n()},SnippetBlock(e,{next:n}){"Identifier"===e.expression?.type&&e.expression.name&&o.add(e.expression.name);for(const n of e.parameters??[])R(n,o);n()},AwaitBlock(e,{next:n}){R(e.value,o),R(e.error,o),n()},LetDirective(e,{next:n}){e.name&&o.add(e.name),n()},ConstTag(e,{next:n}){for(const n of e.declaration?.declarations??[])R(n.id,o);n()},DebugTag(e,{next:n}){for(const n of e.identifiers??[])"Identifier"===n.type&&n.name&&r.add(n.name);n()}}),{shadowedNames:o,debugNames:r}}(r,v,l),k=function(e,n,t){const o=new Set,r=e=>{if(!e)return;const t=n.get(e);t&&o.add(t)};i(e.fragment,{parent:null},{_(e,{state:n,next:o}){"Identifier"===e.type&&e.name&&t.has(e.name)&&N(e,n.parent)&&r(e.name),o({parent:e})}}),e.instance&&i(e.instance,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&N(e,t.parent)&&!L(t.parent)&&r(e.name),o({parent:e})}});return o}(r,s,m);return{id:e,code:n,ast:r,imports:s,props:p,propsDeclaration:l,propsPattern:f,hasRestProp:d,childCalls:h,shadowedNames:y,debugNames:g,escapedComponents:k,barrelChildIds:w,bailReasons:u}}function R(e,n){if(e)switch(e.type){case"Identifier":return void(e.name&&n.add(e.name));case"ObjectPattern":for(const t of e.properties??[])"RestElement"===t.type?R(t.argument,n):"Property"===t.type&&R(t.value??t.key,n);return;case"ArrayPattern":for(const t of e.elements??[])R(t,n);return;case"AssignmentPattern":return void R(e.left,n);case"RestElement":return void R(e.argument,n);default:return}}function N(e,n){return!!n&&(!("MemberExpression"===n.type&&n.property===e&&!n.computed)&&(!("Property"===n.type&&n.key===e&&!n.computed&&!0!==n.shorthand)&&!L(n)))}function L(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function O(e){const n=e.attributes??[];let t=-1;for(let e=0;e<n.length;e++)"SpreadAttribute"===n[e].type&&(t=e);const o=new Map;for(let e=0;e<n.length;e++){const r=n[e],s=r.name;if("BindDirective"===r.type){s&&o.set(s,_(e,t));continue}if("Attribute"!==r.type||!s)continue;const a=B(r.value);o.set(s,a.known?{value:a.value,dynamic:!1,afterLastSpread:e>t}:_(e,t))}for(const r of function(e){const n=e.fragment?.nodes??[],t=[];let o=!1;for(const e of n)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&&t.push(e.expression.name);o&&t.push("children");return t}(e))o.set(r,_(n.length,t));return{hadSpread:t>=0,explicit:o}}function _(e,n){return{value:void 0,dynamic:!0,afterLastSpread:e>n}}function B(e){if(!0===e)return{known:!0,value:!0};if(null==e)return{known:!1};const n=Array.isArray(e)?e:[e];if(1===n.length){const e=n[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 t="";for(const e of n){if("Text"!==e.type)return{known:!1};t+=e.data??e.raw??""}return{known:!0,value:t}}function T(e,n){return e.shadowedNames.has(n)||e.debugNames.has(n)}function j(e,n){const t={id:e.id,bail:!1,reasons:[],constFold:new Map,narrow:new Map,valueSets:new Map};if(e.bailReasons.length>0)return t.bail=!0,t.reasons.push(...e.bailReasons),t;if(!e.props||0===e.props.length)return t;const o=n?.sites??[];if(0===o.length)return t;for(const n of e.props){if(T(e,n.name))continue;const r=W(n,o);t.valueSets.set(n.name,r),r.top||r.dynamic||(1!==r.values.length?r.values.length>=2&&t.narrow.set(n.name,r.values):t.constFold.set(n.name,r.values[0]))}return t}function W(e,n){const t=[];let o=!1,r=!1;const s=e=>{t.some((n=>Object.is(n,e)))||t.push(e)};for(const t of n){const n=t.explicit.get(e.name);if(n?.afterLastSpread){n.dynamic?o=!0:s(n.value);continue}if(t.hadSpread){r=!0;continue}const a=q(e.defaultExpr);a.known?s(a.value):o=!0}return{values:t,dynamic:o,top:r}}function q(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 n=e.imported;return"Identifier"===n?.type&&n.name?n.name:"Literal"===n?.type&&"string"==typeof n.value?n.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 U=8;async function X(e,n,t,o,r,s=0){if(s>U)return null;const i=await o(e,t);if(!i)return null;if(b(e)||b(i))return"default"===n||"*"===n?i:null;let c;try{c=await r(i)}catch{return null}const u=function(e,n){try{const t=a(`<script module>\n${e}\n<\/script>`,n);return t.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 t of e.specifiers??[])if(J(t.exported)===n)return X(String(e.source.value),J(t.local)??"default",i,o,r,s+1)}else if("ExportNamedDeclaration"!==e.type||e.source){if("ExportAllDeclaration"===e.type&&e.source?.value){const t=await X(String(e.source.value),n,i,o,r,s+1);if(t)return t}}else for(const t of e.specifiers??[]){if(J(t.exported)!==n)continue;const e=J(t.local);if(!e)continue;const a=G(u,e);return a?X(a.value,a.imported,i,o,r,s+1):null}return null}function G(e,n){for(const t of e){if("ImportDeclaration"!==t.type)continue;const e=t.source?.value;if("string"==typeof e)for(const o of t.specifiers??[])if(o.local?.name===n){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:V(o)??n}}}return null}const H=64;const K=Symbol("unbounded-class-source");function Q(e,n,t){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,n,t);if(o===K)return K;const s=[];for(const e of r)for(const n of o)if(s.push(e+n),s.length>H)return K;r=s}const s=new Set;for(const e of r)for(const n of e.split(/\s+/))n&&s.add(n);return s}function Y(e,n,t){return"Text"===e.type?new Set([e.data??e.raw??""]):"ExpressionTag"===e.type?function(e,n,t){if(!e)return K;if("Identifier"===e.type&&e.name&&t.has(e.name)){const n=new Set;for(const o of t.get(e.name))n.add(Z(o));return n}const o=u(e,n);return o.known?new Set([Z(o.value)]):K}(e.expression,n,t):K}function Z(e){return String(e)}function ee(e,n,t){const o=e.ast.css;if(!o||!o.children)return 0;const r=function(e,n){const t=new Set;let o=!1;const r=n.constFold,s=n.narrow;return i(e.ast.fragment,null,{_(e,{next:n}){if("RegularElement"===(a=e.type)||"SvelteElement"===a||"Component"===a||"SvelteComponent"===a||"SvelteSelf"===a){var a;for(const n of e.attributes??[]){if("SpreadAttribute"===n.type){o=!0;continue}if("ClassDirective"===n.type){n.name&&t.add(n.name);continue}if("Attribute"!==n.type||"class"!==n.name)continue;const e=Q(n.value,r,s);if(e===K)o=!0;else for(const n of e)t.add(n)}n()}else n()}}),{classes:t,unbounded:o}}(e,n);if(r.unbounded)return 0;let s=0;for(const n of o.children)"Rule"===n.type&&ne(n,r.classes)&&(te(e.code,n,o.children,t),s+=1);return s}function ne(e,n){if(function(e){let n=!1;return i(e,null,{_(e,{next:t}){"PseudoClassSelector"===e.type&&"global"===e.name&&(n=!0),t()}}),n}(e))return!1;const t=e.prelude?.children??[];return 0!==t.length&&t.every((e=>function(e,n){let t=!1;for(const o of e.children??[])for(const e of o.selectors??[])"ClassSelector"===e.type&&e.name&&!n.has(e.name)&&(t=!0);return t}(e,n)))}function te(e,n,t,o){const r=t.indexOf(n),s=t[r-1];let a=n.start;const i=s?s.end:0;for(;a>i&&/\s/.test(e[a-1]);)a-=1;o.remove(a,n.end)}function oe(e,n){return se(e,re(e,n))}function re(e,n){const t=new Map,o=new Map;for(const r of e.values()){const e=new s(r.code);t.set(r.id,e);const a=n.get(r.id);o.set(r.id,a.bail?new Set:le(r,a,e))}for(const n of e.values())he(n,o,t.get(n.id));return t}function se(e,n){const t={};for(const o of e.values())t[o.id]=n.get(o.id).toString();return t}function ae(e,n,t,o){const r=re(e,n);return function(e,n,t,o){const r=new Map;for(const e of n){const n=r.get(e.owner);n?n.push(e):r.set(e.owner,[e])}for(const[n,s]of r){const r=e.get(n),a=o.get(n);if(!r||!a)continue;const i=new Map,c=[];let u=0;for(const e of s){const n=e.node.name??"Cmp";let o=i.get(e.variantId);void 0===o&&(o=`${n}__shaker_v${u++}`,i.set(e.variantId,o),c.push({local:o,spec:t(e.variantId)})),ie(r.code,e.node,o,e.foldedProps,a)}c.length>0&&ue(r,c,a)}}(e,t,o,r),se(e,r)}function ie(e,n,t,o,r){const s=n.name;if(!s)return;const a=n.start+1;e.slice(a,a+s.length)===s&&r.overwrite(a,a+s.length,t);const i=`</${s}`,c=e.lastIndexOf(i,n.end);if(c>=n.start){const e=c+2;r.overwrite(e,e+s.length,t)}for(const t of n.attributes??[])"Attribute"===t.type&&t.name&&o.has(t.name)&&ce(e,t,r)}function ce(e,n,t){let o=n.start;" "!==e[o-1]&&"\t"!==e[o-1]||(o-=1),t.remove(o,n.end)}function ue(e,n,t){const o=n.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];t.appendLeft(e.end,`\n${o}`)}else r&&r.content?t.appendLeft(r.content.start,`\n${o}\n`):t.prepend(`<script>\n${o}\n<\/script>\n`)}function le(e,n,t){return fe(e,n.constFold,n.narrow,n,t)}function fe(e,n,t,o,r){if(0===n.size&&0===t.size)return new Set;const s=e.code,a=[];!function(e,n,t,o,r,s){i(e,null,{IfBlock(e,{next:a}){if(e.elseif||k(e,s))return;const i=v(e,n,t);if(function(e,n,t,o){if(e.kept)return void o.overwrite(e.span[0],e.span[1],function(e,n,t){const o=e?.nodes??[];return 0===o.length?"":pe(o[0].start,o[o.length-1].end,o,n,t)}(e.kept,n,t));for(const[n,t]of e.removed)o.remove(n,t);if(e.headerRewrite){const{from:n,to:t,text:r}=e.headerRewrite;o.overwrite(n,t,r)}}(i,n,o,r),i.kept)s.push(i.span);else for(const e of i.removed)s.push(e);i.recurse&&a()}})}(e.ast.fragment,n,t,s,r,a),function(e,n,t,o,r){if(0===n.size)return;i(e,null,{ConditionalExpression(e,{next:s}){if(k(e,r))return;const a=u(e.test,n);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],n,t)),r.push([e.start,e.end])):s()}})}(e.ast.fragment,n,s,r,a);const c=function(e,n,t){const o=new Map,r=r=>{r&&i(r,{parent:null},{_(r,{state:s,next:a}){"Identifier"===r.type&&r.name&&n.has(r.name)&&!k(r,t)&&r!==e.propsPattern&&!de(r,s.parent)&&(o.get(r.name)??function(e,n){const t=[];return e.set(n,t),t}(o,r.name)).push([r.start,r.end]),a({parent:r})}})};return r(e.ast.instance),r(e.ast.fragment),o}(e,n,a);for(const[e,t]of n)for(const n of c.get(e)??[])r.overwrite(n[0],n[1],ye(t));const l=new Set(n.keys());!function(e,n,t){if(!e.props||0===n.size)return;const o=e.props.filter((e=>!n.has(e.name)));if(0===o.length&&!e.hasRestProp&&e.propsDeclaration)return void function(e,n,t){let o=n.start;for(;o>0&&"\n"!==e[o-1];)o-=1;let r=n.end;for(;r<e.length&&"\n"!==e[r];)r+=1;const s=e.slice(o,n.start),a=e.slice(n.end,r);/^\s*$/.test(s)&&/^\s*;?\s*$/.test(a)?t.remove(o,r<e.length?r+1:r):t.remove(n.start,";"===e[n.end]?n.end+1:n.end)}(e.code,e.propsDeclaration,t);const r=e.propsPattern?.properties??[];for(const o of e.props)n.has(o.name)&&(me(r,o.property,t),e.propsPattern&&ve(e.propsPattern,o.name,t))}(e,l,r);return ee(e,{...o,constFold:n,narrow:t},r),l}function pe(e,n,t,o,r){if(0===o.size)return r.slice(e,n);const s=[];for(const e of t)i(e,{parent:null},{_(e,{state:n,next:t}){"Identifier"===e.type&&e.name&&o.has(e.name)&&!de(e,n.parent)&&s.push({start:e.start,end:e.end,name:e.name}),t({parent:e})}});if(0===s.length)return r.slice(e,n);s.sort(((e,n)=>e.start-n.start));let a="",c=e;for(const e of s)a+=r.slice(c,e.start),a+=ye(o.get(e.name)),c=e.end;return a+=r.slice(c,n),a}function de(e,n){return!!n&&("MemberExpression"===n.type&&n.property===e&&!n.computed||("Property"===n.type&&n.key===e&&!n.computed&&!0!==n.shorthand||("ImportSpecifier"===n.type||"ImportDefaultSpecifier"===n.type||"ImportNamespaceSpecifier"===n.type||"ExportSpecifier"===n.type)))}function me(e,n,t){const o=e.indexOf(n),r=e[o+1],s=e[o-1];r?t.remove(n.start,r.start):s?t.remove(s.end,n.end):t.remove(n.start,n.end)}function ve(e,n,t){const o=e.typeAnnotation?.typeAnnotation?.members??[],r=o.findIndex((e=>"Identifier"===e.key?.type&&e.key.name===n));if(-1===r)return;const s=o[r],a=o[r+1],i=o[r-1];a?t.remove(s.start,a.start):i?t.remove(i.end,s.end):t.remove(s.start,s.end)}function he(e,n,t){i(e.ast.fragment,null,{Component(o,{next:r}){const s=o.name?e.imports.get(o.name):void 0,a=s?n.get(s):void 0;if(a&&a.size>0)for(const n of o.attributes??[])"Attribute"===n.type&&n.name&&a.has(n.name)&&we(n.value)&&ce(e.code,n,t);r()}})}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 ye(e){return void 0===e?"undefined":JSON.stringify(e)}const ge={enabled:!1,maxVariants:8,minSavings:0};function ke(e,n,t=ge,r=[]){const s=new Map,a=[];if(!t.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&&k(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=Se(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const l=Ie(s,a,i);if(l===Ee(s,a)){u.add(r.childId);continue}const f=c.get(r.childId);f?f.push({owner:t.id,node:r.node,shape:i,code:l}):c.set(r.childId,[{owner:t.id,node:r.node,shape:i,code:l}])}}const l=function(e,n){const t=new Map;for(const o of e.values()){const e=n.get(o.id);t.set(o.id,e.bail?o.code:Ee(o,e))}return t}(e,n),f=new Map;for(const n of e.values())f.set(n.id,be(l.get(n.id),n));const p=new Set;for(const e of f.values())for(const n of e)p.add(n);const d=(Array.isArray(r)?r:[r]).filter((n=>e.has(n))).filter((e=>!p.has(e))),m=new Map,v=(e,n)=>{const t=m.get(n);if(void 0!==t)return t;let r;try{const{js:t}=o(n,{generate:"client",dev:!1,filename:e});r=t.code.length}catch{r=null}return null!==r&&m.set(n,r),r},h=new Set;for(const e of c.keys())u.has(e)||h.add(e);for(const[n,o]of c){if(u.has(n))continue;if(o.some((e=>e.owner!==n&&h.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>=t.maxVariants){c=!0;break}const o=`${n}::v${i.length}`;r.set(e.code,o),i.push({id:o,code:e.code})}if(!c&&xe(n,i,e,l,f,d,v,t.minSavings)){for(const e of i){const t=o.find((n=>n.code===e.code));s.set(e.id,{id:e.id,childId:n,code:e.code,foldedProps:t.shape})}for(const e of o)a.push({owner:e.owner,childId:n,node:e.node,variantId:r.get(e.code),foldedProps:e.shape})}}return{variants:s,bindings:a}}function xe(e,n,t,o,r,s,a,i){const c=t.get(e),u=new Map;for(const e of n)u.set(e.id,be(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 n of r.get(e)??[])f.push(n)}}let p=0;for(const e of l){const n=a(e,o.get(e));if(null===n)return!1;p+=n}const d=n.map((e=>e.id)),m=new Set,v=new Set,h=n=>n===e?{comps:[],vars:d}:{comps:[n],vars:[]},w=[],y=[];for(const e of s){const n=h(e);w.push(...n.comps),y.push(...n.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 n of r.get(e)??[]){const e=h(n);w.push(...e.comps),y.push(...e.vars)}continue}const e=y.pop();if(!v.has(e)){v.add(e);for(const n of u.get(e)??[]){const e=h(n);w.push(...e.comps),y.push(...e.vars)}}}let g=0;for(const e of m){const n=a(e,o.get(e));if(null===n)return!1;g+=n}for(const e of v){const t=n.find((n=>n.id===e)).code,o=a(e,t);if(null===o)return!1;g+=o}return g<p*(1-i)}function be(e,n){let t;try{t=a(e,n.id)}catch{return[]}const o=[];return i(t.fragment,null,{Component(e,{next:t}){const r=e.name?n.imports.get(e.name):void 0;r&&o.push(r),t()}}),o}function Se(e,n,t){const o=O(e),r=new Map;for(const e of n.props??[])r.set(e.name,e);const s=new Map;for(const[e,a]of o.explicit)r.has(e)&&(t.constFold.has(e)||T(n,e)||!a.dynamic&&a.afterLastSpread&&s.set(e,a.value));return s}function Ie(e,n,t){const o=new Map(n.constFold);for(const[e,n]of t)o.set(e,n);const r=new Map;for(const[e,t]of n.narrow)o.has(e)||r.set(e,t);const a=new s(e.code);return fe(e,o,r,n,a),a.toString()}const Me=new WeakMap;function Ee(e,n){const t=Me.get(e);if(void 0!==t)return t;const o=new s(e.code);fe(e,n.constFold,n.narrow,n,o);const r=o.toString();return Me.set(e,r),r}const Ae=(e,t)=>e.startsWith(".")?n.resolve(n.dirname(t),e):null;function $e(t){const o=[];let r;try{r=e.readdirSync(t,{withFileTypes:!0})}catch{return o}for(const e of r){if("node_modules"===e.name||e.name.startsWith("."))continue;const r=n.join(t,e.name);e.isDirectory()?o.push(...$e(r)):e.isFile()&&e.name.endsWith(".svelte")&&o.push(r)}return o}const Ce="shaker_variant";function Fe(t={}){const o=function(e){if(2!==e.level||!e.monomorphize)return ge;const n="object"==typeof e.monomorphize?e.monomorphize:{};return{...ge,enabled:!0,...n}}(t);let r={},s=new Map,a=process.cwd();const i=e=>{const n=e.lastIndexOf("::v"),t=e.slice(0,n),o=e.slice(n+3);return`${t}?${Ce}=${o}`};return{name:"vite-plugin-svelte-shaker",enforce:"pre",apply:"build",configResolved(e){a=e.root},async buildStart(){const c=(t.include??["."]).map((e=>n.resolve(a,e))).flatMap($e);if(0===c.length)return r={},void(s=new Map);const u=n=>e.readFileSync(n,"utf-8");if(!o.enabled)return r=await async function(e,n,t){const{models:o,plans:r}=await E(e,n,t);return oe(o,r)}(c,Ae,u),void(s=new Map);const l=await async function(e,n,t,o=ge,r=(e=>e)){const{models:s,plans:a}=await E(e,n,t),i=ke(s,a,o,e);return{files:0===i.bindings.length?oe(s,a):ae(s,a,i.bindings.map((e=>({owner:e.owner,node:e.node,variantId:e.variantId,foldedProps:e.foldedProps}))),r),mono:i}}(c,Ae,u,o,i);r=l.files,s=new Map;for(const e of l.mono.variants.values())s.set(i(e.id),e.code)},resolveId(e,t){if(!e.includes(`${Ce}=`))return null;const[o,r]=e.split("?");if(!o||!o.endsWith(".svelte"))return null;const s=t?n.dirname(t.split("?")[0]):a;return`${o.startsWith(".")?n.resolve(s,o):o}?${r}`},load:e=>e.includes(`${Ce}=`)?s.get(e)??null:null,transform(e,n){if(n.includes(`${Ce}=`))return null;if(n.includes("svelte&type="))return null;const t=n.split("?")[0];if(!t.endsWith(".svelte"))return null;const o=r[t];return null==o||o===e?null:o}}}export{Fe 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";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 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 c(e,null,{IfBlock(e,{next:r}){if(e.elseif||b(e,o))return;const s=v(e,t,n);for(const e of s.removed)o.push(e);s.recurse&&r()}}),o}const S=e=>e.endsWith(".svelte"),I=10,M="escapes as value (e.g. <svelte:component this={X}>)",F="rendered through a barrel/named import (call sites unobservable)";async function C(e,t,n){return E(await A(e,t,n))}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,N(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)}const r=new Set;for(const e of n.values())for(const t of e.barrelChildIds)r.add(t);for(const e of r){const t=n.get(e);t&&!t.bailReasons.includes(F)&&t.bailReasons.push(F)}let s=D(n,$(n,new Map));for(let e=0;e<I;e++){const e=D(n,$(n,O(n,s)));if(P(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function A(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=[];for(const o of H(f)){if("default"===o.imported&&S(o.value)){const n=await t(o.value,e);n&&(a.push({from:e,local:o.local,to:n,kind:"default-svelte"}),p.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 m=T(l,d);for(const e of[...p,...m])u.has(e)||(u.add(e),c.push(e))}return{files:s,edges:a,entries:r}}function $(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 D(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 P(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(!z(o.constFold,e.constFold))return!1;if(!R(o.narrow,e.narrow))return!1}return!0}function z(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 R(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=x(o.ast.fragment,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function N(e,t,n){const{id:o,code:r}=e,s=i(o,r,n),a=new Map,u=new Map;for(const e of t)"default-svelte"===e.kind?a.set(e.local,e.to):u.set(e.local,e.to);const l=[];c(s.fragment,null,{SvelteOptions(e,{next:t}){for(const t of e.attributes??[])"Attribute"!==t.type||"accessors"!==t.name&&"customElement"!==t.name||l.push(`<svelte:options ${t.name}>`);t()}});let f,d,p=null,m=!1;const h=new Set,v=s.instance;if(v){for(const e of H(v))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,d=e.pattern,e.sharesStatement&&l.push("$props() shares a multi-declarator statement"),p=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){m=!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;p.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),g=T(s,u),{shadowedNames:y,debugNames:k}=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??[])L(t,o);t()}});return c(e.fragment,null,{EachBlock(e,{next:t}){L(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??[])L(t,o);t()},AwaitBlock(e,{next:t}){L(e.value,o),L(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??[])L(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,f),b=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};c(e.fragment,{parent:null},{_(e,{state:t,next:o}){"Identifier"===e.type&&e.name&&n.has(e.name)&&j(e,t.parent)&&r(e.name),o({parent:e})}}),e.instance&&c(e.instance,{parent:null},{_(e,{state:n,next:o}){"Identifier"===e.type&&e.name&&t.has(e.name)&&j(e,n.parent)&&!B(n.parent)&&r(e.name),o({parent:e})}});return o}(s,a,h);return{id:o,code:r,ast:s,imports:a,props:p,propsDeclaration:f,propsPattern:d,hasRestProp:m,childCalls:w,shadowedNames:y,debugNames:k,escapedComponents:b,barrelChildIds:g,bailReasons:l}}function L(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?L(n.argument,t):"Property"===n.type&&L(n.value??n.key,t);return;case"ArrayPattern":for(const n of e.elements??[])L(n,t);return;case"AssignmentPattern":return void L(e.left,t);case"RestElement":return void L(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)&&!B(t)))}function B(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function T(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 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,_(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(S(e)||S(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>\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;for(const r of e.values()){const e=new s(r.code);n.set(r.id,e);const a=t.get(r.id);o.set(r.id,a.bail?new Set:he(r,a,e))}for(const t of e.values())be(t,o,n.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){return ve(e,t.constFold,t.narrow,t,n)}function ve(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;const s=e.code,a=[];!function(e,t,n,o,r,s){c(e,null,{IfBlock(e,{next:a}){if(e.elseif||b(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,s,r,a),function(e,t,n,o,r){if(0===t.size)return;c(e,null,{ConditionalExpression(e,{next:s}){if(b(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,s,r,a);const i=function(e,t,n){const o=new Map,r=r=>{r&&ge(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,a);for(const[e,n]of t){const t=Se(n);for(const n of i.get(e)??[])r.overwrite(n.start,n.end,n.head+t+n.tail)}const u=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??[];for(const o of e.props)t.has(o.name)&&(ye(r,o.property,n),e.propsPattern&&ke(e.propsPattern,o.name,n))}(e,u,r);return se(e,{...o,constFold:t,narrow:n},r),u}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+Se(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:"}"};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){const o=e.indexOf(t),r=e[o+1],s=e[o-1];r?n.remove(t.start,r.start):s?n.remove(s.end,t.end):n.remove(t.start,t.end)}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 be(e,t,n){c(e.ast.fragment,null,{Component(o,{next:r}){const s=o.name?e.imports.get(o.name):void 0,a=s?t.get(s):void 0;if(a&&a.size>0)for(const t of o.attributes??[])"Attribute"===t.type&&t.name&&a.has(t.name)&&xe(t.value)&&pe(e.code,t,n);r()}})}function xe(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 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&&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=Ee(r.node,s,a);if(0===i.size){u.add(r.childId);continue}const l=Ae(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,Ce(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,Ce(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 Ce(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 Ee(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)||V(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 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 A([...this.entries],this.resolve,t,n),{models:r,plans:s}=E(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}}const ze=(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 Oe="shaker_variant";function Ne(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}?${Oe}=${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,ze,(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");if(!o.enabled)return r=await async function(e,t,n){const{models:o,plans:r}=await C(e,t,n);return ce(o,r)}(i,ze,l),void(s=new Map);const f=await async function(e,t,n,o=Ie,r=(e=>e)){const{models:s,plans:a}=await C(e,t,n),i=Me(s,a,o,e);return{files: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,ze,l,o,u);r=f.files,s=new Map;for(const e of f.mono.variants.values())s.set(u(e.id),e.code)},resolveId(e,n){if(!e.includes(`${Oe}=`))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(`${Oe}=`)?s.get(e)??null:null,transform(e,t){if(t.includes(`${Oe}=`))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{Ne as shaker};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-shaker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Tree shaking for Svelte components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dead-code-elimination",
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
"@rollup/plugin-node-resolve": "15.2.3",
|
|
58
58
|
"@rollup/plugin-terser": "0.4.4",
|
|
59
59
|
"@rollup/plugin-typescript": "11.1.6",
|
|
60
|
+
"@rsvelte/compiler": "0.6.1",
|
|
60
61
|
"@sveltejs/vite-plugin-svelte": "^4",
|
|
61
62
|
"rollup": "4.18.0",
|
|
62
63
|
"svelte": "^5",
|
|
@@ -68,6 +69,7 @@
|
|
|
68
69
|
},
|
|
69
70
|
"scripts": {
|
|
70
71
|
"build": "rollup -c",
|
|
72
|
+
"build:wasm": "wasm-pack build engine-rs --target nodejs --release --out-dir pkg",
|
|
71
73
|
"dev": "rollup -cw",
|
|
72
74
|
"test:watch": "vitest",
|
|
73
75
|
"test": "vitest run"
|