vue-shaker 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/scan.js +1 -1
- package/dist/types/analyze.d.ts +9 -3
- package/dist/types/css.d.ts +5 -3
- package/dist/types/eval.d.ts +7 -1
- package/dist/types/ir.d.ts +8 -1
- package/dist/types/parse.d.ts +9 -0
- package/dist/types/scan.d.ts +8 -0
- package/dist/vite.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,62 @@
|
|
|
1
1
|
# vue-shaker
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 0d0217b: Shake real-world design systems, not just `import Child from './Child.vue'`.
|
|
8
|
+
|
|
9
|
+
The shaker now follows the component graphs apps actually have:
|
|
10
|
+
|
|
11
|
+
- **Workspace / alias resolution.** The Vite plugin resolves imports through
|
|
12
|
+
`this.resolve`, so a design system imported as a bare workspace package
|
|
13
|
+
(`@scope/design-system`) or via an alias (`@/components/...`) is followed
|
|
14
|
+
exactly as the real build does. Previously only `./relative` imports resolved,
|
|
15
|
+
so every workspace call site was invisible and the library was left unshaken.
|
|
16
|
+
Imports resolving into `node_modules` are skipped (published deps ship compiled
|
|
17
|
+
and can't be shaken).
|
|
18
|
+
- **Namespace-object components.** `import { Components } from 'pkg'; const { A,
|
|
19
|
+
group: { B } } = Components;` then `<A/> <B/>` — a very common way design
|
|
20
|
+
systems are consumed — is resolved to each leaf `.vue`, so those components fold
|
|
21
|
+
like a default import. If the namespace object also leaks as a runtime value (or
|
|
22
|
+
is rendered through `<component :is="NS.x">`), every component it exposes bails.
|
|
23
|
+
- **Non-destructured `defineProps`.** `const props = withDefaults(defineProps<{…}>(),
|
|
24
|
+
{…})` with `props.X` reads is now modeled (names from the type literal, defaults
|
|
25
|
+
from `withDefaults`), in addition to the destructured form.
|
|
26
|
+
- **Script-computed scoped CSS.** `:class="classes"` where `classes` is a
|
|
27
|
+
`const classes = computed(() => ['btn', \`btn-${props.variant}\`, …])`is followed
|
|
28
|
+
into the script and evaluated against the prop value sets, so unreachable scoped
|
|
29
|
+
rules are removed even when the class list is built in`<script>` (template
|
|
30
|
+
literals, ternaries, arrays, spreads of array literals).
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- 0d0217b: Close scoped-CSS-removal and call-site-completeness soundness holes that
|
|
35
|
+
real-world apps hit. Each could previously change observable behavior; the shaker
|
|
36
|
+
now keeps the affected rule / bails the affected component instead.
|
|
37
|
+
|
|
38
|
+
- **`<Transition>` / `<TransitionGroup>` classes.** Vue derives
|
|
39
|
+
`name-enter-active`, `name-leave-to`, … at runtime — never via `:class`, and
|
|
40
|
+
never during SSR — so the differential-SSR oracle can't catch their removal.
|
|
41
|
+
The class-set analysis now accounts for them (and bails on a dynamic name).
|
|
42
|
+
- **Functional pseudo-classes.** `:is()` / `:where()` / `:has()` are disjunctions
|
|
43
|
+
and `:not()` is a negation, but the dead-rule check flattened their inner
|
|
44
|
+
classes and treated them as all-required — wrongly removing live rules. Rules
|
|
45
|
+
using these pseudos are now kept.
|
|
46
|
+
- **Programmatic instantiation.** A `.vue` used as a value in a `.ts`/`.js` file
|
|
47
|
+
(`createApp(Dialog, props)`, `h(Dialog)`, a route `component:`) has call sites
|
|
48
|
+
no template enumerates. Files in scope are scanned and such components bail,
|
|
49
|
+
instead of their props being folded to the defaults the template-only view
|
|
50
|
+
implied. `<component :is="member.expr">` flags its root component too.
|
|
51
|
+
- **Custom directives & imperative classList.** A custom directive or a script
|
|
52
|
+
touching `classList`/`className`/`setAttribute('class', …)` can add classes the
|
|
53
|
+
analysis can't see, so CSS removal for that component bails.
|
|
54
|
+
- **Mutable class identifiers.** `:class="classes"` is followed only when
|
|
55
|
+
`classes` is `const … = computed(() => …)` (immutable binding, recomputed fresh)
|
|
56
|
+
— a `let`/`var` or plain-array `const` (mutable in place) is left unbounded.
|
|
57
|
+
- **Prop / import name collisions.** A prop whose name matches an imported binding
|
|
58
|
+
is no longer folded (the demote would redeclare the import).
|
|
59
|
+
|
|
3
60
|
## 0.1.1
|
|
4
61
|
|
|
5
62
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("@vue/compiler-sfc"),t=require("@babel/parser"),n=require("magic-string"),o=require("postcss"),r=require("postcss-selector-parser");const s=1,i=2,c=3,a=6,u=7,f=1,l=e=>e.type===s&&e.tagType===f,p=["typescript"];function d(n,o){const{descriptor:r}=e.parse(n,{filename:o}),s=r;let i;if(s.scriptSetup){const e=s.scriptSetup,n=e.attrs?.lang;i={content:e.content,base:e.loc.start.offset,endOffset:e.loc.end.offset,ast:(c=e.content,t.parse(c,{sourceType:"module",plugins:[...p]}).program),lang:"string"==typeof n?n:void 0}}var c;const a=(s.styles??[]).map(e=>({content:e.content,base:e.loc.start.offset,scoped:!0===e.scoped}));return{id:o,code:n,template:s.template?.ast??void 0,scriptSetup:i,hasPlainScript:null!=s.script,styles:a}}function m(e,t,n){if(!n)return d(t,e);const o=n.get(e);if(o&&o.code===t)return o.sfc;const r=d(t,e);return n.set(e,{code:t,sfc:r}),r}function y(e){if(null!=e&&""!==e.trim())try{return t.parseExpression(e,{plugins:[...p]})}catch{return}}function v(e){if(e)return"string"==typeof e.content?e.content:void 0}function w(e,t){if(!e)return;if(!1!==t(e))for(const n of e.children??[])w(n,t)}function g(e,t){if(!e)return;const n=e.children;if(n){t(n,e);for(const e of n)g(e,t)}}function h(e,t,n=null){if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n);for(const n of Object.values(e))if(Array.isArray(n))for(const o of n)h(o,t,e);else n&&"object"==typeof n&&"string"==typeof n.type&&h(n,t,e)}}const k={known:!1};function S(e,t){if(!e)return k;switch(e.type){case"Literal":case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return{known:!0,value:e.value};case"NullLiteral":return{known:!0,value:null};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:k}case"UnaryExpression":{const n=S(e.argument,t);if(!n.known)return k;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 k}}case"LogicalExpression":{const n=S(e.left,t);if(!n.known)return k;switch(e.operator){case"&&":return n.value?S(e.right,t):n;case"||":return n.value?n:S(e.right,t);case"??":return null===n.value||void 0===n.value?S(e.right,t):n;default:return k}}case"BinaryExpression":{const n=S(e.left,t),o=S(e.right,t);if(!n.known||!o.known)return k;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 k}}default:return k}}function b(e,t,n){const o=S(e,t);if(o.known)return o;const r=x(e,t,n);return"unknown"===r?k:{known:!0,value:r}}function x(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?P(x(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=x(e.left,t,n),i=()=>x(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=i(),!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,i()):"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=I(e,o),i=S(t,n);if(s&&i.known)return E(s,i.value,r);const c=I(t,o),a=S(e,n);return c&&a.known?E(c,a.value,r):a.known&&i.known?r?a.value==i.value:a.value===i.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?P(s):s}return"unknown"}default:return"unknown"}var o,r}function I(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function E(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){if(e.type===s)for(const t of e.props??[])if(t.type===u){if("if"===t.name)return{dir:t,kind:"if"};if("else-if"===t.name)return{dir:t,kind:"else-if"};if("else"===t.name)return{dir:t,kind:"else"}}}const L=e=>e.type===i&&"string"==typeof e.content&&""===e.content.trim()||e.type===c;function D(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n],r=M(o);if(!r||"if"!==r.kind)continue;const s=[R(o,r.dir,"if")];let i=n+1;for(;i<e.length;i++){const t=e[i];if(L(t))continue;const n=M(t);if(!n||"if"===n.kind)break;if(s.push(R(t,n.dir,n.kind)),"else"===n.kind){i++;break}}t.push(s),n=i-1}return t}function R(e,t,n){return{el:e,dir:t,kind:n,testAst:"else"===n?void 0:y(v(t.exp))}}function j(e,t,n){const o=e.map(e=>"else"===e.kind?{known:!0,value:!0}:b(e.testAst,t,n)),r=e=>e.known&&Boolean(e.value),s=e=>e.known&&!e.value,i=e=>[e.el.loc.start.offset,e.el.loc.end.offset],c=e=>[e.dir.loc.start.offset,e.dir.loc.end.offset];let a=!0;for(let t=0;t<e.length;t++){if(r(o[t])&&a){const n=[];for(let o=0;o<e.length;o++)o!==t&&n.push(i(e[o]));return{removed:n,dirRemovals:[c(e[t])]}}s(o[t])||(a=!1)}const u=o.findIndex(e=>!s(e));if(-1===u)return{removed:e.map(i),dirRemovals:[]};const f=[];for(let t=0;t<u;t++)f.push(i(e[t]));for(let t=u+1;t<e.length;t++)s(o[t])&&f.push(i(e[t]));if(0===u)return{removed:f,dirRemovals:[]};const l=e[u];if("else"===l.kind)return{removed:f,dirRemovals:[c(l)]};const p=l.dir.loc.start.offset;return{removed:f,dirRemovals:[],promotion:{from:p,to:p+9,text:"v-if"}}}function z(e,t){const n=e.loc;return!!n&&function(e,t){return t.some(([t,n])=>e[0]>=t&&e[1]<=n)}([n.start.offset,n.end.offset],t)}function O(e,t,n){if(!e||0===t.size&&0===n.size)return[];const o=[];return g(e,e=>{for(const r of D(e)){if(z(r[0].el,o))continue;const e=j(r,t,n);for(const t of e.removed)o.push(t)}}),o}const A=e=>e.endsWith(".vue"),C='escapes as value (e.g. <component :is="X">)',F="rendered through a barrel/named import (call sites unobservable)";function N(e){return e.sfc.scriptSetup?.base??0}async function q(e,t,n){return B(await $(e,t,n))}function B(e,t){const n=function(e,t){const n=new Map;for(const t of e.edges){const e=n.get(t.from);e?e.push(t):n.set(t.from,[t])}const o=new Map;for(const r of e.files)o.set(r.id,G(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(C)&&t.bailReasons.push(C)}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=U(n,T(n,new Map));for(let e=0;e<10;e++){const e=U(n,T(n,X(n,s)));if(V(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function $(e,t,n,o){const r=Array.isArray(e)?[...e]:[e],s=[],i=[],c=[...r],a=new Set(c);for(;c.length>0;){const e=c.shift();let r,u;try{r=await n(e)}catch{continue}s.push({id:e,code:r});try{u=m(e,r,o)}catch{continue}if(!u.scriptSetup)continue;const f=new Map,l=[];for(const o of ue(u.scriptSetup.ast)){if("default"===o.imported&&A(o.value)){const n=await t(o.value,e);n&&(i.push({from:e,local:o.local,to:n,kind:"default-vue"}),l.push(n));continue}const r=await de(o.value,o.imported,e,t,n);r&&(i.push({from:e,local:o.local,to:r,kind:"barrel"}),f.set(o.local,r))}const p=Z(u.template,f);for(const e of[...l,...p])a.has(e)||(a.add(e),c.push(e))}return{files:s,edges:i,entries:r}}function T(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)??[];N(n);for(const t of n.childCalls)e.length>0&&z(t.node,e)||o(t.childId).sites.push(ee(t.node))}return n}function U(e,t){const n=new Map;for(const o of e.values())n.set(o.id,ie(o,t.get(o.id)));return n}function V(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(!J(o.constFold,e.constFold))return!1;if(!W(o.narrow,e.narrow))return!1}return!0}function J(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 W(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 X(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=O(o.sfc.template,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function G(e,t,n){const{id:o,code:r}=e,i=m(o,r,n),c=new Map,a=new Map;for(const e of t)"default-vue"===e.kind?c.set(e.local,e.to):a.set(e.local,e.to);const f=[];if(i.scriptSetup)for(const e of function(e){const t=new Set;return h(e,e=>{"CallExpression"===e.type&&"Identifier"===e.callee?.type&&e.callee.name&&t.add(e.callee.name)}),t}(i.scriptSetup.ast))"defineExpose"===e&&f.push("defineExpose() (public instance surface)");let p,d,g,k=null,S=!1,b=!1;const x=new Set;if(i.scriptSetup){for(const e of ue(i.scriptSetup.ast))x.add(e.local);const e=function(e){const t=e.body??[];for(const e of t)if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("ObjectPattern"!==o?.type||"CallExpression"!==n?.type)continue;const r="Identifier"===n.callee?.type?n.callee.name:void 0;if("defineProps"===r)return{declaration:e,pattern:o,definePropsCall:n,withDefaults:new Map,sharesStatement:(e.declarations?.length??1)>1};if("withDefaults"===r){const t=n.arguments?.[0];if("CallExpression"!==t?.type||"Identifier"!==t.callee?.type)continue;if("defineProps"!==t.callee.name)continue;return{declaration:e,pattern:o,definePropsCall:t,withDefaults:ye(n.arguments?.[1]),sharesStatement:(e.declarations?.length??1)>1}}}return null}(i.scriptSetup.ast);if(e){d=e.declaration,p=e.pattern,g=e.definePropsCall,b=e.sharesStatement,e.sharesStatement&&f.push("defineProps() shares a multi-declarator statement"),k=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){S=!0;continue}if("ObjectProperty"!==t.type)continue;const n=t.key;if("Identifier"!==n?.type||!n.name)continue;const o=t.value,r="AssignmentPattern"===o?.type?o.right:void 0;k.push({name:n.name,property:t,defaultExpr:r??e.withDefaults.get(n.name)})}}}const I=function(e,t){const n=[];return w(e,e=>{if(l(e)&&e.tag){const o=t.get(e.tag);o&&n.push({childId:o,node:e})}}),n}(i.template,c),E=Z(i.template,a),P=function(e){const t=new Set;w(e.template,e=>{if(e.type===s)for(const n of e.props??[])n.type===u&&("for"===n.name?H(v(n.exp),t):"slot"===n.name&&K(v(n.exp),t))});const n=e.scriptSetup;n&&h(n.ast,e=>{if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])Q(n,t)});return t}(i),M=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};w(e.template,e=>{if(e.type===s)for(const t of e.props??[])if(t.type===u&&"bind"===t.name&&"is"===_(t.arg)){const e=y(v(t.exp));"Identifier"===e?.type&&r(e.name)}});const i=e.scriptSetup;i&&h(i.ast,(e,o)=>{"Identifier"===e.type&&e.name&&t.has(e.name)&&n.has(e.name)&&function(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!!("ObjectProperty"!==t.type&&"Property"!==t.type||t.key!==e||t.computed||!0===t.shorthand)&&!Y(t)))}(e,o)&&!Y(o)&&r(e.name)});return o}(i,c,x);return{id:o,code:r,sfc:i,imports:c,props:k,propsPattern:p,propsDeclaration:d,definePropsCall:g,hasRestProp:S,sharesStatement:b,childCalls:I,shadowedNames:P,escapedComponents:M,barrelChildIds:E,bailReasons:f}}function H(e,t){if(!e)return;const n=/^(.*?)\s+(?:in|of)\s+/s.exec(e),o=(n?n[1]:e).trim();o&&K(o,t)}function K(e,t){if(!e)return;const n=y(`(${e}) => 0`);if("ArrowFunctionExpression"!==n?.type){const n=y(e);return void(n&&Q(n,t))}for(const e of n.params??[])Q(e,t)}function Q(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectExpression":case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?Q(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||Q(n.value??n.key,t);return;case"ArrayExpression":case"ArrayPattern":for(const n of e.elements??[])Q(n,t);return;case"AssignmentPattern":return void Q(e.left,t);case"RestElement":return void Q(e.argument,t);default:return}}function Y(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function Z(e,t){const n=new Set;return 0===t.size||w(e,e=>{if(l(e)&&e.tag){const o=t.get(e.tag);o&&n.add(o)}}),n}function _(e){return v(e)}function ee(e,t){const n=e.props??[];let o=-1;for(let e=0;e<n.length;e++){const t=n[e];t.type===u&&"bind"===t.name&&te(t)&&(o=e)}const r=new Map;for(let e=0;e<n.length;e++){const t=n[e],s=e>o;if(t.type===a){if(!t.name)continue;const e=oe(t);r.set(t.name,{value:e,dynamic:!1,afterLastSpread:s});continue}if(t.type===u){if("bind"===t.name){if(te(t))continue;const e=v(t.arg);if(!e)continue;const n=re(v(t.exp));r.set(e,n.known?{value:n.value,dynamic:!1,afterLastSpread:s}:ne(s));continue}if("model"===t.name){const e=v(t.arg)??"modelValue";r.set(e,ne(s));continue}}}return{hadSpread:o>=0,explicit:r}}function te(e){return null==e.arg||null==v(e.arg)}function ne(e){return{value:void 0,dynamic:!0,afterLastSpread:e}}function oe(e){const t=e.value;return!t||("string"==typeof t.content?t.content:"")}function re(e){const t=y(e);return t?S(t,new Map):{known:!1}}function se(e,t){return e.shadowedNames.has(t)}function ie(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(se(e,t.name))continue;const r=ce(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 ce(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 i=ae(e.defaultExpr);i.known?s(i.value):o=!0}return{values:n,dynamic:o,top:r}}function ae(e){return e?S(e,new Map):{known:!0,value:void 0}}function*ue(e){const t=e.body??[];for(const e of t){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:fe(n)??e}))}}}function fe(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"StringLiteral"===t?.type&&"string"==typeof t.value?t.value:void 0}function le(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}const pe=8;async function de(e,t,n,o,r,s=0){if(s>pe)return null;const i=await o(e,n);if(!i)return null;if(A(e)||A(i))return"default"===t||"*"===t?i:null;let c;try{c=await r(i)}catch{return null}const a=function(e){try{const t=d(`<script setup>\n${e}\n<\/script>`,"barrel.vue");return t.scriptSetup?.ast.body??null}catch{return null}}(c);if(!a)return null;for(const e of a)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(le(n.exported)===t)return de(String(e.source.value),le(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 de(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(le(n.exported)!==t)continue;const e=le(n.local);if(!e)continue;const c=me(a,e);return c?de(c.value,c.imported,i,o,r,s+1):null}return null}function me(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:fe(o)??t}}}return null}function ye(e){const t=new Map;if("ObjectExpression"!==e?.type)return t;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)continue;const e=n.key,o="Identifier"===e?.type?e.name:void 0;o&&n.value&&t.set(o,n.value)}return t}const ve=Symbol("unbounded-class-source");function we(e,t,n){if(!e)return ve;if("ObjectExpression"===e.type){const t=new Set;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)return ve;const e=n.key,o="Identifier"===e?.type?e.name:"StringLiteral"===e?.type?e.value:void 0;if(null==o)return ve;for(const e of o.split(/\s+/))e&&t.add(e)}return t}if("ArrayExpression"===e.type){const o=new Set;for(const r of e.elements??[]){if(!r)continue;const e=we(r,t,n);if(e===ve)return ve;for(const t of e)o.add(t)}return o}const o=ge(e,t,n);if(o===ve)return ve;const r=new Set;for(const e of o)for(const t of e.split(/\s+/))t&&r.add(t);return r}function ge(e,t,n){if(!e)return ve;switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return new Set([String(e.value)]);case"Identifier":{const o=e.name??"";if(n.has(o)){const e=new Set;for(const t of n.get(o))e.add(String(t));return e}const r=S(e,t);return r.known?new Set([String(r.value)]):ve}case"TemplateLiteral":{const o=e.quasis??[],r=e.expressions??[];let s=[he(o[0])];for(let e=0;e<r.length;e++){const i=ge(r[e],t,n);if(i===ve)return ve;const c=he(o[e+1]),a=[];for(const e of s)for(const t of i)if(a.push(e+t+c),a.length>64)return ve;s=a}return new Set(s)}case"BinaryExpression":{if("+"!==e.operator)break;const o=ge(e.left,t,n),r=ge(e.right,t,n);if(o===ve||r===ve)return ve;const s=new Set;for(const e of o)for(const t of r)if(s.add(e+t),s.size>64)return ve;return s}case"ConditionalExpression":{const o=S(e.test,t);if(o.known)return ge(o.value?e.consequent:e.alternate,t,n);const r=ge(e.consequent,t,n),s=ge(e.alternate,t,n);return r===ve||s===ve?ve:new Set([...r,...s])}}const o=S(e,t);return o.known?new Set([String(o.value)]):ve}function he(e){const t=e?.value;return t?.cooked??t?.raw??""}function ke(e,t,n){const r=function(e,t){const n=new Set;let o=!1;const r=t.constFold,i=t.narrow;return w(e.sfc.template,e=>{if(e.type===s)for(const t of e.props??[]){if(t.type===a){if("class"===t.name){const e=t.value,o="string"==typeof e?.content?e.content:"";for(const e of o.split(/\s+/))e&&n.add(e)}continue}if(t.type!==u||"bind"!==t.name)continue;const e=v(t.arg);if(null==t.arg||null==e){o=!0;continue}if("class"!==e)continue;const s=we(y(v(t.exp)),r,i);if(s===ve)o=!0;else for(const e of s)n.add(e)}}),{classes:n,unbounded:o}}(e,t);if(r.unbounded)return 0;let i=0;for(const t of e.sfc.styles){if(!t.scoped)continue;let e;try{e=o.parse(t.content)}catch{continue}let s=0;if(e.each(e=>{"rule"===e.type&&Se(e,r.classes)&&(e.remove(),s+=1)}),s>0){const o=t.base+t.content.length;n.overwrite(t.base,o,e.toString()),i+=s}}return i}function Se(e,t){let n=!1;try{r(e=>{let o=!1,r=!0,s=!1;e.each(e=>{s=!0;const n=[];e.walkPseudos(e=>{const t=e.value;(t.includes("deep")||t.includes("global")||t.includes("slotted"))&&(o=!0)}),e.walkClasses(e=>{n.push(e.value)});n.some(e=>!t.has(e))||(r=!1)}),n=s&&r&&!o}).processSync(e.selector)}catch{return!1}return n}function be(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 i=t.get(s.id);r.set(s.id,i.bail?new Set:xe(s,i.constFold,i.narrow,i,e))}for(const t of e.values())Le(t,r,o.get(t.id));const s={};for(const t of e.values())s[t.id]=o.get(t.id).toString();return s}function xe(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;!function(e,t,n,o,r){const s=e.code;g(e.sfc.template,e=>{for(const i of D(e)){if(z(i[0].el,r))continue;const e=j(i,t,n);for(const[t,n]of e.removed)Ie(s,t,n,o),r.push([t,n]);for(const[t,n]of e.dirRemovals)Ie(s,t,n,o);if(e.promotion){const{from:t,to:n,text:r}=e.promotion;o.overwrite(t,n,r)}}})}(e,t,n,r,[]);const s=function(e,t,n){const o=new Set,r=e.sfc.scriptSetup;if(!(e.props&&0!==t.size&&r&&e.propsDeclaration&&e.propsPattern))return o;const s=r.base,i=e.props.filter(e=>t.has(e.name));if(0===i.length)return o;for(const e of i)o.add(e.name);const c=i.map(e=>{return`const ${e.name} = ${n=t.get(e.name),void 0===n?"undefined":JSON.stringify(n)};`;var n}).join("\n"),a=e.props.filter(e=>!t.has(e.name)),u=s+(e.propsDeclaration.start??0);let f=s+(e.propsDeclaration.end??0);";"===e.code[f]&&(f+=1);if(0===a.length&&!e.hasRestProp)return n.overwrite(u,f,c),o;const l=e.propsPattern?.properties??[];for(const t of i)Ee(l,t.property,s,n,e.code),Pe(e.definePropsCall,t.name,s,n,e.code);return n.appendLeft(f,`\n${c}`),o}(e,t,r);return ke(e,{...o,constFold:t,narrow:n},r),s}function Ie(e,t,n,o){let r=t;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]);)r-=1;o.remove(r,n)}function Ee(e,t,n,o,r){const s=e.indexOf(t);Me(t,e[s-1],n,o,r)}function Pe(e,t,n,o,r){const s=e?.typeParameters?.params?.[0],i=s?.members??[],c=i.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);-1!==c&&Me(i[c],i[c-1],n,o,r)}function Me(e,t,n,o,r){const s=n+(e.start??0),i=n+(e.end??0);let c=i;for(;c<r.length&&(" "===r[c]||"\t"===r[c]);)c+=1;if(","===r[c]){let e=c+1;for(;e<r.length&&(" "===r[e]||"\t"===r[e]||"\n"===r[e]);)e+=1;return void o.remove(s,e)}t?o.remove(n+(t.end??0),i):o.remove(s,i)}function Le(e,t,n){const o=e.code;w(e.sfc.template,r=>{if(!l(r)||!r.tag)return;const s=e.imports.get(r.tag),i=s?t.get(s):void 0;if(i&&0!==i.size)for(const e of r.props??[])if(e.type===a)e.name&&i.has(e.name)&&Re(o,e,n);else if(e.type===u&&"bind"===e.name){const t=v(e.arg);t&&i.has(t)&&De(e)&&Re(o,e,n)}})}function De(e){const t=v(e.exp);if(null==t)return!1;const n=y(t);return null!=n&&S(n,new Map).known}function Re(e,t,n){const o=t.loc;let r=o.start.offset;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]||"\n"===e[r-1]);)r-=1;n.remove(r,o.end.offset)}exports.analyze=q,exports.analyzeInput=B,exports.buildAnalyzeInput=$,exports.transformAll=be,exports.vueShaker=async function(e,t,n){const{models:o,plans:r}=await q(e,t,n);return be(o,r)};
|
|
1
|
+
"use strict";var e=require("@vue/compiler-sfc"),t=require("@babel/parser"),n=require("magic-string"),o=require("postcss"),r=require("postcss-selector-parser");const s=1,i=2,a=3,c=6,u=7,f=1,l=e=>e.type===s&&e.tagType===f,p=["typescript"];function d(e){return t.parse(e,{sourceType:"module",plugins:[...p]}).program}function m(t,n){const{descriptor:o}=e.parse(t,{filename:n}),r=o;let s;if(r.scriptSetup){const e=r.scriptSetup,t=e.attrs?.lang;s={content:e.content,base:e.loc.start.offset,endOffset:e.loc.end.offset,ast:d(e.content),lang:"string"==typeof t?t:void 0}}const i=(r.styles??[]).map(e=>({content:e.content,base:e.loc.start.offset,scoped:!0===e.scoped}));return{id:n,code:t,template:r.template?.ast??void 0,scriptSetup:s,hasPlainScript:null!=r.script,styles:i}}function y(e,t,n){if(!n)return m(t,e);const o=n.get(e);if(o&&o.code===t)return o.sfc;const r=m(t,e);return n.set(e,{code:t,sfc:r}),r}function v(e){if(null!=e&&""!==e.trim())try{return t.parseExpression(e,{plugins:[...p]})}catch{return}}function w(e){if(e)return"string"==typeof e.content?e.content:void 0}function g(e,t){if(!e)return;if(!1!==t(e))for(const n of e.children??[])g(n,t)}function h(e,t){if(!e)return;const n=e.children;if(n){t(n,e);for(const e of n)h(e,t)}}function b(e,t,n=null){if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n);for(const n of Object.values(e))if(Array.isArray(n))for(const o of n)b(o,t,e);else n&&"object"==typeof n&&"string"==typeof n.type&&b(n,t,e)}}const S={known:!1};function k(e,t){return t&&"MemberExpression"===e.type&&!0!==e.computed&&"Identifier"===e.object?.type&&e.object.name===t&&"Identifier"===e.property?.type&&e.property.name?e.property.name:null}function x(e,t,n){if(!e)return S;switch(e.type){case"Literal":case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return{known:!0,value:e.value};case"NullLiteral":return{known:!0,value:null};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:S}case"MemberExpression":{const o=k(e,n);return null!=o&&t.has(o)?{known:!0,value:t.get(o)}:S}case"UnaryExpression":{const o=x(e.argument,t,n);if(!o.known)return S;const r=o.value;switch(e.operator){case"!":return{known:!0,value:!r};case"-":return{known:!0,value:-r};case"+":return{known:!0,value:+r};case"typeof":return{known:!0,value:typeof r};case"void":return{known:!0,value:void 0};default:return S}}case"LogicalExpression":{const o=x(e.left,t,n);if(!o.known)return S;switch(e.operator){case"&&":return o.value?x(e.right,t,n):o;case"||":return o.value?o:x(e.right,t,n);case"??":return null===o.value||void 0===o.value?x(e.right,t,n):o;default:return S}}case"BinaryExpression":{const o=x(e.left,t,n),r=x(e.right,t,n);if(!o.known||!r.known)return S;const s=o.value,i=r.value;switch(e.operator){case"===":return{known:!0,value:s===i};case"!==":return{known:!0,value:s!==i};case"==":return{known:!0,value:s==i};case"!=":return{known:!0,value:s!=i};case"<":return{known:!0,value:s<i};case">":return{known:!0,value:s>i};case"<=":return{known:!0,value:s<=i};case">=":return{known:!0,value:s>=i};case"+":return{known:!0,value:s+i};case"-":return{known:!0,value:s-i};case"*":return{known:!0,value:s*i};case"/":return{known:!0,value:s/i};case"%":return{known:!0,value:s%i};default:return S}}default:return S}}function E(e,t,n){const o=x(e,t);if(o.known)return o;const r=I(e,t,n);return"unknown"===r?S:{known:!0,value:r}}function I(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?D(I(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=I(e.left,t,n),i=()=>I(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=i(),!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,i()):"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),i=x(t,n);if(s&&i.known)return j(s,i.value,r);const a=P(t,o),c=x(e,n);return a&&c.known?j(a,c.value,r):c.known&&i.known?r?c.value==i.value:c.value===i.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?D(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 j(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 M(e){if(e.type===s)for(const t of e.props??[])if(t.type===u){if("if"===t.name)return{dir:t,kind:"if"};if("else-if"===t.name)return{dir:t,kind:"else-if"};if("else"===t.name)return{dir:t,kind:"else"}}}const L=e=>e.type===i&&"string"==typeof e.content&&""===e.content.trim()||e.type===a;function O(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n],r=M(o);if(!r||"if"!==r.kind)continue;const s=[A(o,r.dir,"if")];let i=n+1;for(;i<e.length;i++){const t=e[i];if(L(t))continue;const n=M(t);if(!n||"if"===n.kind)break;if(s.push(A(t,n.dir,n.kind)),"else"===n.kind){i++;break}}t.push(s),n=i-1}return t}function A(e,t,n){return{el:e,dir:t,kind:n,testAst:"else"===n?void 0:v(w(t.exp))}}function C(e,t,n){const o=e.map(e=>"else"===e.kind?{known:!0,value:!0}:E(e.testAst,t,n)),r=e=>e.known&&Boolean(e.value),s=e=>e.known&&!e.value,i=e=>[e.el.loc.start.offset,e.el.loc.end.offset],a=e=>[e.dir.loc.start.offset,e.dir.loc.end.offset];let c=!0;for(let t=0;t<e.length;t++){if(r(o[t])&&c){const n=[];for(let o=0;o<e.length;o++)o!==t&&n.push(i(e[o]));return{removed:n,dirRemovals:[a(e[t])]}}s(o[t])||(c=!1)}const u=o.findIndex(e=>!s(e));if(-1===u)return{removed:e.map(i),dirRemovals:[]};const f=[];for(let t=0;t<u;t++)f.push(i(e[t]));for(let t=u+1;t<e.length;t++)s(o[t])&&f.push(i(e[t]));if(0===u)return{removed:f,dirRemovals:[]};const l=e[u];if("else"===l.kind)return{removed:f,dirRemovals:[a(l)]};const p=l.dir.loc.start.offset;return{removed:f,dirRemovals:[],promotion:{from:p,to:p+9,text:"v-if"}}}function R(e,t){const n=e.loc;return!!n&&function(e,t){return t.some(([t,n])=>e[0]>=t&&e[1]<=n)}([n.start.offset,n.end.offset],t)}function F(e,t,n){if(!e||0===t.size&&0===n.size)return[];const o=[];return h(e,e=>{for(const r of O(e)){if(R(r[0].el,o))continue;const e=C(r,t,n);for(const t of e.removed)o.push(t)}}),o}const z=e=>e.endsWith(".vue"),N='escapes as value (e.g. <component :is="X">)',q="rendered through a barrel/named import (call sites unobservable)",$="reached through a namespace object that also leaks as a value (call sites unobservable)";function B(e){return e.sfc.scriptSetup?.base??0}async function V(e,t,n,o){return T(await W(e,t,n,void 0,o))}function T(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,Q(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(N)&&t.bailReasons.push(N)}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(q)&&t.bailReasons.push(q)}for(const t of e.forcedBails??[]){const e=n.get(t);e&&!e.bailReasons.includes($)&&e.bailReasons.push($)}let s=J(n,U(n,new Map));for(let e=0;e<10;e++){const e=J(n,U(n,K(n,s)));if(X(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function W(e,t,n,o,r){const s=Array.isArray(e)?[...e]:[e],i=[],a=[],c=new Set,u=new Map,f=[...s],l=new Set(f);for(;f.length>0;){const e=f.shift();let r,s;try{r=await n(e)}catch{continue}i.push({id:e,code:r});try{s=y(e,r,o)}catch{continue}if(!s.scriptSetup)continue;const p=s.scriptSetup.ast,d=[...Pe(p)],m=new Map;for(const e of d)m.set(e.local,e);const v=new Map,w=[];for(const o of d){if("default"===o.imported&&z(o.value)){const n=await t(o.value,e);n&&(a.push({from:e,local:o.local,to:n,kind:"default-vue"}),w.push(n));continue}const r=await Le(o.value,o.imported,e,t,n);r&&(a.push({from:e,local:o.local,to:r,kind:"barrel"}),v.set(o.local,r))}for(const[o,r]of ne(p)){const i=m.get(o);if(!i)continue;const f=await se(i.value,i.imported,e,t,n,u);if(!f)continue;if(r.unsafe||le(p,o,r.initNodes)||ve(s.template,o))for(const e of ue(f))c.add(e);else for(const t of r.bindings){const n=ce(f,t.path);n&&(a.push({from:e,local:t.local,to:n,kind:"namespace"}),w.push(n))}}const g=te(s.template,v);for(const e of[...w,...g])l.has(e)||(l.add(e),f.push(e))}const p=await Promise.all((r??[]).map(e=>async function(e,t,n){let o;try{o=await n(e)}catch{return[]}if(!o.includes(".vue"))return[];const r=function(e){try{return d(e)}catch{return null}}(o);if(!r)return[];const s=new Map;for(const e of r.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t&&z(t))for(const n of e.specifiers??[])"ImportDefaultSpecifier"!==n.type&&"ImportNamespaceSpecifier"!==n.type||!n.local?.name||s.set(n.local.name,t)}if(0===s.size)return[];const i=function(e){const t=new Set;return b(e,e=>{if("ObjectExpression"===e.type)for(const n of e.properties??[])"ObjectProperty"!==n.type&&"Property"!==n.type||!n.shorthand||(n.key&&t.add(n.key),n.value&&t.add(n.value))}),t}(r),a=new Set;pe(r,(e,t)=>{s.has(e)&&!i.has(t)&&a.add(e)});const c=await Promise.all([...a].map(n=>t(s.get(n),e)));return c.filter(e=>null!=e)}(e,t,n)));for(const e of p)for(const t of e)c.add(t);return{files:i,edges:a,entries:s,forcedBails:[...c]}}function U(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)??[];B(n);for(const t of n.childCalls)e.length>0&&R(t.node,e)||o(t.childId).sites.push(we(t.node))}return n}function J(e,t){const n=new Map;for(const o of e.values())n.set(o.id,xe(o,t.get(o.id)));return n}function X(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(!G(o.constFold,e.constFold))return!1;if(!H(o.narrow,e.narrow))return!1}return!0}function G(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 H(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 K(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=F(o.sfc.template,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function Q(e,t,n){const{id:o,code:r}=e,i=y(o,r,n),a=new Map,c=new Map,f=new Set;for(const e of t)"default-vue"===e.kind?a.set(e.local,e.to):"namespace"===e.kind?(a.set(e.local,e.to),f.add(e.local)):c.set(e.local,e.to);const p=[];if(i.scriptSetup)for(const e of function(e){const t=new Set;return b(e,e=>{"CallExpression"===e.type&&"Identifier"===e.callee?.type&&e.callee.name&&t.add(e.callee.name)}),t}(i.scriptSetup.ast))"defineExpose"===e&&p.push("defineExpose() (public instance surface)");let d,m,h,S,k=null,x=!1,E=!1;const I=new Set;if(i.scriptSetup){for(const e of Pe(i.scriptSetup.ast))I.add(e.local);for(const e of f)I.add(e);const e=function(e){const t=e.body??[];for(const e of t)if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=Ce(t.init);if(!n)continue;const o=t.id,r=(e.declarations?.length??1)>1;if("ObjectPattern"===o?.type)return{declaration:e,pattern:o,propsLocal:void 0,sharesStatement:r,...n};if("Identifier"===o?.type&&o.name)return{declaration:e,pattern:void 0,propsLocal:o.name,sharesStatement:r,...n}}return null}(i.scriptSetup.ast);if(e)if(h=e.declaration,d=e.pattern,m=e.propsLocal,S=e.definePropsCall,E=e.sharesStatement,e.sharesStatement&&p.push("defineProps() shares a multi-declarator statement"),e.pattern){k=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){x=!0;continue}if("ObjectProperty"!==t.type)continue;const n=t.key;if("Identifier"!==n?.type||!n.name)continue;const o=t.value,r="AssignmentPattern"===o?.type?o.right:void 0;k.push({name:n.name,property:t,defaultExpr:r??e.withDefaults.get(n.name)})}}else k=function(e,t){const n=[],o=e?.typeParameters?.params?.[0];for(const e of o?.members??[]){const o=e.key;"Identifier"===o?.type&&o.name&&n.push({name:o.name,property:e,defaultExpr:t.get(o.name)})}return n}(e.definePropsCall,e.withDefaults)}const P=function(e,t){const n=[];return g(e,e=>{if(l(e)&&e.tag){const o=t.get(e.tag);o&&n.push({childId:o,node:e})}}),n}(i.template,a),j=te(i.template,c),D=function(e){const t=new Set;g(e.template,e=>{if(e.type===s)for(const n of e.props??[])n.type===u&&("for"===n.name?Y(w(n.exp),t):"slot"===n.name&&Z(w(n.exp),t))});const n=e.scriptSetup;n&&b(n.ast,e=>{if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])_(n,t)});return t}(i);for(const e of I)D.add(e);const M=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};g(e.template,e=>{if(e.type===s)for(const t of e.props??[])if(t.type===u&&"bind"===t.name&&"is"===me(t.arg)){const e=v(w(t.exp));"Identifier"===e?.type?r(e.name):"MemberExpression"===e?.type&&r(ye(e))}});const i=e.scriptSetup;if(i){const e=function(e){const t=new Set;return b(e,e=>{if("VariableDeclarator"===e.type)de(e.id,t);else if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type||"ObjectMethod"===e.type||"ClassMethod"===e.type)for(const n of e.params??[])de(n,t);else"ImportDefaultSpecifier"!==e.type&&"ImportSpecifier"!==e.type&&"ImportNamespaceSpecifier"!==e.type||e.local&&t.add(e.local)}),t}(i.ast);b(i.ast,(o,s)=>{"Identifier"===o.type&&o.name&&t.has(o.name)&&n.has(o.name)&&function(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!!("ObjectProperty"!==t.type&&"Property"!==t.type||t.key!==e||t.computed||!0===t.shorthand)&&!ee(t)))}(o,s)&&!ee(s)&&!e.has(o)&&r(o.name)})}return o}(i,a,I);return{id:o,code:r,sfc:i,imports:a,props:k,propsPattern:d,propsLocal:m,propsDeclaration:h,definePropsCall:S,hasRestProp:x,sharesStatement:E,childCalls:P,shadowedNames:D,escapedComponents:M,barrelChildIds:j,bailReasons:p}}function Y(e,t){if(!e)return;const n=/^(.*?)\s+(?:in|of)\s+/s.exec(e),o=(n?n[1]:e).trim();o&&Z(o,t)}function Z(e,t){if(!e)return;const n=v(`(${e}) => 0`);if("ArrowFunctionExpression"!==n?.type){const n=v(e);return void(n&&_(n,t))}for(const e of n.params??[])_(e,t)}function _(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectExpression":case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?_(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||_(n.value??n.key,t);return;case"ArrayExpression":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 ee(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function te(e,t){const n=new Set;return 0===t.size||g(e,e=>{if(l(e)&&e.tag){const o=t.get(e.tag);o&&n.add(o)}}),n}function ne(e){const t=new Map;return b(e,e=>{if("VariableDeclarator"!==e.type)return;const n=e.init,o=e.id;if("Identifier"!==n?.type||!n.name||"ObjectPattern"!==o?.type)return;let r=t.get(n.name);r||(r={initNodes:new Set,bindings:[],unsafe:!1},t.set(n.name,r)),r.initNodes.add(n),oe(o,[],r)}),t}function oe(e,t,n){for(const o of e.properties??[]){if("RestElement"===o.type){n.unsafe=!0;continue}if("ObjectProperty"!==o.type&&"Property"!==o.type)continue;if(o.computed){n.unsafe=!0;continue}const e=re(o.key);if(!e){n.unsafe=!0;continue}const r=o.value??void 0;"Identifier"===r?.type&&r.name?n.bindings.push({path:[...t,e],local:r.name}):"ObjectPattern"===r?.type?oe(r,[...t,e],n):n.unsafe=!0}}function re(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}async function se(e,t,n,o,r,s){const i=await o(e,n);if(!i)return null;const a=`${i}\0${t}`,c=s.get(a);if(void 0!==c)return c;const u=await async function(e,t,n,o){let r;try{r=await o(e)}catch{return null}const s=Ae(r);if(!s)return null;const i=new Map;for(const e of s){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?i.set(e,{value:t,imported:"default"}):"ImportSpecifier"===n.type&&i.set(e,{value:t,imported:je(n)??e}))}}const a=function(e,t){for(const n of e){if("ExportNamedDeclaration"===n.type&&"VariableDeclaration"===n.declaration?.type){const e=ie(n.declaration,t);if(e)return e}if("VariableDeclaration"===n.type){const e=ie(n,t);if(e)return e}if("ExportDefaultDeclaration"===n.type&&"default"===t&&"ObjectExpression"===n.declaration?.type)return n.declaration}return null}(s,t);return a?ae(a,e,i,n):null}(i,t,o,r);return s.set(a,u),u}function ie(e,t){for(const n of e.declarations??[])if("Identifier"===n.id?.type&&n.id.name===t&&"ObjectExpression"===n.init?.type)return n.init;return null}async function ae(e,t,n,o){const r=new Map,s=await Promise.all((e.properties??[]).map(async e=>{if("ObjectProperty"!==e.type&&"Property"!==e.type)return null;if(e.computed)return null;const r=re(e.key);if(!r)return null;const s=e.value;if("Identifier"===s?.type&&s.name){const e=n.get(s.name);if(!e||"default"!==e.imported||!z(e.value))return null;const i=await o(e.value,t);return i?[r,i]:null}return"ObjectExpression"===s?.type?[r,await ae(s,t,n,o)]:null}));for(const e of s)e&&r.set(e[0],e[1]);return r}function ce(e,t){let n=e;for(const e of t){if("string"==typeof n)return null;const t=n.get(e);if(void 0===t)return null;n=t}return"string"==typeof n?n:null}function ue(e){const t=[];for(const n of e.values())"string"==typeof n?t.push(n):t.push(...ue(n));return t}const fe=new Set(["typeAnnotation","typeParameters","returnType","typeArguments"]);function le(e,t,n){let o=!1;return pe(e,(e,r)=>{e!==t||n.has(r)||(o=!0)}),o}function pe(e,t){if(e&&"object"==typeof e&&"string"==typeof e.type&&"ImportDeclaration"!==e.type)if("Identifier"!==e.type){for(const[n,o]of Object.entries(e))if(!fe.has(n))if(Array.isArray(o))for(const e of o)pe(e,t);else o&&"object"==typeof o&&"string"==typeof o.type&&pe(o,t)}else e.name&&t(e.name,e)}function de(e,t){if(e)switch(e.type){case"Identifier":return void t.add(e);case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?de(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||(de(n.value??n.key,t),n.shorthand&&n.key&&t.add(n.key));return;case"ArrayPattern":for(const n of e.elements??[])de(n,t);return;case"AssignmentPattern":return void de(e.left,t);case"RestElement":return void de(e.argument,t);default:return}}function me(e){return w(e)}function ye(e){let t=e;for(;"MemberExpression"===t?.type;)t=t.object;return"Identifier"===t?.type?t.name:void 0}function ve(e,t){let n=!1;return g(e,e=>{if(e.type===s)for(const o of e.props??[]){if(o.type!==u||"bind"!==o.name||"is"!==me(o.arg))continue;const e=v(w(o.exp));("Identifier"===e?.type?e.name===t:ye(e)===t)&&(n=!0)}}),n}function we(e,t){const n=e.props??[];let o=-1;for(let e=0;e<n.length;e++){const t=n[e];t.type===u&&"bind"===t.name&&ge(t)&&(o=e)}const r=new Map;for(let e=0;e<n.length;e++){const t=n[e],s=e>o;if(t.type===c){if(!t.name)continue;const e=be(t);r.set(t.name,{value:e,dynamic:!1,afterLastSpread:s});continue}if(t.type===u){if("bind"===t.name){if(ge(t))continue;const e=w(t.arg);if(!e)continue;const n=Se(w(t.exp));r.set(e,n.known?{value:n.value,dynamic:!1,afterLastSpread:s}:he(s));continue}if("model"===t.name){const e=w(t.arg)??"modelValue";r.set(e,he(s));continue}}}return{hadSpread:o>=0,explicit:r}}function ge(e){return null==e.arg||null==w(e.arg)}function he(e){return{value:void 0,dynamic:!0,afterLastSpread:e}}function be(e){const t=e.value;return!t||("string"==typeof t.content?t.content:"")}function Se(e){const t=v(e);return t?x(t,new Map):{known:!1}}function ke(e,t){return e.shadowedNames.has(t)}function xe(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(ke(e,t.name))continue;const r=Ee(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 Ee(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 i=Ie(e.defaultExpr);i.known?s(i.value):o=!0}return{values:n,dynamic:o,top:r}}function Ie(e){return e?x(e,new Map):{known:!0,value:void 0}}function*Pe(e){const t=e.body??[];for(const e of t){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:je(n)??e}))}}}function je(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"StringLiteral"===t?.type&&"string"==typeof t.value?t.value:void 0}function De(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}const Me=8;async function Le(e,t,n,o,r,s=0){if(s>Me)return null;const i=await o(e,n);if(!i)return null;if(z(e)||z(i))return"default"===t||"*"===t?i:null;let a;try{a=await r(i)}catch{return null}const c=Ae(a);if(!c)return null;for(const e of c)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(De(n.exported)===t)return Le(String(e.source.value),De(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 Le(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(De(n.exported)!==t)continue;const e=De(n.local);if(!e)continue;const a=Oe(c,e);return a?Le(a.value,a.imported,i,o,r,s+1):null}return null}function Oe(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:je(o)??t}}}return null}function Ae(e){try{const t=m(`<script setup>\n${e}\n<\/script>`,"barrel.vue");return t.scriptSetup?.ast.body??null}catch{return null}}function Ce(e){if("CallExpression"!==e?.type)return null;const t="Identifier"===e.callee?.type?e.callee.name:void 0;if("defineProps"===t)return{definePropsCall:e,withDefaults:new Map};if("withDefaults"===t){const t=e.arguments?.[0];return"CallExpression"!==t?.type||"Identifier"!==t.callee?.type||"defineProps"!==t.callee.name?null:{definePropsCall:t,withDefaults:Re(e.arguments?.[1])}}return null}function Re(e){const t=new Map;if("ObjectExpression"!==e?.type)return t;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)continue;const e=n.key,o="Identifier"===e?.type?e.name:void 0;o&&n.value&&t.set(o,n.value)}return t}const Fe=Symbol("unbounded-class-source");function ze(e,t){const n=new Set;let o=!1;const r={env:t.constFold,setEnv:t.narrow,propsLocal:e.propsLocal,resolveClassExpr:Te(e)};return g(e.sfc.template,e=>{if(e.type===s){(function(e){if(!e)return!1;const t=e.toLowerCase().replace(/-/g,"");return"transition"===t||"transitiongroup"===t})(e.tag)&&!function(e,t){let n="v";const o="transitiongroup"===(e.tag??"").toLowerCase().replace(/-/g,"");for(const o of e.props??[])if(o.type!==c){if(o.type===u&&"bind"===o.name){const e=w(o.arg);if(null==e||"name"===e||e.endsWith("Class")||e.endsWith("-class"))return!1}}else if("name"===o.name){const e=o.value;"string"==typeof e?.content&&(n=e.content)}else if(o.name&&o.name.endsWith("-class")){const e=o.value,n="string"==typeof e?.content?e.content:"";for(const e of n.split(/\s+/))e&&t.add(e)}for(const e of qe)t.add(`${n}-${e}`);o&&t.add(`${n}-move`);return!0}(e,n)&&(o=!0);for(const t of e.props??[]){if(t.type===u&&t.name&&!Ne.has(t.name)){o=!0;continue}if(t.type===c){if("class"===t.name){const e=t.value,o="string"==typeof e?.content?e.content:"";for(const e of o.split(/\s+/))e&&n.add(e)}continue}if(t.type!==u||"bind"!==t.name)continue;const e=w(t.arg);if(null==t.arg||null==e){o=!0;continue}if("class"!==e)continue;const s=$e(v(w(t.exp)),r);if(s===Fe)o=!0;else for(const e of s)n.add(e)}}}),function(e){const t=e.sfc.scriptSetup?.content;return!!t&&/\.classList\b|\.className\b|setAttribute\(\s*['"`]class/.test(t)}(e)&&(o=!0),{classes:n,unbounded:o}}const Ne=new Set(["if","else","else-if","for","bind","on","model","show","slot","html","text","once","memo","cloak","pre"]);const qe=["enter-from","enter-active","enter-to","leave-from","leave-active","leave-to","appear-from","appear-active","appear-to"];function $e(e,t){if(!e)return Fe;if("ObjectExpression"===e.type){const t=new Set;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)return Fe;const e=n.key,o="Identifier"===e?.type?e.name:"StringLiteral"===e?.type?e.value:void 0;if(null==o)return Fe;for(const e of o.split(/\s+/))e&&t.add(e)}return t}if("ArrayExpression"===e.type){const n=new Set;for(const o of e.elements??[]){if(!o)continue;const e=$e("SpreadElement"===o.type?o.argument:o,t);if(e===Fe)return Fe;for(const t of e)n.add(t)}return n}if("ConditionalExpression"===e.type){const n=x(e.test,t.env,t.propsLocal);if(n.known)return $e(n.value?e.consequent:e.alternate,t);const o=$e(e.consequent,t),r=$e(e.alternate,t);return o===Fe||r===Fe?Fe:new Set([...o,...r])}if("Identifier"===e.type&&e.name&&!function(e,t){return t.env.has(e)||t.setEnv.has(e)}(e.name,t)){const n=t.resolveClassExpr(e.name);if(!n)return Fe;const o=new Set;for(const e of n){const n=$e(e,t);if(n===Fe)return Fe;for(const e of n)o.add(e)}return o}const n=Be(e,t);if(n===Fe)return Fe;const o=new Set;for(const e of n)for(const t of e.split(/\s+/))t&&o.add(t);return o}function Be(e,t){if(!e)return Fe;switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return new Set([String(e.value)]);case"Identifier":return Ve(e.name??"",e,t);case"MemberExpression":{const n=k(e,t.propsLocal);return null==n?Fe:Ve(n,e,t)}case"TemplateLiteral":{const n=e.quasis??[],o=e.expressions??[];let r=[We(n[0])];for(let e=0;e<o.length;e++){const s=Be(o[e],t);if(s===Fe)return Fe;const i=We(n[e+1]),a=[];for(const e of r)for(const t of s)if(a.push(e+t+i),a.length>64)return Fe;r=a}return new Set(r)}case"BinaryExpression":{if("+"!==e.operator)break;const n=Be(e.left,t),o=Be(e.right,t);if(n===Fe||o===Fe)return Fe;const r=new Set;for(const e of n)for(const t of o)if(r.add(e+t),r.size>64)return Fe;return r}case"ConditionalExpression":{const n=x(e.test,t.env,t.propsLocal);if(n.known)return Be(n.value?e.consequent:e.alternate,t);const o=Be(e.consequent,t),r=Be(e.alternate,t);return o===Fe||r===Fe?Fe:new Set([...o,...r])}}const n=x(e,t.env,t.propsLocal);return n.known?new Set([String(n.value)]):Fe}function Ve(e,t,n){if(n.setEnv.has(e)){const t=new Set;for(const o of n.setEnv.get(e))t.add(String(o));return t}const o=x(t,n.env,n.propsLocal);return o.known?new Set([String(o.value)]):Fe}function Te(e){const t=e.sfc.scriptSetup;if(!t)return()=>null;const n=new Map;for(const e of t.ast.body??[])if("VariableDeclaration"===e.type&&"const"===e.kind)for(const t of e.declarations??[]){if("Identifier"!==t.id?.type||!t.id.name)continue;const e=t.init;if("CallExpression"!==e?.type||"Identifier"!==e.callee?.type)continue;if("computed"!==e.callee.name)continue;const o=e.arguments?.[0];"ArrowFunctionExpression"!==o?.type&&"FunctionExpression"!==o?.type||n.set(t.id.name,o)}return e=>{const t=n.get(e);return t?function(e){const t=e.body;if(!t)return[];if(!Array.isArray(t)&&"BlockStatement"!==t.type)return[t];const n=[],o=e=>{if(e&&"object"==typeof e&&"string"==typeof e.type&&"FunctionDeclaration"!==e.type&&"FunctionExpression"!==e.type&&"ArrowFunctionExpression"!==e.type)if("ReturnStatement"!==e.type)for(const t of Object.values(e))if(Array.isArray(t))for(const e of t)o(e);else t&&"object"==typeof t&&"string"==typeof t.type&&o(t);else e.argument&&n.push(e.argument)};return o(t),n}(t):null}}function We(e){const t=e?.value;return t?.cooked??t?.raw??""}const Ue=new Set([":is",":where",":not",":has",":matches",":-moz-any",":-webkit-any"]);function Je(e,t){let n=!1;try{r(e=>{let o=!1,r=!1,s=!0,i=!1;e.each(e=>{i=!0;const n=[];e.walkPseudos(e=>{const t=e.value;(t.includes("deep")||t.includes("global")||t.includes("slotted"))&&(o=!0),Ue.has(t)&&(r=!0)}),e.walkClasses(e=>{n.push(e.value)});n.some(e=>!t.has(e))||(s=!1)}),n=i&&s&&!o&&!r}).processSync(e.selector)}catch{return!1}return n}function Xe(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 i=t.get(s.id);r.set(s.id,i.bail?new Set:Ge(s,i.constFold,i.narrow,i,e))}for(const t of e.values())Ze(t,r,o.get(t.id));const s={};for(const t of e.values())s[t.id]=o.get(t.id).toString();return s}function Ge(e,t,n,r,s){if(0===t.size&&0===n.size)return new Set;!function(e,t,n,o,r){const s=e.code;h(e.sfc.template,e=>{for(const i of O(e)){if(R(i[0].el,r))continue;const e=C(i,t,n);for(const[t,n]of e.removed)He(s,t,n,o),r.push([t,n]);for(const[t,n]of e.dirRemovals)He(s,t,n,o);if(e.promotion){const{from:t,to:n,text:r}=e.promotion;o.overwrite(t,n,r)}}})}(e,t,n,s,[]);const i=function(e,t,n){const o=new Set,r=e.sfc.scriptSetup;if(!(e.props&&0!==t.size&&r&&e.propsDeclaration&&e.propsPattern))return o;const s=r.base,i=e.props.filter(e=>t.has(e.name));if(0===i.length)return o;for(const e of i)o.add(e.name);const a=i.map(e=>{return`const ${e.name} = ${n=t.get(e.name),void 0===n?"undefined":JSON.stringify(n)};`;var n}).join("\n"),c=e.props.filter(e=>!t.has(e.name)),u=s+(e.propsDeclaration.start??0);let f=s+(e.propsDeclaration.end??0);";"===e.code[f]&&(f+=1);if(0===c.length&&!e.hasRestProp)return n.overwrite(u,f,a),o;const l=e.propsPattern?.properties??[];for(const t of i)Ke(l,t.property,s,n,e.code),Qe(e.definePropsCall,t.name,s,n,e.code);return n.appendLeft(f,`\n${a}`),o}(e,t,s);return function(e,t,n){const r=ze(e,t);if(r.unbounded)return 0;let s=0;for(const t of e.sfc.styles){if(!t.scoped)continue;let e;try{e=o.parse(t.content)}catch{continue}let i=0;if(e.each(e=>{"rule"===e.type&&Je(e,r.classes)&&(e.remove(),i+=1)}),i>0){const o=t.base+t.content.length;n.overwrite(t.base,o,e.toString()),s+=i}}}(e,{...r,constFold:t,narrow:n},s),i}function He(e,t,n,o){let r=t;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]);)r-=1;o.remove(r,n)}function Ke(e,t,n,o,r){const s=e.indexOf(t);Ye(t,e[s-1],n,o,r)}function Qe(e,t,n,o,r){const s=e?.typeParameters?.params?.[0],i=s?.members??[],a=i.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);-1!==a&&Ye(i[a],i[a-1],n,o,r)}function Ye(e,t,n,o,r){const s=n+(e.start??0),i=n+(e.end??0);let a=i;for(;a<r.length&&(" "===r[a]||"\t"===r[a]);)a+=1;if(","===r[a]){let e=a+1;for(;e<r.length&&(" "===r[e]||"\t"===r[e]||"\n"===r[e]);)e+=1;return void o.remove(s,e)}t?o.remove(n+(t.end??0),i):o.remove(s,i)}function Ze(e,t,n){const o=e.code;g(e.sfc.template,r=>{if(!l(r)||!r.tag)return;const s=e.imports.get(r.tag),i=s?t.get(s):void 0;if(i&&0!==i.size)for(const e of r.props??[])if(e.type===c)e.name&&i.has(e.name)&&et(o,e,n);else if(e.type===u&&"bind"===e.name){const t=w(e.arg);t&&i.has(t)&&_e(e)&&et(o,e,n)}})}function _e(e){const t=w(e.exp);if(null==t)return!1;const n=v(t);return null!=n&&x(n,new Map).known}function et(e,t,n){const o=t.loc;let r=o.start.offset;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]||"\n"===e[r-1]);)r-=1;n.remove(r,o.end.offset)}exports.analyze=V,exports.analyzeInput=T,exports.buildAnalyzeInput=W,exports.transformAll=Xe,exports.vueShaker=async function(e,t,n){const{models:o,plans:r}=await V(e,t,n);return Xe(o,r)};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parse as e}from"@vue/compiler-sfc";import{parseExpression as t,parse as n}from"@babel/parser";import o from"magic-string";import{parse as r}from"postcss";import s from"postcss-selector-parser";const i=1,c=2,a=3,u=6,f=7,l=1,p=e=>e.type===i&&e.tagType===l,d=["typescript"];function m(t,o){const{descriptor:r}=e(t,{filename:o}),s=r;let i;if(s.scriptSetup){const e=s.scriptSetup,t=e.attrs?.lang;i={content:e.content,base:e.loc.start.offset,endOffset:e.loc.end.offset,ast:(c=e.content,n(c,{sourceType:"module",plugins:[...d]}).program),lang:"string"==typeof t?t:void 0}}var c;const a=(s.styles??[]).map(e=>({content:e.content,base:e.loc.start.offset,scoped:!0===e.scoped}));return{id:o,code:t,template:s.template?.ast??void 0,scriptSetup:i,hasPlainScript:null!=s.script,styles:a}}function y(e,t,n){if(!n)return m(t,e);const o=n.get(e);if(o&&o.code===t)return o.sfc;const r=m(t,e);return n.set(e,{code:t,sfc:r}),r}function v(e){if(null!=e&&""!==e.trim())try{return t(e,{plugins:[...d]})}catch{return}}function w(e){if(e)return"string"==typeof e.content?e.content:void 0}function g(e,t){if(!e)return;if(!1!==t(e))for(const n of e.children??[])g(n,t)}function h(e,t){if(!e)return;const n=e.children;if(n){t(n,e);for(const e of n)h(e,t)}}function k(e,t,n=null){if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n);for(const n of Object.values(e))if(Array.isArray(n))for(const o of n)k(o,t,e);else n&&"object"==typeof n&&"string"==typeof n.type&&k(n,t,e)}}const S={known:!1};function b(e,t){if(!e)return S;switch(e.type){case"Literal":case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return{known:!0,value:e.value};case"NullLiteral":return{known:!0,value:null};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:S}case"UnaryExpression":{const n=b(e.argument,t);if(!n.known)return S;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 S}}case"LogicalExpression":{const n=b(e.left,t);if(!n.known)return S;switch(e.operator){case"&&":return n.value?b(e.right,t):n;case"||":return n.value?n:b(e.right,t);case"??":return null===n.value||void 0===n.value?b(e.right,t):n;default:return S}}case"BinaryExpression":{const n=b(e.left,t),o=b(e.right,t);if(!n.known||!o.known)return S;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 S}}default:return S}}function x(e,t,n){const o=b(e,t);if(o.known)return o;const r=I(e,t,n);return"unknown"===r?S:{known:!0,value:r}}function I(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?M(I(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=I(e.left,t,n),i=()=>I(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=i(),!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,i()):"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=E(e,o),i=b(t,n);if(s&&i.known)return P(s,i.value,r);const c=E(t,o),a=b(e,n);return c&&a.known?P(c,a.value,r):a.known&&i.known?r?a.value==i.value:a.value===i.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?M(s):s}return"unknown"}default:return"unknown"}var o,r}function E(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function P(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function M(e){return!0!==e&&(!1===e||"unknown")}function L(e){if(e.type===i)for(const t of e.props??[])if(t.type===f){if("if"===t.name)return{dir:t,kind:"if"};if("else-if"===t.name)return{dir:t,kind:"else-if"};if("else"===t.name)return{dir:t,kind:"else"}}}const D=e=>e.type===c&&"string"==typeof e.content&&""===e.content.trim()||e.type===a;function R(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n],r=L(o);if(!r||"if"!==r.kind)continue;const s=[j(o,r.dir,"if")];let i=n+1;for(;i<e.length;i++){const t=e[i];if(D(t))continue;const n=L(t);if(!n||"if"===n.kind)break;if(s.push(j(t,n.dir,n.kind)),"else"===n.kind){i++;break}}t.push(s),n=i-1}return t}function j(e,t,n){return{el:e,dir:t,kind:n,testAst:"else"===n?void 0:v(w(t.exp))}}function O(e,t,n){const o=e.map(e=>"else"===e.kind?{known:!0,value:!0}:x(e.testAst,t,n)),r=e=>e.known&&Boolean(e.value),s=e=>e.known&&!e.value,i=e=>[e.el.loc.start.offset,e.el.loc.end.offset],c=e=>[e.dir.loc.start.offset,e.dir.loc.end.offset];let a=!0;for(let t=0;t<e.length;t++){if(r(o[t])&&a){const n=[];for(let o=0;o<e.length;o++)o!==t&&n.push(i(e[o]));return{removed:n,dirRemovals:[c(e[t])]}}s(o[t])||(a=!1)}const u=o.findIndex(e=>!s(e));if(-1===u)return{removed:e.map(i),dirRemovals:[]};const f=[];for(let t=0;t<u;t++)f.push(i(e[t]));for(let t=u+1;t<e.length;t++)s(o[t])&&f.push(i(e[t]));if(0===u)return{removed:f,dirRemovals:[]};const l=e[u];if("else"===l.kind)return{removed:f,dirRemovals:[c(l)]};const p=l.dir.loc.start.offset;return{removed:f,dirRemovals:[],promotion:{from:p,to:p+9,text:"v-if"}}}function C(e,t){const n=e.loc;return!!n&&function(e,t){return t.some(([t,n])=>e[0]>=t&&e[1]<=n)}([n.start.offset,n.end.offset],t)}function z(e,t,n){if(!e||0===t.size&&0===n.size)return[];const o=[];return h(e,e=>{for(const r of R(e)){if(C(r[0].el,o))continue;const e=O(r,t,n);for(const t of e.removed)o.push(t)}}),o}const A=e=>e.endsWith(".vue"),F='escapes as value (e.g. <component :is="X">)',N="rendered through a barrel/named import (call sites unobservable)";function B(e){return e.sfc.scriptSetup?.base??0}async function $(e,t,n){return q(await T(e,t,n))}function q(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,H(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(F)&&t.bailReasons.push(F)}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(N)&&t.bailReasons.push(N)}let s=V(n,U(n,new Map));for(let e=0;e<10;e++){const e=V(n,U(n,G(n,s)));if(J(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function T(e,t,n,o){const r=Array.isArray(e)?[...e]:[e],s=[],i=[],c=[...r],a=new Set(c);for(;c.length>0;){const e=c.shift();let r,u;try{r=await n(e)}catch{continue}s.push({id:e,code:r});try{u=y(e,r,o)}catch{continue}if(!u.scriptSetup)continue;const f=new Map,l=[];for(const o of fe(u.scriptSetup.ast)){if("default"===o.imported&&A(o.value)){const n=await t(o.value,e);n&&(i.push({from:e,local:o.local,to:n,kind:"default-vue"}),l.push(n));continue}const r=await me(o.value,o.imported,e,t,n);r&&(i.push({from:e,local:o.local,to:r,kind:"barrel"}),f.set(o.local,r))}const p=_(u.template,f);for(const e of[...l,...p])a.has(e)||(a.add(e),c.push(e))}return{files:s,edges:i,entries:r}}function U(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)??[];B(n);for(const t of n.childCalls)e.length>0&&C(t.node,e)||o(t.childId).sites.push(te(t.node))}return n}function V(e,t){const n=new Map;for(const o of e.values())n.set(o.id,ce(o,t.get(o.id)));return n}function J(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(!W(o.constFold,e.constFold))return!1;if(!X(o.narrow,e.narrow))return!1}return!0}function W(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 X(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 G(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=z(o.sfc.template,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function H(e,t,n){const{id:o,code:r}=e,s=y(o,r,n),c=new Map,a=new Map;for(const e of t)"default-vue"===e.kind?c.set(e.local,e.to):a.set(e.local,e.to);const u=[];if(s.scriptSetup)for(const e of function(e){const t=new Set;return k(e,e=>{"CallExpression"===e.type&&"Identifier"===e.callee?.type&&e.callee.name&&t.add(e.callee.name)}),t}(s.scriptSetup.ast))"defineExpose"===e&&u.push("defineExpose() (public instance surface)");let l,d,m,h=null,S=!1,b=!1;const x=new Set;if(s.scriptSetup){for(const e of fe(s.scriptSetup.ast))x.add(e.local);const e=function(e){const t=e.body??[];for(const e of t)if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("ObjectPattern"!==o?.type||"CallExpression"!==n?.type)continue;const r="Identifier"===n.callee?.type?n.callee.name:void 0;if("defineProps"===r)return{declaration:e,pattern:o,definePropsCall:n,withDefaults:new Map,sharesStatement:(e.declarations?.length??1)>1};if("withDefaults"===r){const t=n.arguments?.[0];if("CallExpression"!==t?.type||"Identifier"!==t.callee?.type)continue;if("defineProps"!==t.callee.name)continue;return{declaration:e,pattern:o,definePropsCall:t,withDefaults:ve(n.arguments?.[1]),sharesStatement:(e.declarations?.length??1)>1}}}return null}(s.scriptSetup.ast);if(e){d=e.declaration,l=e.pattern,m=e.definePropsCall,b=e.sharesStatement,e.sharesStatement&&u.push("defineProps() shares a multi-declarator statement"),h=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){S=!0;continue}if("ObjectProperty"!==t.type)continue;const n=t.key;if("Identifier"!==n?.type||!n.name)continue;const o=t.value,r="AssignmentPattern"===o?.type?o.right:void 0;h.push({name:n.name,property:t,defaultExpr:r??e.withDefaults.get(n.name)})}}}const I=function(e,t){const n=[];return g(e,e=>{if(p(e)&&e.tag){const o=t.get(e.tag);o&&n.push({childId:o,node:e})}}),n}(s.template,c),E=_(s.template,a),P=function(e){const t=new Set;g(e.template,e=>{if(e.type===i)for(const n of e.props??[])n.type===f&&("for"===n.name?K(w(n.exp),t):"slot"===n.name&&Q(w(n.exp),t))});const n=e.scriptSetup;n&&k(n.ast,e=>{if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])Y(n,t)});return t}(s),M=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};g(e.template,e=>{if(e.type===i)for(const t of e.props??[])if(t.type===f&&"bind"===t.name&&"is"===ee(t.arg)){const e=v(w(t.exp));"Identifier"===e?.type&&r(e.name)}});const s=e.scriptSetup;s&&k(s.ast,(e,o)=>{"Identifier"===e.type&&e.name&&t.has(e.name)&&n.has(e.name)&&function(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!!("ObjectProperty"!==t.type&&"Property"!==t.type||t.key!==e||t.computed||!0===t.shorthand)&&!Z(t)))}(e,o)&&!Z(o)&&r(e.name)});return o}(s,c,x);return{id:o,code:r,sfc:s,imports:c,props:h,propsPattern:l,propsDeclaration:d,definePropsCall:m,hasRestProp:S,sharesStatement:b,childCalls:I,shadowedNames:P,escapedComponents:M,barrelChildIds:E,bailReasons:u}}function K(e,t){if(!e)return;const n=/^(.*?)\s+(?:in|of)\s+/s.exec(e),o=(n?n[1]:e).trim();o&&Q(o,t)}function Q(e,t){if(!e)return;const n=v(`(${e}) => 0`);if("ArrowFunctionExpression"!==n?.type){const n=v(e);return void(n&&Y(n,t))}for(const e of n.params??[])Y(e,t)}function Y(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectExpression":case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?Y(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||Y(n.value??n.key,t);return;case"ArrayExpression":case"ArrayPattern":for(const n of e.elements??[])Y(n,t);return;case"AssignmentPattern":return void Y(e.left,t);case"RestElement":return void Y(e.argument,t);default:return}}function Z(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function _(e,t){const n=new Set;return 0===t.size||g(e,e=>{if(p(e)&&e.tag){const o=t.get(e.tag);o&&n.add(o)}}),n}function ee(e){return w(e)}function te(e,t){const n=e.props??[];let o=-1;for(let e=0;e<n.length;e++){const t=n[e];t.type===f&&"bind"===t.name&&ne(t)&&(o=e)}const r=new Map;for(let e=0;e<n.length;e++){const t=n[e],s=e>o;if(t.type===u){if(!t.name)continue;const e=re(t);r.set(t.name,{value:e,dynamic:!1,afterLastSpread:s});continue}if(t.type===f){if("bind"===t.name){if(ne(t))continue;const e=w(t.arg);if(!e)continue;const n=se(w(t.exp));r.set(e,n.known?{value:n.value,dynamic:!1,afterLastSpread:s}:oe(s));continue}if("model"===t.name){const e=w(t.arg)??"modelValue";r.set(e,oe(s));continue}}}return{hadSpread:o>=0,explicit:r}}function ne(e){return null==e.arg||null==w(e.arg)}function oe(e){return{value:void 0,dynamic:!0,afterLastSpread:e}}function re(e){const t=e.value;return!t||("string"==typeof t.content?t.content:"")}function se(e){const t=v(e);return t?b(t,new Map):{known:!1}}function ie(e,t){return e.shadowedNames.has(t)}function ce(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(ie(e,t.name))continue;const r=ae(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 ae(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 i=ue(e.defaultExpr);i.known?s(i.value):o=!0}return{values:n,dynamic:o,top:r}}function ue(e){return e?b(e,new Map):{known:!0,value:void 0}}function*fe(e){const t=e.body??[];for(const e of t){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:le(n)??e}))}}}function le(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"StringLiteral"===t?.type&&"string"==typeof t.value?t.value:void 0}function pe(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}const de=8;async function me(e,t,n,o,r,s=0){if(s>de)return null;const i=await o(e,n);if(!i)return null;if(A(e)||A(i))return"default"===t||"*"===t?i:null;let c;try{c=await r(i)}catch{return null}const a=function(e){try{const t=m(`<script setup>\n${e}\n<\/script>`,"barrel.vue");return t.scriptSetup?.ast.body??null}catch{return null}}(c);if(!a)return null;for(const e of a)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(pe(n.exported)===t)return me(String(e.source.value),pe(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 me(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(pe(n.exported)!==t)continue;const e=pe(n.local);if(!e)continue;const c=ye(a,e);return c?me(c.value,c.imported,i,o,r,s+1):null}return null}function ye(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:le(o)??t}}}return null}function ve(e){const t=new Map;if("ObjectExpression"!==e?.type)return t;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)continue;const e=n.key,o="Identifier"===e?.type?e.name:void 0;o&&n.value&&t.set(o,n.value)}return t}const we=Symbol("unbounded-class-source");function ge(e,t,n){if(!e)return we;if("ObjectExpression"===e.type){const t=new Set;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)return we;const e=n.key,o="Identifier"===e?.type?e.name:"StringLiteral"===e?.type?e.value:void 0;if(null==o)return we;for(const e of o.split(/\s+/))e&&t.add(e)}return t}if("ArrayExpression"===e.type){const o=new Set;for(const r of e.elements??[]){if(!r)continue;const e=ge(r,t,n);if(e===we)return we;for(const t of e)o.add(t)}return o}const o=he(e,t,n);if(o===we)return we;const r=new Set;for(const e of o)for(const t of e.split(/\s+/))t&&r.add(t);return r}function he(e,t,n){if(!e)return we;switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return new Set([String(e.value)]);case"Identifier":{const o=e.name??"";if(n.has(o)){const e=new Set;for(const t of n.get(o))e.add(String(t));return e}const r=b(e,t);return r.known?new Set([String(r.value)]):we}case"TemplateLiteral":{const o=e.quasis??[],r=e.expressions??[];let s=[ke(o[0])];for(let e=0;e<r.length;e++){const i=he(r[e],t,n);if(i===we)return we;const c=ke(o[e+1]),a=[];for(const e of s)for(const t of i)if(a.push(e+t+c),a.length>64)return we;s=a}return new Set(s)}case"BinaryExpression":{if("+"!==e.operator)break;const o=he(e.left,t,n),r=he(e.right,t,n);if(o===we||r===we)return we;const s=new Set;for(const e of o)for(const t of r)if(s.add(e+t),s.size>64)return we;return s}case"ConditionalExpression":{const o=b(e.test,t);if(o.known)return he(o.value?e.consequent:e.alternate,t,n);const r=he(e.consequent,t,n),s=he(e.alternate,t,n);return r===we||s===we?we:new Set([...r,...s])}}const o=b(e,t);return o.known?new Set([String(o.value)]):we}function ke(e){const t=e?.value;return t?.cooked??t?.raw??""}function Se(e,t,n){const o=function(e,t){const n=new Set;let o=!1;const r=t.constFold,s=t.narrow;return g(e.sfc.template,e=>{if(e.type===i)for(const t of e.props??[]){if(t.type===u){if("class"===t.name){const e=t.value,o="string"==typeof e?.content?e.content:"";for(const e of o.split(/\s+/))e&&n.add(e)}continue}if(t.type!==f||"bind"!==t.name)continue;const e=w(t.arg);if(null==t.arg||null==e){o=!0;continue}if("class"!==e)continue;const i=ge(v(w(t.exp)),r,s);if(i===we)o=!0;else for(const e of i)n.add(e)}}),{classes:n,unbounded:o}}(e,t);if(o.unbounded)return 0;let s=0;for(const t of e.sfc.styles){if(!t.scoped)continue;let e;try{e=r(t.content)}catch{continue}let i=0;if(e.each(e=>{"rule"===e.type&&be(e,o.classes)&&(e.remove(),i+=1)}),i>0){const o=t.base+t.content.length;n.overwrite(t.base,o,e.toString()),s+=i}}return s}function be(e,t){let n=!1;try{s(e=>{let o=!1,r=!0,s=!1;e.each(e=>{s=!0;const n=[];e.walkPseudos(e=>{const t=e.value;(t.includes("deep")||t.includes("global")||t.includes("slotted"))&&(o=!0)}),e.walkClasses(e=>{n.push(e.value)});n.some(e=>!t.has(e))||(r=!1)}),n=s&&r&&!o}).processSync(e.selector)}catch{return!1}return n}function xe(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 i=t.get(s.id);r.set(s.id,i.bail?new Set:Ie(s,i.constFold,i.narrow,i,e))}for(const t of e.values())De(t,r,n.get(t.id));const s={};for(const t of e.values())s[t.id]=n.get(t.id).toString();return s}function Ie(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;!function(e,t,n,o,r){const s=e.code;h(e.sfc.template,e=>{for(const i of R(e)){if(C(i[0].el,r))continue;const e=O(i,t,n);for(const[t,n]of e.removed)Ee(s,t,n,o),r.push([t,n]);for(const[t,n]of e.dirRemovals)Ee(s,t,n,o);if(e.promotion){const{from:t,to:n,text:r}=e.promotion;o.overwrite(t,n,r)}}})}(e,t,n,r,[]);const s=function(e,t,n){const o=new Set,r=e.sfc.scriptSetup;if(!(e.props&&0!==t.size&&r&&e.propsDeclaration&&e.propsPattern))return o;const s=r.base,i=e.props.filter(e=>t.has(e.name));if(0===i.length)return o;for(const e of i)o.add(e.name);const c=i.map(e=>{return`const ${e.name} = ${n=t.get(e.name),void 0===n?"undefined":JSON.stringify(n)};`;var n}).join("\n"),a=e.props.filter(e=>!t.has(e.name)),u=s+(e.propsDeclaration.start??0);let f=s+(e.propsDeclaration.end??0);";"===e.code[f]&&(f+=1);if(0===a.length&&!e.hasRestProp)return n.overwrite(u,f,c),o;const l=e.propsPattern?.properties??[];for(const t of i)Pe(l,t.property,s,n,e.code),Me(e.definePropsCall,t.name,s,n,e.code);return n.appendLeft(f,`\n${c}`),o}(e,t,r);return Se(e,{...o,constFold:t,narrow:n},r),s}function Ee(e,t,n,o){let r=t;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]);)r-=1;o.remove(r,n)}function Pe(e,t,n,o,r){const s=e.indexOf(t);Le(t,e[s-1],n,o,r)}function Me(e,t,n,o,r){const s=e?.typeParameters?.params?.[0],i=s?.members??[],c=i.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);-1!==c&&Le(i[c],i[c-1],n,o,r)}function Le(e,t,n,o,r){const s=n+(e.start??0),i=n+(e.end??0);let c=i;for(;c<r.length&&(" "===r[c]||"\t"===r[c]);)c+=1;if(","===r[c]){let e=c+1;for(;e<r.length&&(" "===r[e]||"\t"===r[e]||"\n"===r[e]);)e+=1;return void o.remove(s,e)}t?o.remove(n+(t.end??0),i):o.remove(s,i)}function De(e,t,n){const o=e.code;g(e.sfc.template,r=>{if(!p(r)||!r.tag)return;const s=e.imports.get(r.tag),i=s?t.get(s):void 0;if(i&&0!==i.size)for(const e of r.props??[])if(e.type===u)e.name&&i.has(e.name)&&je(o,e,n);else if(e.type===f&&"bind"===e.name){const t=w(e.arg);t&&i.has(t)&&Re(e)&&je(o,e,n)}})}function Re(e){const t=w(e.exp);if(null==t)return!1;const n=v(t);return null!=n&&b(n,new Map).known}function je(e,t,n){const o=t.loc;let r=o.start.offset;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]||"\n"===e[r-1]);)r-=1;n.remove(r,o.end.offset)}async function Oe(e,t,n){const{models:o,plans:r}=await $(e,t,n);return xe(o,r)}export{$ as analyze,q as analyzeInput,T as buildAnalyzeInput,xe as transformAll,Oe as vueShaker};
|
|
1
|
+
import{parse as e}from"@vue/compiler-sfc";import{parseExpression as t,parse as n}from"@babel/parser";import o from"magic-string";import{parse as r}from"postcss";import s from"postcss-selector-parser";const i=1,a=2,c=3,f=6,u=7,l=1,p=e=>e.type===i&&e.tagType===l,d=["typescript"];function m(e){return n(e,{sourceType:"module",plugins:[...d]}).program}function y(t,n){const{descriptor:o}=e(t,{filename:n}),r=o;let s;if(r.scriptSetup){const e=r.scriptSetup,t=e.attrs?.lang;s={content:e.content,base:e.loc.start.offset,endOffset:e.loc.end.offset,ast:m(e.content),lang:"string"==typeof t?t:void 0}}const i=(r.styles??[]).map(e=>({content:e.content,base:e.loc.start.offset,scoped:!0===e.scoped}));return{id:n,code:t,template:r.template?.ast??void 0,scriptSetup:s,hasPlainScript:null!=r.script,styles:i}}function v(e,t,n){if(!n)return y(t,e);const o=n.get(e);if(o&&o.code===t)return o.sfc;const r=y(t,e);return n.set(e,{code:t,sfc:r}),r}function w(e){if(null!=e&&""!==e.trim())try{return t(e,{plugins:[...d]})}catch{return}}function g(e){if(e)return"string"==typeof e.content?e.content:void 0}function h(e,t){if(!e)return;if(!1!==t(e))for(const n of e.children??[])h(n,t)}function b(e,t){if(!e)return;const n=e.children;if(n){t(n,e);for(const e of n)b(e,t)}}function S(e,t,n=null){if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n);for(const n of Object.values(e))if(Array.isArray(n))for(const o of n)S(o,t,e);else n&&"object"==typeof n&&"string"==typeof n.type&&S(n,t,e)}}const k={known:!1};function x(e,t){return t&&"MemberExpression"===e.type&&!0!==e.computed&&"Identifier"===e.object?.type&&e.object.name===t&&"Identifier"===e.property?.type&&e.property.name?e.property.name:null}function E(e,t,n){if(!e)return k;switch(e.type){case"Literal":case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return{known:!0,value:e.value};case"NullLiteral":return{known:!0,value:null};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:k}case"MemberExpression":{const o=x(e,n);return null!=o&&t.has(o)?{known:!0,value:t.get(o)}:k}case"UnaryExpression":{const o=E(e.argument,t,n);if(!o.known)return k;const r=o.value;switch(e.operator){case"!":return{known:!0,value:!r};case"-":return{known:!0,value:-r};case"+":return{known:!0,value:+r};case"typeof":return{known:!0,value:typeof r};case"void":return{known:!0,value:void 0};default:return k}}case"LogicalExpression":{const o=E(e.left,t,n);if(!o.known)return k;switch(e.operator){case"&&":return o.value?E(e.right,t,n):o;case"||":return o.value?o:E(e.right,t,n);case"??":return null===o.value||void 0===o.value?E(e.right,t,n):o;default:return k}}case"BinaryExpression":{const o=E(e.left,t,n),r=E(e.right,t,n);if(!o.known||!r.known)return k;const s=o.value,i=r.value;switch(e.operator){case"===":return{known:!0,value:s===i};case"!==":return{known:!0,value:s!==i};case"==":return{known:!0,value:s==i};case"!=":return{known:!0,value:s!=i};case"<":return{known:!0,value:s<i};case">":return{known:!0,value:s>i};case"<=":return{known:!0,value:s<=i};case">=":return{known:!0,value:s>=i};case"+":return{known:!0,value:s+i};case"-":return{known:!0,value:s-i};case"*":return{known:!0,value:s*i};case"/":return{known:!0,value:s/i};case"%":return{known:!0,value:s%i};default:return k}}default:return k}}function I(e,t,n){const o=E(e,t);if(o.known)return o;const r=P(e,t,n);return"unknown"===r?k:{known:!0,value:r}}function P(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?M(P(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=P(e.left,t,n),i=()=>P(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=i(),!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,i()):"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=j(e,o),i=E(t,n);if(s&&i.known)return D(s,i.value,r);const a=j(t,o),c=E(e,n);return a&&c.known?D(a,c.value,r):c.known&&i.known?r?c.value==i.value:c.value===i.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?M(s):s}return"unknown"}default:return"unknown"}var o,r}function j(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 M(e){return!0!==e&&(!1===e||"unknown")}function L(e){if(e.type===i)for(const t of e.props??[])if(t.type===u){if("if"===t.name)return{dir:t,kind:"if"};if("else-if"===t.name)return{dir:t,kind:"else-if"};if("else"===t.name)return{dir:t,kind:"else"}}}const O=e=>e.type===a&&"string"==typeof e.content&&""===e.content.trim()||e.type===c;function A(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n],r=L(o);if(!r||"if"!==r.kind)continue;const s=[C(o,r.dir,"if")];let i=n+1;for(;i<e.length;i++){const t=e[i];if(O(t))continue;const n=L(t);if(!n||"if"===n.kind)break;if(s.push(C(t,n.dir,n.kind)),"else"===n.kind){i++;break}}t.push(s),n=i-1}return t}function C(e,t,n){return{el:e,dir:t,kind:n,testAst:"else"===n?void 0:w(g(t.exp))}}function R(e,t,n){const o=e.map(e=>"else"===e.kind?{known:!0,value:!0}:I(e.testAst,t,n)),r=e=>e.known&&Boolean(e.value),s=e=>e.known&&!e.value,i=e=>[e.el.loc.start.offset,e.el.loc.end.offset],a=e=>[e.dir.loc.start.offset,e.dir.loc.end.offset];let c=!0;for(let t=0;t<e.length;t++){if(r(o[t])&&c){const n=[];for(let o=0;o<e.length;o++)o!==t&&n.push(i(e[o]));return{removed:n,dirRemovals:[a(e[t])]}}s(o[t])||(c=!1)}const f=o.findIndex(e=>!s(e));if(-1===f)return{removed:e.map(i),dirRemovals:[]};const u=[];for(let t=0;t<f;t++)u.push(i(e[t]));for(let t=f+1;t<e.length;t++)s(o[t])&&u.push(i(e[t]));if(0===f)return{removed:u,dirRemovals:[]};const l=e[f];if("else"===l.kind)return{removed:u,dirRemovals:[a(l)]};const p=l.dir.loc.start.offset;return{removed:u,dirRemovals:[],promotion:{from:p,to:p+9,text:"v-if"}}}function F(e,t){const n=e.loc;return!!n&&function(e,t){return t.some(([t,n])=>e[0]>=t&&e[1]<=n)}([n.start.offset,n.end.offset],t)}function N(e,t,n){if(!e||0===t.size&&0===n.size)return[];const o=[];return b(e,e=>{for(const r of A(e)){if(F(r[0].el,o))continue;const e=R(r,t,n);for(const t of e.removed)o.push(t)}}),o}const z=e=>e.endsWith(".vue"),$='escapes as value (e.g. <component :is="X">)',B="rendered through a barrel/named import (call sites unobservable)",V="reached through a namespace object that also leaks as a value (call sites unobservable)";function q(e){return e.sfc.scriptSetup?.base??0}async function T(e,t,n,o){return W(await U(e,t,n,void 0,o))}function W(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,Y(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($)&&t.bailReasons.push($)}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)}for(const t of e.forcedBails??[]){const e=n.get(t);e&&!e.bailReasons.includes(V)&&e.bailReasons.push(V)}let s=X(n,J(n,new Map));for(let e=0;e<10;e++){const e=X(n,J(n,Q(n,s)));if(G(s,e)){s=e;break}s=e}return{models:n,plans:s}}async function U(e,t,n,o,r){const s=Array.isArray(e)?[...e]:[e],i=[],a=[],c=new Set,f=new Map,u=[...s],l=new Set(u);for(;u.length>0;){const e=u.shift();let r,s;try{r=await n(e)}catch{continue}i.push({id:e,code:r});try{s=v(e,r,o)}catch{continue}if(!s.scriptSetup)continue;const p=s.scriptSetup.ast,d=[...je(p)],m=new Map;for(const e of d)m.set(e.local,e);const y=new Map,w=[];for(const o of d){if("default"===o.imported&&z(o.value)){const n=await t(o.value,e);n&&(a.push({from:e,local:o.local,to:n,kind:"default-vue"}),w.push(n));continue}const r=await Oe(o.value,o.imported,e,t,n);r&&(a.push({from:e,local:o.local,to:r,kind:"barrel"}),y.set(o.local,r))}for(const[o,r]of oe(p)){const i=m.get(o);if(!i)continue;const u=await ie(i.value,i.imported,e,t,n,f);if(!u)continue;if(r.unsafe||pe(p,o,r.initNodes)||we(s.template,o))for(const e of ue(u))c.add(e);else for(const t of r.bindings){const n=fe(u,t.path);n&&(a.push({from:e,local:t.local,to:n,kind:"namespace"}),w.push(n))}}const g=ne(s.template,y);for(const e of[...w,...g])l.has(e)||(l.add(e),u.push(e))}const p=await Promise.all((r??[]).map(e=>async function(e,t,n){let o;try{o=await n(e)}catch{return[]}if(!o.includes(".vue"))return[];const r=function(e){try{return m(e)}catch{return null}}(o);if(!r)return[];const s=new Map;for(const e of r.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t&&z(t))for(const n of e.specifiers??[])"ImportDefaultSpecifier"!==n.type&&"ImportNamespaceSpecifier"!==n.type||!n.local?.name||s.set(n.local.name,t)}if(0===s.size)return[];const i=function(e){const t=new Set;return S(e,e=>{if("ObjectExpression"===e.type)for(const n of e.properties??[])"ObjectProperty"!==n.type&&"Property"!==n.type||!n.shorthand||(n.key&&t.add(n.key),n.value&&t.add(n.value))}),t}(r),a=new Set;de(r,(e,t)=>{s.has(e)&&!i.has(t)&&a.add(e)});const c=await Promise.all([...a].map(n=>t(s.get(n),e)));return c.filter(e=>null!=e)}(e,t,n)));for(const e of p)for(const t of e)c.add(t);return{files:i,edges:a,entries:s,forcedBails:[...c]}}function J(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)??[];q(n);for(const t of n.childCalls)e.length>0&&F(t.node,e)||o(t.childId).sites.push(ge(t.node))}return n}function X(e,t){const n=new Map;for(const o of e.values())n.set(o.id,Ee(o,t.get(o.id)));return n}function G(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(!H(o.constFold,e.constFold))return!1;if(!K(o.narrow,e.narrow))return!1}return!0}function H(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 K(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 Q(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=N(o.sfc.template,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function Y(e,t,n){const{id:o,code:r}=e,s=v(o,r,n),a=new Map,c=new Map,f=new Set;for(const e of t)"default-vue"===e.kind?a.set(e.local,e.to):"namespace"===e.kind?(a.set(e.local,e.to),f.add(e.local)):c.set(e.local,e.to);const l=[];if(s.scriptSetup)for(const e of function(e){const t=new Set;return S(e,e=>{"CallExpression"===e.type&&"Identifier"===e.callee?.type&&e.callee.name&&t.add(e.callee.name)}),t}(s.scriptSetup.ast))"defineExpose"===e&&l.push("defineExpose() (public instance surface)");let d,m,y,b,k=null,x=!1,E=!1;const I=new Set;if(s.scriptSetup){for(const e of je(s.scriptSetup.ast))I.add(e.local);for(const e of f)I.add(e);const e=function(e){const t=e.body??[];for(const e of t)if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=Re(t.init);if(!n)continue;const o=t.id,r=(e.declarations?.length??1)>1;if("ObjectPattern"===o?.type)return{declaration:e,pattern:o,propsLocal:void 0,sharesStatement:r,...n};if("Identifier"===o?.type&&o.name)return{declaration:e,pattern:void 0,propsLocal:o.name,sharesStatement:r,...n}}return null}(s.scriptSetup.ast);if(e)if(y=e.declaration,d=e.pattern,m=e.propsLocal,b=e.definePropsCall,E=e.sharesStatement,e.sharesStatement&&l.push("defineProps() shares a multi-declarator statement"),e.pattern){k=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){x=!0;continue}if("ObjectProperty"!==t.type)continue;const n=t.key;if("Identifier"!==n?.type||!n.name)continue;const o=t.value,r="AssignmentPattern"===o?.type?o.right:void 0;k.push({name:n.name,property:t,defaultExpr:r??e.withDefaults.get(n.name)})}}else k=function(e,t){const n=[],o=e?.typeParameters?.params?.[0];for(const e of o?.members??[]){const o=e.key;"Identifier"===o?.type&&o.name&&n.push({name:o.name,property:e,defaultExpr:t.get(o.name)})}return n}(e.definePropsCall,e.withDefaults)}const P=function(e,t){const n=[];return h(e,e=>{if(p(e)&&e.tag){const o=t.get(e.tag);o&&n.push({childId:o,node:e})}}),n}(s.template,a),j=ne(s.template,c),D=function(e){const t=new Set;h(e.template,e=>{if(e.type===i)for(const n of e.props??[])n.type===u&&("for"===n.name?Z(g(n.exp),t):"slot"===n.name&&_(g(n.exp),t))});const n=e.scriptSetup;n&&S(n.ast,e=>{if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])ee(n,t)});return t}(s);for(const e of I)D.add(e);const M=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};h(e.template,e=>{if(e.type===i)for(const t of e.props??[])if(t.type===u&&"bind"===t.name&&"is"===ye(t.arg)){const e=w(g(t.exp));"Identifier"===e?.type?r(e.name):"MemberExpression"===e?.type&&r(ve(e))}});const s=e.scriptSetup;if(s){const e=function(e){const t=new Set;return S(e,e=>{if("VariableDeclarator"===e.type)me(e.id,t);else if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type||"ObjectMethod"===e.type||"ClassMethod"===e.type)for(const n of e.params??[])me(n,t);else"ImportDefaultSpecifier"!==e.type&&"ImportSpecifier"!==e.type&&"ImportNamespaceSpecifier"!==e.type||e.local&&t.add(e.local)}),t}(s.ast);S(s.ast,(o,s)=>{"Identifier"===o.type&&o.name&&t.has(o.name)&&n.has(o.name)&&function(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!!("ObjectProperty"!==t.type&&"Property"!==t.type||t.key!==e||t.computed||!0===t.shorthand)&&!te(t)))}(o,s)&&!te(s)&&!e.has(o)&&r(o.name)})}return o}(s,a,I);return{id:o,code:r,sfc:s,imports:a,props:k,propsPattern:d,propsLocal:m,propsDeclaration:y,definePropsCall:b,hasRestProp:x,sharesStatement:E,childCalls:P,shadowedNames:D,escapedComponents:M,barrelChildIds:j,bailReasons:l}}function Z(e,t){if(!e)return;const n=/^(.*?)\s+(?:in|of)\s+/s.exec(e),o=(n?n[1]:e).trim();o&&_(o,t)}function _(e,t){if(!e)return;const n=w(`(${e}) => 0`);if("ArrowFunctionExpression"!==n?.type){const n=w(e);return void(n&&ee(n,t))}for(const e of n.params??[])ee(e,t)}function ee(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectExpression":case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?ee(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||ee(n.value??n.key,t);return;case"ArrayExpression":case"ArrayPattern":for(const n of e.elements??[])ee(n,t);return;case"AssignmentPattern":return void ee(e.left,t);case"RestElement":return void ee(e.argument,t);default:return}}function te(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function ne(e,t){const n=new Set;return 0===t.size||h(e,e=>{if(p(e)&&e.tag){const o=t.get(e.tag);o&&n.add(o)}}),n}function oe(e){const t=new Map;return S(e,e=>{if("VariableDeclarator"!==e.type)return;const n=e.init,o=e.id;if("Identifier"!==n?.type||!n.name||"ObjectPattern"!==o?.type)return;let r=t.get(n.name);r||(r={initNodes:new Set,bindings:[],unsafe:!1},t.set(n.name,r)),r.initNodes.add(n),re(o,[],r)}),t}function re(e,t,n){for(const o of e.properties??[]){if("RestElement"===o.type){n.unsafe=!0;continue}if("ObjectProperty"!==o.type&&"Property"!==o.type)continue;if(o.computed){n.unsafe=!0;continue}const e=se(o.key);if(!e){n.unsafe=!0;continue}const r=o.value??void 0;"Identifier"===r?.type&&r.name?n.bindings.push({path:[...t,e],local:r.name}):"ObjectPattern"===r?.type?re(r,[...t,e],n):n.unsafe=!0}}function se(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}async function ie(e,t,n,o,r,s){const i=await o(e,n);if(!i)return null;const a=`${i}\0${t}`,c=s.get(a);if(void 0!==c)return c;const f=await async function(e,t,n,o){let r;try{r=await o(e)}catch{return null}const s=Ce(r);if(!s)return null;const i=new Map;for(const e of s){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?i.set(e,{value:t,imported:"default"}):"ImportSpecifier"===n.type&&i.set(e,{value:t,imported:De(n)??e}))}}const a=function(e,t){for(const n of e){if("ExportNamedDeclaration"===n.type&&"VariableDeclaration"===n.declaration?.type){const e=ae(n.declaration,t);if(e)return e}if("VariableDeclaration"===n.type){const e=ae(n,t);if(e)return e}if("ExportDefaultDeclaration"===n.type&&"default"===t&&"ObjectExpression"===n.declaration?.type)return n.declaration}return null}(s,t);return a?ce(a,e,i,n):null}(i,t,o,r);return s.set(a,f),f}function ae(e,t){for(const n of e.declarations??[])if("Identifier"===n.id?.type&&n.id.name===t&&"ObjectExpression"===n.init?.type)return n.init;return null}async function ce(e,t,n,o){const r=new Map,s=await Promise.all((e.properties??[]).map(async e=>{if("ObjectProperty"!==e.type&&"Property"!==e.type)return null;if(e.computed)return null;const r=se(e.key);if(!r)return null;const s=e.value;if("Identifier"===s?.type&&s.name){const e=n.get(s.name);if(!e||"default"!==e.imported||!z(e.value))return null;const i=await o(e.value,t);return i?[r,i]:null}return"ObjectExpression"===s?.type?[r,await ce(s,t,n,o)]:null}));for(const e of s)e&&r.set(e[0],e[1]);return r}function fe(e,t){let n=e;for(const e of t){if("string"==typeof n)return null;const t=n.get(e);if(void 0===t)return null;n=t}return"string"==typeof n?n:null}function ue(e){const t=[];for(const n of e.values())"string"==typeof n?t.push(n):t.push(...ue(n));return t}const le=new Set(["typeAnnotation","typeParameters","returnType","typeArguments"]);function pe(e,t,n){let o=!1;return de(e,(e,r)=>{e!==t||n.has(r)||(o=!0)}),o}function de(e,t){if(e&&"object"==typeof e&&"string"==typeof e.type&&"ImportDeclaration"!==e.type)if("Identifier"!==e.type){for(const[n,o]of Object.entries(e))if(!le.has(n))if(Array.isArray(o))for(const e of o)de(e,t);else o&&"object"==typeof o&&"string"==typeof o.type&&de(o,t)}else e.name&&t(e.name,e)}function me(e,t){if(e)switch(e.type){case"Identifier":return void t.add(e);case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?me(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||(me(n.value??n.key,t),n.shorthand&&n.key&&t.add(n.key));return;case"ArrayPattern":for(const n of e.elements??[])me(n,t);return;case"AssignmentPattern":return void me(e.left,t);case"RestElement":return void me(e.argument,t);default:return}}function ye(e){return g(e)}function ve(e){let t=e;for(;"MemberExpression"===t?.type;)t=t.object;return"Identifier"===t?.type?t.name:void 0}function we(e,t){let n=!1;return h(e,e=>{if(e.type===i)for(const o of e.props??[]){if(o.type!==u||"bind"!==o.name||"is"!==ye(o.arg))continue;const e=w(g(o.exp));("Identifier"===e?.type?e.name===t:ve(e)===t)&&(n=!0)}}),n}function ge(e,t){const n=e.props??[];let o=-1;for(let e=0;e<n.length;e++){const t=n[e];t.type===u&&"bind"===t.name&&he(t)&&(o=e)}const r=new Map;for(let e=0;e<n.length;e++){const t=n[e],s=e>o;if(t.type===f){if(!t.name)continue;const e=Se(t);r.set(t.name,{value:e,dynamic:!1,afterLastSpread:s});continue}if(t.type===u){if("bind"===t.name){if(he(t))continue;const e=g(t.arg);if(!e)continue;const n=ke(g(t.exp));r.set(e,n.known?{value:n.value,dynamic:!1,afterLastSpread:s}:be(s));continue}if("model"===t.name){const e=g(t.arg)??"modelValue";r.set(e,be(s));continue}}}return{hadSpread:o>=0,explicit:r}}function he(e){return null==e.arg||null==g(e.arg)}function be(e){return{value:void 0,dynamic:!0,afterLastSpread:e}}function Se(e){const t=e.value;return!t||("string"==typeof t.content?t.content:"")}function ke(e){const t=w(e);return t?E(t,new Map):{known:!1}}function xe(e,t){return e.shadowedNames.has(t)}function Ee(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(xe(e,t.name))continue;const r=Ie(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 Ie(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 i=Pe(e.defaultExpr);i.known?s(i.value):o=!0}return{values:n,dynamic:o,top:r}}function Pe(e){return e?E(e,new Map):{known:!0,value:void 0}}function*je(e){const t=e.body??[];for(const e of t){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:De(n)??e}))}}}function De(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"StringLiteral"===t?.type&&"string"==typeof t.value?t.value:void 0}function Me(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}const Le=8;async function Oe(e,t,n,o,r,s=0){if(s>Le)return null;const i=await o(e,n);if(!i)return null;if(z(e)||z(i))return"default"===t||"*"===t?i:null;let a;try{a=await r(i)}catch{return null}const c=Ce(a);if(!c)return null;for(const e of c)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(Me(n.exported)===t)return Oe(String(e.source.value),Me(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 Oe(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(Me(n.exported)!==t)continue;const e=Me(n.local);if(!e)continue;const a=Ae(c,e);return a?Oe(a.value,a.imported,i,o,r,s+1):null}return null}function Ae(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:De(o)??t}}}return null}function Ce(e){try{const t=y(`<script setup>\n${e}\n<\/script>`,"barrel.vue");return t.scriptSetup?.ast.body??null}catch{return null}}function Re(e){if("CallExpression"!==e?.type)return null;const t="Identifier"===e.callee?.type?e.callee.name:void 0;if("defineProps"===t)return{definePropsCall:e,withDefaults:new Map};if("withDefaults"===t){const t=e.arguments?.[0];return"CallExpression"!==t?.type||"Identifier"!==t.callee?.type||"defineProps"!==t.callee.name?null:{definePropsCall:t,withDefaults:Fe(e.arguments?.[1])}}return null}function Fe(e){const t=new Map;if("ObjectExpression"!==e?.type)return t;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)continue;const e=n.key,o="Identifier"===e?.type?e.name:void 0;o&&n.value&&t.set(o,n.value)}return t}const Ne=Symbol("unbounded-class-source");function ze(e,t){const n=new Set;let o=!1;const r={env:t.constFold,setEnv:t.narrow,propsLocal:e.propsLocal,resolveClassExpr:We(e)};return h(e.sfc.template,e=>{if(e.type===i){(function(e){if(!e)return!1;const t=e.toLowerCase().replace(/-/g,"");return"transition"===t||"transitiongroup"===t})(e.tag)&&!function(e,t){let n="v";const o="transitiongroup"===(e.tag??"").toLowerCase().replace(/-/g,"");for(const o of e.props??[])if(o.type!==f){if(o.type===u&&"bind"===o.name){const e=g(o.arg);if(null==e||"name"===e||e.endsWith("Class")||e.endsWith("-class"))return!1}}else if("name"===o.name){const e=o.value;"string"==typeof e?.content&&(n=e.content)}else if(o.name&&o.name.endsWith("-class")){const e=o.value,n="string"==typeof e?.content?e.content:"";for(const e of n.split(/\s+/))e&&t.add(e)}for(const e of Be)t.add(`${n}-${e}`);o&&t.add(`${n}-move`);return!0}(e,n)&&(o=!0);for(const t of e.props??[]){if(t.type===u&&t.name&&!$e.has(t.name)){o=!0;continue}if(t.type===f){if("class"===t.name){const e=t.value,o="string"==typeof e?.content?e.content:"";for(const e of o.split(/\s+/))e&&n.add(e)}continue}if(t.type!==u||"bind"!==t.name)continue;const e=g(t.arg);if(null==t.arg||null==e){o=!0;continue}if("class"!==e)continue;const s=Ve(w(g(t.exp)),r);if(s===Ne)o=!0;else for(const e of s)n.add(e)}}}),function(e){const t=e.sfc.scriptSetup?.content;return!!t&&/\.classList\b|\.className\b|setAttribute\(\s*['"`]class/.test(t)}(e)&&(o=!0),{classes:n,unbounded:o}}const $e=new Set(["if","else","else-if","for","bind","on","model","show","slot","html","text","once","memo","cloak","pre"]);const Be=["enter-from","enter-active","enter-to","leave-from","leave-active","leave-to","appear-from","appear-active","appear-to"];function Ve(e,t){if(!e)return Ne;if("ObjectExpression"===e.type){const t=new Set;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)return Ne;const e=n.key,o="Identifier"===e?.type?e.name:"StringLiteral"===e?.type?e.value:void 0;if(null==o)return Ne;for(const e of o.split(/\s+/))e&&t.add(e)}return t}if("ArrayExpression"===e.type){const n=new Set;for(const o of e.elements??[]){if(!o)continue;const e=Ve("SpreadElement"===o.type?o.argument:o,t);if(e===Ne)return Ne;for(const t of e)n.add(t)}return n}if("ConditionalExpression"===e.type){const n=E(e.test,t.env,t.propsLocal);if(n.known)return Ve(n.value?e.consequent:e.alternate,t);const o=Ve(e.consequent,t),r=Ve(e.alternate,t);return o===Ne||r===Ne?Ne:new Set([...o,...r])}if("Identifier"===e.type&&e.name&&!function(e,t){return t.env.has(e)||t.setEnv.has(e)}(e.name,t)){const n=t.resolveClassExpr(e.name);if(!n)return Ne;const o=new Set;for(const e of n){const n=Ve(e,t);if(n===Ne)return Ne;for(const e of n)o.add(e)}return o}const n=qe(e,t);if(n===Ne)return Ne;const o=new Set;for(const e of n)for(const t of e.split(/\s+/))t&&o.add(t);return o}function qe(e,t){if(!e)return Ne;switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return new Set([String(e.value)]);case"Identifier":return Te(e.name??"",e,t);case"MemberExpression":{const n=x(e,t.propsLocal);return null==n?Ne:Te(n,e,t)}case"TemplateLiteral":{const n=e.quasis??[],o=e.expressions??[];let r=[Ue(n[0])];for(let e=0;e<o.length;e++){const s=qe(o[e],t);if(s===Ne)return Ne;const i=Ue(n[e+1]),a=[];for(const e of r)for(const t of s)if(a.push(e+t+i),a.length>64)return Ne;r=a}return new Set(r)}case"BinaryExpression":{if("+"!==e.operator)break;const n=qe(e.left,t),o=qe(e.right,t);if(n===Ne||o===Ne)return Ne;const r=new Set;for(const e of n)for(const t of o)if(r.add(e+t),r.size>64)return Ne;return r}case"ConditionalExpression":{const n=E(e.test,t.env,t.propsLocal);if(n.known)return qe(n.value?e.consequent:e.alternate,t);const o=qe(e.consequent,t),r=qe(e.alternate,t);return o===Ne||r===Ne?Ne:new Set([...o,...r])}}const n=E(e,t.env,t.propsLocal);return n.known?new Set([String(n.value)]):Ne}function Te(e,t,n){if(n.setEnv.has(e)){const t=new Set;for(const o of n.setEnv.get(e))t.add(String(o));return t}const o=E(t,n.env,n.propsLocal);return o.known?new Set([String(o.value)]):Ne}function We(e){const t=e.sfc.scriptSetup;if(!t)return()=>null;const n=new Map;for(const e of t.ast.body??[])if("VariableDeclaration"===e.type&&"const"===e.kind)for(const t of e.declarations??[]){if("Identifier"!==t.id?.type||!t.id.name)continue;const e=t.init;if("CallExpression"!==e?.type||"Identifier"!==e.callee?.type)continue;if("computed"!==e.callee.name)continue;const o=e.arguments?.[0];"ArrowFunctionExpression"!==o?.type&&"FunctionExpression"!==o?.type||n.set(t.id.name,o)}return e=>{const t=n.get(e);return t?function(e){const t=e.body;if(!t)return[];if(!Array.isArray(t)&&"BlockStatement"!==t.type)return[t];const n=[],o=e=>{if(e&&"object"==typeof e&&"string"==typeof e.type&&"FunctionDeclaration"!==e.type&&"FunctionExpression"!==e.type&&"ArrowFunctionExpression"!==e.type)if("ReturnStatement"!==e.type)for(const t of Object.values(e))if(Array.isArray(t))for(const e of t)o(e);else t&&"object"==typeof t&&"string"==typeof t.type&&o(t);else e.argument&&n.push(e.argument)};return o(t),n}(t):null}}function Ue(e){const t=e?.value;return t?.cooked??t?.raw??""}const Je=new Set([":is",":where",":not",":has",":matches",":-moz-any",":-webkit-any"]);function Xe(e,t){let n=!1;try{s(e=>{let o=!1,r=!1,s=!0,i=!1;e.each(e=>{i=!0;const n=[];e.walkPseudos(e=>{const t=e.value;(t.includes("deep")||t.includes("global")||t.includes("slotted"))&&(o=!0),Je.has(t)&&(r=!0)}),e.walkClasses(e=>{n.push(e.value)});n.some(e=>!t.has(e))||(s=!1)}),n=i&&s&&!o&&!r}).processSync(e.selector)}catch{return!1}return n}function Ge(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 i=t.get(s.id);r.set(s.id,i.bail?new Set:He(s,i.constFold,i.narrow,i,e))}for(const t of e.values())_e(t,r,n.get(t.id));const s={};for(const t of e.values())s[t.id]=n.get(t.id).toString();return s}function He(e,t,n,o,s){if(0===t.size&&0===n.size)return new Set;!function(e,t,n,o,r){const s=e.code;b(e.sfc.template,e=>{for(const i of A(e)){if(F(i[0].el,r))continue;const e=R(i,t,n);for(const[t,n]of e.removed)Ke(s,t,n,o),r.push([t,n]);for(const[t,n]of e.dirRemovals)Ke(s,t,n,o);if(e.promotion){const{from:t,to:n,text:r}=e.promotion;o.overwrite(t,n,r)}}})}(e,t,n,s,[]);const i=function(e,t,n){const o=new Set,r=e.sfc.scriptSetup;if(!(e.props&&0!==t.size&&r&&e.propsDeclaration&&e.propsPattern))return o;const s=r.base,i=e.props.filter(e=>t.has(e.name));if(0===i.length)return o;for(const e of i)o.add(e.name);const a=i.map(e=>{return`const ${e.name} = ${n=t.get(e.name),void 0===n?"undefined":JSON.stringify(n)};`;var n}).join("\n"),c=e.props.filter(e=>!t.has(e.name)),f=s+(e.propsDeclaration.start??0);let u=s+(e.propsDeclaration.end??0);";"===e.code[u]&&(u+=1);if(0===c.length&&!e.hasRestProp)return n.overwrite(f,u,a),o;const l=e.propsPattern?.properties??[];for(const t of i)Qe(l,t.property,s,n,e.code),Ye(e.definePropsCall,t.name,s,n,e.code);return n.appendLeft(u,`\n${a}`),o}(e,t,s);return function(e,t,n){const o=ze(e,t);if(o.unbounded)return 0;let s=0;for(const t of e.sfc.styles){if(!t.scoped)continue;let e;try{e=r(t.content)}catch{continue}let i=0;if(e.each(e=>{"rule"===e.type&&Xe(e,o.classes)&&(e.remove(),i+=1)}),i>0){const o=t.base+t.content.length;n.overwrite(t.base,o,e.toString()),s+=i}}}(e,{...o,constFold:t,narrow:n},s),i}function Ke(e,t,n,o){let r=t;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]);)r-=1;o.remove(r,n)}function Qe(e,t,n,o,r){const s=e.indexOf(t);Ze(t,e[s-1],n,o,r)}function Ye(e,t,n,o,r){const s=e?.typeParameters?.params?.[0],i=s?.members??[],a=i.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);-1!==a&&Ze(i[a],i[a-1],n,o,r)}function Ze(e,t,n,o,r){const s=n+(e.start??0),i=n+(e.end??0);let a=i;for(;a<r.length&&(" "===r[a]||"\t"===r[a]);)a+=1;if(","===r[a]){let e=a+1;for(;e<r.length&&(" "===r[e]||"\t"===r[e]||"\n"===r[e]);)e+=1;return void o.remove(s,e)}t?o.remove(n+(t.end??0),i):o.remove(s,i)}function _e(e,t,n){const o=e.code;h(e.sfc.template,r=>{if(!p(r)||!r.tag)return;const s=e.imports.get(r.tag),i=s?t.get(s):void 0;if(i&&0!==i.size)for(const e of r.props??[])if(e.type===f)e.name&&i.has(e.name)&&tt(o,e,n);else if(e.type===u&&"bind"===e.name){const t=g(e.arg);t&&i.has(t)&&et(e)&&tt(o,e,n)}})}function et(e){const t=g(e.exp);if(null==t)return!1;const n=w(t);return null!=n&&E(n,new Map).known}function tt(e,t,n){const o=t.loc;let r=o.start.offset;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]||"\n"===e[r-1]);)r-=1;n.remove(r,o.end.offset)}async function nt(e,t,n){const{models:o,plans:r}=await T(e,t,n);return Ge(o,r)}export{T as analyze,W as analyzeInput,U as buildAnalyzeInput,Ge as transformAll,nt as vueShaker};
|
package/dist/scan.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import*as t from"node:fs";import*as e from"node:path";const n=(t,n)=>t.startsWith(".")?e.resolve(e.dirname(n),t):null,s=e=>t.readFileSync(e,"utf-8");function o(n){const s=[];let i;try{i=t.readdirSync(n,{withFileTypes:!0})}catch{return s}for(const t of i){if("node_modules"===t.name||t.name.startsWith("."))continue;const i=e.join(n,t.name);t.isDirectory()?s.push(...o(i)):t.isFile()&&t.name.endsWith(".vue")&&s.push(i)}return s}const i=/\.(?:ts|tsx|js|jsx|mjs|cjs)$/;function r(n){const s=[];let o;try{o=t.readdirSync(n,{withFileTypes:!0})}catch{return s}for(const t of o){if("node_modules"===t.name||t.name.startsWith("."))continue;const o=e.join(n,t.name);t.isDirectory()?s.push(...r(o)):t.isFile()&&i.test(t.name)&&!t.name.endsWith(".d.ts")&&s.push(o)}return s}export{r as collectScriptFiles,o as collectVueFiles,s as fsReadFile,n as fsResolve};
|
package/dist/types/analyze.d.ts
CHANGED
|
@@ -18,10 +18,16 @@ export interface FileModel {
|
|
|
18
18
|
sfc: ParsedSfc;
|
|
19
19
|
/** local import name (`Child`) -> resolved child component id. */
|
|
20
20
|
imports: Map<string, ComponentId>;
|
|
21
|
-
/** Declared props, or `null` if the component has no
|
|
21
|
+
/** Declared props, or `null` if the component has no `defineProps`. */
|
|
22
22
|
props: PropDecl[] | null;
|
|
23
23
|
/** The `const { … } = defineProps()` declarator's ObjectPattern, for editing. */
|
|
24
24
|
propsPattern?: AnyNode | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* The local name when props are NOT destructured (`const props =
|
|
27
|
+
* defineProps()` → `'props'`), so script reads `props.X` can be evaluated.
|
|
28
|
+
* Undefined for the destructured form (refs are then bare names).
|
|
29
|
+
*/
|
|
30
|
+
propsLocal?: string | undefined;
|
|
25
31
|
/** The whole `VariableDeclaration` statement (absolute span via scriptBase). */
|
|
26
32
|
propsDeclaration?: AnyNode | undefined;
|
|
27
33
|
/** The `defineProps<{…}>()` call, whose type args hold the prop type members. */
|
|
@@ -59,7 +65,7 @@ export interface AnalyzeResult {
|
|
|
59
65
|
models: Map<ComponentId, FileModel>;
|
|
60
66
|
plans: Map<ComponentId, ComponentPlan>;
|
|
61
67
|
}
|
|
62
|
-
export declare function analyze(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile): Promise<AnalyzeResult>;
|
|
68
|
+
export declare function analyze(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, escapeScanFiles?: ComponentId[]): Promise<AnalyzeResult>;
|
|
63
69
|
/**
|
|
64
70
|
* The pure, environment-free engine entry (docs/RUST-MIGRATION.md §2): given a
|
|
65
71
|
* fully-resolved, batched {@link AnalyzeInput}, build every component's model and
|
|
@@ -71,7 +77,7 @@ export declare function analyzeInput(input: AnalyzeInput, parseCache?: ParseCach
|
|
|
71
77
|
* the component graph from `entries`, resolving every import edge and reading
|
|
72
78
|
* every reachable `.vue` file up front into a batched {@link AnalyzeInput}.
|
|
73
79
|
*/
|
|
74
|
-
export declare function buildAnalyzeInput(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, parseCache?: ParseCache): Promise<AnalyzeInput>;
|
|
80
|
+
export declare function buildAnalyzeInput(entries: ComponentId | ComponentId[], resolve: Resolve, readFile: ReadFile, parseCache?: ParseCache, escapeScanFiles?: ComponentId[]): Promise<AnalyzeInput>;
|
|
75
81
|
export declare function deadSpansForPlans(models: Map<ComponentId, FileModel>, plans: Map<ComponentId, ComponentPlan>): Map<ComponentId, Span[]>;
|
|
76
82
|
/**
|
|
77
83
|
* Read one `<Child .../>` into a {@link CallSite}. Props are in source order, so
|
package/dist/types/css.d.ts
CHANGED
|
@@ -7,9 +7,11 @@ export interface PossibleClasses {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Compute the component's possible class set (docs §3, step 1). Sources: static
|
|
10
|
-
* `class="a b"`, `:class` with foldable/narrowable expressions,
|
|
11
|
-
* `:class` syntaxes (object keys are always-possible classes)
|
|
12
|
-
*
|
|
10
|
+
* `class="a b"`, `:class` with foldable/narrowable expressions, object/array
|
|
11
|
+
* `:class` syntaxes (object keys are always-possible classes), and — crucially
|
|
12
|
+
* for design systems — `:class="someComputed"` where the classes are built in the
|
|
13
|
+
* script from `props.X` (e.g. `\`fui-button-pattern-${props.pattern}\``). Any
|
|
14
|
+
* spread or non-enumerable source makes the set unbounded (and CSS shaking bails).
|
|
13
15
|
*/
|
|
14
16
|
export declare function computePossibleClasses(model: FileModel, plan: ComponentPlan): PossibleClasses;
|
|
15
17
|
/**
|
package/dist/types/eval.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ export type EvalResult = {
|
|
|
6
6
|
} | {
|
|
7
7
|
known: false;
|
|
8
8
|
};
|
|
9
|
+
/**
|
|
10
|
+
* If `node` is a non-computed `props.X` member read on the props local, return
|
|
11
|
+
* `X`; otherwise null. Lets the evaluator treat the non-destructured props form
|
|
12
|
+
* (`const props = defineProps()`; reads `props.size`) like a bare prop name.
|
|
13
|
+
*/
|
|
14
|
+
export declare function propsMemberName(node: AnyNode, propsLocal: string | undefined): string | null;
|
|
9
15
|
/**
|
|
10
16
|
* A deliberately tiny, total constant evaluator over an ESTree/Babel expression,
|
|
11
17
|
* given an environment of statically-known identifiers. It never throws and
|
|
@@ -19,7 +25,7 @@ export type EvalResult = {
|
|
|
19
25
|
* docs/ARCHITECTURE.md §13 — same contract (sound over-approximation, falls to
|
|
20
26
|
* unknown on non-distributive ops), just without the interprocedural lattice.
|
|
21
27
|
*/
|
|
22
|
-
export declare function evaluate(node: AnyNode | null | undefined, env: Map<string, Literal
|
|
28
|
+
export declare function evaluate(node: AnyNode | null | undefined, env: Map<string, Literal>, propsLocal?: string): EvalResult;
|
|
23
29
|
/**
|
|
24
30
|
* Sound set-aware predicate. `constEnv` holds props collapsed to a single
|
|
25
31
|
* literal (`constFold`); `setEnv` holds props whose reachable value set is known
|
package/dist/types/ir.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ 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
5
|
/** How an imported local name binds to a child `.vue` component. */
|
|
6
|
-
export type EdgeKind = 'default-vue' | 'barrel';
|
|
6
|
+
export type EdgeKind = 'default-vue' | 'namespace' | 'barrel';
|
|
7
7
|
/** One reachable `.vue` source the engine will model. */
|
|
8
8
|
export interface InputFile {
|
|
9
9
|
id: ComponentId;
|
|
@@ -26,6 +26,13 @@ export interface AnalyzeInput {
|
|
|
26
26
|
files: InputFile[];
|
|
27
27
|
edges: ResolvedEdge[];
|
|
28
28
|
entries: ComponentId[];
|
|
29
|
+
/**
|
|
30
|
+
* Components the Shell proved unsafe to fold during resolution — e.g. a
|
|
31
|
+
* namespace object (`const { C } = NS`) that ALSO leaks as a runtime value, so
|
|
32
|
+
* some of its call sites are not enumerable. The engine bails these wholesale,
|
|
33
|
+
* exactly as it bails escaped/barreled components (docs §4.1/§4.2).
|
|
34
|
+
*/
|
|
35
|
+
forcedBails?: ComponentId[];
|
|
29
36
|
}
|
|
30
37
|
/**
|
|
31
38
|
* The delta the dev engine returns after applying file changes (docs §2.1, the
|
package/dist/types/parse.d.ts
CHANGED
|
@@ -34,6 +34,8 @@ export interface AnyNode {
|
|
|
34
34
|
key?: AnyNode | undefined;
|
|
35
35
|
id?: AnyNode | undefined;
|
|
36
36
|
init?: AnyNode | null | undefined;
|
|
37
|
+
/** `VariableDeclaration.kind`: `'const' | 'let' | 'var'`. */
|
|
38
|
+
kind?: string | undefined;
|
|
37
39
|
declarations?: AnyNode[] | undefined;
|
|
38
40
|
declaration?: AnyNode | null | undefined;
|
|
39
41
|
specifiers?: AnyNode[] | undefined;
|
|
@@ -121,6 +123,13 @@ export interface ParsedSfc {
|
|
|
121
123
|
hasPlainScript: boolean;
|
|
122
124
|
styles: StyleBlock[];
|
|
123
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Parse a standalone `.ts`/`.js` module into a Babel `Program`, or null if it
|
|
128
|
+
* does not parse. Used to scan non-`.vue` files in scope for components that
|
|
129
|
+
* escape into script (e.g. `createApp(Dialog, props)`), whose props are passed
|
|
130
|
+
* outside any template call site and so must bail.
|
|
131
|
+
*/
|
|
132
|
+
export declare function parseModuleProgram(code: string): AnyNode | null;
|
|
124
133
|
/**
|
|
125
134
|
* Parse one `.vue` SFC into the engine's loose model. Throws nothing the caller
|
|
126
135
|
* must handle beyond Vue's own parse errors (a malformed SFC); the Shell decides
|
package/dist/types/scan.d.ts
CHANGED
|
@@ -10,3 +10,11 @@ export declare const fsReadFile: ReadFile;
|
|
|
10
10
|
* use it to seed the whole-program crawl (the call-site-completeness set).
|
|
11
11
|
*/
|
|
12
12
|
export declare function collectVueFiles(dir: string): ComponentId[];
|
|
13
|
+
/**
|
|
14
|
+
* Recursively collect every `.ts`/`.js`(x)/`.mjs`/`.cjs` file under `dir`
|
|
15
|
+
* (skipping `node_modules`, dot-dirs, and `.d.ts`). These are scanned for
|
|
16
|
+
* components that escape into script — a `.vue` imported and instantiated
|
|
17
|
+
* programmatically (`createApp(Dialog, props)`) has call sites no template
|
|
18
|
+
* enumerates, so its props must not be folded (docs §4.1).
|
|
19
|
+
*/
|
|
20
|
+
export declare function collectScriptFiles(dir: string): ComponentId[];
|
package/dist/vite.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import*as e from"node:path";import{parse as t}from"@vue/compiler-sfc";import{parseExpression as n,parse as o}from"@babel/parser";import r from"magic-string";import{parse as s}from"postcss";import i from"postcss-selector-parser";import*as c from"node:fs";const a=1,u=2,f=3,l=6,p=7,d=1,m=e=>e.type===a&&e.tagType===d,y=["typescript"];function v(e,n){const{descriptor:r}=t(e,{filename:n}),s=r;let i;if(s.scriptSetup){const e=s.scriptSetup,t=e.attrs?.lang;i={content:e.content,base:e.loc.start.offset,endOffset:e.loc.end.offset,ast:(c=e.content,o(c,{sourceType:"module",plugins:[...y]}).program),lang:"string"==typeof t?t:void 0}}var c;const a=(s.styles??[]).map(e=>({content:e.content,base:e.loc.start.offset,scoped:!0===e.scoped}));return{id:n,code:e,template:s.template?.ast??void 0,scriptSetup:i,hasPlainScript:null!=s.script,styles:a}}function w(e,t,n){return v(t,e)}function h(e){if(null!=e&&""!==e.trim())try{return n(e,{plugins:[...y]})}catch{return}}function g(e){if(e)return"string"==typeof e.content?e.content:void 0}function k(e,t){if(!e)return;if(!1!==t(e))for(const n of e.children??[])k(n,t)}function S(e,t){if(!e)return;const n=e.children;if(n){t(n,e);for(const e of n)S(e,t)}}function b(e,t,n=null){if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n);for(const n of Object.values(e))if(Array.isArray(n))for(const o of n)b(o,t,e);else n&&"object"==typeof n&&"string"==typeof n.type&&b(n,t,e)}}const x={known:!1};function I(e,t){if(!e)return x;switch(e.type){case"Literal":case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return{known:!0,value:e.value};case"NullLiteral":return{known:!0,value:null};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:x}case"UnaryExpression":{const n=I(e.argument,t);if(!n.known)return x;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 x}}case"LogicalExpression":{const n=I(e.left,t);if(!n.known)return x;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 x}}case"BinaryExpression":{const n=I(e.left,t),o=I(e.right,t);if(!n.known||!o.known)return x;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 x}}default:return x}}function E(e,t,n){const o=I(e,t);if(o.known)return o;const r=P(e,t,n);return"unknown"===r?x:{known:!0,value:r}}function P(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?L(P(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=P(e.left,t,n),i=()=>P(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=i(),!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,i()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=M(e,o),i=I(t,n);if(s&&i.known)return D(s,i.value,r);const c=M(t,o),a=I(e,n);return c&&a.known?D(c,a.value,r):a.known&&i.known?r?a.value==i.value:a.value===i.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?L(s):s}return"unknown"}default:return"unknown"}var o,r}function M(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function D(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function L(e){return!0!==e&&(!1===e||"unknown")}function R(e){if(e.type===a)for(const t of e.props??[])if(t.type===p){if("if"===t.name)return{dir:t,kind:"if"};if("else-if"===t.name)return{dir:t,kind:"else-if"};if("else"===t.name)return{dir:t,kind:"else"}}}const j=e=>e.type===u&&"string"==typeof e.content&&""===e.content.trim()||e.type===f;function O(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n],r=R(o);if(!r||"if"!==r.kind)continue;const s=[C(o,r.dir,"if")];let i=n+1;for(;i<e.length;i++){const t=e[i];if(j(t))continue;const n=R(t);if(!n||"if"===n.kind)break;if(s.push(C(t,n.dir,n.kind)),"else"===n.kind){i++;break}}t.push(s),n=i-1}return t}function C(e,t,n){return{el:e,dir:t,kind:n,testAst:"else"===n?void 0:h(g(t.exp))}}function z(e,t,n){const o=e.map(e=>"else"===e.kind?{known:!0,value:!0}:E(e.testAst,t,n)),r=e=>e.known&&Boolean(e.value),s=e=>e.known&&!e.value,i=e=>[e.el.loc.start.offset,e.el.loc.end.offset],c=e=>[e.dir.loc.start.offset,e.dir.loc.end.offset];let a=!0;for(let t=0;t<e.length;t++){if(r(o[t])&&a){const n=[];for(let o=0;o<e.length;o++)o!==t&&n.push(i(e[o]));return{removed:n,dirRemovals:[c(e[t])]}}s(o[t])||(a=!1)}const u=o.findIndex(e=>!s(e));if(-1===u)return{removed:e.map(i),dirRemovals:[]};const f=[];for(let t=0;t<u;t++)f.push(i(e[t]));for(let t=u+1;t<e.length;t++)s(o[t])&&f.push(i(e[t]));if(0===u)return{removed:f,dirRemovals:[]};const l=e[u];if("else"===l.kind)return{removed:f,dirRemovals:[c(l)]};const p=l.dir.loc.start.offset;return{removed:f,dirRemovals:[],promotion:{from:p,to:p+9,text:"v-if"}}}function F(e,t){const n=e.loc;return!!n&&function(e,t){return t.some(([t,n])=>e[0]>=t&&e[1]<=n)}([n.start.offset,n.end.offset],t)}function A(e,t,n){if(!e||0===t.size&&0===n.size)return[];const o=[];return S(e,e=>{for(const r of O(e)){if(F(r[0].el,o))continue;const e=z(r,t,n);for(const t of e.removed)o.push(t)}}),o}const N=e=>e.endsWith(".vue"),B='escapes as value (e.g. <component :is="X">)',W="rendered through a barrel/named import (call sites unobservable)";function $(e){return e.sfc.scriptSetup?.base??0}async function T(e,t,n){return function(e){const t=function(e){const t=new Map;for(const n of e.edges){const e=t.get(n.from);e?e.push(n):t.set(n.from,[n])}const n=new Map;for(const o of e.files)n.set(o.id,G(o,t.get(o.id)??[]));return n}(e),n=new Set;for(const e of t.values())for(const t of e.escapedComponents)n.add(t);for(const e of n){const n=t.get(e);n&&!n.bailReasons.includes(B)&&n.bailReasons.push(B)}const o=new Set;for(const e of t.values())for(const t of e.barrelChildIds)o.add(t);for(const e of o){const n=t.get(e);n&&!n.bailReasons.includes(W)&&n.bailReasons.push(W)}let r=U(t,q(t,new Map));for(let e=0;e<10;e++){const e=U(t,q(t,_(t,r)));if(V(r,e)){r=e;break}r=e}return{models:t,plans:r}}(await async function(e,t,n){const o=Array.isArray(e)?[...e]:[e],r=[],s=[],i=[...o],c=new Set(i);for(;i.length>0;){const e=i.shift();let o,a;try{o=await n(e)}catch{continue}r.push({id:e,code:o});try{a=w(e,o)}catch{continue}if(!a.scriptSetup)continue;const u=new Map,f=[];for(const o of fe(a.scriptSetup.ast)){if("default"===o.imported&&N(o.value)){const n=await t(o.value,e);n&&(s.push({from:e,local:o.local,to:n,kind:"default-vue"}),f.push(n));continue}const r=await me(o.value,o.imported,e,t,n);r&&(s.push({from:e,local:o.local,to:r,kind:"barrel"}),u.set(o.local,r))}const l=Z(a.template,u);for(const e of[...f,...l])c.has(e)||(c.add(e),i.push(e))}return{files:r,edges:s,entries:o}}(e,t,n))}function q(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)??[];$(n);for(const t of n.childCalls)e.length>0&&F(t.node,e)||o(t.childId).sites.push(te(t.node))}return n}function U(e,t){const n=new Map;for(const o of e.values())n.set(o.id,ce(o,t.get(o.id)));return n}function V(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(!J(o.constFold,e.constFold))return!1;if(!X(o.narrow,e.narrow))return!1}return!0}function J(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 X(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=A(o.sfc.template,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function G(e,t,n){const{id:o,code:r}=e,s=w(o,r),i=new Map,c=new Map;for(const e of t)"default-vue"===e.kind?i.set(e.local,e.to):c.set(e.local,e.to);const u=[];if(s.scriptSetup)for(const e of function(e){const t=new Set;return b(e,e=>{"CallExpression"===e.type&&"Identifier"===e.callee?.type&&e.callee.name&&t.add(e.callee.name)}),t}(s.scriptSetup.ast))"defineExpose"===e&&u.push("defineExpose() (public instance surface)");let f,l,d,y=null,v=!1,S=!1;const x=new Set;if(s.scriptSetup){for(const e of fe(s.scriptSetup.ast))x.add(e.local);const e=function(e){const t=e.body??[];for(const e of t)if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=t.init,o=t.id;if("ObjectPattern"!==o?.type||"CallExpression"!==n?.type)continue;const r="Identifier"===n.callee?.type?n.callee.name:void 0;if("defineProps"===r)return{declaration:e,pattern:o,definePropsCall:n,withDefaults:new Map,sharesStatement:(e.declarations?.length??1)>1};if("withDefaults"===r){const t=n.arguments?.[0];if("CallExpression"!==t?.type||"Identifier"!==t.callee?.type)continue;if("defineProps"!==t.callee.name)continue;return{declaration:e,pattern:o,definePropsCall:t,withDefaults:ve(n.arguments?.[1]),sharesStatement:(e.declarations?.length??1)>1}}}return null}(s.scriptSetup.ast);if(e){l=e.declaration,f=e.pattern,d=e.definePropsCall,S=e.sharesStatement,e.sharesStatement&&u.push("defineProps() shares a multi-declarator statement"),y=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){v=!0;continue}if("ObjectProperty"!==t.type)continue;const n=t.key;if("Identifier"!==n?.type||!n.name)continue;const o=t.value,r="AssignmentPattern"===o?.type?o.right:void 0;y.push({name:n.name,property:t,defaultExpr:r??e.withDefaults.get(n.name)})}}}const I=function(e,t){const n=[];return k(e,e=>{if(m(e)&&e.tag){const o=t.get(e.tag);o&&n.push({childId:o,node:e})}}),n}(s.template,i),E=Z(s.template,c),P=function(e){const t=new Set;k(e.template,e=>{if(e.type===a)for(const n of e.props??[])n.type===p&&("for"===n.name?H(g(n.exp),t):"slot"===n.name&&K(g(n.exp),t))});const n=e.scriptSetup;n&&b(n.ast,e=>{if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])Q(n,t)});return t}(s),M=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};k(e.template,e=>{if(e.type===a)for(const t of e.props??[])if(t.type===p&&"bind"===t.name&&"is"===ee(t.arg)){const e=h(g(t.exp));"Identifier"===e?.type&&r(e.name)}});const s=e.scriptSetup;s&&b(s.ast,(e,o)=>{"Identifier"===e.type&&e.name&&t.has(e.name)&&n.has(e.name)&&function(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!!("ObjectProperty"!==t.type&&"Property"!==t.type||t.key!==e||t.computed||!0===t.shorthand)&&!Y(t)))}(e,o)&&!Y(o)&&r(e.name)});return o}(s,i,x);return{id:o,code:r,sfc:s,imports:i,props:y,propsPattern:f,propsDeclaration:l,definePropsCall:d,hasRestProp:v,sharesStatement:S,childCalls:I,shadowedNames:P,escapedComponents:M,barrelChildIds:E,bailReasons:u}}function H(e,t){if(!e)return;const n=/^(.*?)\s+(?:in|of)\s+/s.exec(e),o=(n?n[1]:e).trim();o&&K(o,t)}function K(e,t){if(!e)return;const n=h(`(${e}) => 0`);if("ArrowFunctionExpression"!==n?.type){const n=h(e);return void(n&&Q(n,t))}for(const e of n.params??[])Q(e,t)}function Q(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectExpression":case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?Q(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||Q(n.value??n.key,t);return;case"ArrayExpression":case"ArrayPattern":for(const n of e.elements??[])Q(n,t);return;case"AssignmentPattern":return void Q(e.left,t);case"RestElement":return void Q(e.argument,t);default:return}}function Y(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function Z(e,t){const n=new Set;return 0===t.size||k(e,e=>{if(m(e)&&e.tag){const o=t.get(e.tag);o&&n.add(o)}}),n}function ee(e){return g(e)}function te(e,t){const n=e.props??[];let o=-1;for(let e=0;e<n.length;e++){const t=n[e];t.type===p&&"bind"===t.name&&ne(t)&&(o=e)}const r=new Map;for(let e=0;e<n.length;e++){const t=n[e],s=e>o;if(t.type===l){if(!t.name)continue;const e=re(t);r.set(t.name,{value:e,dynamic:!1,afterLastSpread:s});continue}if(t.type===p){if("bind"===t.name){if(ne(t))continue;const e=g(t.arg);if(!e)continue;const n=se(g(t.exp));r.set(e,n.known?{value:n.value,dynamic:!1,afterLastSpread:s}:oe(s));continue}if("model"===t.name){const e=g(t.arg)??"modelValue";r.set(e,oe(s));continue}}}return{hadSpread:o>=0,explicit:r}}function ne(e){return null==e.arg||null==g(e.arg)}function oe(e){return{value:void 0,dynamic:!0,afterLastSpread:e}}function re(e){const t=e.value;return!t||("string"==typeof t.content?t.content:"")}function se(e){const t=h(e);return t?I(t,new Map):{known:!1}}function ie(e,t){return e.shadowedNames.has(t)}function ce(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(ie(e,t.name))continue;const r=ae(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 ae(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 i=ue(e.defaultExpr);i.known?s(i.value):o=!0}return{values:n,dynamic:o,top:r}}function ue(e){return e?I(e,new Map):{known:!0,value:void 0}}function*fe(e){const t=e.body??[];for(const e of t){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:le(n)??e}))}}}function le(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"StringLiteral"===t?.type&&"string"==typeof t.value?t.value:void 0}function pe(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}const de=8;async function me(e,t,n,o,r,s=0){if(s>de)return null;const i=await o(e,n);if(!i)return null;if(N(e)||N(i))return"default"===t||"*"===t?i:null;let c;try{c=await r(i)}catch{return null}const a=function(e){try{const t=v(`<script setup>\n${e}\n<\/script>`,"barrel.vue");return t.scriptSetup?.ast.body??null}catch{return null}}(c);if(!a)return null;for(const e of a)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(pe(n.exported)===t)return me(String(e.source.value),pe(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 me(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(pe(n.exported)!==t)continue;const e=pe(n.local);if(!e)continue;const c=ye(a,e);return c?me(c.value,c.imported,i,o,r,s+1):null}return null}function ye(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:le(o)??t}}}return null}function ve(e){const t=new Map;if("ObjectExpression"!==e?.type)return t;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)continue;const e=n.key,o="Identifier"===e?.type?e.name:void 0;o&&n.value&&t.set(o,n.value)}return t}const we=Symbol("unbounded-class-source");function he(e,t,n){if(!e)return we;if("ObjectExpression"===e.type){const t=new Set;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)return we;const e=n.key,o="Identifier"===e?.type?e.name:"StringLiteral"===e?.type?e.value:void 0;if(null==o)return we;for(const e of o.split(/\s+/))e&&t.add(e)}return t}if("ArrayExpression"===e.type){const o=new Set;for(const r of e.elements??[]){if(!r)continue;const e=he(r,t,n);if(e===we)return we;for(const t of e)o.add(t)}return o}const o=ge(e,t,n);if(o===we)return we;const r=new Set;for(const e of o)for(const t of e.split(/\s+/))t&&r.add(t);return r}function ge(e,t,n){if(!e)return we;switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return new Set([String(e.value)]);case"Identifier":{const o=e.name??"";if(n.has(o)){const e=new Set;for(const t of n.get(o))e.add(String(t));return e}const r=I(e,t);return r.known?new Set([String(r.value)]):we}case"TemplateLiteral":{const o=e.quasis??[],r=e.expressions??[];let s=[ke(o[0])];for(let e=0;e<r.length;e++){const i=ge(r[e],t,n);if(i===we)return we;const c=ke(o[e+1]),a=[];for(const e of s)for(const t of i)if(a.push(e+t+c),a.length>64)return we;s=a}return new Set(s)}case"BinaryExpression":{if("+"!==e.operator)break;const o=ge(e.left,t,n),r=ge(e.right,t,n);if(o===we||r===we)return we;const s=new Set;for(const e of o)for(const t of r)if(s.add(e+t),s.size>64)return we;return s}case"ConditionalExpression":{const o=I(e.test,t);if(o.known)return ge(o.value?e.consequent:e.alternate,t,n);const r=ge(e.consequent,t,n),s=ge(e.alternate,t,n);return r===we||s===we?we:new Set([...r,...s])}}const o=I(e,t);return o.known?new Set([String(o.value)]):we}function ke(e){const t=e?.value;return t?.cooked??t?.raw??""}function Se(e,t,n){const o=function(e,t){const n=new Set;let o=!1;const r=t.constFold,s=t.narrow;return k(e.sfc.template,e=>{if(e.type===a)for(const t of e.props??[]){if(t.type===l){if("class"===t.name){const e=t.value,o="string"==typeof e?.content?e.content:"";for(const e of o.split(/\s+/))e&&n.add(e)}continue}if(t.type!==p||"bind"!==t.name)continue;const e=g(t.arg);if(null==t.arg||null==e){o=!0;continue}if("class"!==e)continue;const i=he(h(g(t.exp)),r,s);if(i===we)o=!0;else for(const e of i)n.add(e)}}),{classes:n,unbounded:o}}(e,t);if(o.unbounded)return 0;let r=0;for(const t of e.sfc.styles){if(!t.scoped)continue;let e;try{e=s(t.content)}catch{continue}let i=0;if(e.each(e=>{"rule"===e.type&&be(e,o.classes)&&(e.remove(),i+=1)}),i>0){const o=t.base+t.content.length;n.overwrite(t.base,o,e.toString()),r+=i}}return r}function be(e,t){let n=!1;try{i(e=>{let o=!1,r=!0,s=!1;e.each(e=>{s=!0;const n=[];e.walkPseudos(e=>{const t=e.value;(t.includes("deep")||t.includes("global")||t.includes("slotted"))&&(o=!0)}),e.walkClasses(e=>{n.push(e.value)});n.some(e=>!t.has(e))||(r=!1)}),n=s&&r&&!o}).processSync(e.selector)}catch{return!1}return n}function xe(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;!function(e,t,n,o,r){const s=e.code;S(e.sfc.template,e=>{for(const i of O(e)){if(F(i[0].el,r))continue;const e=z(i,t,n);for(const[t,n]of e.removed)Ie(s,t,n,o),r.push([t,n]);for(const[t,n]of e.dirRemovals)Ie(s,t,n,o);if(e.promotion){const{from:t,to:n,text:r}=e.promotion;o.overwrite(t,n,r)}}})}(e,t,n,r,[]);const s=function(e,t,n){const o=new Set,r=e.sfc.scriptSetup;if(!(e.props&&0!==t.size&&r&&e.propsDeclaration&&e.propsPattern))return o;const s=r.base,i=e.props.filter(e=>t.has(e.name));if(0===i.length)return o;for(const e of i)o.add(e.name);const c=i.map(e=>{return`const ${e.name} = ${n=t.get(e.name),void 0===n?"undefined":JSON.stringify(n)};`;var n}).join("\n"),a=e.props.filter(e=>!t.has(e.name)),u=s+(e.propsDeclaration.start??0);let f=s+(e.propsDeclaration.end??0);";"===e.code[f]&&(f+=1);if(0===a.length&&!e.hasRestProp)return n.overwrite(u,f,c),o;const l=e.propsPattern?.properties??[];for(const t of i)Ee(l,t.property,s,n,e.code),Pe(e.definePropsCall,t.name,s,n,e.code);return n.appendLeft(f,`\n${c}`),o}(e,t,r);return Se(e,{...o,constFold:t,narrow:n},r),s}function Ie(e,t,n,o){let r=t;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]);)r-=1;o.remove(r,n)}function Ee(e,t,n,o,r){const s=e.indexOf(t);Me(t,e[s-1],n,o,r)}function Pe(e,t,n,o,r){const s=e?.typeParameters?.params?.[0],i=s?.members??[],c=i.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);-1!==c&&Me(i[c],i[c-1],n,o,r)}function Me(e,t,n,o,r){const s=n+(e.start??0),i=n+(e.end??0);let c=i;for(;c<r.length&&(" "===r[c]||"\t"===r[c]);)c+=1;if(","===r[c]){let e=c+1;for(;e<r.length&&(" "===r[e]||"\t"===r[e]||"\n"===r[e]);)e+=1;return void o.remove(s,e)}t?o.remove(n+(t.end??0),i):o.remove(s,i)}function De(e,t,n){const o=e.code;k(e.sfc.template,r=>{if(!m(r)||!r.tag)return;const s=e.imports.get(r.tag),i=s?t.get(s):void 0;if(i&&0!==i.size)for(const e of r.props??[])if(e.type===l)e.name&&i.has(e.name)&&Re(o,e,n);else if(e.type===p&&"bind"===e.name){const t=g(e.arg);t&&i.has(t)&&Le(e)&&Re(o,e,n)}})}function Le(e){const t=g(e.exp);if(null==t)return!1;const n=h(t);return null!=n&&I(n,new Map).known}function Re(e,t,n){const o=t.loc;let r=o.start.offset;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]||"\n"===e[r-1]);)r-=1;n.remove(r,o.end.offset)}const je=(t,n)=>t.startsWith(".")?e.resolve(e.dirname(n),t):null,Oe=e=>c.readFileSync(e,"utf-8");function Ce(t){const n=[];let o;try{o=c.readdirSync(t,{withFileTypes:!0})}catch{return n}for(const r of o){if("node_modules"===r.name||r.name.startsWith("."))continue;const o=e.join(t,r.name);r.isDirectory()?n.push(...Ce(o)):r.isFile()&&r.name.endsWith(".vue")&&n.push(o)}return n}function ze(t={}){const n=t.include??["."];let o=process.cwd(),s={};return{name:"vue-shaker",enforce:"pre",apply:"build",configResolved(e){o=e.root},async buildStart(){const t=n.flatMap(t=>Ce(e.resolve(o,t)));if(0===t.length)return;const{models:i,plans:c}=await T(t,je,Oe);s=function(e,t){const n=new Map,o=new Map;for(const s of e.values()){const e=new r(s.code);n.set(s.id,e);const i=t.get(s.id);o.set(s.id,i.bail?new Set:xe(s,i.constFold,i.narrow,i,e))}for(const t of e.values())De(t,o,n.get(t.id));const s={};for(const t of e.values())s[t.id]=n.get(t.id).toString();return s}(i,c)},transform(t,n){if(n.includes("?"))return null;const o=n.split("?",1)[0];if(!o.endsWith(".vue"))return null;const r=s[o]??s[e.normalize(o)];return null==r||r===t?null:{code:r,map:null}}}}export{ze as shaker};
|
|
1
|
+
import*as e from"node:path";import{parse as t}from"@vue/compiler-sfc";import{parseExpression as n,parse as o}from"@babel/parser";import r from"magic-string";import{parse as s}from"postcss";import i from"postcss-selector-parser";import*as a from"node:fs";const c=1,u=2,f=3,l=6,p=7,d=1,m=e=>e.type===c&&e.tagType===d,y=["typescript"];function v(e){return o(e,{sourceType:"module",plugins:[...y]}).program}function w(e,n){const{descriptor:o}=t(e,{filename:n}),r=o;let s;if(r.scriptSetup){const e=r.scriptSetup,t=e.attrs?.lang;s={content:e.content,base:e.loc.start.offset,endOffset:e.loc.end.offset,ast:v(e.content),lang:"string"==typeof t?t:void 0}}const i=(r.styles??[]).map(e=>({content:e.content,base:e.loc.start.offset,scoped:!0===e.scoped}));return{id:n,code:e,template:r.template?.ast??void 0,scriptSetup:s,hasPlainScript:null!=r.script,styles:i}}function h(e,t,n){return w(t,e)}function g(e){if(null!=e&&""!==e.trim())try{return n(e,{plugins:[...y]})}catch{return}}function b(e){if(e)return"string"==typeof e.content?e.content:void 0}function S(e,t){if(!e)return;if(!1!==t(e))for(const n of e.children??[])S(n,t)}function k(e,t){if(!e)return;const n=e.children;if(n){t(n,e);for(const e of n)k(e,t)}}function x(e,t,n=null){if(e&&"object"==typeof e&&"string"==typeof e.type){t(e,n);for(const n of Object.values(e))if(Array.isArray(n))for(const o of n)x(o,t,e);else n&&"object"==typeof n&&"string"==typeof n.type&&x(n,t,e)}}const E={known:!1};function I(e,t){return t&&"MemberExpression"===e.type&&!0!==e.computed&&"Identifier"===e.object?.type&&e.object.name===t&&"Identifier"===e.property?.type&&e.property.name?e.property.name:null}function P(e,t,n){if(!e)return E;switch(e.type){case"Literal":case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return{known:!0,value:e.value};case"NullLiteral":return{known:!0,value:null};case"Identifier":{const n=e.name??"";return"undefined"===n?{known:!0,value:void 0}:t.has(n)?{known:!0,value:t.get(n)}:E}case"MemberExpression":{const o=I(e,n);return null!=o&&t.has(o)?{known:!0,value:t.get(o)}:E}case"UnaryExpression":{const o=P(e.argument,t,n);if(!o.known)return E;const r=o.value;switch(e.operator){case"!":return{known:!0,value:!r};case"-":return{known:!0,value:-r};case"+":return{known:!0,value:+r};case"typeof":return{known:!0,value:typeof r};case"void":return{known:!0,value:void 0};default:return E}}case"LogicalExpression":{const o=P(e.left,t,n);if(!o.known)return E;switch(e.operator){case"&&":return o.value?P(e.right,t,n):o;case"||":return o.value?o:P(e.right,t,n);case"??":return null===o.value||void 0===o.value?P(e.right,t,n):o;default:return E}}case"BinaryExpression":{const o=P(e.left,t,n),r=P(e.right,t,n);if(!o.known||!r.known)return E;const s=o.value,i=r.value;switch(e.operator){case"===":return{known:!0,value:s===i};case"!==":return{known:!0,value:s!==i};case"==":return{known:!0,value:s==i};case"!=":return{known:!0,value:s!=i};case"<":return{known:!0,value:s<i};case">":return{known:!0,value:s>i};case"<=":return{known:!0,value:s<=i};case">=":return{known:!0,value:s>=i};case"+":return{known:!0,value:s+i};case"-":return{known:!0,value:s-i};case"*":return{known:!0,value:s*i};case"/":return{known:!0,value:s/i};case"%":return{known:!0,value:s%i};default:return E}}default:return E}}function j(e,t,n){const o=P(e,t);if(o.known)return o;const r=D(e,t,n);return"unknown"===r?E:{known:!0,value:r}}function D(e,t,n){if(!e)return"unknown";switch(e.type){case"UnaryExpression":return"!"===e.operator?O(D(e.argument,t,n)):"unknown";case"LogicalExpression":{const s=D(e.left,t,n),i=()=>D(e.right,t,n);return"&&"===e.operator?!1!==s&&(o=s,r=i(),!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,i()):"unknown"}case"BinaryExpression":{const o=e.operator;if("==="===o||"=="===o||"!=="===o||"!="===o){const r="=="===o||"!="===o,s=function(e,t,n,o,r){const s=M(e,o),i=P(t,n);if(s&&i.known)return L(s,i.value,r);const a=M(t,o),c=P(e,n);return a&&c.known?L(a,c.value,r):c.known&&i.known?r?c.value==i.value:c.value===i.value:"unknown"}(e.left,e.right,t,n,r);return"!=="===o||"!="===o?O(s):s}return"unknown"}default:return"unknown"}var o,r}function M(e,t){return"Identifier"===e?.type&&e.name&&t.has(e.name)?t.get(e.name):null}function L(e,t,n){const o=n?e=>e==t:e=>e===t;return!!e.some(o)&&(!!e.every(o)||"unknown")}function O(e){return!0!==e&&(!1===e||"unknown")}function A(e){if(e.type===c)for(const t of e.props??[])if(t.type===p){if("if"===t.name)return{dir:t,kind:"if"};if("else-if"===t.name)return{dir:t,kind:"else-if"};if("else"===t.name)return{dir:t,kind:"else"}}}const C=e=>e.type===u&&"string"==typeof e.content&&""===e.content.trim()||e.type===f;function F(e){const t=[];for(let n=0;n<e.length;n++){const o=e[n],r=A(o);if(!r||"if"!==r.kind)continue;const s=[R(o,r.dir,"if")];let i=n+1;for(;i<e.length;i++){const t=e[i];if(C(t))continue;const n=A(t);if(!n||"if"===n.kind)break;if(s.push(R(t,n.dir,n.kind)),"else"===n.kind){i++;break}}t.push(s),n=i-1}return t}function R(e,t,n){return{el:e,dir:t,kind:n,testAst:"else"===n?void 0:g(b(t.exp))}}function N(e,t,n){const o=e.map(e=>"else"===e.kind?{known:!0,value:!0}:j(e.testAst,t,n)),r=e=>e.known&&Boolean(e.value),s=e=>e.known&&!e.value,i=e=>[e.el.loc.start.offset,e.el.loc.end.offset],a=e=>[e.dir.loc.start.offset,e.dir.loc.end.offset];let c=!0;for(let t=0;t<e.length;t++){if(r(o[t])&&c){const n=[];for(let o=0;o<e.length;o++)o!==t&&n.push(i(e[o]));return{removed:n,dirRemovals:[a(e[t])]}}s(o[t])||(c=!1)}const u=o.findIndex(e=>!s(e));if(-1===u)return{removed:e.map(i),dirRemovals:[]};const f=[];for(let t=0;t<u;t++)f.push(i(e[t]));for(let t=u+1;t<e.length;t++)s(o[t])&&f.push(i(e[t]));if(0===u)return{removed:f,dirRemovals:[]};const l=e[u];if("else"===l.kind)return{removed:f,dirRemovals:[a(l)]};const p=l.dir.loc.start.offset;return{removed:f,dirRemovals:[],promotion:{from:p,to:p+9,text:"v-if"}}}function z(e,t){const n=e.loc;return!!n&&function(e,t){return t.some(([t,n])=>e[0]>=t&&e[1]<=n)}([n.start.offset,n.end.offset],t)}function $(e,t,n){if(!e||0===t.size&&0===n.size)return[];const o=[];return k(e,e=>{for(const r of F(e)){if(z(r[0].el,o))continue;const e=N(r,t,n);for(const t of e.removed)o.push(t)}}),o}const B=e=>e.endsWith(".vue"),W='escapes as value (e.g. <component :is="X">)',V="rendered through a barrel/named import (call sites unobservable)",T="reached through a namespace object that also leaks as a value (call sites unobservable)";function q(e){return e.sfc.scriptSetup?.base??0}async function _(e,t,n,o){return function(e){const t=function(e){const t=new Map;for(const n of e.edges){const e=t.get(n.from);e?e.push(n):t.set(n.from,[n])}const n=new Map;for(const o of e.files)n.set(o.id,Q(o,t.get(o.id)??[]));return n}(e),n=new Set;for(const e of t.values())for(const t of e.escapedComponents)n.add(t);for(const e of n){const n=t.get(e);n&&!n.bailReasons.includes(W)&&n.bailReasons.push(W)}const o=new Set;for(const e of t.values())for(const t of e.barrelChildIds)o.add(t);for(const e of o){const n=t.get(e);n&&!n.bailReasons.includes(V)&&n.bailReasons.push(V)}for(const n of e.forcedBails??[]){const e=t.get(n);e&&!e.bailReasons.includes(T)&&e.bailReasons.push(T)}let r=J(t,U(t,new Map));for(let e=0;e<10;e++){const e=J(t,U(t,K(t,r)));if(X(r,e)){r=e;break}r=e}return{models:t,plans:r}}(await async function(e,t,n,o,r){const s=Array.isArray(e)?[...e]:[e],i=[],a=[],c=new Set,u=new Map,f=[...s],l=new Set(f);for(;f.length>0;){const e=f.shift();let o,r;try{o=await n(e)}catch{continue}i.push({id:e,code:o});try{r=h(e,o)}catch{continue}if(!r.scriptSetup)continue;const s=r.scriptSetup.ast,p=[...je(s)],d=new Map;for(const e of p)d.set(e.local,e);const m=new Map,y=[];for(const o of p){if("default"===o.imported&&B(o.value)){const n=await t(o.value,e);n&&(a.push({from:e,local:o.local,to:n,kind:"default-vue"}),y.push(n));continue}const r=await Oe(o.value,o.imported,e,t,n);r&&(a.push({from:e,local:o.local,to:r,kind:"barrel"}),m.set(o.local,r))}for(const[o,i]of oe(s)){const f=d.get(o);if(!f)continue;const l=await ie(f.value,f.imported,e,t,n,u);if(!l)continue;if(i.unsafe||pe(s,o,i.initNodes)||we(r.template,o))for(const e of fe(l))c.add(e);else for(const t of i.bindings){const n=ue(l,t.path);n&&(a.push({from:e,local:t.local,to:n,kind:"namespace"}),y.push(n))}}const v=ne(r.template,m);for(const e of[...y,...v])l.has(e)||(l.add(e),f.push(e))}const p=await Promise.all((r??[]).map(e=>async function(e,t,n){let o;try{o=await n(e)}catch{return[]}if(!o.includes(".vue"))return[];const r=function(e){try{return v(e)}catch{return null}}(o);if(!r)return[];const s=new Map;for(const e of r.body??[]){if("ImportDeclaration"!==e.type)continue;const t=e.source?.value;if("string"==typeof t&&B(t))for(const n of e.specifiers??[])"ImportDefaultSpecifier"!==n.type&&"ImportNamespaceSpecifier"!==n.type||!n.local?.name||s.set(n.local.name,t)}if(0===s.size)return[];const i=function(e){const t=new Set;return x(e,e=>{if("ObjectExpression"===e.type)for(const n of e.properties??[])"ObjectProperty"!==n.type&&"Property"!==n.type||!n.shorthand||(n.key&&t.add(n.key),n.value&&t.add(n.value))}),t}(r),a=new Set;de(r,(e,t)=>{s.has(e)&&!i.has(t)&&a.add(e)});const c=await Promise.all([...a].map(n=>t(s.get(n),e)));return c.filter(e=>null!=e)}(e,t,n)));for(const e of p)for(const t of e)c.add(t);return{files:i,edges:a,entries:s,forcedBails:[...c]}}(e,t,n,0,o))}function U(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)??[];q(n);for(const t of n.childCalls)e.length>0&&z(t.node,e)||o(t.childId).sites.push(he(t.node))}return n}function J(e,t){const n=new Map;for(const o of e.values())n.set(o.id,Ee(o,t.get(o.id)));return n}function X(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(!G(o.constFold,e.constFold))return!1;if(!H(o.narrow,e.narrow))return!1}return!0}function G(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 H(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 K(e,t){const n=new Map;for(const o of e.values()){const e=t.get(o.id);if(e.bail)continue;const r=$(o.sfc.template,e.constFold,e.narrow);r.length>0&&n.set(o.id,r)}return n}function Q(e,t,n){const{id:o,code:r}=e,s=h(o,r),i=new Map,a=new Map,u=new Set;for(const e of t)"default-vue"===e.kind?i.set(e.local,e.to):"namespace"===e.kind?(i.set(e.local,e.to),u.add(e.local)):a.set(e.local,e.to);const f=[];if(s.scriptSetup)for(const e of function(e){const t=new Set;return x(e,e=>{"CallExpression"===e.type&&"Identifier"===e.callee?.type&&e.callee.name&&t.add(e.callee.name)}),t}(s.scriptSetup.ast))"defineExpose"===e&&f.push("defineExpose() (public instance surface)");let l,d,y,v,w=null,k=!1,E=!1;const I=new Set;if(s.scriptSetup){for(const e of je(s.scriptSetup.ast))I.add(e.local);for(const e of u)I.add(e);const e=function(e){const t=e.body??[];for(const e of t)if("VariableDeclaration"===e.type)for(const t of e.declarations??[]){const n=Fe(t.init);if(!n)continue;const o=t.id,r=(e.declarations?.length??1)>1;if("ObjectPattern"===o?.type)return{declaration:e,pattern:o,propsLocal:void 0,sharesStatement:r,...n};if("Identifier"===o?.type&&o.name)return{declaration:e,pattern:void 0,propsLocal:o.name,sharesStatement:r,...n}}return null}(s.scriptSetup.ast);if(e)if(y=e.declaration,l=e.pattern,d=e.propsLocal,v=e.definePropsCall,E=e.sharesStatement,e.sharesStatement&&f.push("defineProps() shares a multi-declarator statement"),e.pattern){w=[];for(const t of e.pattern.properties??[]){if("RestElement"===t.type){k=!0;continue}if("ObjectProperty"!==t.type)continue;const n=t.key;if("Identifier"!==n?.type||!n.name)continue;const o=t.value,r="AssignmentPattern"===o?.type?o.right:void 0;w.push({name:n.name,property:t,defaultExpr:r??e.withDefaults.get(n.name)})}}else w=function(e,t){const n=[],o=e?.typeParameters?.params?.[0];for(const e of o?.members??[]){const o=e.key;"Identifier"===o?.type&&o.name&&n.push({name:o.name,property:e,defaultExpr:t.get(o.name)})}return n}(e.definePropsCall,e.withDefaults)}const P=function(e,t){const n=[];return S(e,e=>{if(m(e)&&e.tag){const o=t.get(e.tag);o&&n.push({childId:o,node:e})}}),n}(s.template,i),j=ne(s.template,a),D=function(e){const t=new Set;S(e.template,e=>{if(e.type===c)for(const n of e.props??[])n.type===p&&("for"===n.name?Y(b(n.exp),t):"slot"===n.name&&Z(b(n.exp),t))});const n=e.scriptSetup;n&&x(n.ast,e=>{if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)for(const n of e.params??[])ee(n,t)});return t}(s);for(const e of I)D.add(e);const M=function(e,t,n){const o=new Set,r=e=>{if(!e)return;const n=t.get(e);n&&o.add(n)};S(e.template,e=>{if(e.type===c)for(const t of e.props??[])if(t.type===p&&"bind"===t.name&&"is"===ye(t.arg)){const e=g(b(t.exp));"Identifier"===e?.type?r(e.name):"MemberExpression"===e?.type&&r(ve(e))}});const s=e.scriptSetup;if(s){const e=function(e){const t=new Set;return x(e,e=>{if("VariableDeclarator"===e.type)me(e.id,t);else if("FunctionDeclaration"===e.type||"FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type||"ObjectMethod"===e.type||"ClassMethod"===e.type)for(const n of e.params??[])me(n,t);else"ImportDefaultSpecifier"!==e.type&&"ImportSpecifier"!==e.type&&"ImportNamespaceSpecifier"!==e.type||e.local&&t.add(e.local)}),t}(s.ast);x(s.ast,(o,s)=>{"Identifier"===o.type&&o.name&&t.has(o.name)&&n.has(o.name)&&function(e,t){return!!t&&(!("MemberExpression"===t.type&&t.property===e&&!t.computed)&&(!!("ObjectProperty"!==t.type&&"Property"!==t.type||t.key!==e||t.computed||!0===t.shorthand)&&!te(t)))}(o,s)&&!te(s)&&!e.has(o)&&r(o.name)})}return o}(s,i,I);return{id:o,code:r,sfc:s,imports:i,props:w,propsPattern:l,propsLocal:d,propsDeclaration:y,definePropsCall:v,hasRestProp:k,sharesStatement:E,childCalls:P,shadowedNames:D,escapedComponents:M,barrelChildIds:j,bailReasons:f}}function Y(e,t){if(!e)return;const n=/^(.*?)\s+(?:in|of)\s+/s.exec(e),o=(n?n[1]:e).trim();o&&Z(o,t)}function Z(e,t){if(!e)return;const n=g(`(${e}) => 0`);if("ArrowFunctionExpression"!==n?.type){const n=g(e);return void(n&&ee(n,t))}for(const e of n.params??[])ee(e,t)}function ee(e,t){if(e)switch(e.type){case"Identifier":return void(e.name&&t.add(e.name));case"ObjectExpression":case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?ee(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||ee(n.value??n.key,t);return;case"ArrayExpression":case"ArrayPattern":for(const n of e.elements??[])ee(n,t);return;case"AssignmentPattern":return void ee(e.left,t);case"RestElement":return void ee(e.argument,t);default:return}}function te(e){return null!=e&&("ImportSpecifier"===e.type||"ImportDefaultSpecifier"===e.type||"ImportNamespaceSpecifier"===e.type||"ExportSpecifier"===e.type)}function ne(e,t){const n=new Set;return 0===t.size||S(e,e=>{if(m(e)&&e.tag){const o=t.get(e.tag);o&&n.add(o)}}),n}function oe(e){const t=new Map;return x(e,e=>{if("VariableDeclarator"!==e.type)return;const n=e.init,o=e.id;if("Identifier"!==n?.type||!n.name||"ObjectPattern"!==o?.type)return;let r=t.get(n.name);r||(r={initNodes:new Set,bindings:[],unsafe:!1},t.set(n.name,r)),r.initNodes.add(n),re(o,[],r)}),t}function re(e,t,n){for(const o of e.properties??[]){if("RestElement"===o.type){n.unsafe=!0;continue}if("ObjectProperty"!==o.type&&"Property"!==o.type)continue;if(o.computed){n.unsafe=!0;continue}const e=se(o.key);if(!e){n.unsafe=!0;continue}const r=o.value??void 0;"Identifier"===r?.type&&r.name?n.bindings.push({path:[...t,e],local:r.name}):"ObjectPattern"===r?.type?re(r,[...t,e],n):n.unsafe=!0}}function se(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}async function ie(e,t,n,o,r,s){const i=await o(e,n);if(!i)return null;const a=`${i}\0${t}`,c=s.get(a);if(void 0!==c)return c;const u=await async function(e,t,n,o){let r;try{r=await o(e)}catch{return null}const s=Ce(r);if(!s)return null;const i=new Map;for(const e of s){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?i.set(e,{value:t,imported:"default"}):"ImportSpecifier"===n.type&&i.set(e,{value:t,imported:De(n)??e}))}}const a=function(e,t){for(const n of e){if("ExportNamedDeclaration"===n.type&&"VariableDeclaration"===n.declaration?.type){const e=ae(n.declaration,t);if(e)return e}if("VariableDeclaration"===n.type){const e=ae(n,t);if(e)return e}if("ExportDefaultDeclaration"===n.type&&"default"===t&&"ObjectExpression"===n.declaration?.type)return n.declaration}return null}(s,t);return a?ce(a,e,i,n):null}(i,t,o,r);return s.set(a,u),u}function ae(e,t){for(const n of e.declarations??[])if("Identifier"===n.id?.type&&n.id.name===t&&"ObjectExpression"===n.init?.type)return n.init;return null}async function ce(e,t,n,o){const r=new Map,s=await Promise.all((e.properties??[]).map(async e=>{if("ObjectProperty"!==e.type&&"Property"!==e.type)return null;if(e.computed)return null;const r=se(e.key);if(!r)return null;const s=e.value;if("Identifier"===s?.type&&s.name){const e=n.get(s.name);if(!e||"default"!==e.imported||!B(e.value))return null;const i=await o(e.value,t);return i?[r,i]:null}return"ObjectExpression"===s?.type?[r,await ce(s,t,n,o)]:null}));for(const e of s)e&&r.set(e[0],e[1]);return r}function ue(e,t){let n=e;for(const e of t){if("string"==typeof n)return null;const t=n.get(e);if(void 0===t)return null;n=t}return"string"==typeof n?n:null}function fe(e){const t=[];for(const n of e.values())"string"==typeof n?t.push(n):t.push(...fe(n));return t}const le=new Set(["typeAnnotation","typeParameters","returnType","typeArguments"]);function pe(e,t,n){let o=!1;return de(e,(e,r)=>{e!==t||n.has(r)||(o=!0)}),o}function de(e,t){if(e&&"object"==typeof e&&"string"==typeof e.type&&"ImportDeclaration"!==e.type)if("Identifier"!==e.type){for(const[n,o]of Object.entries(e))if(!le.has(n))if(Array.isArray(o))for(const e of o)de(e,t);else o&&"object"==typeof o&&"string"==typeof o.type&&de(o,t)}else e.name&&t(e.name,e)}function me(e,t){if(e)switch(e.type){case"Identifier":return void t.add(e);case"ObjectPattern":for(const n of e.properties??[])"RestElement"===n.type?me(n.argument,t):"ObjectProperty"!==n.type&&"Property"!==n.type||(me(n.value??n.key,t),n.shorthand&&n.key&&t.add(n.key));return;case"ArrayPattern":for(const n of e.elements??[])me(n,t);return;case"AssignmentPattern":return void me(e.left,t);case"RestElement":return void me(e.argument,t);default:return}}function ye(e){return b(e)}function ve(e){let t=e;for(;"MemberExpression"===t?.type;)t=t.object;return"Identifier"===t?.type?t.name:void 0}function we(e,t){let n=!1;return S(e,e=>{if(e.type===c)for(const o of e.props??[]){if(o.type!==p||"bind"!==o.name||"is"!==ye(o.arg))continue;const e=g(b(o.exp));("Identifier"===e?.type?e.name===t:ve(e)===t)&&(n=!0)}}),n}function he(e,t){const n=e.props??[];let o=-1;for(let e=0;e<n.length;e++){const t=n[e];t.type===p&&"bind"===t.name&&ge(t)&&(o=e)}const r=new Map;for(let e=0;e<n.length;e++){const t=n[e],s=e>o;if(t.type===l){if(!t.name)continue;const e=Se(t);r.set(t.name,{value:e,dynamic:!1,afterLastSpread:s});continue}if(t.type===p){if("bind"===t.name){if(ge(t))continue;const e=b(t.arg);if(!e)continue;const n=ke(b(t.exp));r.set(e,n.known?{value:n.value,dynamic:!1,afterLastSpread:s}:be(s));continue}if("model"===t.name){const e=b(t.arg)??"modelValue";r.set(e,be(s));continue}}}return{hadSpread:o>=0,explicit:r}}function ge(e){return null==e.arg||null==b(e.arg)}function be(e){return{value:void 0,dynamic:!0,afterLastSpread:e}}function Se(e){const t=e.value;return!t||("string"==typeof t.content?t.content:"")}function ke(e){const t=g(e);return t?P(t,new Map):{known:!1}}function xe(e,t){return e.shadowedNames.has(t)}function Ee(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(xe(e,t.name))continue;const r=Ie(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 Ie(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 i=Pe(e.defaultExpr);i.known?s(i.value):o=!0}return{values:n,dynamic:o,top:r}}function Pe(e){return e?P(e,new Map):{known:!0,value:void 0}}function*je(e){const t=e.body??[];for(const e of t){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:De(n)??e}))}}}function De(e){const t=e.imported;return"Identifier"===t?.type&&t.name?t.name:"StringLiteral"===t?.type&&"string"==typeof t.value?t.value:void 0}function Me(e){return"Identifier"===e?.type&&e.name?e.name:"StringLiteral"===e?.type&&"string"==typeof e.value?e.value:void 0}const Le=8;async function Oe(e,t,n,o,r,s=0){if(s>Le)return null;const i=await o(e,n);if(!i)return null;if(B(e)||B(i))return"default"===t||"*"===t?i:null;let a;try{a=await r(i)}catch{return null}const c=Ce(a);if(!c)return null;for(const e of c)if("ExportNamedDeclaration"===e.type&&e.source?.value){for(const n of e.specifiers??[])if(Me(n.exported)===t)return Oe(String(e.source.value),Me(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 Oe(String(e.source.value),t,i,o,r,s+1);if(n)return n}}else for(const n of e.specifiers??[]){if(Me(n.exported)!==t)continue;const e=Me(n.local);if(!e)continue;const a=Ae(c,e);return a?Oe(a.value,a.imported,i,o,r,s+1):null}return null}function Ae(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:De(o)??t}}}return null}function Ce(e){try{const t=w(`<script setup>\n${e}\n<\/script>`,"barrel.vue");return t.scriptSetup?.ast.body??null}catch{return null}}function Fe(e){if("CallExpression"!==e?.type)return null;const t="Identifier"===e.callee?.type?e.callee.name:void 0;if("defineProps"===t)return{definePropsCall:e,withDefaults:new Map};if("withDefaults"===t){const t=e.arguments?.[0];return"CallExpression"!==t?.type||"Identifier"!==t.callee?.type||"defineProps"!==t.callee.name?null:{definePropsCall:t,withDefaults:Re(e.arguments?.[1])}}return null}function Re(e){const t=new Map;if("ObjectExpression"!==e?.type)return t;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)continue;const e=n.key,o="Identifier"===e?.type?e.name:void 0;o&&n.value&&t.set(o,n.value)}return t}const Ne=Symbol("unbounded-class-source");function ze(e,t){const n=new Set;let o=!1;const r={env:t.constFold,setEnv:t.narrow,propsLocal:e.propsLocal,resolveClassExpr:qe(e)};return S(e.sfc.template,e=>{if(e.type===c){(function(e){if(!e)return!1;const t=e.toLowerCase().replace(/-/g,"");return"transition"===t||"transitiongroup"===t})(e.tag)&&!function(e,t){let n="v";const o="transitiongroup"===(e.tag??"").toLowerCase().replace(/-/g,"");for(const o of e.props??[])if(o.type!==l){if(o.type===p&&"bind"===o.name){const e=b(o.arg);if(null==e||"name"===e||e.endsWith("Class")||e.endsWith("-class"))return!1}}else if("name"===o.name){const e=o.value;"string"==typeof e?.content&&(n=e.content)}else if(o.name&&o.name.endsWith("-class")){const e=o.value,n="string"==typeof e?.content?e.content:"";for(const e of n.split(/\s+/))e&&t.add(e)}for(const e of Be)t.add(`${n}-${e}`);o&&t.add(`${n}-move`);return!0}(e,n)&&(o=!0);for(const t of e.props??[]){if(t.type===p&&t.name&&!$e.has(t.name)){o=!0;continue}if(t.type===l){if("class"===t.name){const e=t.value,o="string"==typeof e?.content?e.content:"";for(const e of o.split(/\s+/))e&&n.add(e)}continue}if(t.type!==p||"bind"!==t.name)continue;const e=b(t.arg);if(null==t.arg||null==e){o=!0;continue}if("class"!==e)continue;const s=We(g(b(t.exp)),r);if(s===Ne)o=!0;else for(const e of s)n.add(e)}}}),function(e){const t=e.sfc.scriptSetup?.content;return!!t&&/\.classList\b|\.className\b|setAttribute\(\s*['"`]class/.test(t)}(e)&&(o=!0),{classes:n,unbounded:o}}const $e=new Set(["if","else","else-if","for","bind","on","model","show","slot","html","text","once","memo","cloak","pre"]);const Be=["enter-from","enter-active","enter-to","leave-from","leave-active","leave-to","appear-from","appear-active","appear-to"];function We(e,t){if(!e)return Ne;if("ObjectExpression"===e.type){const t=new Set;for(const n of e.properties??[]){if("ObjectProperty"!==n.type)return Ne;const e=n.key,o="Identifier"===e?.type?e.name:"StringLiteral"===e?.type?e.value:void 0;if(null==o)return Ne;for(const e of o.split(/\s+/))e&&t.add(e)}return t}if("ArrayExpression"===e.type){const n=new Set;for(const o of e.elements??[]){if(!o)continue;const e=We("SpreadElement"===o.type?o.argument:o,t);if(e===Ne)return Ne;for(const t of e)n.add(t)}return n}if("ConditionalExpression"===e.type){const n=P(e.test,t.env,t.propsLocal);if(n.known)return We(n.value?e.consequent:e.alternate,t);const o=We(e.consequent,t),r=We(e.alternate,t);return o===Ne||r===Ne?Ne:new Set([...o,...r])}if("Identifier"===e.type&&e.name&&!function(e,t){return t.env.has(e)||t.setEnv.has(e)}(e.name,t)){const n=t.resolveClassExpr(e.name);if(!n)return Ne;const o=new Set;for(const e of n){const n=We(e,t);if(n===Ne)return Ne;for(const e of n)o.add(e)}return o}const n=Ve(e,t);if(n===Ne)return Ne;const o=new Set;for(const e of n)for(const t of e.split(/\s+/))t&&o.add(t);return o}function Ve(e,t){if(!e)return Ne;switch(e.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":return new Set([String(e.value)]);case"Identifier":return Te(e.name??"",e,t);case"MemberExpression":{const n=I(e,t.propsLocal);return null==n?Ne:Te(n,e,t)}case"TemplateLiteral":{const n=e.quasis??[],o=e.expressions??[];let r=[_e(n[0])];for(let e=0;e<o.length;e++){const s=Ve(o[e],t);if(s===Ne)return Ne;const i=_e(n[e+1]),a=[];for(const e of r)for(const t of s)if(a.push(e+t+i),a.length>64)return Ne;r=a}return new Set(r)}case"BinaryExpression":{if("+"!==e.operator)break;const n=Ve(e.left,t),o=Ve(e.right,t);if(n===Ne||o===Ne)return Ne;const r=new Set;for(const e of n)for(const t of o)if(r.add(e+t),r.size>64)return Ne;return r}case"ConditionalExpression":{const n=P(e.test,t.env,t.propsLocal);if(n.known)return Ve(n.value?e.consequent:e.alternate,t);const o=Ve(e.consequent,t),r=Ve(e.alternate,t);return o===Ne||r===Ne?Ne:new Set([...o,...r])}}const n=P(e,t.env,t.propsLocal);return n.known?new Set([String(n.value)]):Ne}function Te(e,t,n){if(n.setEnv.has(e)){const t=new Set;for(const o of n.setEnv.get(e))t.add(String(o));return t}const o=P(t,n.env,n.propsLocal);return o.known?new Set([String(o.value)]):Ne}function qe(e){const t=e.sfc.scriptSetup;if(!t)return()=>null;const n=new Map;for(const e of t.ast.body??[])if("VariableDeclaration"===e.type&&"const"===e.kind)for(const t of e.declarations??[]){if("Identifier"!==t.id?.type||!t.id.name)continue;const e=t.init;if("CallExpression"!==e?.type||"Identifier"!==e.callee?.type)continue;if("computed"!==e.callee.name)continue;const o=e.arguments?.[0];"ArrowFunctionExpression"!==o?.type&&"FunctionExpression"!==o?.type||n.set(t.id.name,o)}return e=>{const t=n.get(e);return t?function(e){const t=e.body;if(!t)return[];if(!Array.isArray(t)&&"BlockStatement"!==t.type)return[t];const n=[],o=e=>{if(e&&"object"==typeof e&&"string"==typeof e.type&&"FunctionDeclaration"!==e.type&&"FunctionExpression"!==e.type&&"ArrowFunctionExpression"!==e.type)if("ReturnStatement"!==e.type)for(const t of Object.values(e))if(Array.isArray(t))for(const e of t)o(e);else t&&"object"==typeof t&&"string"==typeof t.type&&o(t);else e.argument&&n.push(e.argument)};return o(t),n}(t):null}}function _e(e){const t=e?.value;return t?.cooked??t?.raw??""}const Ue=new Set([":is",":where",":not",":has",":matches",":-moz-any",":-webkit-any"]);function Je(e,t){let n=!1;try{i(e=>{let o=!1,r=!1,s=!0,i=!1;e.each(e=>{i=!0;const n=[];e.walkPseudos(e=>{const t=e.value;(t.includes("deep")||t.includes("global")||t.includes("slotted"))&&(o=!0),Ue.has(t)&&(r=!0)}),e.walkClasses(e=>{n.push(e.value)});n.some(e=>!t.has(e))||(s=!1)}),n=i&&s&&!o&&!r}).processSync(e.selector)}catch{return!1}return n}function Xe(e,t,n,o,r){if(0===t.size&&0===n.size)return new Set;!function(e,t,n,o,r){const s=e.code;k(e.sfc.template,e=>{for(const i of F(e)){if(z(i[0].el,r))continue;const e=N(i,t,n);for(const[t,n]of e.removed)Ge(s,t,n,o),r.push([t,n]);for(const[t,n]of e.dirRemovals)Ge(s,t,n,o);if(e.promotion){const{from:t,to:n,text:r}=e.promotion;o.overwrite(t,n,r)}}})}(e,t,n,r,[]);const i=function(e,t,n){const o=new Set,r=e.sfc.scriptSetup;if(!(e.props&&0!==t.size&&r&&e.propsDeclaration&&e.propsPattern))return o;const s=r.base,i=e.props.filter(e=>t.has(e.name));if(0===i.length)return o;for(const e of i)o.add(e.name);const a=i.map(e=>{return`const ${e.name} = ${n=t.get(e.name),void 0===n?"undefined":JSON.stringify(n)};`;var n}).join("\n"),c=e.props.filter(e=>!t.has(e.name)),u=s+(e.propsDeclaration.start??0);let f=s+(e.propsDeclaration.end??0);";"===e.code[f]&&(f+=1);if(0===c.length&&!e.hasRestProp)return n.overwrite(u,f,a),o;const l=e.propsPattern?.properties??[];for(const t of i)He(l,t.property,s,n,e.code),Ke(e.definePropsCall,t.name,s,n,e.code);return n.appendLeft(f,`\n${a}`),o}(e,t,r);return function(e,t,n){const o=ze(e,t);if(o.unbounded)return 0;let r=0;for(const t of e.sfc.styles){if(!t.scoped)continue;let e;try{e=s(t.content)}catch{continue}let i=0;if(e.each(e=>{"rule"===e.type&&Je(e,o.classes)&&(e.remove(),i+=1)}),i>0){const o=t.base+t.content.length;n.overwrite(t.base,o,e.toString()),r+=i}}}(e,{...o,constFold:t,narrow:n},r),i}function Ge(e,t,n,o){let r=t;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]);)r-=1;o.remove(r,n)}function He(e,t,n,o,r){const s=e.indexOf(t);Qe(t,e[s-1],n,o,r)}function Ke(e,t,n,o,r){const s=e?.typeParameters?.params?.[0],i=s?.members??[],a=i.findIndex(e=>"Identifier"===e.key?.type&&e.key.name===t);-1!==a&&Qe(i[a],i[a-1],n,o,r)}function Qe(e,t,n,o,r){const s=n+(e.start??0),i=n+(e.end??0);let a=i;for(;a<r.length&&(" "===r[a]||"\t"===r[a]);)a+=1;if(","===r[a]){let e=a+1;for(;e<r.length&&(" "===r[e]||"\t"===r[e]||"\n"===r[e]);)e+=1;return void o.remove(s,e)}t?o.remove(n+(t.end??0),i):o.remove(s,i)}function Ye(e,t,n){const o=e.code;S(e.sfc.template,r=>{if(!m(r)||!r.tag)return;const s=e.imports.get(r.tag),i=s?t.get(s):void 0;if(i&&0!==i.size)for(const e of r.props??[])if(e.type===l)e.name&&i.has(e.name)&&et(o,e,n);else if(e.type===p&&"bind"===e.name){const t=b(e.arg);t&&i.has(t)&&Ze(e)&&et(o,e,n)}})}function Ze(e){const t=b(e.exp);if(null==t)return!1;const n=g(t);return null!=n&&P(n,new Map).known}function et(e,t,n){const o=t.loc;let r=o.start.offset;for(;r>0&&(" "===e[r-1]||"\t"===e[r-1]||"\n"===e[r-1]);)r-=1;n.remove(r,o.end.offset)}const tt=e=>a.readFileSync(e,"utf-8");function nt(t){const n=[];let o;try{o=a.readdirSync(t,{withFileTypes:!0})}catch{return n}for(const r of o){if("node_modules"===r.name||r.name.startsWith("."))continue;const o=e.join(t,r.name);r.isDirectory()?n.push(...nt(o)):r.isFile()&&r.name.endsWith(".vue")&&n.push(o)}return n}const ot=/\.(?:ts|tsx|js|jsx|mjs|cjs)$/;function rt(t){const n=[];let o;try{o=a.readdirSync(t,{withFileTypes:!0})}catch{return n}for(const r of o){if("node_modules"===r.name||r.name.startsWith("."))continue;const o=e.join(t,r.name);r.isDirectory()?n.push(...rt(o)):r.isFile()&&ot.test(r.name)&&!r.name.endsWith(".d.ts")&&n.push(o)}return n}function st(t={}){const n=t.include??["."];let o=process.cwd(),s={};return{name:"vue-shaker",enforce:"pre",apply:"build",configResolved(e){o=e.root},async buildStart(){const t=n.map(t=>e.resolve(o,t)),i=t.flatMap(nt);if(0===i.length)return;const a=t.flatMap(rt),{models:c,plans:u}=await _(i,async(e,t)=>{const n=await this.resolve(e,t,{skipSelf:!0});if(!n||n.external)return null;const o=n.id.split("?",1)[0];return o.includes("\0")||o.includes("/node_modules/")?null:o},tt,a);s=function(e,t){const n=new Map,o=new Map;for(const s of e.values()){const e=new r(s.code);n.set(s.id,e);const i=t.get(s.id);o.set(s.id,i.bail?new Set:Xe(s,i.constFold,i.narrow,i,e))}for(const t of e.values())Ye(t,o,n.get(t.id));const s={};for(const t of e.values())s[t.id]=n.get(t.id).toString();return s}(c,u)},transform(t,n){if(n.includes("?"))return null;const o=n.split("?",1)[0];if(!o.endsWith(".vue"))return null;const r=s[o]??s[e.normalize(o)];return null==r||r===t?null:{code:r,map:null}}}}export{st as shaker};
|
package/package.json
CHANGED