@fluixi/compiler 1.0.0-alpha.83 → 1.0.0-alpha.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyze/expr-shape.cjs +62 -1
- package/dist/analyze/expr-shape.d.cts +14 -0
- package/dist/analyze/expr-shape.d.ts +2 -2
- package/dist/analyze/expr-shape.js +1 -1
- package/dist/analyze/expr-shape.mjs +41 -1
- package/dist/analyze/intrinsics.cjs +80 -1
- package/dist/analyze/intrinsics.d.cts +66 -0
- package/dist/analyze/intrinsics.d.ts +7 -7
- package/dist/analyze/intrinsics.d.ts.map +1 -1
- package/dist/analyze/intrinsics.js +3 -3
- package/dist/analyze/intrinsics.mjs +59 -1
- package/dist/analyze/paren.cjs +134 -0
- package/dist/analyze/paren.d.cts +23 -0
- package/dist/analyze/paren.d.ts +24 -0
- package/dist/analyze/paren.d.ts.map +1 -0
- package/dist/analyze/paren.js +168 -0
- package/dist/analyze/paren.mjs +113 -0
- package/dist/analyze/props-destructure.cjs +226 -1
- package/dist/analyze/props-destructure.d.cts +83 -0
- package/dist/analyze/props-destructure.d.ts +3 -3
- package/dist/analyze/props-destructure.d.ts.map +1 -1
- package/dist/analyze/props-destructure.js +4 -4
- package/dist/analyze/props-destructure.mjs +205 -1
- package/dist/analyze/reactive-bindings.cjs +190 -1
- package/dist/analyze/reactive-bindings.d.cts +42 -0
- package/dist/analyze/reactive-bindings.d.ts +1 -1
- package/dist/analyze/reactive-bindings.js +2 -2
- package/dist/analyze/reactive-bindings.mjs +167 -1
- package/dist/analyze/reactive-expr.cjs +38 -1
- package/dist/analyze/reactive-expr.d.cts +38 -0
- package/dist/analyze/reactive-expr.d.ts +4 -4
- package/dist/analyze/reactive-expr.d.ts.map +1 -1
- package/dist/analyze/reactive-expr.js +2 -2
- package/dist/analyze/reactive-expr.mjs +17 -1
- package/dist/analyze/static-graph.cjs +811 -1
- package/dist/analyze/static-graph.d.cts +102 -0
- package/dist/analyze/static-graph.d.ts +1 -1
- package/dist/analyze/static-graph.d.ts.map +1 -1
- package/dist/analyze/static-graph.js +11 -11
- package/dist/analyze/static-graph.mjs +793 -1
- package/dist/babel-TGY76XJ7.mjs +636 -0
- package/dist/chunk-4PLHS2FP.mjs +158 -0
- package/dist/chunk-6DB5FXS4.mjs +1718 -0
- package/dist/chunk-F74GFKSL.mjs +16 -0
- package/dist/chunk-G6QC2WRJ.mjs +11 -0
- package/dist/chunk-RUDJDZAA.mjs +1078 -0
- package/dist/codegen/backend.cjs +18 -1
- package/dist/codegen/backend.d.cts +35 -0
- package/dist/codegen/backends/imperative.cjs +542 -1
- package/dist/codegen/backends/imperative.d.cts +11 -0
- package/dist/codegen/backends/imperative.d.ts +1 -1
- package/dist/codegen/backends/imperative.d.ts.map +1 -1
- package/dist/codegen/backends/imperative.js +24 -20
- package/dist/codegen/backends/imperative.mjs +519 -1
- package/dist/codegen/contract.cjs +52 -1
- package/dist/codegen/contract.d.cts +24 -0
- package/dist/codegen/contract.d.ts +2 -2
- package/dist/codegen/contract.js +2 -2
- package/dist/codegen/contract.mjs +31 -1
- package/dist/codegen/partial-template.cjs +176 -1
- package/dist/codegen/partial-template.d.cts +61 -0
- package/dist/codegen/partial-template.d.ts +4 -4
- package/dist/codegen/partial-template.js +0 -0
- package/dist/codegen/partial-template.mjs +153 -1
- package/dist/codegen/serialize-static.cjs +125 -1
- package/dist/codegen/serialize-static.d.cts +38 -0
- package/dist/codegen/serialize-static.d.ts +3 -3
- package/dist/codegen/serialize-static.js +6 -6
- package/dist/codegen/serialize-static.mjs +104 -1
- package/dist/frontend/babel/build-ir-lit.cjs +1509 -1
- package/dist/frontend/babel/build-ir-lit.d.cts +26 -0
- package/dist/frontend/babel/build-ir-lit.d.ts +2 -2
- package/dist/frontend/babel/build-ir-lit.mjs +1478 -1
- package/dist/frontend/babel/build-ir.cjs +1524 -1
- package/dist/frontend/babel/build-ir.d.cts +35 -0
- package/dist/frontend/babel/build-ir.d.ts +2 -2
- package/dist/frontend/babel/build-ir.js +2 -2
- package/dist/frontend/babel/build-ir.mjs +1495 -1
- package/dist/frontend/babel/index.cjs +2186 -1
- package/dist/frontend/babel/index.d.cts +33 -0
- package/dist/frontend/babel/index.d.ts +1 -1
- package/dist/frontend/babel/index.js +1 -1
- package/dist/frontend/babel/index.mjs +2156 -1
- package/dist/frontend/babel/lower-template.cjs +1507 -1
- package/dist/frontend/babel/lower-template.d.cts +23 -0
- package/dist/frontend/babel/lower-template.d.ts +1 -1
- package/dist/frontend/babel/lower-template.js +2 -2
- package/dist/frontend/babel/lower-template.mjs +1478 -1
- package/dist/frontend/babel/plugin.cjs +2132 -1
- package/dist/frontend/babel/plugin.d.cts +95 -0
- package/dist/frontend/babel/plugin.d.ts +5 -5
- package/dist/frontend/babel/plugin.d.ts.map +1 -1
- package/dist/frontend/babel/plugin.js +14 -14
- package/dist/frontend/babel/plugin.mjs +2103 -1
- package/dist/frontend/babel/server-functions.cjs +110 -1
- package/dist/frontend/babel/server-functions.d.cts +10 -0
- package/dist/frontend/babel/server-functions.mjs +89 -1
- package/dist/frontend/babel/types.cjs +18 -1
- package/dist/frontend/babel/types.d.cts +33 -0
- package/dist/frontend/types.cjs +18 -1
- package/dist/frontend/types.d.cts +25 -0
- package/dist/frontend/vocabulary.cjs +98 -0
- package/dist/frontend/vocabulary.d.cts +79 -0
- package/dist/frontend/vocabulary.d.ts +80 -0
- package/dist/frontend/vocabulary.d.ts.map +1 -0
- package/dist/frontend/vocabulary.js +124 -0
- package/dist/frontend/vocabulary.mjs +77 -0
- package/dist/index.cjs +1595 -14
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -4
- package/dist/index.mjs +1575 -14
- package/dist/integrations.cjs +4312 -26
- package/dist/integrations.d.cts +215 -0
- package/dist/integrations.d.ts +9 -9
- package/dist/integrations.d.ts.map +1 -1
- package/dist/integrations.js +29 -8
- package/dist/integrations.mjs +536 -18
- package/dist/ir/nodes.cjs +18 -1
- package/dist/ir/nodes.d.cts +168 -0
- package/dist/ir/nodes.d.ts +6 -6
- package/dist/lower/compile-template.cjs +1356 -1
- package/dist/lower/compile-template.d.cts +69 -0
- package/dist/lower/compile-template.d.ts +3 -3
- package/dist/lower/compile-template.js +1 -1
- package/dist/lower/compile-template.mjs +1336 -1
- package/dist/lower/jsx.cjs +529 -1
- package/dist/lower/jsx.d.cts +25 -0
- package/dist/lower/jsx.d.ts +1 -1
- package/dist/lower/jsx.d.ts.map +1 -1
- package/dist/lower/jsx.js +6 -5
- package/dist/lower/jsx.mjs +506 -1
- package/dist/lower/template.cjs +1061 -1
- package/dist/lower/template.d.cts +53 -0
- package/dist/lower/template.d.ts +3 -3
- package/dist/lower/template.d.ts.map +1 -1
- package/dist/lower/template.js +12 -11
- package/dist/lower/template.mjs +1042 -1
- package/dist/lower/text.cjs +42 -1
- package/dist/lower/text.d.cts +10 -0
- package/dist/lower/text.d.ts +2 -2
- package/dist/lower/text.d.ts.map +1 -1
- package/dist/lower/text.js +2 -2
- package/dist/lower/text.mjs +21 -1
- package/dist/optimize/analyze-static.cjs +112 -1
- package/dist/optimize/analyze-static.d.cts +39 -0
- package/dist/optimize/analyze-static.d.ts +2 -2
- package/dist/optimize/analyze-static.js +2 -2
- package/dist/optimize/analyze-static.mjs +91 -1
- package/dist/optimize/collapse-strategies.cjs +90 -1
- package/dist/optimize/collapse-strategies.d.cts +30 -0
- package/dist/optimize/collapse-strategies.d.ts +3 -3
- package/dist/optimize/collapse-strategies.mjs +67 -1
- package/dist/options.cjs +33 -1
- package/dist/options.d.cts +39 -0
- package/dist/options.d.ts +2 -2
- package/dist/options.mjs +12 -1
- package/dist/resolve/builtins.cjs +73 -1
- package/dist/resolve/builtins.d.cts +68 -0
- package/dist/resolve/builtins.d.ts +8 -8
- package/dist/resolve/builtins.d.ts.map +1 -1
- package/dist/resolve/builtins.js +4 -4
- package/dist/resolve/builtins.mjs +53 -1
- package/dist/resolve/component-globals.cjs +234 -14
- package/dist/resolve/component-globals.d.cts +80 -0
- package/dist/resolve/component-globals.d.ts +5 -5
- package/dist/resolve/component-globals.js +10 -10
- package/dist/resolve/component-globals.mjs +211 -14
- package/dist/resolve/component-resolver.cjs +80 -1
- package/dist/resolve/component-resolver.d.cts +68 -0
- package/dist/resolve/component-resolver.d.ts +3 -3
- package/dist/resolve/component-resolver.d.ts.map +1 -1
- package/dist/resolve/component-resolver.js +3 -3
- package/dist/resolve/component-resolver.mjs +59 -1
- package/dist/resolve/load-directive.cjs +93 -1
- package/dist/resolve/load-directive.d.cts +53 -0
- package/dist/resolve/load-directive.d.ts +5 -5
- package/dist/resolve/load-directive.js +6 -6
- package/dist/resolve/load-directive.mjs +72 -1
- package/dist/resolve/resolve-ir.cjs +108 -1
- package/dist/resolve/resolve-ir.d.cts +53 -0
- package/dist/resolve/resolve-ir.d.ts +6 -6
- package/dist/resolve/resolve-ir.d.ts.map +1 -1
- package/dist/resolve/resolve-ir.js +1 -1
- package/dist/resolve/resolve-ir.mjs +85 -1
- package/dist/resolve/write-globals.cjs +319 -14
- package/dist/resolve/write-globals.d.cts +30 -0
- package/dist/resolve/write-globals.js +2 -2
- package/dist/resolve/write-globals.mjs +298 -14
- package/dist/server-fns-ZYWQTSZU.mjs +78 -0
- package/dist/server-functions-PCERFA7A.mjs +82 -0
- package/dist/transform/bindings.cjs +71 -1
- package/dist/transform/bindings.d.cts +16 -0
- package/dist/transform/bindings.d.ts +1 -1
- package/dist/transform/bindings.js +1 -1
- package/dist/transform/bindings.mjs +50 -1
- package/dist/transform/index.cjs +2732 -8
- package/dist/transform/index.d.cts +15 -0
- package/dist/transform/index.mjs +3773 -17
- package/dist/transform/intrinsics.cjs +216 -3
- package/dist/transform/intrinsics.d.cts +21 -0
- package/dist/transform/intrinsics.js +1 -1
- package/dist/transform/intrinsics.mjs +193 -3
- package/dist/transform/props.cjs +441 -3
- package/dist/transform/props.d.cts +45 -0
- package/dist/transform/props.d.ts +1 -1
- package/dist/transform/props.d.ts.map +1 -1
- package/dist/transform/props.js +4 -4
- package/dist/transform/props.mjs +1482 -12
- package/dist/transform/server-fn-id.cjs +35 -1
- package/dist/transform/server-fn-id.d.cts +9 -0
- package/dist/transform/server-fn-id.d.ts +1 -1
- package/dist/transform/server-fn-id.js +2 -2
- package/dist/transform/server-fn-id.mjs +14 -1
- package/dist/transform/server-fns.cjs +114 -2
- package/dist/transform/server-fns.d.cts +12 -0
- package/dist/transform/server-fns.js +3 -3
- package/dist/transform/server-fns.mjs +1155 -11
- package/dist/transform/source-locations.cjs +330 -1
- package/dist/transform/source-locations.d.cts +43 -0
- package/dist/transform/source-locations.d.ts +1 -1
- package/dist/transform/source-locations.js +5 -5
- package/dist/transform/source-locations.mjs +307 -1
- package/dist/transform/templates.cjs +2701 -7
- package/dist/transform/templates.d.cts +69 -0
- package/dist/transform/templates.d.ts +2 -2
- package/dist/transform/templates.d.ts.map +1 -1
- package/dist/transform/templates.js +7 -7
- package/dist/transform/templates.mjs +3743 -16
- package/dist/transform-RWDTQUF5.mjs +893 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +6 -5
- package/dist/babel-VXKH6MRQ.mjs +0 -1
- package/dist/chunk-3SAEGOMQ.mjs +0 -1
- package/dist/chunk-5KMMN5QP.mjs +0 -1
- package/dist/chunk-IBRM4W7I.mjs +0 -10
- package/dist/chunk-OHHZCYIQ.mjs +0 -1
- package/dist/chunk-YKZ54NVE.mjs +0 -1
- package/dist/server-fns-6QM243HC.mjs +0 -2
- package/dist/server-functions-E3LKCZ4R.mjs +0 -1
- package/dist/transform-C4ZXRVGP.mjs +0 -8
package/dist/transform/index.mjs
CHANGED
|
@@ -1,17 +1,3773 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
`?s=!0:u!=="\r"&&s&&(s=!1,a===l.start||(this._splitChunk(l,a),l=l.next),l.prependRight(e))}a+=1}a=l.end,l=l.next}return this.outro=this.outro.replace(r,o),this}insert(){throw new Error("magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)")}insertLeft(e,t){return _.insertLeft||(console.warn("magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead"),_.insertLeft=!0),this.appendLeft(e,t)}insertRight(e,t){return _.insertRight||(console.warn("magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead"),_.insertRight=!0),this.prependRight(e,t)}move(e,t,r){if(e=e+this.offset,t=t+this.offset,r=r+this.offset,r>=e&&r<=t)throw new Error("Cannot move a selection inside itself");this._split(e),this._split(t),this._split(r);let i=this.byStart[e],s=this.byEnd[t],o=i.previous,a=s.next,l=this.byStart[r];if(!l&&s===this.lastChunk)return this;let c=l?l.previous:this.lastChunk;return o&&(o.next=a),a&&(a.previous=o),c&&(c.next=i),l&&(l.previous=s),i.previous||(this.firstChunk=s.next),s.next||(this.lastChunk=i.previous,this.lastChunk.next=null),i.previous=c,s.next=l||null,c||(this.firstChunk=i),l||(this.lastChunk=s),this}overwrite(e,t,r,i){return i=i||{},this.update(e,t,r,{...i,overwrite:!i.contentOnly})}update(e,t,r,i){if(e=e+this.offset,t=t+this.offset,typeof r!="string")throw new TypeError("replacement content must be a string");if(this.original.length!==0){for(;e<0;)e+=this.original.length;for(;t<0;)t+=this.original.length}if(t>this.original.length)throw new Error("end is out of bounds");if(e===t)throw new Error("Cannot overwrite a zero-length range – use appendLeft or prependRight instead");this._split(e),this._split(t),i===!0&&(_.storeName||(console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string"),_.storeName=!0),i={storeName:!0});let s=i!==void 0?i.storeName:!1,o=i!==void 0?i.overwrite:!1;if(s){let c=this.original.slice(e,t);Object.defineProperty(this.storedNames,c,{writable:!0,value:!0,enumerable:!0})}let a=this.byStart[e],l=this.byEnd[t];if(a){let c=a;for(;c!==l;){if(c.next!==this.byStart[c.end])throw new Error("Cannot overwrite across a split point");c=c.next,c.edit("",!1)}a.edit(r,s,!o)}else{let c=new le(e,t,"").edit(r,s);l.next=c,c.previous=l}return this}prepend(e){if(typeof e!="string")throw new TypeError("outro content must be a string");return this.intro=e+this.intro,this}prependLeft(e,t){if(e=e+this.offset,typeof t!="string")throw new TypeError("inserted content must be a string");this._split(e);let r=this.byEnd[e];return r?r.prependLeft(t):this.intro=t+this.intro,this}prependRight(e,t){if(e=e+this.offset,typeof t!="string")throw new TypeError("inserted content must be a string");this._split(e);let r=this.byStart[e];return r?r.prependRight(t):this.outro=t+this.outro,this}remove(e,t){if(e=e+this.offset,t=t+this.offset,this.original.length!==0){for(;e<0;)e+=this.original.length;for(;t<0;)t+=this.original.length}if(e===t)return this;if(e<0||t>this.original.length)throw new Error("Character is out of bounds");if(e>t)throw new Error("end must be greater than start");this._split(e),this._split(t);let r=this.byStart[e];for(;r;)r.intro="",r.outro="",r.edit(""),r=t>r.end?this.byStart[r.end]:null;return this}reset(e,t){if(e=e+this.offset,t=t+this.offset,this.original.length!==0){for(;e<0;)e+=this.original.length;for(;t<0;)t+=this.original.length}if(e===t)return this;if(e<0||t>this.original.length)throw new Error("Character is out of bounds");if(e>t)throw new Error("end must be greater than start");this._split(e),this._split(t);let r=this.byStart[e];for(;r;)r.reset(),r=t>r.end?this.byStart[r.end]:null;return this}lastChar(){if(this.outro.length)return this.outro[this.outro.length-1];let e=this.lastChunk;do{if(e.outro.length)return e.outro[e.outro.length-1];if(e.content.length)return e.content[e.content.length-1];if(e.intro.length)return e.intro[e.intro.length-1]}while(e=e.previous);return this.intro.length?this.intro[this.intro.length-1]:""}lastLine(){let e=this.outro.lastIndexOf(X);if(e!==-1)return this.outro.substr(e+1);let t=this.outro,r=this.lastChunk;do{if(r.outro.length>0){if(e=r.outro.lastIndexOf(X),e!==-1)return r.outro.substr(e+1)+t;t=r.outro+t}if(r.content.length>0){if(e=r.content.lastIndexOf(X),e!==-1)return r.content.substr(e+1)+t;t=r.content+t}if(r.intro.length>0){if(e=r.intro.lastIndexOf(X),e!==-1)return r.intro.substr(e+1)+t;t=r.intro+t}}while(r=r.previous);return e=this.intro.lastIndexOf(X),e!==-1?this.intro.substr(e+1)+t:this.intro+t}slice(e=0,t=this.original.length-this.offset){if(e=e+this.offset,t=t+this.offset,this.original.length!==0){for(;e<0;)e+=this.original.length;for(;t<0;)t+=this.original.length}let r="",i=this.firstChunk;for(;i&&(i.start>e||i.end<=e);){if(i.start<t&&i.end>=t)return r;i=i.next}if(i&&i.edited&&i.start!==e)throw new Error(`Cannot use replaced character ${e} as slice start anchor.`);let s=i;for(;i;){i.intro&&(s!==i||i.start===e)&&(r+=i.intro);let o=i.start<t&&i.end>=t;if(o&&i.edited&&i.end!==t)throw new Error(`Cannot use replaced character ${t} as slice end anchor.`);let a=s===i?e-i.start:0,l=o?i.content.length+t-i.end:i.content.length;if(r+=i.content.slice(a,l),i.outro&&(!o||i.end===t)&&(r+=i.outro),o)break;i=i.next}return r}snip(e,t){let r=this.clone();return r.remove(0,e),r.remove(t,r.original.length),r}_split(e){if(this.byStart[e]||this.byEnd[e])return;let t=this.lastSearchedChunk,r=t,i=e>t.end;for(;t;){if(t.contains(e))return this._splitChunk(t,e);if(t=i?this.byStart[t.end]:this.byEnd[t.start],t===r)return;r=t}}_splitChunk(e,t){if(e.edited&&e.content.length){let i=Ge(this.original)(t);throw new Error(`Cannot split a chunk that has already been edited (${i.line}:${i.column} – "${e.original}")`)}let r=e.split(t);return this.byEnd[t]=e,this.byStart[t]=r,this.byEnd[r.end]=r,e===this.lastChunk&&(this.lastChunk=r),this.lastSearchedChunk=e,!0}toString(){let e=this.intro,t=this.firstChunk;for(;t;)e+=t.toString(),t=t.next;return e+this.outro}isEmpty(){let e=this.firstChunk;do if(e.intro.length&&e.intro.trim()||e.content.length&&e.content.trim()||e.outro.length&&e.outro.trim())return!1;while(e=e.next);return!0}length(){let e=this.firstChunk,t=0;do t+=e.intro.length+e.content.length+e.outro.length;while(e=e.next);return t}trimLines(){return this.trim("[\\r\\n]")}trim(e){return this.trimStart(e).trimEnd(e)}trimEndAborted(e){let t=new RegExp((e||"\\s")+"+$");if(this.outro=this.outro.replace(t,""),this.outro.length)return!0;let r=this.lastChunk;do{let i=r.end,s=r.trimEnd(t);if(r.end!==i&&(this.lastChunk===r&&(this.lastChunk=r.next),this.byEnd[r.end]=r,this.byStart[r.next.start]=r.next,this.byEnd[r.next.end]=r.next),s)return!0;r=r.previous}while(r);return!1}trimEnd(e){return this.trimEndAborted(e),this}trimStartAborted(e){let t=new RegExp("^"+(e||"\\s")+"+");if(this.intro=this.intro.replace(t,""),this.intro.length)return!0;let r=this.firstChunk;do{let i=r.end,s=r.trimStart(t);if(r.end!==i&&(r===this.lastChunk&&(this.lastChunk=r.next),this.byEnd[r.end]=r,this.byStart[r.next.start]=r.next,this.byEnd[r.next.end]=r.next),s)return!0;r=r.next}while(r);return!1}trimStart(e){return this.trimStartAborted(e),this}hasChanged(){return this.original!==this.toString()}_replaceRegexp(e,t){function r(s,o){return typeof t=="string"?t.replace(/\$(\$|&|\d+)/g,(a,l)=>l==="$"?"$":l==="&"?s[0]:+l<s.length?s[+l]:`$${l}`):t(...s,s.index,o,s.groups)}function i(s,o){let a,l=[];for(;a=s.exec(o);)l.push(a);return l}if(e.global)i(e,this.original).forEach(o=>{if(o.index!=null){let a=r(o,this.original);a!==o[0]&&this.overwrite(o.index,o.index+o[0].length,a)}});else{let s=this.original.match(e);if(s&&s.index!=null){let o=r(s,this.original);o!==s[0]&&this.overwrite(s.index,s.index+s[0].length,o)}}return this}_replaceString(e,t){let{original:r}=this,i=r.indexOf(e);return i!==-1&&(typeof t=="function"&&(t=t(e,i,r)),e!==t&&this.overwrite(i,i+e.length,t)),this}replace(e,t){return typeof e=="string"?this._replaceString(e,t):this._replaceRegexp(e,t)}_replaceAllString(e,t){let{original:r}=this,i=e.length;for(let s=r.indexOf(e);s!==-1;s=r.indexOf(e,s+i)){let o=r.slice(s,s+i),a=t;typeof t=="function"&&(a=t(o,s,r)),o!==a&&this.overwrite(s,s+i,a)}return this}replaceAll(e,t){if(typeof e=="string")return this._replaceAllString(e,t);if(!e.global)throw new TypeError("MagicString.prototype.replaceAll called with a non-global RegExp argument");return this._replaceRegexp(e,t)}};function Kt(n){switch(n.kind){case"event":return"event";case"ref":return"ref";case"spread":return"spread";case"class":case"style":return"class-or-style-directive";case"attr":case"prop":return n.reactive?"reactive-prop":n.expr!==void 0?"expression-prop":void 0;default:return"expression-prop"}}function B(n){let e=[],t=new Map,r=i=>{switch(i.kind){case"text":return!0;case"expr":return!1;case"component":case"control":for(let s of i.children)r(s);return!1;case"fragment":for(let s of i.children)r(s);return!1;case"element":{let s=!0;for(let a of i.children)r(a)||(s=!1);let o;for(let a of i.props)if(o=Kt(a),o)break;return!o&&!s&&(o=Gt(i)),i.static=!o,o?t.set(i,o):e.push(i),i.static}default:return!1}};for(let i of Array.isArray(n)?n:[n])r(i);return{staticElements:e,reasons:t}}function Gt(n){for(let e of n.children){if(e.kind==="expr")return"expression-child";if(e.kind==="component")return"component-child";if(e.kind==="control")return"control-child";if(e.kind==="element"&&!e.static||e.kind==="fragment")return"expression-child"}return"expression-child"}var ce={kind:"eager"},Yt=["pointerdown","focusin","keydown"],Ye=new Set(["eager","idle","visible","interaction","media","never"]),O=class extends Error{};function Ee(n){return n.startsWith("load:")}function ue(n,e){let t=n.slice(5);if(!Ye.has(t))throw new O(`Unknown load strategy 'load:${t}'. Expected one of ${[...Ye].join(", ")}.`);switch(t){case"eager":return ce;case"idle":return{kind:"idle"};case"never":return{kind:"never"};case"visible":return e?{kind:"visible",rootMargin:e}:{kind:"visible"};case"interaction":return{kind:"interaction",events:e?Zt(e):[...Yt]};case"media":if(!e)throw new O(`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`);return{kind:"media",query:e};default:throw new O(`Unhandled load strategy '${t}'.`)}}function Zt(n){let e=n.split(/[\s,]+/).map(t=>t.trim()).filter(Boolean);if(e.length===0)throw new O("'load:interaction' was given no event names.");return e}function V(n){return n.kind!=="eager"&&n.kind!=="never"}var z=["Show","For","Index","Switch","Match","Portal","Dynamic","ErrorBoundary"],W=["Suspense","SuspenseList","Await"],F=["Router","Outlet","Redirect","Link"],xr=[...z,...W,...F],ke=new Set([...z,...W]);function Ze(n={}){let{controlFlowModule:e="@fluixi/dom",coreModule:t="@fluixi/core",routerModule:r="@fluixi/core/router"}=n,i={};for(let s of z)i[s]=e;for(let s of W)i[s]=t;for(let s of F)i[s]=r;return i}function de(n,e={}){let{resolver:t,isBound:r,modules:i,strategyFor:s}=e,o=Ze(i),a=new Map,l=new Set,c=f=>{let{name:d}=f;if(!d||d.includes(".")||r?.(d))return;let h=u(d);if(!h){l.add(d);return}let m=h.origin==="builtin"&&ke.has(d)?ce:s?.(f)??ce;f.source={...h,loading:m},a.set(d,f.source)},u=f=>{if(ke.has(f))return{module:o[f],export:f,origin:"builtin"};let d=t?.resolve(f);if(d)return{module:d.module,export:d.export,origin:"rule"};if(F.includes(f))return{module:o[f],export:f,origin:"builtin"}};return fe(n,f=>{f.kind==="component"&&c(f)}),{resolved:a,unresolved:[...l]}}function fe(n,e){let t=Array.isArray(n)?n:[n];for(let r of t){e(r);let i=r.children;i&&fe(i,e)}}function pe(n){let e=[];fe(n,s=>{s.kind==="component"&&s.source&&e.push(s)});let t=new Set,r=new Set;for(let s of e){let{module:o,loading:a}=s.source;a.kind==="eager"&&t.add(o),a.kind==="never"&&r.add(o)}let i=new Map;for(let s of e){let o=s.source;V(o.loading)&&t.has(o.module)&&(o.loading={kind:"eager"},i.set(o.module,(i.get(o.module)??0)+1))}return{collapsed:i,conflicted:[...r].filter(s=>t.has(s))}}var Qt=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),Qe={className:"class",htmlFor:"for"},en=new Set(["innerHTML","outerHTML","innerText","textContent","value","checked","selected","muted","defaultValue","defaultChecked","children","ref"]),tn=new Set(["script","style","textarea","title"]);function Ce(n){return tn.has(n)}function et(n){return Qe[n]??n}function K(n){return n.kind!=="attr"||n.expr!==void 0||n.name.includes(":")?!1:!en.has(n.name)}function Ie(n){if(!n.static||Ce(n.tag))return!1;for(let e of n.props)if(!K(e))return!1;for(let e of n.children)if(e.kind!=="text"&&!(e.kind==="element"&&Ie(e)))return!1;return!0}function G(n){let e=n.props.map(rn).filter(Boolean).join(""),t=`<${n.tag}${e}>`;return Qt.has(n.tag)?t:`${t}${n.children.map(nn).join("")}</${n.tag}>`}function nn(n){if(n.kind==="text")return on(n.value);if(n.kind!=="element")throw new Error(`serializeStatic: unexpected ${n.kind}`);return G(n)}function rn(n){let e=Qe[n.name]??n.name,t=n.literal;return t===!0||t===void 0?` ${e}`:t===!1||t===null?"":` ${e}="${sn(String(t))}"`}function sn(n){return n.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">")}function on(n){return n.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function $e(n){let e=[],t,r=i=>{t??=i.at?.file,e.push(i.at?.line??0,i.at?.column??0);for(let s of i.children)s.kind==="element"&&r(s)};return r(n),t&&e.some(i=>i!==0)?{file:t,pos:e}:void 0}var an="<!--fx-->",ln="<!--fx/-->";function he(n){return n.kind==="expr"||n.kind==="component"||n.kind==="control"}function cn(n){return n.kind==="text"||n.kind==="element"&&tt(n)}function tt(n){return n.svg||Ce(n.tag)||!n.props.every(e=>K(e))?!1:n.children.every(e=>cn(e)||he(e))}function me(n){if(he(n))return!0;let e=n.children;return e?e.some(me):!1}function nt(n){for(let e=0;e<n.children.length;e++){let t=n.children[e];if(t.kind==="text"&&(t.value===""||n.children[e+1]?.kind==="text"))return!1}return n.children.every(e=>e.kind!=="element"||nt(e))}function rt(n){if(!tt(n)||!nt(n)||!n.children.some(me))return null;let e=[],t=[],r=0,i=o=>{let a=`_n$${r++}`;return e.push({ref:a,expr:o}),a};return s(n,"_el$"),{html:un(n),tag:n.tag,steps:e,holes:t};function s(o,a){let l=-1;o.children.forEach((u,f)=>{me(u)&&(l=f)});let c=null;for(let u=0;u<=l;u++){let f=o.children[u],d=c?`${c}.nextSibling`:`${a}.firstChild`;if(he(f)){let m=i(d),y=i(`holeEnd(${m})`);t.push({parentRef:a,startRef:m,endRef:y,node:f}),c=y;continue}let h=i(d);f.kind==="element"&&me(f)&&s(f,h),c=h}}}function un(n){return G(it(n)).split(st).join(an+ln)}function it(n){return{...n,children:n.children.map(e=>he(e)?{kind:"text",value:st}:e.kind==="element"?it(e):e)}}var st="\0fx-hole\0";var Te={version:1,module:"@fluixi/dom",symbols:["createNativeElement","insert","spread","setAttribute","delegateEvents","createComponent","createMemo","untrack","Show","For","Index","Switch","Match","Dynamic","ErrorBoundary"]},Pr={version:2,module:"@fluixi/dom",symbols:[...Te.symbols,"template","cloneTemplate","walk"]};var A="@fluixi/reactive/signal",C={$signal:{export:"signal",module:A,returns:"signal-handle"},$memo:{export:"memo",module:A,returns:"memo-handle"},$effect:{export:"effect",module:A,returns:"effect"},$store:{export:"store",module:"@fluixi/reactive/store",returns:"store-handle"},$resource:{export:"resource",module:A,returns:"resource-handle"},$selector:{export:"createSelector",module:A,returns:"memo-accessor"},$deferred:{export:"createDeferred",module:A,returns:"memo-accessor"},$untrack:{export:"untrack",module:A,returns:"plain"},$untrackStore:{export:"untrackStore",module:"@fluixi/reactive/store",returns:"plain"}},ot={signal:"signal-handle",memo:"memo-handle",effect:"effect",store:"store-handle",createSignal:["signal-accessor","signal-setter"],createMemo:"memo-accessor",createEffect:"effect",createRenderEffect:"effect",createStore:"reactive-object",resource:"resource-handle",createResource:"reactive-object",createSelector:"memo-accessor",createDeferred:"memo-accessor"},dn=["@fluixi/reactive","@fluixi/reactive/signal","@fluixi/reactive/store","@fluixi/core"],fn=new RegExp(`\\$(?:${Object.keys(C).map(n=>n.slice(1)).join("|")})\\s*\\(`);function at(n){return fn.test(n)}function ge(n){return Object.prototype.hasOwnProperty.call(C,n)}function lt(n){return dn.includes(n)}function b(n,e){if(n)switch(n.type){case"Identifier":e.add(n.name);return;case"ObjectPattern":for(let t of n.properties??[])t.type==="RestElement"?b(t.argument,e):b(t.value,e);return;case"ArrayPattern":for(let t of n.elements??[])b(t,e);return;case"AssignmentPattern":b(n.left,e);return;case"RestElement":b(n.argument,e);return}}function Y(n,e){if(!(!n||typeof n!="object"||typeof n.type!="string")){e(n);for(let t of Object.keys(n)){if(t==="loc"||t==="leadingComments"||t==="trailingComments")continue;let r=n[t];if(Array.isArray(r))for(let i of r)Y(i,e);else r&&typeof r=="object"&&Y(r,e)}}}function pn(n,e){if(n.type!=="CallExpression")return;let t=n.callee;if(t?.type!=="Identifier")return;let r=t.name,i=e.primitives.get(r);if(i)return i;let s=C[r];if(s&&!e.shadowed.has(r))return s.returns}function mn(n,e,t){if(Array.isArray(e)){n.type==="ArrayPattern"&&n.elements.forEach((r,i)=>{let s=e[i];r?.type==="Identifier"&&s&&t.set(r.name,s)});return}n.type==="Identifier"&&t.set(n.name,e)}function ct(n){let e=new Set;return Y(n,t=>{switch(t.type){case"VariableDeclarator":b(t.id,e);return;case"FunctionDeclaration":case"FunctionExpression":case"ArrowFunctionExpression":b(t.id,e);for(let r of t.params??[])b(r,e);return;case"ClassDeclaration":case"ClassExpression":b(t.id,e);return;case"ImportSpecifier":case"ImportDefaultSpecifier":case"ImportNamespaceSpecifier":b(t.local,e);return;case"CatchClause":b(t.param,e);return}}),e}function ye(n){let e={kinds:new Map,primitives:new Map,shadowed:new Set},t=n;Y(t,r=>{let i=new Set;if(r.type==="VariableDeclarator")b(r.id,i);else if(r.type==="FunctionDeclaration"||r.type==="ClassDeclaration")b(r.id,i);else if(r.type==="ImportDefaultSpecifier"||r.type==="ImportNamespaceSpecifier")b(r.local,i);else if(r.type==="ImportSpecifier")b(r.local,i);else if(r.type==="FunctionExpression"||r.type==="ArrowFunctionExpression"||r.type==="FunctionDeclaration")for(let s of r.params??[])b(s,i);for(let s of i)C[s]&&e.shadowed.add(s)});for(let r of t.body??[])if(r.type==="ImportDeclaration"&<(r.source?.value))for(let i of r.specifiers??[]){if(i.type!=="ImportSpecifier")continue;let s=i.imported,o=s.type==="Identifier"?s.name:s.value,a=ot[o];a&&e.primitives.set(i.local.name,a)}return Y(t,r=>{if(r.type!=="VariableDeclarator"||!r.init)return;let i=pn(r.init,e);i&&mn(r.id,i,e.kinds)}),e}var Pe="__fx_source",hn="__fx_component",gn="__fx_hole",Z=(n,e,t,r)=>`(globalThis.${gn} ?? ((f) => f))(${n},${e},${t}${r===void 0?"":`,${JSON.stringify(r)}`})`,yn="__fx_props",dt=(n,e)=>`(globalThis.${yn} ?? ((p) => p))(${n},${JSON.stringify(e)})`,ft=new Set(["signal","memo","effect","store","resource","watch","createSignal","createMemo","createEffect","createStore","createResource"]);function pt(n){for(let e of ft)if(n.includes(e))return!0;return n.includes("$signal")||n.includes("$memo")||n.includes("$effect")||n.includes("$store")}var vn=new Set(["IfStatement","ForStatement","ForInStatement","ForOfStatement","WhileStatement","DoWhileStatement","LabeledStatement","WithStatement"]);function bn(n,e){return!e||n.type==="BlockStatement"?!1:vn.has(e.type)}function ut(n){return n.type==="ExportNamedDeclaration"||n.type==="ExportDefaultDeclaration"}function Nn(n){return n.type.endsWith("Statement")||n.type==="VariableDeclaration"}function Oe(n,e,t=[]){if(!(!n||typeof n.type!="string")){e(n,t),t.push(n);for(let r of Object.keys(n)){if(r==="loc"||r==="parent")continue;let i=n[r];if(Array.isArray(i))for(let s of i)Oe(s,e,t);else i&&typeof i.type=="string"&&Oe(i,e,t)}t.pop()}}function xn(n){let e=n[n.length-1];if(e?.type!=="VariableDeclarator")return;let t=e.id;if(t?.type==="Identifier")return t.name;if(t?.type==="ArrayPattern"){let r=t.elements?.[0];if(r?.type==="Identifier")return r.name}}function Sn(n){if(n.type==="FunctionDeclaration"){let e=n.id?.name;return e&&e[0]===e[0]?.toUpperCase()?e:void 0}if(n.type==="VariableDeclarator"){let e=n.id,t=n.init;if(e?.type!=="Identifier"||!t||t.type!=="ArrowFunctionExpression"&&t.type!=="FunctionExpression")return;let r=e.name;return r[0]===r[0]?.toUpperCase()?r:void 0}}function mt(n,e){let t=[],r=new Set,{primitives:i,shadowed:s}=ye(n);return Oe(n,(o,a)=>{let l=Sn(o);if(l){let g=o.loc?.start,$=o.end;for(let j=a.length-1;j>=0;j--){let M=a[j];if(M.type!=="VariableDeclaration"&&M.type!=="ExportNamedDeclaration"&&M.type!=="ExportDefaultDeclaration")break;$=M.end}g&&t.push({start:$,end:$,code:`;globalThis.${hn}?.(${JSON.stringify(l)},${JSON.stringify(e)},${g.line},${g.column});`})}if(o.type!=="CallExpression")return;let c=o.callee;if(c?.type!=="Identifier")return;let u=c.name,f=ge(u)&&!s.has(u)?C[u]:void 0,d=i.has(u)||ft.has(u);if(!f&&!d)return;let h=o.loc?.start;if(!h)return;let m,y,S=-1;for(let g=a.length-1;g>=0;g--)if(Nn(a[g])||ut(a[g])){m=a[g],y=a[g-1],S=g;break}if(!m)return;let x=m.start,v=m.end;for(let g=S-1;g>=0&&ut(a[g]);g--)x=a[g].start,v=a[g].end,y=a[g-1];if(r.has(x))return;r.add(x);let I=xn(a),L=[JSON.stringify(e),String(h.line),String(h.column),I?JSON.stringify(I):void 0].filter(g=>g!==void 0).join(","),k=`globalThis.${Pe}?.(${L});`;if(bn(m,y)){t.push({start:x,end:x,code:`{${k}`}),t.push({start:v,end:v,code:"}"});return}t.push({start:x,end:x,code:k})}),{edits:t,marked:t.length}}var wn={show:"Show",for:"For",index:"Index",switch:"Switch",match:"Match",dynamic:"Dynamic",errorBoundary:"ErrorBoundary",suspense:"Suspense"};function Rn(n){return/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)}function yt(n){return Rn(n)?n:JSON.stringify(n)}function En(n){if(n.expr!==void 0){if(n.name==="ref"&&n.at)return Z(`(${n.expr})`,n.at.line,n.at.column,n.expr);if(!n.reactive)return`(${n.expr})`;let e=`() => (${n.expr})`;return n.at?Z(e,n.at.line,n.at.column,n.directive):e}return JSON.stringify(n.literal??!0)}function kn(n){return n.expr!==void 0?`(${n.expr})`:JSON.stringify(n.literal??!0)}function Cn(n,e,t){let r=n.filter(a=>a.kind==="spread"),s=n.filter(a=>a.kind!=="spread").map(a=>`${yt(a.name)}: ${En(a)}`);e!=null&&s.push(`children: ${e}`);let o=`{ ${s.join(", ")} }`;return r.length>0?(t.add("mergeProps"),`mergeProps(${r.map(a=>a.expr).join(", ")}, ${o})`):o}function ht(n,e){return e?Z(n,e.line,e.column):n}function vt(n,e,t){return n.length===1?Q(n[0],e,t):`[${n.map(r=>Q(r,e,t)).join(", ")}]`}function gt(n,e,t,r,i,s){r.add("createMemo"),r.add("createComponent");let o=e.filter(d=>d.kind==="spread"),a=e.filter(d=>d.kind!=="spread"),l=a.map(d=>`get ${yt(d.name)}() { return ${kn(d)}; }`);t.length>0&&l.push(`get children() { return ${vt(t,r,i)}; }`);let c=`{ ${l.join(", ")} }`;o.length>0&&(r.add("mergeProps"),c=`mergeProps(${o.map(d=>d.expr).join(", ")}, ${c})`);let u={};for(let d of a)d.at&&(u[d.name]=[d.at.line,d.at.column]);return Object.keys(u).length&&(c=dt(c,u)),`createMemo((${s?`globalThis.${Pe}?.(${JSON.stringify(s.file)},${s.line},${s.column},${JSON.stringify(n)}), `:""}() => createComponent(${n}, ${c})))`}function In(n,e){let t=et(e.name),r=e.literal;return r===!0||r===void 0?`${n}.setAttribute(${JSON.stringify(t)}, "");`:r===!1||r===null?"":`${n}.setAttribute(${JSON.stringify(t)}, ${JSON.stringify(String(r))});`}var bt=!1;function Q(n,e,t){switch(n.kind){case"text":return JSON.stringify(n.value);case"expr":{if(!n.reactive)return`(${n.code})`;let r=`() => (${n.code})`;return n.at?Z(r,n.at.line,n.at.column):r}case"fragment":return n.children.length===0?"null":vt(n.children,e,t);case"component":return ht(gt(n.name,n.props,n.children,e,t,n.at),n.bindAt??n.at);case"control":{let r=wn[n.control]??n.control;return e.add(r),ht(gt(r,n.props,n.children,e,t),n.bindAt)}case"element":{if(t&&n.static&&!n.svg&&Ie(n)){e.add("templateNode");let a=`_tmpl$${t.length}`;t.push({id:a,html:G(n),tag:n.tag,svg:!1});let l=$e(n);return l?`templateNode(${a}, ${JSON.stringify(n.tag)}, false, false, ${JSON.stringify(l)})`:`templateNode(${a}, ${JSON.stringify(n.tag)})`}if(t&&bt){let a=rt(n);if(a){e.add("templateNode"),e.add("insert"),e.add("holeEnd"),e.add("holeContent"),e.add("holeScope");let l=`_tmpl$${t.length}`;t.push({id:l,html:a.html,tag:a.tag,svg:!1});let c=$e(n),u=[`const _el$ = templateNode(${l}, ${JSON.stringify(a.tag)}, true${c?`, false, ${JSON.stringify(c)}`:""});`];for(let f of a.steps)u.push(`const ${f.ref} = ${f.expr};`);for(let f of a.holes)u.push(`insert(${f.parentRef}, holeScope(${f.startRef}, () => (${Q(f.node,e,t)})), ${f.endRef}, holeContent(${f.startRef}, ${f.endRef}));`);return u.push("return _el$;"),`(() => { ${u.join(" ")} })()`}}e.add("createNativeElement");let r=JSON.stringify(n.tag),i="_el$",s=[],o=n.at?`${r}, ${n.svg}, ${JSON.stringify(n.at)}`:n.svg?`${r}, true`:r;if(s.push(`const ${i} = createNativeElement(${o});`),n.props.length>0)if(!n.svg&&n.props.every(K))for(let a of n.props)s.push(In(i,a));else{e.add("spread");let a=n.svg?", isSVG: true":"";s.push(`spread({ element: ${i}, props: ${Cn(n.props,null,e)}${a} });`)}for(let a of n.children){e.add("insert");let l=Q(a,e,t),c=a.kind==="expr"&&a.reactive||a.kind==="component"||a.kind==="control";s.push(c?`insert(${i}, ${l}, null);`:`insert(${i}, ${l});`)}return s.push(`return ${i};`),`(() => { ${s.join(" ")} })()`}}}function ve(n,e){if(!e||e.length===0)return n;let t=new Map(e.map(r=>[r.id,JSON.stringify(r.html)]));return n.replace(/_tmpl\$\d+/g,r=>t.get(r)??r)}var H={name:"imperative",contract:Te,emit(n,e){let t=new Set,r=e?.templateClone!==!1?[]:void 0;return bt=e?.partialTemplates===!0,{code:Q(n,t,r),imports:Array.from(t),templates:r}}};var $n=n=>"components"in n;function Nt(n,e){return n.replace(/\$(\d+)/g,(t,r)=>e[Number(r)]??t)}function Tn(n,e){return typeof e=="string"?{module:e,export:n}:e}function be(n=[]){let e=new Map,t=[],r=[];for(let o of n){if(!$n(o)){r.push(o);continue}for(let[a,l]of Object.entries(o.components)){let c=Tn(a,l),u=e.get(a);if(u&&u.module!==c.module){let f=t.find(d=>d.name===a);f?f.modules.push(c.module):t.push({name:a,modules:[u.module,c.module]});continue}e.set(a,c)}}let i=new Map;return{resolve:o=>{if(i.has(o))return i.get(o);let a=e.get(o);if(!a)for(let l of r){let c=o.match(new RegExp(l.match.source,l.match.flags.replace("g","")));if(c){a={module:Nt(l.module,c),export:l.export?Nt(l.export,c):o};break}}return i.set(o,a),a},names:()=>[...e.keys()],conflicts:()=>t}}import{parseTemplate as On}from"@fluixi/template-parser";function ee(n){let e=n.split(/\r\n|\n|\r/),t=0;for(let i=0;i<e.length;i++)/[^ \t]/.test(e[i])&&(t=i);let r="";for(let i=0;i<e.length;i++){let s=e[i].replace(/\t/g," ");i!==0&&(s=s.replace(/^ +/,"")),i!==e.length-1&&(s=s.replace(/ +$/,"")),s&&(i!==t&&(s+=" "),r+=s)}return r}var xt=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]);function Pn(n){return n.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,"\\${")}function St(n){return n.charAt(0).toUpperCase()+n.slice(1)}var Le=class{constructor(e,t,r,i){this.holes=e;this.used=t;this.sourceFile=r;this.positionAt=i}tagAt(e){let t=e.loc?.start;if(!this.sourceFile||!this.positionAt||t==null)return;let{line:r,column:i}=this.positionAt(t);return{file:this.sourceFile,line:r,column:i}}hole(e){return this.holes[e]??{code:"undefined",reactive:!1}}emit(e){let{code:t,imports:r,templates:i}=H.emit(e,{});for(let s of r)this.used.add(s);return ve(t,i)}lowerRoot(e){let t=this.lowerChildren(e);return t.length===1?t[0]:{kind:"fragment",children:t}}lowerChildren(e){let t=[];for(let r=0;r<e.length;r++){let i=e[r];if(i.kind==="Element"||i.kind==="Component"){let o=i.attributes.find(a=>a.kind==="IfDirective");if(o&&o.kind==="IfDirective"){let a=r+1;a<e.length&&this.isBlankText(e[a])&&a++;let l=e[a],c=l&&(l.kind==="Element"||l.kind==="Component")&&l.attributes.some(u=>u.kind==="ElseDirective");t.push(this.lowerIf(i,o.hole,c?l:null)),c&&(r=a);continue}if(i.attributes.some(a=>a.kind==="EachDirective")){t.push(this.lowerEach(i));continue}}let s=this.lowerNode(i);s&&t.push(s)}return t}isBlankText(e){return e.kind==="Text"&&!e.raw&&ee(e.value)===""}lowerNode(e){switch(e.kind){case"Text":{if(e.raw)return{kind:"text",value:e.value};let t=ee(e.value);return t?{kind:"text",value:t}:null}case"Comment":return null;case"Expression":{let t=this.hole(e.hole);return{kind:"expr",code:t.code,reactive:t.reactive,...t.at?{at:t.at}:{}}}case"Fragment":return{kind:"fragment",children:this.lowerChildren(e.children)};case"Element":return this.lowerElement(e);case"Component":return this.lowerComponent(e);default:return null}}lowerElement(e){let t=e.attributes.find(i=>i.kind==="Attribute"&&i.name==="is");if(e.tag==="component"&&t&&t.value&&t.value.kind==="hole"){let i=e.attributes.filter(o=>o!==t),s=this.lowerComponent({...e,kind:"Component",tag:"Dynamic",tagHole:null,attributes:i});return s.props.unshift({name:"component",kind:"attr",expr:`() => (${this.hole(t.value.hole).code})`}),s}let r=this.tagAt(e);return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:this.lowerAttributes(e.attributes),children:this.lowerChildren(e.children),static:!1,...r?{at:r}:{}}}lowerComponent(e){let t=e.tagHole!=null?this.hole(e.tagHole).code:e.tag,r=this.lowerAttributes(e.attributes),{slots:i,rest:s}=this.partitionSlots(e.children);for(let[u,f]of i){let d=f.length===1?f[0]:{kind:"fragment",children:f},h={name:u,kind:"attr",expr:this.emit(d),jsxElement:!0},m=r.findIndex(y=>y.name===u);m>=0?r[m]=h:r.push(h)}let o=e.attributes.find(u=>u.kind==="LoadDirective"),a=o?ue(`load:${o.strategy}`,o.modifier):void 0,l=r.find(u=>u.at&&u.reactive)?.at,c=this.tagAt(e);return{kind:"component",name:t,props:r,children:this.lowerChildren(s),...c?{at:c}:{},...l?{bindAt:l}:{},...a?{load:a}:{}}}partitionSlots(e){let t=new Map,r=[];for(let i of e){if(i.kind==="Element"||i.kind==="Component"){let s=i.attributes.find(o=>o.kind==="Attribute"&&o.name==="slot");if(s&&s.value&&s.value.kind==="static"){let o={...i,attributes:i.attributes.filter(c=>c!==s)},a=o.kind==="Element"?this.lowerElement(o):this.lowerComponent(o),l=t.get(s.value.value)??[];l.push(a),t.set(s.value.value,l);continue}}r.push(i)}return{slots:t,rest:r}}lowerIf(e,t,r){let i=this.hole(t),s=[{name:"when",kind:"attr",expr:i.code,reactive:i.reactive,...i.at?{at:i.at}:{}}];if(r){let l=this.stripAndLower(r,c=>c.kind==="ElseDirective");s.push({name:"fallback",kind:"attr",expr:this.emit(l),jsxElement:!0})}let o=this.stripAndLower(e,l=>l.kind==="IfDirective"),a=this.tagAt(e);return{kind:"component",name:"Show",props:s,children:[o],...a?{at:a}:{},...i.at?{bindAt:i.at}:{}}}lowerEach(e){let t=e.attributes.find(c=>c.kind==="EachDirective");if(!t||t.kind!=="EachDirective")return this.lowerNode(e);let r=this.hole(t.hole),i=[{name:"each",kind:"attr",expr:r.code,reactive:r.reactive,...r.at?{at:r.at}:{}}];if(t.key){let c="static"in t.key?`(item) => item[${JSON.stringify(t.key.static)}]`:this.hole(t.key.hole).code;i.push({name:"by",kind:"attr",expr:c})}let s=this.itemArrow(e),o;if(s){let c={kind:"expr",code:s.body,reactive:s.bodyReactive},u=this.rebuildWithChildren(e,[c]);o=`(${s.params.join(", ")}) => (${this.emit(u)})`}else{let c=this.stripAndLower(e,u=>u.kind==="EachDirective");o=`() => (${this.emit(c)})`}let a=[{kind:"expr",code:o,reactive:!1}],l=this.tagAt(e);return{kind:"component",name:"For",props:i,children:a,...l?{at:l}:{},...r.at?{bindAt:r.at}:{}}}itemArrow(e){let t=e.children.filter(r=>!this.isBlankText(r));return t.length!==1||t[0].kind!=="Expression"?null:this.hole(t[0].hole).arrow??null}stripAndLower(e,t){let r={...e,attributes:e.attributes.filter(i=>!t(i))};return r.kind==="Element"?this.lowerElement(r):this.lowerComponent(r)}rebuildWithChildren(e,t){let r=this.lowerAttributes(e.attributes.filter(i=>i.kind!=="EachDirective"));if(e.kind==="Element"){let i=this.tagAt(e);return{kind:"element",tag:e.tag,svg:e.namespace==="svg",props:r,children:t,static:!1,...i?{at:i}:{}}}return{kind:"component",name:e.tag,props:r,children:t}}lowerAttributes(e){let t=[],r=[],i=!1,s,o=[],a=!1,l,c=[];for(let u of e)switch(u.kind){case"IfDirective":case"EachDirective":case"ElseDirective":break;case"Attribute":t.push(this.plainAttr(u));break;case"PropertyBinding":t.push({name:u.name,kind:"prop",expr:this.hole(u.hole).code,reactive:this.hole(u.hole).reactive,...this.hole(u.hole).at?{at:this.hole(u.hole).at}:{}});break;case"EventBinding":t.push(this.eventProp(u));break;case"RefBinding":t.push({name:"ref",kind:"ref",expr:this.hole(u.hole).code,reactive:this.hole(u.hole).reactive,...this.hole(u.hole).at?{at:this.hole(u.hole).at}:{}});break;case"Spread":t.push({name:"",kind:"spread",expr:this.hole(u.hole).code});break;case"ClassDirective":{let f=this.hole(u.hole);r.push(`${JSON.stringify(u.name)}: ${f.code}`),i=i||f.reactive,s??=f.at;break}case"StyleDirective":{let f=this.hole(u.hole);o.push(`${JSON.stringify(u.name)}: ${f.code}`),a=a||f.reactive,l??=f.at;break}case"BindDirective":t.push(...this.bindProps(u.name,this.hole(u.hole)));break;case"UseDirective":{let f=u.name??(u.hole!=null?this.hole(u.hole).code:null);if(!f)break;c.push(u.name!=null&&u.hole!=null?`[${f}, () => (${this.hole(u.hole).code})]`:`[${f}]`);break}}return r.length>0&&t.push({name:"classList",kind:"attr",expr:`{ ${r.join(", ")} }`,reactive:i,...s?{at:s}:{}}),o.length>0&&t.push({name:"style",kind:"attr",expr:`{ ${o.join(", ")} }`,reactive:a,...l?{at:l}:{}}),c.length>0&&t.push({name:"use",kind:"attr",expr:`[${c.join(", ")}]`}),t}eventProp(e){let t=e.name.toLowerCase(),r=this.hole(e.hole).code;if(!(e.syntax==="colon"||e.modifiers.length>0))return{name:"on"+St(e.name),kind:"event",event:{name:t,delegated:xt.has(t)},expr:r};let s=this.wrapHandler(r,e.modifiers),o=this.eventOptions(e.modifiers),a=o?`[${s}, ${o}]`:s;return{name:"on:"+t,kind:"attr",expr:a}}wrapHandler(e,t){let r=t.includes("self")?"if (e.target !== e.currentTarget) return; ":"",i=[];return t.includes("prevent")&&i.push("e.preventDefault();"),t.includes("stop")&&i.push("e.stopPropagation();"),!r&&i.length===0?e:`(e) => { ${r}${i.join(" ")} return (${e})(e); }`}eventOptions(e){let t=[];return e.includes("capture")&&t.push("capture: true"),e.includes("once")&&t.push("once: true"),e.includes("passive")&&t.push("passive: true"),t.length?`{ ${t.join(", ")} }`:null}bindProps(e,t){let r=t.code,i=e==="checked",s=i?"change":"input",o=i?"checked":"value";this.used.add("bindPair");let a=`bindPair(${r})`;return[{name:e,kind:"attr",expr:`${a}[0]()`,reactive:!0,directive:`bind:${e}`,...t.at?{at:t.at}:{}},{name:"on"+St(s),kind:"event",event:{name:s,delegated:xt.has(s)},expr:`(e) => ${a}[1](e.target.${o})`}]}plainAttr(e){let t=e.value,r=e.name;e.name==="class"?r=t&&t.kind==="hole"&&this.hole(t.hole).object?"classList":"className":e.name==="html"&&(r="innerHTML");let s={name:r,kind:"attr"};if(t==null)return s.literal=!0,s;if(t.kind==="static")return s.literal=t.value,s;if(t.kind==="hole"){let c=this.hole(t.hole);return s.expr=c.code,s.reactive=c.reactive,c.at&&(s.at=c.at),s}let o=!1,a,l=t.parts.map(c=>{if("text"in c)return Pn(c.text);let u=this.hole(c.hole);return o=o||u.reactive,a??=u.at,"${"+u.code+"}"}).join("");return s.expr="`"+l+"`",s.reactive=o,a&&(s.at=a),s}};function wt(n,e,t,r={}){let{root:i}=On(n,{svg:r.svg});return new Le(e,t,r.sourceFile,r.positionAt).lowerRoot(i.children)}function Rt(n,e,t={}){let r=new Set,i=wt(n,[...e],r,{svg:t.svg,...t.sourceFile?{sourceFile:t.sourceFile}:{},...t.positionAt?{positionAt:t.positionAt}:{}});B(i),de(i,{resolver:be(t.resolve??[]),modules:{controlFlowModule:t.controlFlowModule??"@fluixi/dom",coreModule:t.coreModule??"@fluixi/core",routerModule:t.routerModule??"@fluixi/core/router"},strategyFor:l=>l.load}),pe(i);let{code:s,imports:o,templates:a}=H.emit(i,{templateClone:t.templateClone,partialTemplates:t.partialTemplates});for(let l of o)r.add(l);return t.hoistTemplates?{code:s,imports:[...r],ir:i,templates:a}:{code:ve(s,a),imports:[...r],ir:i}}var Ln="children";function U(n){switch(n.kind){case"call":return!0;case"member":return n.property!==Ln;case"compound":return n.parts.some(U);case"opaque":return!1}}var Mn=new Set(["CallExpression","OptionalCallExpression"]),An=new Set(["MemberExpression","OptionalMemberExpression"]);function D(n){if(!n)return{kind:"opaque"};if(Mn.has(n.type))return{kind:"call"};if(An.has(n.type)){let e=n.property;return{kind:"member",property:!(n.computed===!0)&&e?.type==="Identifier"?e.name:null}}return n.type==="ConditionalExpression"?{kind:"compound",parts:[n.test,n.consequent,n.alternate].map(te)}:n.type==="LogicalExpression"||n.type==="BinaryExpression"?{kind:"compound",parts:[n.left,n.right].map(te)}:n.type==="TemplateLiteral"?{kind:"compound",parts:n.expressions.map(te)}:n.type==="ObjectExpression"?{kind:"compound",parts:n.properties.filter(t=>(t.type==="ObjectProperty"||t.type==="Property")&&t.computed!==!0).map(t=>te(t.value))}:n.type==="ArrayExpression"?{kind:"compound",parts:n.elements.filter(t=>t!=null&&t.type!=="SpreadElement").map(te)}:{kind:"opaque"}}var te=n=>D(n);var Dn=new Set(["svg","path","circle","rect","line","polygon","polyline","ellipse","g","defs","clipPath","text"]),jn=/^on[A-Z]/,_n=new Set(["click","dblclick","input","change","submit","focus","blur","keydown","keyup","keypress","mousedown","mouseup"]),Ct=n=>U(D(n)),ne=(n,e)=>{if(!e.sourceFile)return;let t=n.loc?.start;return t?{line:t.line,column:t.column}:void 0};function re(n){if(n.type==="StringLiteral"||n.type==="NumericLiteral"||n.type==="BooleanLiteral"||n.type==="Literal"&&(typeof n.value=="string"||typeof n.value=="number"||typeof n.value=="boolean"))return n.value}var Me=n=>!!n&&typeof re(n)=="string";function Jn(n,e){return n.type==="JSXIdentifier"?{tag:n.name,component:n.name[0]!==n.name[0].toLowerCase()}:n.type==="JSXMemberExpression"?{tag:e.code(n),component:!0}:n.type==="JSXNamespacedName"?{tag:`${n.namespace.name}:${n.name.name}`,component:!1}:{tag:"div",component:!1}}function Ae(n){return n.type==="JSXIdentifier"?n.name==="class"?"className":n.name:n.type==="JSXNamespacedName"?`${n.namespace.name}:${n.name.name}`:"unknown"}function Bn(n,e){let t=Ae(n.name),r=jn.test(t),i=r||t.startsWith("on:")||t==="ref",o={name:t,kind:r?"event":"attr"};if(r){let l=t.slice(2).toLowerCase();o.event={name:l,delegated:_n.has(l)}}let a=n.value;if(a==null)return o.literal=!0,o.at=ne(n,e),o;if(Me(a))return o.literal=re(a),o.at=ne(a,e),o;if(a.type==="JSXExpressionContainer"&&a.expression?.type!=="JSXEmptyExpression"){let l=a.expression,c=re(l);return c!==void 0?(o.literal=c,o.at=ne(l,e),o):(o.expr=e.code(l),o.reactive=i?!1:Ct(l),o.at=ne(l,e),(l.type==="JSXElement"||l.type==="JSXFragment")&&(o.jsxElement=!0),o)}return o.literal=!0,o}function Et(n,e){let t=n.value;return t==null?null:Me(t)?JSON.stringify(re(t)):t.type==="JSXExpressionContainer"&&t.expression?.type!=="JSXEmptyExpression"?e.code(t.expression):null}function Fn(n,e){let t=[],r=[];for(let i of n){if(i.type==="JSXSpreadAttribute"){t.push({name:"",kind:"spread",expr:e.code(i.argument)});continue}if(i.type!=="JSXAttribute"||Ee(Ae(i.name)))continue;let s=i.name.type==="JSXNamespacedName"?i.name.namespace.name:null;if(s==="use"){let o=i.name.name.name;e.used.add(o);let a=Et(i,e);r.push(a!=null?`[${o}, () => (${a})]`:`[${o}]`);continue}if(s==="oncapture"){let o=i.name.name.name.toLowerCase(),a=Et(i,e)??"undefined";t.push({name:"on:"+o,kind:"attr",expr:`[${a}, { capture: true }]`});continue}t.push(Bn(i,e))}return r.length>0&&t.push({name:"use",kind:"attr",expr:`[${r.join(", ")}]`}),t}function kt(n,e){let t=[];for(let r of n)if(r.type==="JSXText"){let i=ee(r.value);i&&t.push({kind:"text",value:i})}else if(r.type==="JSXExpressionContainer"){if(r.expression?.type!=="JSXEmptyExpression"){let i=r.expression,s=Ct(i);t.push({kind:"expr",code:e.code(i),reactive:s,...s?{at:ne(i,e)}:{}})}}else r.type==="JSXElement"||r.type==="JSXFragment"?t.push(It(r,e)):r.type==="JSXSpreadChild"&&t.push({kind:"expr",code:e.code(r.expression),reactive:!1});return t}function Hn(n){for(let e of n){if(e.type!=="JSXAttribute")continue;let t=Ae(e.name);if(Ee(t)){if(e.value&&!Me(e.value))throw new O(`'${t}' needs a literal value, not an expression — the strategy is compile-time.`);return ue(t,e.value?re(e.value):null)}}}function It(n,e){if(n.type==="JSXFragment")return{kind:"fragment",children:kt(n.children,e)};let{tag:t,component:r}=Jn(n.openingElement.name,e),i=Fn(n.openingElement.attributes,e),s=kt(n.children,e),o=e.sourceFile&&n.loc?.start?{file:e.sourceFile,line:n.loc.start.line,column:n.loc.start.column}:void 0;if(r){let a=Hn(n.openingElement.attributes);return{kind:"component",name:t,props:i,children:s,...a?{load:a}:{},...o?{at:o}:{}}}return{kind:"element",tag:t,svg:Dn.has(t),props:i,children:s,static:!1,...o?{at:o}:{}}}function $t(n,e){let t=It(n,e);return B(t),t}function P(n,e){if(n)switch(n.type){case"Identifier":e.add(n.name);return;case"ObjectPattern":for(let t of n.properties)t.type==="RestElement"?P(t.argument,e):P(t.value,e);return;case"ArrayPattern":for(let t of n.elements)P(t,e);return;case"AssignmentPattern":P(n.left,e);return;case"RestElement":P(n.argument,e);return}}function Tt(n,e){switch(n.type){case"ImportDeclaration":for(let t of n.specifiers)P(t.local,e);return;case"VariableDeclaration":for(let t of n.declarations)P(t.id,e);return;case"FunctionDeclaration":case"ClassDeclaration":P(n.id,e);return;case"ExportNamedDeclaration":case"ExportDefaultDeclaration":n.declaration&&Tt(n.declaration,e);return}}function ie(n){let e=new Set;for(let t of n.body)Tt(t,e);return e}import{parse as Xn}from"@babel/parser";function E(n,e){if(n)switch(n.type){case"Identifier":e.add(n.name);return;case"ObjectPattern":for(let t of n.properties)t.type==="RestElement"?E(t.argument,e):E(t.value,e);return;case"ArrayPattern":for(let t of n.elements)E(t,e);return;case"AssignmentPattern":E(n.left,e);return;case"RestElement":E(n.argument,e);return}}function De(n){switch(n.type){case"StringLiteral":case"NumericLiteral":case"BooleanLiteral":case"NullLiteral":case"BigIntLiteral":case"RegExpLiteral":case"Identifier":return!0;case"Literal":return!0;case"TemplateLiteral":return n.expressions.every(De);case"UnaryExpression":return De(n.argument);case"MemberExpression":return!1;default:return!1}}function Ne(n,e){if(!(!n||typeof n!="object")){e(n);for(let t of Object.keys(n)){if(t==="loc"||t==="range"||t==="leadingComments"||t==="trailingComments")continue;let r=n[t];if(Array.isArray(r))for(let i of r)i&&typeof i=="object"&&Ne(i,e);else r&&typeof r=="object"&&typeof r.type=="string"&&Ne(r,e)}}}function Un(n){let e=new Set;return Ne(n,t=>{if(t.type==="AssignmentExpression")E(t.left,e);else if(t.type==="UpdateExpression"){let r=t.argument;r?.type==="Identifier"&&e.add(r.name)}}),e}function qn(n,e){let t=new Set,r=i=>{let s=new Set;E(i,s);for(let o of s)e.has(o)&&t.add(o)};return Ne(n,i=>{switch(i.type){case"VariableDeclarator":r(i.id);return;case"FunctionDeclaration":case"FunctionExpression":case"ArrowFunctionExpression":r(i.id);for(let s of i.params??[])r(s);return;case"CatchClause":r(i.param);return;case"ClassDeclaration":case"ClassExpression":r(i.id);return}}),t}function Ot(n,e,t){for(let r of n.properties??[]){if(r.type==="RestElement"){let u=r.argument;if(u?.type!=="Identifier"||e.length>0){let f=new Set;E(u,f);for(let d of f)t.bails.push({name:d,reason:"rest"});continue}t.rest={name:u.name,keys:[]};continue}if(r.computed){let u=new Set;E(r.value,u);for(let f of u)t.bails.push({name:f,reason:"computed"});continue}let i=r.key,s=i.type==="Identifier"?i.name:i.value,o=r.value,a;o.type==="AssignmentPattern"&&(a=o.right,o=o.left);let l=[...e,s];if(o.type==="Identifier"){if(a&&!De(a)){t.bails.push({name:o.name,reason:"unsafe-default"});continue}t.reads.push({name:o.name,path:l,...a?{fallback:a}:{}});continue}if(o.type==="ObjectPattern"){if(a){let u=new Set;E(o,u);for(let f of u)t.bails.push({name:f,reason:"unsafe-default"});continue}Ot(o,l,t);continue}let c=new Set;E(o,c);for(let u of c)t.bails.push({name:u,reason:"computed"})}}function je(n,e){let t={reads:[],bails:[]};if(n?.type!=="ObjectPattern"||(Ot(n,[],t),t.rest&&(e?.type!=="BlockStatement"?(t.bails.push({name:t.rest.name,reason:"rest"}),delete t.rest):t.rest.keys=(n.properties??[]).filter(a=>a.type!=="RestElement"&&!a.computed).map(a=>{let l=a.key;return l.type==="Identifier"?l.name:l.value})),t.reads.length===0&&!t.rest))return t;let r=new Set(t.reads.map(a=>a.name));t.rest&&r.add(t.rest.name);let i=Un(e),s=qn(e,r),o=[];for(let a of t.reads)i.has(a.name)?t.bails.push({name:a.name,reason:"reassigned"}):s.has(a.name)?t.bails.push({name:a.name,reason:"shadowed"}):o.push(a);return t.reads=o,t.rest&&i.has(t.rest.name)?(t.bails.push({name:t.rest.name,reason:"reassigned"}),delete t.rest):t.rest&&s.has(t.rest.name)&&(t.bails.push({name:t.rest.name,reason:"shadowed"}),delete t.rest),t}function _e(n){switch(n){case"rest":return"this rest element cannot be served by splitProps, and copying the props into a plain object would lose the getters";case"computed":return"the property is not known until it runs";case"reassigned":return"the binding is assigned to, and props are read-only";case"shadowed":return"an inner scope binds the same name";case"unsafe-default":return"the default would have to run again on every read"}}function se(n,e,t=null){if(!(!n||typeof n!="object"||typeof n.type!="string")&&!n.type.startsWith("TS")&&e(n,t)!==!1)for(let r of Object.keys(n)){if(r==="loc"||r==="leadingComments"||r==="trailingComments")continue;let i=n[r];if(Array.isArray(i))for(let s of i)se(s,e,n);else i&&typeof i=="object"&&se(i,e,n)}}function Pt(n){return!!n&&n[0]>="A"&&n[0]<="Z"}function Vn(n){let e=[];return se(n,t=>{if(t.type==="FunctionDeclaration"&&Pt(t.id?.name)){e.push(t);return}if(t.type==="VariableDeclarator"&&Pt(t.id?.name)){let r=t.init;r&&(r.type==="ArrowFunctionExpression"||r.type==="FunctionExpression")&&e.push(r)}}),e}function zn(n){let e=new Set;return se(n,t=>{t.type==="Identifier"&&e.add(t.name)}),e}function Wn(n,e){if(!e)return!0;switch(e.type){case"MemberExpression":case"OptionalMemberExpression":return!(e.property===n&&!e.computed);case"ObjectProperty":case"ObjectMethod":case"ClassProperty":case"ClassMethod":return!(e.key===n&&!e.computed);case"LabeledStatement":case"BreakStatement":case"ContinueStatement":return e.label!==n;case"ImportSpecifier":case"ExportSpecifier":return!1;default:return!0}}function Je(n,e,t={}){let r=[],i=[],s=new Set;for(let o of Vn(n)){let a=o.params?.[0];if(a?.type!=="ObjectPattern")continue;let l=je(a,o.body);for(let m of l.bails)i.push({name:m.name,reason:m.reason,message:_e(m.reason),start:a.start});if(l.bails.length>0||l.reads.length===0&&!l.rest)continue;let c=zn(o),u=t.parameterName??"props";for(;c.has(u);)u=`_${u}`;let f=new Map;for(let m of l.reads){let y=`${u}.${m.path.join(".")}`,S=m.fallback;f.set(m.name,S?`(${y} === undefined ? ${e.slice(S.start,S.end)} : ${y})`:y)}let d=a.typeAnnotation;if(r.push({start:a.start,end:d?.start??a.end,code:u}),l.rest){let m=l.rest.keys.map(S=>JSON.stringify(S)).join(", "),y=o.body;r.push({start:y.start+1,end:y.start+1,code:`
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
/*! @fluixi/compiler v1.0.0-alpha.85 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
|
|
3
|
+
// src/transform/templates.ts
|
|
4
|
+
import { parse as parse2 } from "@babel/parser";
|
|
5
|
+
|
|
6
|
+
// ../../node_modules/.pnpm/@jridgewell+sourcemap-codec@1.5.5/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
7
|
+
var comma = ",".charCodeAt(0);
|
|
8
|
+
var semicolon = ";".charCodeAt(0);
|
|
9
|
+
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
10
|
+
var intToChar = new Uint8Array(64);
|
|
11
|
+
var charToInt = new Uint8Array(128);
|
|
12
|
+
for (let i = 0; i < chars.length; i++) {
|
|
13
|
+
const c = chars.charCodeAt(i);
|
|
14
|
+
intToChar[i] = c;
|
|
15
|
+
charToInt[c] = i;
|
|
16
|
+
}
|
|
17
|
+
function encodeInteger(builder, num, relative) {
|
|
18
|
+
let delta = num - relative;
|
|
19
|
+
delta = delta < 0 ? -delta << 1 | 1 : delta << 1;
|
|
20
|
+
do {
|
|
21
|
+
let clamped = delta & 31;
|
|
22
|
+
delta >>>= 5;
|
|
23
|
+
if (delta > 0) clamped |= 32;
|
|
24
|
+
builder.write(intToChar[clamped]);
|
|
25
|
+
} while (delta > 0);
|
|
26
|
+
return num;
|
|
27
|
+
}
|
|
28
|
+
var bufLength = 1024 * 16;
|
|
29
|
+
var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
|
|
30
|
+
decode(buf) {
|
|
31
|
+
const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
32
|
+
return out.toString();
|
|
33
|
+
}
|
|
34
|
+
} : {
|
|
35
|
+
decode(buf) {
|
|
36
|
+
let out = "";
|
|
37
|
+
for (let i = 0; i < buf.length; i++) {
|
|
38
|
+
out += String.fromCharCode(buf[i]);
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var StringWriter = class {
|
|
44
|
+
constructor() {
|
|
45
|
+
this.pos = 0;
|
|
46
|
+
this.out = "";
|
|
47
|
+
this.buffer = new Uint8Array(bufLength);
|
|
48
|
+
}
|
|
49
|
+
write(v) {
|
|
50
|
+
const { buffer } = this;
|
|
51
|
+
buffer[this.pos++] = v;
|
|
52
|
+
if (this.pos === bufLength) {
|
|
53
|
+
this.out += td.decode(buffer);
|
|
54
|
+
this.pos = 0;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
flush() {
|
|
58
|
+
const { buffer, out, pos } = this;
|
|
59
|
+
return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
function encode(decoded) {
|
|
63
|
+
const writer = new StringWriter();
|
|
64
|
+
let sourcesIndex = 0;
|
|
65
|
+
let sourceLine = 0;
|
|
66
|
+
let sourceColumn = 0;
|
|
67
|
+
let namesIndex = 0;
|
|
68
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
69
|
+
const line = decoded[i];
|
|
70
|
+
if (i > 0) writer.write(semicolon);
|
|
71
|
+
if (line.length === 0) continue;
|
|
72
|
+
let genColumn = 0;
|
|
73
|
+
for (let j = 0; j < line.length; j++) {
|
|
74
|
+
const segment = line[j];
|
|
75
|
+
if (j > 0) writer.write(comma);
|
|
76
|
+
genColumn = encodeInteger(writer, segment[0], genColumn);
|
|
77
|
+
if (segment.length === 1) continue;
|
|
78
|
+
sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
|
|
79
|
+
sourceLine = encodeInteger(writer, segment[2], sourceLine);
|
|
80
|
+
sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
|
|
81
|
+
if (segment.length === 4) continue;
|
|
82
|
+
namesIndex = encodeInteger(writer, segment[4], namesIndex);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return writer.flush();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ../../node_modules/.pnpm/magic-string@0.30.21/node_modules/magic-string/dist/magic-string.es.mjs
|
|
89
|
+
var BitSet = class _BitSet {
|
|
90
|
+
constructor(arg) {
|
|
91
|
+
this.bits = arg instanceof _BitSet ? arg.bits.slice() : [];
|
|
92
|
+
}
|
|
93
|
+
add(n2) {
|
|
94
|
+
this.bits[n2 >> 5] |= 1 << (n2 & 31);
|
|
95
|
+
}
|
|
96
|
+
has(n2) {
|
|
97
|
+
return !!(this.bits[n2 >> 5] & 1 << (n2 & 31));
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var Chunk = class _Chunk {
|
|
101
|
+
constructor(start, end, content) {
|
|
102
|
+
this.start = start;
|
|
103
|
+
this.end = end;
|
|
104
|
+
this.original = content;
|
|
105
|
+
this.intro = "";
|
|
106
|
+
this.outro = "";
|
|
107
|
+
this.content = content;
|
|
108
|
+
this.storeName = false;
|
|
109
|
+
this.edited = false;
|
|
110
|
+
{
|
|
111
|
+
this.previous = null;
|
|
112
|
+
this.next = null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
appendLeft(content) {
|
|
116
|
+
this.outro += content;
|
|
117
|
+
}
|
|
118
|
+
appendRight(content) {
|
|
119
|
+
this.intro = this.intro + content;
|
|
120
|
+
}
|
|
121
|
+
clone() {
|
|
122
|
+
const chunk = new _Chunk(this.start, this.end, this.original);
|
|
123
|
+
chunk.intro = this.intro;
|
|
124
|
+
chunk.outro = this.outro;
|
|
125
|
+
chunk.content = this.content;
|
|
126
|
+
chunk.storeName = this.storeName;
|
|
127
|
+
chunk.edited = this.edited;
|
|
128
|
+
return chunk;
|
|
129
|
+
}
|
|
130
|
+
contains(index) {
|
|
131
|
+
return this.start < index && index < this.end;
|
|
132
|
+
}
|
|
133
|
+
eachNext(fn) {
|
|
134
|
+
let chunk = this;
|
|
135
|
+
while (chunk) {
|
|
136
|
+
fn(chunk);
|
|
137
|
+
chunk = chunk.next;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
eachPrevious(fn) {
|
|
141
|
+
let chunk = this;
|
|
142
|
+
while (chunk) {
|
|
143
|
+
fn(chunk);
|
|
144
|
+
chunk = chunk.previous;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
edit(content, storeName, contentOnly) {
|
|
148
|
+
this.content = content;
|
|
149
|
+
if (!contentOnly) {
|
|
150
|
+
this.intro = "";
|
|
151
|
+
this.outro = "";
|
|
152
|
+
}
|
|
153
|
+
this.storeName = storeName;
|
|
154
|
+
this.edited = true;
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
prependLeft(content) {
|
|
158
|
+
this.outro = content + this.outro;
|
|
159
|
+
}
|
|
160
|
+
prependRight(content) {
|
|
161
|
+
this.intro = content + this.intro;
|
|
162
|
+
}
|
|
163
|
+
reset() {
|
|
164
|
+
this.intro = "";
|
|
165
|
+
this.outro = "";
|
|
166
|
+
if (this.edited) {
|
|
167
|
+
this.content = this.original;
|
|
168
|
+
this.storeName = false;
|
|
169
|
+
this.edited = false;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
split(index) {
|
|
173
|
+
const sliceIndex = index - this.start;
|
|
174
|
+
const originalBefore = this.original.slice(0, sliceIndex);
|
|
175
|
+
const originalAfter = this.original.slice(sliceIndex);
|
|
176
|
+
this.original = originalBefore;
|
|
177
|
+
const newChunk = new _Chunk(index, this.end, originalAfter);
|
|
178
|
+
newChunk.outro = this.outro;
|
|
179
|
+
this.outro = "";
|
|
180
|
+
this.end = index;
|
|
181
|
+
if (this.edited) {
|
|
182
|
+
newChunk.edit("", false);
|
|
183
|
+
this.content = "";
|
|
184
|
+
} else {
|
|
185
|
+
this.content = originalBefore;
|
|
186
|
+
}
|
|
187
|
+
newChunk.next = this.next;
|
|
188
|
+
if (newChunk.next) newChunk.next.previous = newChunk;
|
|
189
|
+
newChunk.previous = this;
|
|
190
|
+
this.next = newChunk;
|
|
191
|
+
return newChunk;
|
|
192
|
+
}
|
|
193
|
+
toString() {
|
|
194
|
+
return this.intro + this.content + this.outro;
|
|
195
|
+
}
|
|
196
|
+
trimEnd(rx) {
|
|
197
|
+
this.outro = this.outro.replace(rx, "");
|
|
198
|
+
if (this.outro.length) return true;
|
|
199
|
+
const trimmed = this.content.replace(rx, "");
|
|
200
|
+
if (trimmed.length) {
|
|
201
|
+
if (trimmed !== this.content) {
|
|
202
|
+
this.split(this.start + trimmed.length).edit("", void 0, true);
|
|
203
|
+
if (this.edited) {
|
|
204
|
+
this.edit(trimmed, this.storeName, true);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
} else {
|
|
209
|
+
this.edit("", void 0, true);
|
|
210
|
+
this.intro = this.intro.replace(rx, "");
|
|
211
|
+
if (this.intro.length) return true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
trimStart(rx) {
|
|
215
|
+
this.intro = this.intro.replace(rx, "");
|
|
216
|
+
if (this.intro.length) return true;
|
|
217
|
+
const trimmed = this.content.replace(rx, "");
|
|
218
|
+
if (trimmed.length) {
|
|
219
|
+
if (trimmed !== this.content) {
|
|
220
|
+
const newChunk = this.split(this.end - trimmed.length);
|
|
221
|
+
if (this.edited) {
|
|
222
|
+
newChunk.edit(trimmed, this.storeName, true);
|
|
223
|
+
}
|
|
224
|
+
this.edit("", void 0, true);
|
|
225
|
+
}
|
|
226
|
+
return true;
|
|
227
|
+
} else {
|
|
228
|
+
this.edit("", void 0, true);
|
|
229
|
+
this.outro = this.outro.replace(rx, "");
|
|
230
|
+
if (this.outro.length) return true;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
function getBtoa() {
|
|
235
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
|
|
236
|
+
return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
|
|
237
|
+
} else if (typeof Buffer === "function") {
|
|
238
|
+
return (str) => Buffer.from(str, "utf-8").toString("base64");
|
|
239
|
+
} else {
|
|
240
|
+
return () => {
|
|
241
|
+
throw new Error("Unsupported environment: `window.btoa` or `Buffer` should be supported.");
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
var btoa = /* @__PURE__ */ getBtoa();
|
|
246
|
+
var SourceMap = class {
|
|
247
|
+
constructor(properties) {
|
|
248
|
+
this.version = 3;
|
|
249
|
+
this.file = properties.file;
|
|
250
|
+
this.sources = properties.sources;
|
|
251
|
+
this.sourcesContent = properties.sourcesContent;
|
|
252
|
+
this.names = properties.names;
|
|
253
|
+
this.mappings = encode(properties.mappings);
|
|
254
|
+
if (typeof properties.x_google_ignoreList !== "undefined") {
|
|
255
|
+
this.x_google_ignoreList = properties.x_google_ignoreList;
|
|
256
|
+
}
|
|
257
|
+
if (typeof properties.debugId !== "undefined") {
|
|
258
|
+
this.debugId = properties.debugId;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
toString() {
|
|
262
|
+
return JSON.stringify(this);
|
|
263
|
+
}
|
|
264
|
+
toUrl() {
|
|
265
|
+
return "data:application/json;charset=utf-8;base64," + btoa(this.toString());
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
function guessIndent(code) {
|
|
269
|
+
const lines = code.split("\n");
|
|
270
|
+
const tabbed = lines.filter((line) => /^\t+/.test(line));
|
|
271
|
+
const spaced = lines.filter((line) => /^ {2,}/.test(line));
|
|
272
|
+
if (tabbed.length === 0 && spaced.length === 0) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
if (tabbed.length >= spaced.length) {
|
|
276
|
+
return " ";
|
|
277
|
+
}
|
|
278
|
+
const min = spaced.reduce((previous, current) => {
|
|
279
|
+
const numSpaces = /^ +/.exec(current)[0].length;
|
|
280
|
+
return Math.min(numSpaces, previous);
|
|
281
|
+
}, Infinity);
|
|
282
|
+
return new Array(min + 1).join(" ");
|
|
283
|
+
}
|
|
284
|
+
function getRelativePath(from, to) {
|
|
285
|
+
const fromParts = from.split(/[/\\]/);
|
|
286
|
+
const toParts = to.split(/[/\\]/);
|
|
287
|
+
fromParts.pop();
|
|
288
|
+
while (fromParts[0] === toParts[0]) {
|
|
289
|
+
fromParts.shift();
|
|
290
|
+
toParts.shift();
|
|
291
|
+
}
|
|
292
|
+
if (fromParts.length) {
|
|
293
|
+
let i = fromParts.length;
|
|
294
|
+
while (i--) fromParts[i] = "..";
|
|
295
|
+
}
|
|
296
|
+
return fromParts.concat(toParts).join("/");
|
|
297
|
+
}
|
|
298
|
+
var toString = Object.prototype.toString;
|
|
299
|
+
function isObject(thing) {
|
|
300
|
+
return toString.call(thing) === "[object Object]";
|
|
301
|
+
}
|
|
302
|
+
function getLocator(source) {
|
|
303
|
+
const originalLines = source.split("\n");
|
|
304
|
+
const lineOffsets = [];
|
|
305
|
+
for (let i = 0, pos = 0; i < originalLines.length; i++) {
|
|
306
|
+
lineOffsets.push(pos);
|
|
307
|
+
pos += originalLines[i].length + 1;
|
|
308
|
+
}
|
|
309
|
+
return function locate(index) {
|
|
310
|
+
let i = 0;
|
|
311
|
+
let j = lineOffsets.length;
|
|
312
|
+
while (i < j) {
|
|
313
|
+
const m = i + j >> 1;
|
|
314
|
+
if (index < lineOffsets[m]) {
|
|
315
|
+
j = m;
|
|
316
|
+
} else {
|
|
317
|
+
i = m + 1;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
const line = i - 1;
|
|
321
|
+
const column = index - lineOffsets[line];
|
|
322
|
+
return { line, column };
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
var wordRegex = /\w/;
|
|
326
|
+
var Mappings = class {
|
|
327
|
+
constructor(hires) {
|
|
328
|
+
this.hires = hires;
|
|
329
|
+
this.generatedCodeLine = 0;
|
|
330
|
+
this.generatedCodeColumn = 0;
|
|
331
|
+
this.raw = [];
|
|
332
|
+
this.rawSegments = this.raw[this.generatedCodeLine] = [];
|
|
333
|
+
this.pending = null;
|
|
334
|
+
}
|
|
335
|
+
addEdit(sourceIndex, content, loc, nameIndex) {
|
|
336
|
+
if (content.length) {
|
|
337
|
+
const contentLengthMinusOne = content.length - 1;
|
|
338
|
+
let contentLineEnd = content.indexOf("\n", 0);
|
|
339
|
+
let previousContentLineEnd = -1;
|
|
340
|
+
while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
|
|
341
|
+
const segment2 = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
342
|
+
if (nameIndex >= 0) {
|
|
343
|
+
segment2.push(nameIndex);
|
|
344
|
+
}
|
|
345
|
+
this.rawSegments.push(segment2);
|
|
346
|
+
this.generatedCodeLine += 1;
|
|
347
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
348
|
+
this.generatedCodeColumn = 0;
|
|
349
|
+
previousContentLineEnd = contentLineEnd;
|
|
350
|
+
contentLineEnd = content.indexOf("\n", contentLineEnd + 1);
|
|
351
|
+
}
|
|
352
|
+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
353
|
+
if (nameIndex >= 0) {
|
|
354
|
+
segment.push(nameIndex);
|
|
355
|
+
}
|
|
356
|
+
this.rawSegments.push(segment);
|
|
357
|
+
this.advance(content.slice(previousContentLineEnd + 1));
|
|
358
|
+
} else if (this.pending) {
|
|
359
|
+
this.rawSegments.push(this.pending);
|
|
360
|
+
this.advance(content);
|
|
361
|
+
}
|
|
362
|
+
this.pending = null;
|
|
363
|
+
}
|
|
364
|
+
addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
|
|
365
|
+
let originalCharIndex = chunk.start;
|
|
366
|
+
let first = true;
|
|
367
|
+
let charInHiresBoundary = false;
|
|
368
|
+
while (originalCharIndex < chunk.end) {
|
|
369
|
+
if (original[originalCharIndex] === "\n") {
|
|
370
|
+
loc.line += 1;
|
|
371
|
+
loc.column = 0;
|
|
372
|
+
this.generatedCodeLine += 1;
|
|
373
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
374
|
+
this.generatedCodeColumn = 0;
|
|
375
|
+
first = true;
|
|
376
|
+
charInHiresBoundary = false;
|
|
377
|
+
} else {
|
|
378
|
+
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
|
|
379
|
+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
380
|
+
if (this.hires === "boundary") {
|
|
381
|
+
if (wordRegex.test(original[originalCharIndex])) {
|
|
382
|
+
if (!charInHiresBoundary) {
|
|
383
|
+
this.rawSegments.push(segment);
|
|
384
|
+
charInHiresBoundary = true;
|
|
385
|
+
}
|
|
386
|
+
} else {
|
|
387
|
+
this.rawSegments.push(segment);
|
|
388
|
+
charInHiresBoundary = false;
|
|
389
|
+
}
|
|
390
|
+
} else {
|
|
391
|
+
this.rawSegments.push(segment);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
loc.column += 1;
|
|
395
|
+
this.generatedCodeColumn += 1;
|
|
396
|
+
first = false;
|
|
397
|
+
}
|
|
398
|
+
originalCharIndex += 1;
|
|
399
|
+
}
|
|
400
|
+
this.pending = null;
|
|
401
|
+
}
|
|
402
|
+
advance(str) {
|
|
403
|
+
if (!str) return;
|
|
404
|
+
const lines = str.split("\n");
|
|
405
|
+
if (lines.length > 1) {
|
|
406
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
407
|
+
this.generatedCodeLine++;
|
|
408
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
409
|
+
}
|
|
410
|
+
this.generatedCodeColumn = 0;
|
|
411
|
+
}
|
|
412
|
+
this.generatedCodeColumn += lines[lines.length - 1].length;
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
var n = "\n";
|
|
416
|
+
var warned = {
|
|
417
|
+
insertLeft: false,
|
|
418
|
+
insertRight: false,
|
|
419
|
+
storeName: false
|
|
420
|
+
};
|
|
421
|
+
var MagicString = class _MagicString {
|
|
422
|
+
constructor(string, options = {}) {
|
|
423
|
+
const chunk = new Chunk(0, string.length, string);
|
|
424
|
+
Object.defineProperties(this, {
|
|
425
|
+
original: { writable: true, value: string },
|
|
426
|
+
outro: { writable: true, value: "" },
|
|
427
|
+
intro: { writable: true, value: "" },
|
|
428
|
+
firstChunk: { writable: true, value: chunk },
|
|
429
|
+
lastChunk: { writable: true, value: chunk },
|
|
430
|
+
lastSearchedChunk: { writable: true, value: chunk },
|
|
431
|
+
byStart: { writable: true, value: {} },
|
|
432
|
+
byEnd: { writable: true, value: {} },
|
|
433
|
+
filename: { writable: true, value: options.filename },
|
|
434
|
+
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
|
|
435
|
+
sourcemapLocations: { writable: true, value: new BitSet() },
|
|
436
|
+
storedNames: { writable: true, value: {} },
|
|
437
|
+
indentStr: { writable: true, value: void 0 },
|
|
438
|
+
ignoreList: { writable: true, value: options.ignoreList },
|
|
439
|
+
offset: { writable: true, value: options.offset || 0 }
|
|
440
|
+
});
|
|
441
|
+
this.byStart[0] = chunk;
|
|
442
|
+
this.byEnd[string.length] = chunk;
|
|
443
|
+
}
|
|
444
|
+
addSourcemapLocation(char) {
|
|
445
|
+
this.sourcemapLocations.add(char);
|
|
446
|
+
}
|
|
447
|
+
append(content) {
|
|
448
|
+
if (typeof content !== "string") throw new TypeError("outro content must be a string");
|
|
449
|
+
this.outro += content;
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
appendLeft(index, content) {
|
|
453
|
+
index = index + this.offset;
|
|
454
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
455
|
+
this._split(index);
|
|
456
|
+
const chunk = this.byEnd[index];
|
|
457
|
+
if (chunk) {
|
|
458
|
+
chunk.appendLeft(content);
|
|
459
|
+
} else {
|
|
460
|
+
this.intro += content;
|
|
461
|
+
}
|
|
462
|
+
return this;
|
|
463
|
+
}
|
|
464
|
+
appendRight(index, content) {
|
|
465
|
+
index = index + this.offset;
|
|
466
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
467
|
+
this._split(index);
|
|
468
|
+
const chunk = this.byStart[index];
|
|
469
|
+
if (chunk) {
|
|
470
|
+
chunk.appendRight(content);
|
|
471
|
+
} else {
|
|
472
|
+
this.outro += content;
|
|
473
|
+
}
|
|
474
|
+
return this;
|
|
475
|
+
}
|
|
476
|
+
clone() {
|
|
477
|
+
const cloned = new _MagicString(this.original, { filename: this.filename, offset: this.offset });
|
|
478
|
+
let originalChunk = this.firstChunk;
|
|
479
|
+
let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
|
|
480
|
+
while (originalChunk) {
|
|
481
|
+
cloned.byStart[clonedChunk.start] = clonedChunk;
|
|
482
|
+
cloned.byEnd[clonedChunk.end] = clonedChunk;
|
|
483
|
+
const nextOriginalChunk = originalChunk.next;
|
|
484
|
+
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
|
485
|
+
if (nextClonedChunk) {
|
|
486
|
+
clonedChunk.next = nextClonedChunk;
|
|
487
|
+
nextClonedChunk.previous = clonedChunk;
|
|
488
|
+
clonedChunk = nextClonedChunk;
|
|
489
|
+
}
|
|
490
|
+
originalChunk = nextOriginalChunk;
|
|
491
|
+
}
|
|
492
|
+
cloned.lastChunk = clonedChunk;
|
|
493
|
+
if (this.indentExclusionRanges) {
|
|
494
|
+
cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
|
495
|
+
}
|
|
496
|
+
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
|
497
|
+
cloned.intro = this.intro;
|
|
498
|
+
cloned.outro = this.outro;
|
|
499
|
+
return cloned;
|
|
500
|
+
}
|
|
501
|
+
generateDecodedMap(options) {
|
|
502
|
+
options = options || {};
|
|
503
|
+
const sourceIndex = 0;
|
|
504
|
+
const names = Object.keys(this.storedNames);
|
|
505
|
+
const mappings = new Mappings(options.hires);
|
|
506
|
+
const locate = getLocator(this.original);
|
|
507
|
+
if (this.intro) {
|
|
508
|
+
mappings.advance(this.intro);
|
|
509
|
+
}
|
|
510
|
+
this.firstChunk.eachNext((chunk) => {
|
|
511
|
+
const loc = locate(chunk.start);
|
|
512
|
+
if (chunk.intro.length) mappings.advance(chunk.intro);
|
|
513
|
+
if (chunk.edited) {
|
|
514
|
+
mappings.addEdit(
|
|
515
|
+
sourceIndex,
|
|
516
|
+
chunk.content,
|
|
517
|
+
loc,
|
|
518
|
+
chunk.storeName ? names.indexOf(chunk.original) : -1
|
|
519
|
+
);
|
|
520
|
+
} else {
|
|
521
|
+
mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
|
|
522
|
+
}
|
|
523
|
+
if (chunk.outro.length) mappings.advance(chunk.outro);
|
|
524
|
+
});
|
|
525
|
+
if (this.outro) {
|
|
526
|
+
mappings.advance(this.outro);
|
|
527
|
+
}
|
|
528
|
+
return {
|
|
529
|
+
file: options.file ? options.file.split(/[/\\]/).pop() : void 0,
|
|
530
|
+
sources: [
|
|
531
|
+
options.source ? getRelativePath(options.file || "", options.source) : options.file || ""
|
|
532
|
+
],
|
|
533
|
+
sourcesContent: options.includeContent ? [this.original] : void 0,
|
|
534
|
+
names,
|
|
535
|
+
mappings: mappings.raw,
|
|
536
|
+
x_google_ignoreList: this.ignoreList ? [sourceIndex] : void 0
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
generateMap(options) {
|
|
540
|
+
return new SourceMap(this.generateDecodedMap(options));
|
|
541
|
+
}
|
|
542
|
+
_ensureindentStr() {
|
|
543
|
+
if (this.indentStr === void 0) {
|
|
544
|
+
this.indentStr = guessIndent(this.original);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
_getRawIndentString() {
|
|
548
|
+
this._ensureindentStr();
|
|
549
|
+
return this.indentStr;
|
|
550
|
+
}
|
|
551
|
+
getIndentString() {
|
|
552
|
+
this._ensureindentStr();
|
|
553
|
+
return this.indentStr === null ? " " : this.indentStr;
|
|
554
|
+
}
|
|
555
|
+
indent(indentStr, options) {
|
|
556
|
+
const pattern = /^[^\r\n]/gm;
|
|
557
|
+
if (isObject(indentStr)) {
|
|
558
|
+
options = indentStr;
|
|
559
|
+
indentStr = void 0;
|
|
560
|
+
}
|
|
561
|
+
if (indentStr === void 0) {
|
|
562
|
+
this._ensureindentStr();
|
|
563
|
+
indentStr = this.indentStr || " ";
|
|
564
|
+
}
|
|
565
|
+
if (indentStr === "") return this;
|
|
566
|
+
options = options || {};
|
|
567
|
+
const isExcluded = {};
|
|
568
|
+
if (options.exclude) {
|
|
569
|
+
const exclusions = typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude;
|
|
570
|
+
exclusions.forEach((exclusion) => {
|
|
571
|
+
for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
|
|
572
|
+
isExcluded[i] = true;
|
|
573
|
+
}
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
let shouldIndentNextCharacter = options.indentStart !== false;
|
|
577
|
+
const replacer = (match) => {
|
|
578
|
+
if (shouldIndentNextCharacter) return `${indentStr}${match}`;
|
|
579
|
+
shouldIndentNextCharacter = true;
|
|
580
|
+
return match;
|
|
581
|
+
};
|
|
582
|
+
this.intro = this.intro.replace(pattern, replacer);
|
|
583
|
+
let charIndex = 0;
|
|
584
|
+
let chunk = this.firstChunk;
|
|
585
|
+
while (chunk) {
|
|
586
|
+
const end = chunk.end;
|
|
587
|
+
if (chunk.edited) {
|
|
588
|
+
if (!isExcluded[charIndex]) {
|
|
589
|
+
chunk.content = chunk.content.replace(pattern, replacer);
|
|
590
|
+
if (chunk.content.length) {
|
|
591
|
+
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
} else {
|
|
595
|
+
charIndex = chunk.start;
|
|
596
|
+
while (charIndex < end) {
|
|
597
|
+
if (!isExcluded[charIndex]) {
|
|
598
|
+
const char = this.original[charIndex];
|
|
599
|
+
if (char === "\n") {
|
|
600
|
+
shouldIndentNextCharacter = true;
|
|
601
|
+
} else if (char !== "\r" && shouldIndentNextCharacter) {
|
|
602
|
+
shouldIndentNextCharacter = false;
|
|
603
|
+
if (charIndex === chunk.start) {
|
|
604
|
+
chunk.prependRight(indentStr);
|
|
605
|
+
} else {
|
|
606
|
+
this._splitChunk(chunk, charIndex);
|
|
607
|
+
chunk = chunk.next;
|
|
608
|
+
chunk.prependRight(indentStr);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
charIndex += 1;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
charIndex = chunk.end;
|
|
616
|
+
chunk = chunk.next;
|
|
617
|
+
}
|
|
618
|
+
this.outro = this.outro.replace(pattern, replacer);
|
|
619
|
+
return this;
|
|
620
|
+
}
|
|
621
|
+
insert() {
|
|
622
|
+
throw new Error(
|
|
623
|
+
"magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)"
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
insertLeft(index, content) {
|
|
627
|
+
if (!warned.insertLeft) {
|
|
628
|
+
console.warn(
|
|
629
|
+
"magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead"
|
|
630
|
+
);
|
|
631
|
+
warned.insertLeft = true;
|
|
632
|
+
}
|
|
633
|
+
return this.appendLeft(index, content);
|
|
634
|
+
}
|
|
635
|
+
insertRight(index, content) {
|
|
636
|
+
if (!warned.insertRight) {
|
|
637
|
+
console.warn(
|
|
638
|
+
"magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead"
|
|
639
|
+
);
|
|
640
|
+
warned.insertRight = true;
|
|
641
|
+
}
|
|
642
|
+
return this.prependRight(index, content);
|
|
643
|
+
}
|
|
644
|
+
move(start, end, index) {
|
|
645
|
+
start = start + this.offset;
|
|
646
|
+
end = end + this.offset;
|
|
647
|
+
index = index + this.offset;
|
|
648
|
+
if (index >= start && index <= end) throw new Error("Cannot move a selection inside itself");
|
|
649
|
+
this._split(start);
|
|
650
|
+
this._split(end);
|
|
651
|
+
this._split(index);
|
|
652
|
+
const first = this.byStart[start];
|
|
653
|
+
const last = this.byEnd[end];
|
|
654
|
+
const oldLeft = first.previous;
|
|
655
|
+
const oldRight = last.next;
|
|
656
|
+
const newRight = this.byStart[index];
|
|
657
|
+
if (!newRight && last === this.lastChunk) return this;
|
|
658
|
+
const newLeft = newRight ? newRight.previous : this.lastChunk;
|
|
659
|
+
if (oldLeft) oldLeft.next = oldRight;
|
|
660
|
+
if (oldRight) oldRight.previous = oldLeft;
|
|
661
|
+
if (newLeft) newLeft.next = first;
|
|
662
|
+
if (newRight) newRight.previous = last;
|
|
663
|
+
if (!first.previous) this.firstChunk = last.next;
|
|
664
|
+
if (!last.next) {
|
|
665
|
+
this.lastChunk = first.previous;
|
|
666
|
+
this.lastChunk.next = null;
|
|
667
|
+
}
|
|
668
|
+
first.previous = newLeft;
|
|
669
|
+
last.next = newRight || null;
|
|
670
|
+
if (!newLeft) this.firstChunk = first;
|
|
671
|
+
if (!newRight) this.lastChunk = last;
|
|
672
|
+
return this;
|
|
673
|
+
}
|
|
674
|
+
overwrite(start, end, content, options) {
|
|
675
|
+
options = options || {};
|
|
676
|
+
return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
|
|
677
|
+
}
|
|
678
|
+
update(start, end, content, options) {
|
|
679
|
+
start = start + this.offset;
|
|
680
|
+
end = end + this.offset;
|
|
681
|
+
if (typeof content !== "string") throw new TypeError("replacement content must be a string");
|
|
682
|
+
if (this.original.length !== 0) {
|
|
683
|
+
while (start < 0) start += this.original.length;
|
|
684
|
+
while (end < 0) end += this.original.length;
|
|
685
|
+
}
|
|
686
|
+
if (end > this.original.length) throw new Error("end is out of bounds");
|
|
687
|
+
if (start === end)
|
|
688
|
+
throw new Error(
|
|
689
|
+
"Cannot overwrite a zero-length range – use appendLeft or prependRight instead"
|
|
690
|
+
);
|
|
691
|
+
this._split(start);
|
|
692
|
+
this._split(end);
|
|
693
|
+
if (options === true) {
|
|
694
|
+
if (!warned.storeName) {
|
|
695
|
+
console.warn(
|
|
696
|
+
"The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string"
|
|
697
|
+
);
|
|
698
|
+
warned.storeName = true;
|
|
699
|
+
}
|
|
700
|
+
options = { storeName: true };
|
|
701
|
+
}
|
|
702
|
+
const storeName = options !== void 0 ? options.storeName : false;
|
|
703
|
+
const overwrite = options !== void 0 ? options.overwrite : false;
|
|
704
|
+
if (storeName) {
|
|
705
|
+
const original = this.original.slice(start, end);
|
|
706
|
+
Object.defineProperty(this.storedNames, original, {
|
|
707
|
+
writable: true,
|
|
708
|
+
value: true,
|
|
709
|
+
enumerable: true
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
const first = this.byStart[start];
|
|
713
|
+
const last = this.byEnd[end];
|
|
714
|
+
if (first) {
|
|
715
|
+
let chunk = first;
|
|
716
|
+
while (chunk !== last) {
|
|
717
|
+
if (chunk.next !== this.byStart[chunk.end]) {
|
|
718
|
+
throw new Error("Cannot overwrite across a split point");
|
|
719
|
+
}
|
|
720
|
+
chunk = chunk.next;
|
|
721
|
+
chunk.edit("", false);
|
|
722
|
+
}
|
|
723
|
+
first.edit(content, storeName, !overwrite);
|
|
724
|
+
} else {
|
|
725
|
+
const newChunk = new Chunk(start, end, "").edit(content, storeName);
|
|
726
|
+
last.next = newChunk;
|
|
727
|
+
newChunk.previous = last;
|
|
728
|
+
}
|
|
729
|
+
return this;
|
|
730
|
+
}
|
|
731
|
+
prepend(content) {
|
|
732
|
+
if (typeof content !== "string") throw new TypeError("outro content must be a string");
|
|
733
|
+
this.intro = content + this.intro;
|
|
734
|
+
return this;
|
|
735
|
+
}
|
|
736
|
+
prependLeft(index, content) {
|
|
737
|
+
index = index + this.offset;
|
|
738
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
739
|
+
this._split(index);
|
|
740
|
+
const chunk = this.byEnd[index];
|
|
741
|
+
if (chunk) {
|
|
742
|
+
chunk.prependLeft(content);
|
|
743
|
+
} else {
|
|
744
|
+
this.intro = content + this.intro;
|
|
745
|
+
}
|
|
746
|
+
return this;
|
|
747
|
+
}
|
|
748
|
+
prependRight(index, content) {
|
|
749
|
+
index = index + this.offset;
|
|
750
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
751
|
+
this._split(index);
|
|
752
|
+
const chunk = this.byStart[index];
|
|
753
|
+
if (chunk) {
|
|
754
|
+
chunk.prependRight(content);
|
|
755
|
+
} else {
|
|
756
|
+
this.outro = content + this.outro;
|
|
757
|
+
}
|
|
758
|
+
return this;
|
|
759
|
+
}
|
|
760
|
+
remove(start, end) {
|
|
761
|
+
start = start + this.offset;
|
|
762
|
+
end = end + this.offset;
|
|
763
|
+
if (this.original.length !== 0) {
|
|
764
|
+
while (start < 0) start += this.original.length;
|
|
765
|
+
while (end < 0) end += this.original.length;
|
|
766
|
+
}
|
|
767
|
+
if (start === end) return this;
|
|
768
|
+
if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds");
|
|
769
|
+
if (start > end) throw new Error("end must be greater than start");
|
|
770
|
+
this._split(start);
|
|
771
|
+
this._split(end);
|
|
772
|
+
let chunk = this.byStart[start];
|
|
773
|
+
while (chunk) {
|
|
774
|
+
chunk.intro = "";
|
|
775
|
+
chunk.outro = "";
|
|
776
|
+
chunk.edit("");
|
|
777
|
+
chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
|
778
|
+
}
|
|
779
|
+
return this;
|
|
780
|
+
}
|
|
781
|
+
reset(start, end) {
|
|
782
|
+
start = start + this.offset;
|
|
783
|
+
end = end + this.offset;
|
|
784
|
+
if (this.original.length !== 0) {
|
|
785
|
+
while (start < 0) start += this.original.length;
|
|
786
|
+
while (end < 0) end += this.original.length;
|
|
787
|
+
}
|
|
788
|
+
if (start === end) return this;
|
|
789
|
+
if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds");
|
|
790
|
+
if (start > end) throw new Error("end must be greater than start");
|
|
791
|
+
this._split(start);
|
|
792
|
+
this._split(end);
|
|
793
|
+
let chunk = this.byStart[start];
|
|
794
|
+
while (chunk) {
|
|
795
|
+
chunk.reset();
|
|
796
|
+
chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
|
797
|
+
}
|
|
798
|
+
return this;
|
|
799
|
+
}
|
|
800
|
+
lastChar() {
|
|
801
|
+
if (this.outro.length) return this.outro[this.outro.length - 1];
|
|
802
|
+
let chunk = this.lastChunk;
|
|
803
|
+
do {
|
|
804
|
+
if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
|
|
805
|
+
if (chunk.content.length) return chunk.content[chunk.content.length - 1];
|
|
806
|
+
if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
|
|
807
|
+
} while (chunk = chunk.previous);
|
|
808
|
+
if (this.intro.length) return this.intro[this.intro.length - 1];
|
|
809
|
+
return "";
|
|
810
|
+
}
|
|
811
|
+
lastLine() {
|
|
812
|
+
let lineIndex = this.outro.lastIndexOf(n);
|
|
813
|
+
if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
|
|
814
|
+
let lineStr = this.outro;
|
|
815
|
+
let chunk = this.lastChunk;
|
|
816
|
+
do {
|
|
817
|
+
if (chunk.outro.length > 0) {
|
|
818
|
+
lineIndex = chunk.outro.lastIndexOf(n);
|
|
819
|
+
if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
|
|
820
|
+
lineStr = chunk.outro + lineStr;
|
|
821
|
+
}
|
|
822
|
+
if (chunk.content.length > 0) {
|
|
823
|
+
lineIndex = chunk.content.lastIndexOf(n);
|
|
824
|
+
if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
|
|
825
|
+
lineStr = chunk.content + lineStr;
|
|
826
|
+
}
|
|
827
|
+
if (chunk.intro.length > 0) {
|
|
828
|
+
lineIndex = chunk.intro.lastIndexOf(n);
|
|
829
|
+
if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
|
|
830
|
+
lineStr = chunk.intro + lineStr;
|
|
831
|
+
}
|
|
832
|
+
} while (chunk = chunk.previous);
|
|
833
|
+
lineIndex = this.intro.lastIndexOf(n);
|
|
834
|
+
if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
|
|
835
|
+
return this.intro + lineStr;
|
|
836
|
+
}
|
|
837
|
+
slice(start = 0, end = this.original.length - this.offset) {
|
|
838
|
+
start = start + this.offset;
|
|
839
|
+
end = end + this.offset;
|
|
840
|
+
if (this.original.length !== 0) {
|
|
841
|
+
while (start < 0) start += this.original.length;
|
|
842
|
+
while (end < 0) end += this.original.length;
|
|
843
|
+
}
|
|
844
|
+
let result = "";
|
|
845
|
+
let chunk = this.firstChunk;
|
|
846
|
+
while (chunk && (chunk.start > start || chunk.end <= start)) {
|
|
847
|
+
if (chunk.start < end && chunk.end >= end) {
|
|
848
|
+
return result;
|
|
849
|
+
}
|
|
850
|
+
chunk = chunk.next;
|
|
851
|
+
}
|
|
852
|
+
if (chunk && chunk.edited && chunk.start !== start)
|
|
853
|
+
throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
|
|
854
|
+
const startChunk = chunk;
|
|
855
|
+
while (chunk) {
|
|
856
|
+
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
|
|
857
|
+
result += chunk.intro;
|
|
858
|
+
}
|
|
859
|
+
const containsEnd = chunk.start < end && chunk.end >= end;
|
|
860
|
+
if (containsEnd && chunk.edited && chunk.end !== end)
|
|
861
|
+
throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
|
|
862
|
+
const sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
|
863
|
+
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
|
864
|
+
result += chunk.content.slice(sliceStart, sliceEnd);
|
|
865
|
+
if (chunk.outro && (!containsEnd || chunk.end === end)) {
|
|
866
|
+
result += chunk.outro;
|
|
867
|
+
}
|
|
868
|
+
if (containsEnd) {
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
chunk = chunk.next;
|
|
872
|
+
}
|
|
873
|
+
return result;
|
|
874
|
+
}
|
|
875
|
+
// TODO deprecate this? not really very useful
|
|
876
|
+
snip(start, end) {
|
|
877
|
+
const clone = this.clone();
|
|
878
|
+
clone.remove(0, start);
|
|
879
|
+
clone.remove(end, clone.original.length);
|
|
880
|
+
return clone;
|
|
881
|
+
}
|
|
882
|
+
_split(index) {
|
|
883
|
+
if (this.byStart[index] || this.byEnd[index]) return;
|
|
884
|
+
let chunk = this.lastSearchedChunk;
|
|
885
|
+
let previousChunk = chunk;
|
|
886
|
+
const searchForward = index > chunk.end;
|
|
887
|
+
while (chunk) {
|
|
888
|
+
if (chunk.contains(index)) return this._splitChunk(chunk, index);
|
|
889
|
+
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
|
|
890
|
+
if (chunk === previousChunk) return;
|
|
891
|
+
previousChunk = chunk;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
_splitChunk(chunk, index) {
|
|
895
|
+
if (chunk.edited && chunk.content.length) {
|
|
896
|
+
const loc = getLocator(this.original)(index);
|
|
897
|
+
throw new Error(
|
|
898
|
+
`Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
const newChunk = chunk.split(index);
|
|
902
|
+
this.byEnd[index] = chunk;
|
|
903
|
+
this.byStart[index] = newChunk;
|
|
904
|
+
this.byEnd[newChunk.end] = newChunk;
|
|
905
|
+
if (chunk === this.lastChunk) this.lastChunk = newChunk;
|
|
906
|
+
this.lastSearchedChunk = chunk;
|
|
907
|
+
return true;
|
|
908
|
+
}
|
|
909
|
+
toString() {
|
|
910
|
+
let str = this.intro;
|
|
911
|
+
let chunk = this.firstChunk;
|
|
912
|
+
while (chunk) {
|
|
913
|
+
str += chunk.toString();
|
|
914
|
+
chunk = chunk.next;
|
|
915
|
+
}
|
|
916
|
+
return str + this.outro;
|
|
917
|
+
}
|
|
918
|
+
isEmpty() {
|
|
919
|
+
let chunk = this.firstChunk;
|
|
920
|
+
do {
|
|
921
|
+
if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim())
|
|
922
|
+
return false;
|
|
923
|
+
} while (chunk = chunk.next);
|
|
924
|
+
return true;
|
|
925
|
+
}
|
|
926
|
+
length() {
|
|
927
|
+
let chunk = this.firstChunk;
|
|
928
|
+
let length = 0;
|
|
929
|
+
do {
|
|
930
|
+
length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
|
931
|
+
} while (chunk = chunk.next);
|
|
932
|
+
return length;
|
|
933
|
+
}
|
|
934
|
+
trimLines() {
|
|
935
|
+
return this.trim("[\\r\\n]");
|
|
936
|
+
}
|
|
937
|
+
trim(charType) {
|
|
938
|
+
return this.trimStart(charType).trimEnd(charType);
|
|
939
|
+
}
|
|
940
|
+
trimEndAborted(charType) {
|
|
941
|
+
const rx = new RegExp((charType || "\\s") + "+$");
|
|
942
|
+
this.outro = this.outro.replace(rx, "");
|
|
943
|
+
if (this.outro.length) return true;
|
|
944
|
+
let chunk = this.lastChunk;
|
|
945
|
+
do {
|
|
946
|
+
const end = chunk.end;
|
|
947
|
+
const aborted = chunk.trimEnd(rx);
|
|
948
|
+
if (chunk.end !== end) {
|
|
949
|
+
if (this.lastChunk === chunk) {
|
|
950
|
+
this.lastChunk = chunk.next;
|
|
951
|
+
}
|
|
952
|
+
this.byEnd[chunk.end] = chunk;
|
|
953
|
+
this.byStart[chunk.next.start] = chunk.next;
|
|
954
|
+
this.byEnd[chunk.next.end] = chunk.next;
|
|
955
|
+
}
|
|
956
|
+
if (aborted) return true;
|
|
957
|
+
chunk = chunk.previous;
|
|
958
|
+
} while (chunk);
|
|
959
|
+
return false;
|
|
960
|
+
}
|
|
961
|
+
trimEnd(charType) {
|
|
962
|
+
this.trimEndAborted(charType);
|
|
963
|
+
return this;
|
|
964
|
+
}
|
|
965
|
+
trimStartAborted(charType) {
|
|
966
|
+
const rx = new RegExp("^" + (charType || "\\s") + "+");
|
|
967
|
+
this.intro = this.intro.replace(rx, "");
|
|
968
|
+
if (this.intro.length) return true;
|
|
969
|
+
let chunk = this.firstChunk;
|
|
970
|
+
do {
|
|
971
|
+
const end = chunk.end;
|
|
972
|
+
const aborted = chunk.trimStart(rx);
|
|
973
|
+
if (chunk.end !== end) {
|
|
974
|
+
if (chunk === this.lastChunk) this.lastChunk = chunk.next;
|
|
975
|
+
this.byEnd[chunk.end] = chunk;
|
|
976
|
+
this.byStart[chunk.next.start] = chunk.next;
|
|
977
|
+
this.byEnd[chunk.next.end] = chunk.next;
|
|
978
|
+
}
|
|
979
|
+
if (aborted) return true;
|
|
980
|
+
chunk = chunk.next;
|
|
981
|
+
} while (chunk);
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
trimStart(charType) {
|
|
985
|
+
this.trimStartAborted(charType);
|
|
986
|
+
return this;
|
|
987
|
+
}
|
|
988
|
+
hasChanged() {
|
|
989
|
+
return this.original !== this.toString();
|
|
990
|
+
}
|
|
991
|
+
_replaceRegexp(searchValue, replacement) {
|
|
992
|
+
function getReplacement(match, str) {
|
|
993
|
+
if (typeof replacement === "string") {
|
|
994
|
+
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
|
|
995
|
+
if (i === "$") return "$";
|
|
996
|
+
if (i === "&") return match[0];
|
|
997
|
+
const num = +i;
|
|
998
|
+
if (num < match.length) return match[+i];
|
|
999
|
+
return `$${i}`;
|
|
1000
|
+
});
|
|
1001
|
+
} else {
|
|
1002
|
+
return replacement(...match, match.index, str, match.groups);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
function matchAll(re, str) {
|
|
1006
|
+
let match;
|
|
1007
|
+
const matches = [];
|
|
1008
|
+
while (match = re.exec(str)) {
|
|
1009
|
+
matches.push(match);
|
|
1010
|
+
}
|
|
1011
|
+
return matches;
|
|
1012
|
+
}
|
|
1013
|
+
if (searchValue.global) {
|
|
1014
|
+
const matches = matchAll(searchValue, this.original);
|
|
1015
|
+
matches.forEach((match) => {
|
|
1016
|
+
if (match.index != null) {
|
|
1017
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1018
|
+
if (replacement2 !== match[0]) {
|
|
1019
|
+
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
} else {
|
|
1024
|
+
const match = this.original.match(searchValue);
|
|
1025
|
+
if (match && match.index != null) {
|
|
1026
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1027
|
+
if (replacement2 !== match[0]) {
|
|
1028
|
+
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return this;
|
|
1033
|
+
}
|
|
1034
|
+
_replaceString(string, replacement) {
|
|
1035
|
+
const { original } = this;
|
|
1036
|
+
const index = original.indexOf(string);
|
|
1037
|
+
if (index !== -1) {
|
|
1038
|
+
if (typeof replacement === "function") {
|
|
1039
|
+
replacement = replacement(string, index, original);
|
|
1040
|
+
}
|
|
1041
|
+
if (string !== replacement) {
|
|
1042
|
+
this.overwrite(index, index + string.length, replacement);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
return this;
|
|
1046
|
+
}
|
|
1047
|
+
replace(searchValue, replacement) {
|
|
1048
|
+
if (typeof searchValue === "string") {
|
|
1049
|
+
return this._replaceString(searchValue, replacement);
|
|
1050
|
+
}
|
|
1051
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1052
|
+
}
|
|
1053
|
+
_replaceAllString(string, replacement) {
|
|
1054
|
+
const { original } = this;
|
|
1055
|
+
const stringLength = string.length;
|
|
1056
|
+
for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
|
|
1057
|
+
const previous = original.slice(index, index + stringLength);
|
|
1058
|
+
let _replacement = replacement;
|
|
1059
|
+
if (typeof replacement === "function") {
|
|
1060
|
+
_replacement = replacement(previous, index, original);
|
|
1061
|
+
}
|
|
1062
|
+
if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);
|
|
1063
|
+
}
|
|
1064
|
+
return this;
|
|
1065
|
+
}
|
|
1066
|
+
replaceAll(searchValue, replacement) {
|
|
1067
|
+
if (typeof searchValue === "string") {
|
|
1068
|
+
return this._replaceAllString(searchValue, replacement);
|
|
1069
|
+
}
|
|
1070
|
+
if (!searchValue.global) {
|
|
1071
|
+
throw new TypeError(
|
|
1072
|
+
"MagicString.prototype.replaceAll called with a non-global RegExp argument"
|
|
1073
|
+
);
|
|
1074
|
+
}
|
|
1075
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1076
|
+
}
|
|
1077
|
+
};
|
|
1078
|
+
|
|
1079
|
+
// src/optimize/analyze-static.ts
|
|
1080
|
+
function propReason(prop) {
|
|
1081
|
+
switch (prop.kind) {
|
|
1082
|
+
case "event":
|
|
1083
|
+
return "event";
|
|
1084
|
+
case "ref":
|
|
1085
|
+
return "ref";
|
|
1086
|
+
case "spread":
|
|
1087
|
+
return "spread";
|
|
1088
|
+
case "class":
|
|
1089
|
+
case "style":
|
|
1090
|
+
return "class-or-style-directive";
|
|
1091
|
+
case "attr":
|
|
1092
|
+
case "prop":
|
|
1093
|
+
if (prop.reactive) return "reactive-prop";
|
|
1094
|
+
if (prop.expr !== void 0) return "expression-prop";
|
|
1095
|
+
return void 0;
|
|
1096
|
+
/* c8 ignore next 2 */
|
|
1097
|
+
default:
|
|
1098
|
+
return "expression-prop";
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
function analyzeStatic(root) {
|
|
1102
|
+
const staticElements = [];
|
|
1103
|
+
const reasons = /* @__PURE__ */ new Map();
|
|
1104
|
+
const visit = (node) => {
|
|
1105
|
+
switch (node.kind) {
|
|
1106
|
+
case "text":
|
|
1107
|
+
return true;
|
|
1108
|
+
case "expr":
|
|
1109
|
+
return false;
|
|
1110
|
+
case "component":
|
|
1111
|
+
case "control":
|
|
1112
|
+
for (const child of node.children) visit(child);
|
|
1113
|
+
return false;
|
|
1114
|
+
case "fragment":
|
|
1115
|
+
for (const child of node.children) visit(child);
|
|
1116
|
+
return false;
|
|
1117
|
+
case "element": {
|
|
1118
|
+
let childrenStatic = true;
|
|
1119
|
+
for (const child of node.children) {
|
|
1120
|
+
if (!visit(child)) childrenStatic = false;
|
|
1121
|
+
}
|
|
1122
|
+
let reason;
|
|
1123
|
+
for (const prop of node.props) {
|
|
1124
|
+
reason = propReason(prop);
|
|
1125
|
+
if (reason) break;
|
|
1126
|
+
}
|
|
1127
|
+
if (!reason && !childrenStatic) {
|
|
1128
|
+
reason = childReason(node);
|
|
1129
|
+
}
|
|
1130
|
+
node.static = !reason;
|
|
1131
|
+
if (reason) reasons.set(node, reason);
|
|
1132
|
+
else staticElements.push(node);
|
|
1133
|
+
return node.static;
|
|
1134
|
+
}
|
|
1135
|
+
/* c8 ignore next 2 */
|
|
1136
|
+
default:
|
|
1137
|
+
return false;
|
|
1138
|
+
}
|
|
1139
|
+
};
|
|
1140
|
+
for (const node of Array.isArray(root) ? root : [root]) visit(node);
|
|
1141
|
+
return { staticElements, reasons };
|
|
1142
|
+
}
|
|
1143
|
+
function childReason(node) {
|
|
1144
|
+
for (const child of node.children) {
|
|
1145
|
+
if (child.kind === "expr") return "expression-child";
|
|
1146
|
+
if (child.kind === "component") return "component-child";
|
|
1147
|
+
if (child.kind === "control") return "control-child";
|
|
1148
|
+
if (child.kind === "element" && !child.static) return "expression-child";
|
|
1149
|
+
if (child.kind === "fragment") return "expression-child";
|
|
1150
|
+
}
|
|
1151
|
+
return "expression-child";
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
// src/resolve/load-directive.ts
|
|
1155
|
+
var EAGER = { kind: "eager" };
|
|
1156
|
+
var DEFAULT_INTERACTION_EVENTS = ["pointerdown", "focusin", "keydown"];
|
|
1157
|
+
var STRATEGIES = /* @__PURE__ */ new Set(["eager", "idle", "visible", "interaction", "media", "never"]);
|
|
1158
|
+
var LoadDirectiveError = class extends Error {
|
|
1159
|
+
};
|
|
1160
|
+
function isLoadDirective(name) {
|
|
1161
|
+
return name.startsWith("load:");
|
|
1162
|
+
}
|
|
1163
|
+
function parseLoadDirective(name, value) {
|
|
1164
|
+
const strategy = name.slice("load:".length);
|
|
1165
|
+
if (!STRATEGIES.has(strategy)) {
|
|
1166
|
+
throw new LoadDirectiveError(
|
|
1167
|
+
`Unknown load strategy 'load:${strategy}'. Expected one of ${[...STRATEGIES].join(", ")}.`
|
|
1168
|
+
);
|
|
1169
|
+
}
|
|
1170
|
+
switch (strategy) {
|
|
1171
|
+
case "eager":
|
|
1172
|
+
return EAGER;
|
|
1173
|
+
case "idle":
|
|
1174
|
+
return { kind: "idle" };
|
|
1175
|
+
case "never":
|
|
1176
|
+
return { kind: "never" };
|
|
1177
|
+
case "visible":
|
|
1178
|
+
return value ? { kind: "visible", rootMargin: value } : { kind: "visible" };
|
|
1179
|
+
case "interaction":
|
|
1180
|
+
return {
|
|
1181
|
+
kind: "interaction",
|
|
1182
|
+
events: value ? splitEvents(value) : [...DEFAULT_INTERACTION_EVENTS]
|
|
1183
|
+
};
|
|
1184
|
+
case "media":
|
|
1185
|
+
if (!value) {
|
|
1186
|
+
throw new LoadDirectiveError(
|
|
1187
|
+
`'load:media' needs a query, e.g. load:media="(min-width: 1024px)".`
|
|
1188
|
+
);
|
|
1189
|
+
}
|
|
1190
|
+
return { kind: "media", query: value };
|
|
1191
|
+
default:
|
|
1192
|
+
throw new LoadDirectiveError(`Unhandled load strategy '${strategy}'.`);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
function splitEvents(value) {
|
|
1196
|
+
const events = value.split(/[\s,]+/).map((e) => e.trim()).filter(Boolean);
|
|
1197
|
+
if (events.length === 0) {
|
|
1198
|
+
throw new LoadDirectiveError(`'load:interaction' was given no event names.`);
|
|
1199
|
+
}
|
|
1200
|
+
return events;
|
|
1201
|
+
}
|
|
1202
|
+
function isDeferred(strategy) {
|
|
1203
|
+
return strategy.kind !== "eager" && strategy.kind !== "never";
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
// src/resolve/builtins.ts
|
|
1207
|
+
var DOM_CONTROL_FLOW = [
|
|
1208
|
+
"Show",
|
|
1209
|
+
"For",
|
|
1210
|
+
"Index",
|
|
1211
|
+
"Switch",
|
|
1212
|
+
"Match",
|
|
1213
|
+
"Portal",
|
|
1214
|
+
"Dynamic",
|
|
1215
|
+
"ErrorBoundary"
|
|
1216
|
+
];
|
|
1217
|
+
var CORE_CONTROL_FLOW = ["Suspense", "SuspenseList", "Await"];
|
|
1218
|
+
var ROUTER_COMPONENTS = ["Router", "Outlet", "Redirect", "Link"];
|
|
1219
|
+
var BUILTIN_COMPONENTS = [
|
|
1220
|
+
...DOM_CONTROL_FLOW,
|
|
1221
|
+
...CORE_CONTROL_FLOW,
|
|
1222
|
+
...ROUTER_COMPONENTS
|
|
1223
|
+
];
|
|
1224
|
+
var RESERVED_COMPONENTS = /* @__PURE__ */ new Set([
|
|
1225
|
+
...DOM_CONTROL_FLOW,
|
|
1226
|
+
...CORE_CONTROL_FLOW
|
|
1227
|
+
]);
|
|
1228
|
+
function builtinComponentMap(modules = {}) {
|
|
1229
|
+
const {
|
|
1230
|
+
controlFlowModule = "@fluixi/dom",
|
|
1231
|
+
coreModule = "@fluixi/core",
|
|
1232
|
+
routerModule = "@fluixi/core/router"
|
|
1233
|
+
} = modules;
|
|
1234
|
+
const map = {};
|
|
1235
|
+
for (const name of DOM_CONTROL_FLOW) map[name] = controlFlowModule;
|
|
1236
|
+
for (const name of CORE_CONTROL_FLOW) map[name] = coreModule;
|
|
1237
|
+
for (const name of ROUTER_COMPONENTS) map[name] = routerModule;
|
|
1238
|
+
return map;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
// src/resolve/resolve-ir.ts
|
|
1242
|
+
function resolveComponentSources(root, options = {}) {
|
|
1243
|
+
const { resolver, isBound, modules, strategyFor } = options;
|
|
1244
|
+
const builtins = builtinComponentMap(modules);
|
|
1245
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
1246
|
+
const unresolved = /* @__PURE__ */ new Set();
|
|
1247
|
+
const annotate = (node) => {
|
|
1248
|
+
const { name } = node;
|
|
1249
|
+
if (!name || name.includes(".")) return;
|
|
1250
|
+
if (isBound?.(name)) return;
|
|
1251
|
+
const source = sourceFor(name);
|
|
1252
|
+
if (!source) {
|
|
1253
|
+
unresolved.add(name);
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
const loading = source.origin === "builtin" && RESERVED_COMPONENTS.has(name) ? EAGER : strategyFor?.(node) ?? EAGER;
|
|
1257
|
+
node.source = { ...source, loading };
|
|
1258
|
+
resolved.set(name, node.source);
|
|
1259
|
+
};
|
|
1260
|
+
const sourceFor = (name) => {
|
|
1261
|
+
if (RESERVED_COMPONENTS.has(name)) {
|
|
1262
|
+
return { module: builtins[name], export: name, origin: "builtin" };
|
|
1263
|
+
}
|
|
1264
|
+
const rule = resolver?.resolve(name);
|
|
1265
|
+
if (rule) return { module: rule.module, export: rule.export, origin: "rule" };
|
|
1266
|
+
if (ROUTER_COMPONENTS.includes(name)) {
|
|
1267
|
+
return { module: builtins[name], export: name, origin: "builtin" };
|
|
1268
|
+
}
|
|
1269
|
+
return void 0;
|
|
1270
|
+
};
|
|
1271
|
+
walk(root, (node) => {
|
|
1272
|
+
if (node.kind === "component") annotate(node);
|
|
1273
|
+
});
|
|
1274
|
+
return { resolved, unresolved: [...unresolved] };
|
|
1275
|
+
}
|
|
1276
|
+
function walk(root, visit) {
|
|
1277
|
+
const nodes = Array.isArray(root) ? root : [root];
|
|
1278
|
+
for (const node of nodes) {
|
|
1279
|
+
visit(node);
|
|
1280
|
+
const children = node.children;
|
|
1281
|
+
if (children) walk(children, visit);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
// src/optimize/collapse-strategies.ts
|
|
1286
|
+
function collapseStrategies(root) {
|
|
1287
|
+
const components = [];
|
|
1288
|
+
walk(root, (node) => {
|
|
1289
|
+
if (node.kind === "component" && node.source) components.push(node);
|
|
1290
|
+
});
|
|
1291
|
+
const eager = /* @__PURE__ */ new Set();
|
|
1292
|
+
const never = /* @__PURE__ */ new Set();
|
|
1293
|
+
for (const node of components) {
|
|
1294
|
+
const { module, loading } = node.source;
|
|
1295
|
+
if (loading.kind === "eager") eager.add(module);
|
|
1296
|
+
if (loading.kind === "never") never.add(module);
|
|
1297
|
+
}
|
|
1298
|
+
const collapsed = /* @__PURE__ */ new Map();
|
|
1299
|
+
for (const node of components) {
|
|
1300
|
+
const source = node.source;
|
|
1301
|
+
if (!isDeferred(source.loading)) continue;
|
|
1302
|
+
if (!eager.has(source.module)) continue;
|
|
1303
|
+
source.loading = { kind: "eager" };
|
|
1304
|
+
collapsed.set(source.module, (collapsed.get(source.module) ?? 0) + 1);
|
|
1305
|
+
}
|
|
1306
|
+
return {
|
|
1307
|
+
collapsed,
|
|
1308
|
+
conflicted: [...never].filter((m) => eager.has(m))
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// src/codegen/serialize-static.ts
|
|
1313
|
+
var VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
1314
|
+
"area",
|
|
1315
|
+
"base",
|
|
1316
|
+
"br",
|
|
1317
|
+
"col",
|
|
1318
|
+
"embed",
|
|
1319
|
+
"hr",
|
|
1320
|
+
"img",
|
|
1321
|
+
"input",
|
|
1322
|
+
"link",
|
|
1323
|
+
"meta",
|
|
1324
|
+
"param",
|
|
1325
|
+
"source",
|
|
1326
|
+
"track",
|
|
1327
|
+
"wbr"
|
|
1328
|
+
]);
|
|
1329
|
+
var HTML_NAME = {
|
|
1330
|
+
className: "class",
|
|
1331
|
+
htmlFor: "for"
|
|
1332
|
+
};
|
|
1333
|
+
var PROPERTY_ONLY = /* @__PURE__ */ new Set([
|
|
1334
|
+
"innerHTML",
|
|
1335
|
+
"outerHTML",
|
|
1336
|
+
"innerText",
|
|
1337
|
+
"textContent",
|
|
1338
|
+
"value",
|
|
1339
|
+
"checked",
|
|
1340
|
+
"selected",
|
|
1341
|
+
"muted",
|
|
1342
|
+
"defaultValue",
|
|
1343
|
+
"defaultChecked",
|
|
1344
|
+
"children",
|
|
1345
|
+
"ref"
|
|
1346
|
+
]);
|
|
1347
|
+
var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set(["script", "style", "textarea", "title"]);
|
|
1348
|
+
function isRawTextElement(tag) {
|
|
1349
|
+
return RAW_TEXT_ELEMENTS.has(tag);
|
|
1350
|
+
}
|
|
1351
|
+
function htmlAttrName(name) {
|
|
1352
|
+
return HTML_NAME[name] ?? name;
|
|
1353
|
+
}
|
|
1354
|
+
function isPlainLiteralAttr(prop) {
|
|
1355
|
+
if (prop.kind !== "attr") return false;
|
|
1356
|
+
if (prop.expr !== void 0) return false;
|
|
1357
|
+
if (prop.name.includes(":")) return false;
|
|
1358
|
+
return !PROPERTY_ONLY.has(prop.name);
|
|
1359
|
+
}
|
|
1360
|
+
function isSerializable(node) {
|
|
1361
|
+
if (!node.static) return false;
|
|
1362
|
+
if (isRawTextElement(node.tag)) return false;
|
|
1363
|
+
for (const prop of node.props) {
|
|
1364
|
+
if (!isPlainLiteralAttr(prop)) return false;
|
|
1365
|
+
}
|
|
1366
|
+
for (const child of node.children) {
|
|
1367
|
+
if (child.kind === "text") continue;
|
|
1368
|
+
if (child.kind === "element" && isSerializable(child)) continue;
|
|
1369
|
+
return false;
|
|
1370
|
+
}
|
|
1371
|
+
return true;
|
|
1372
|
+
}
|
|
1373
|
+
function serializeStatic(node) {
|
|
1374
|
+
const attrs = node.props.map(serializeProp).filter(Boolean).join("");
|
|
1375
|
+
const open = `<${node.tag}${attrs}>`;
|
|
1376
|
+
if (VOID_ELEMENTS.has(node.tag)) return open;
|
|
1377
|
+
return `${open}${node.children.map(serializeChild).join("")}</${node.tag}>`;
|
|
1378
|
+
}
|
|
1379
|
+
function serializeChild(child) {
|
|
1380
|
+
if (child.kind === "text") return escapeText(child.value);
|
|
1381
|
+
if (child.kind !== "element") throw new Error(`serializeStatic: unexpected ${child.kind}`);
|
|
1382
|
+
return serializeStatic(child);
|
|
1383
|
+
}
|
|
1384
|
+
function serializeProp(prop) {
|
|
1385
|
+
const name = HTML_NAME[prop.name] ?? prop.name;
|
|
1386
|
+
const value = prop.literal;
|
|
1387
|
+
if (value === true || value === void 0) return ` ${name}`;
|
|
1388
|
+
if (value === false || value === null) return "";
|
|
1389
|
+
return ` ${name}="${escapeAttr(String(value))}"`;
|
|
1390
|
+
}
|
|
1391
|
+
function escapeAttr(value) {
|
|
1392
|
+
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
1393
|
+
}
|
|
1394
|
+
function escapeText(value) {
|
|
1395
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1396
|
+
}
|
|
1397
|
+
function templatePositions(root) {
|
|
1398
|
+
const pos = [];
|
|
1399
|
+
let file;
|
|
1400
|
+
const walk8 = (el) => {
|
|
1401
|
+
file ??= el.at?.file;
|
|
1402
|
+
pos.push(el.at?.line ?? 0, el.at?.column ?? 0);
|
|
1403
|
+
for (const child of el.children) if (child.kind === "element") walk8(child);
|
|
1404
|
+
};
|
|
1405
|
+
walk8(root);
|
|
1406
|
+
return file && pos.some((n2) => n2 !== 0) ? { file, pos } : void 0;
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// src/codegen/partial-template.ts
|
|
1410
|
+
var HOLE_START = "<!--fx-->";
|
|
1411
|
+
var HOLE_END = "<!--fx/-->";
|
|
1412
|
+
function isHole(node) {
|
|
1413
|
+
return node.kind === "expr" || node.kind === "component" || node.kind === "control";
|
|
1414
|
+
}
|
|
1415
|
+
function isTemplateChild(node) {
|
|
1416
|
+
return node.kind === "text" || node.kind === "element" && isTemplateElement(node);
|
|
1417
|
+
}
|
|
1418
|
+
function isTemplateElement(node) {
|
|
1419
|
+
if (node.svg) return false;
|
|
1420
|
+
if (isRawTextElement(node.tag)) return false;
|
|
1421
|
+
if (!node.props.every((p) => isPlainLiteralAttr(p))) return false;
|
|
1422
|
+
return node.children.every((c) => isTemplateChild(c) || isHole(c));
|
|
1423
|
+
}
|
|
1424
|
+
function containsHole(node) {
|
|
1425
|
+
if (isHole(node)) return true;
|
|
1426
|
+
const children = node.children;
|
|
1427
|
+
return children ? children.some(containsHole) : false;
|
|
1428
|
+
}
|
|
1429
|
+
function addressable(el) {
|
|
1430
|
+
for (let i = 0; i < el.children.length; i++) {
|
|
1431
|
+
const child = el.children[i];
|
|
1432
|
+
if (child.kind === "text") {
|
|
1433
|
+
if (child.value === "") return false;
|
|
1434
|
+
if (el.children[i + 1]?.kind === "text") return false;
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return el.children.every((c) => c.kind !== "element" || addressable(c));
|
|
1438
|
+
}
|
|
1439
|
+
function planPartialTemplate(root) {
|
|
1440
|
+
if (!isTemplateElement(root)) return null;
|
|
1441
|
+
if (!addressable(root)) return null;
|
|
1442
|
+
if (!root.children.some(containsHole)) return null;
|
|
1443
|
+
const steps = [];
|
|
1444
|
+
const holes = [];
|
|
1445
|
+
let refCount = 0;
|
|
1446
|
+
const declare = (expr) => {
|
|
1447
|
+
const ref = `_n$${refCount++}`;
|
|
1448
|
+
steps.push({ ref, expr });
|
|
1449
|
+
return ref;
|
|
1450
|
+
};
|
|
1451
|
+
walk8(root, "_el$");
|
|
1452
|
+
return { html: serializeWithMarkers(root), tag: root.tag, steps, holes };
|
|
1453
|
+
function walk8(el, elementRef) {
|
|
1454
|
+
let last = -1;
|
|
1455
|
+
el.children.forEach((child, i) => {
|
|
1456
|
+
if (containsHole(child)) last = i;
|
|
1457
|
+
});
|
|
1458
|
+
let previous = null;
|
|
1459
|
+
for (let i = 0; i <= last; i++) {
|
|
1460
|
+
const child = el.children[i];
|
|
1461
|
+
const expr = previous ? `${previous}.nextSibling` : `${elementRef}.firstChild`;
|
|
1462
|
+
if (isHole(child)) {
|
|
1463
|
+
const startRef = declare(expr);
|
|
1464
|
+
const endRef = declare(`holeEnd(${startRef})`);
|
|
1465
|
+
holes.push({ parentRef: elementRef, startRef, endRef, node: child });
|
|
1466
|
+
previous = endRef;
|
|
1467
|
+
continue;
|
|
1468
|
+
}
|
|
1469
|
+
const ref = declare(expr);
|
|
1470
|
+
if (child.kind === "element" && containsHole(child)) walk8(child, ref);
|
|
1471
|
+
previous = ref;
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
function serializeWithMarkers(el) {
|
|
1476
|
+
return serializeStatic(withSentinels(el)).split(SENTINEL).join(HOLE_START + HOLE_END);
|
|
1477
|
+
}
|
|
1478
|
+
function withSentinels(el) {
|
|
1479
|
+
return {
|
|
1480
|
+
...el,
|
|
1481
|
+
children: el.children.map(
|
|
1482
|
+
(child) => isHole(child) ? { kind: "text", value: SENTINEL } : child.kind === "element" ? withSentinels(child) : child
|
|
1483
|
+
)
|
|
1484
|
+
};
|
|
1485
|
+
}
|
|
1486
|
+
var SENTINEL = "\0fx-hole\0";
|
|
1487
|
+
|
|
1488
|
+
// src/codegen/contract.ts
|
|
1489
|
+
var RUNTIME_CONTRACT_V1 = {
|
|
1490
|
+
version: 1,
|
|
1491
|
+
module: "@fluixi/dom",
|
|
1492
|
+
symbols: [
|
|
1493
|
+
"createNativeElement",
|
|
1494
|
+
"insert",
|
|
1495
|
+
"spread",
|
|
1496
|
+
"setAttribute",
|
|
1497
|
+
"delegateEvents",
|
|
1498
|
+
"createComponent",
|
|
1499
|
+
"createMemo",
|
|
1500
|
+
"untrack",
|
|
1501
|
+
"Show",
|
|
1502
|
+
"For",
|
|
1503
|
+
"Index",
|
|
1504
|
+
"Switch",
|
|
1505
|
+
"Match",
|
|
1506
|
+
"Dynamic",
|
|
1507
|
+
"ErrorBoundary"
|
|
1508
|
+
]
|
|
1509
|
+
};
|
|
1510
|
+
var RUNTIME_CONTRACT_V2 = {
|
|
1511
|
+
version: 2,
|
|
1512
|
+
module: "@fluixi/dom",
|
|
1513
|
+
symbols: [...RUNTIME_CONTRACT_V1.symbols, "template", "cloneTemplate", "walk"]
|
|
1514
|
+
};
|
|
1515
|
+
|
|
1516
|
+
// src/analyze/intrinsics.ts
|
|
1517
|
+
var REACTIVE_MODULE = "@fluixi/reactive/signal";
|
|
1518
|
+
var INTRINSICS = {
|
|
1519
|
+
$signal: { export: "signal", module: REACTIVE_MODULE, returns: "signal-handle" },
|
|
1520
|
+
$memo: { export: "memo", module: REACTIVE_MODULE, returns: "memo-handle" },
|
|
1521
|
+
$effect: { export: "effect", module: REACTIVE_MODULE, returns: "effect" },
|
|
1522
|
+
$store: { export: "store", module: "@fluixi/reactive/store", returns: "store-handle" },
|
|
1523
|
+
$resource: { export: "resource", module: REACTIVE_MODULE, returns: "resource-handle" },
|
|
1524
|
+
// No short runtime form for these yet, so the intrinsic stands for the `create*`
|
|
1525
|
+
// name. Still worth having: no import to write, and when a shorter form is
|
|
1526
|
+
// designed, the change is this line rather than a compiler pass.
|
|
1527
|
+
$selector: { export: "createSelector", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
1528
|
+
$deferred: { export: "createDeferred", module: REACTIVE_MODULE, returns: "memo-accessor" },
|
|
1529
|
+
// Not value constructors, but the compiler has to know them: they change what a read
|
|
1530
|
+
// does. `untrack` suppresses dependency collection, which is exactly the kind of thing
|
|
1531
|
+
// dependency analysis must not guess at.
|
|
1532
|
+
$untrack: { export: "untrack", module: REACTIVE_MODULE, returns: "plain" },
|
|
1533
|
+
$untrackStore: { export: "untrackStore", module: "@fluixi/reactive/store", returns: "plain" }
|
|
1534
|
+
};
|
|
1535
|
+
var RUNTIME_PRIMITIVES = {
|
|
1536
|
+
signal: "signal-handle",
|
|
1537
|
+
memo: "memo-handle",
|
|
1538
|
+
effect: "effect",
|
|
1539
|
+
store: "store-handle",
|
|
1540
|
+
createSignal: ["signal-accessor", "signal-setter"],
|
|
1541
|
+
createMemo: "memo-accessor",
|
|
1542
|
+
createEffect: "effect",
|
|
1543
|
+
createRenderEffect: "effect",
|
|
1544
|
+
createStore: "reactive-object",
|
|
1545
|
+
resource: "resource-handle",
|
|
1546
|
+
createResource: "reactive-object",
|
|
1547
|
+
createSelector: "memo-accessor",
|
|
1548
|
+
createDeferred: "memo-accessor"
|
|
1549
|
+
};
|
|
1550
|
+
var PRIMITIVE_MODULES = [
|
|
1551
|
+
"@fluixi/reactive",
|
|
1552
|
+
"@fluixi/reactive/signal",
|
|
1553
|
+
"@fluixi/reactive/store",
|
|
1554
|
+
"@fluixi/core"
|
|
1555
|
+
];
|
|
1556
|
+
var INTRINSIC_CALL = new RegExp(`\\$(?:${Object.keys(INTRINSICS).map((n2) => n2.slice(1)).join("|")})\\s*\\(`);
|
|
1557
|
+
function mayHaveIntrinsic(code) {
|
|
1558
|
+
return INTRINSIC_CALL.test(code);
|
|
1559
|
+
}
|
|
1560
|
+
function isIntrinsicName(name) {
|
|
1561
|
+
return Object.prototype.hasOwnProperty.call(INTRINSICS, name);
|
|
1562
|
+
}
|
|
1563
|
+
function isPrimitiveModule(source) {
|
|
1564
|
+
return PRIMITIVE_MODULES.includes(source);
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
// src/analyze/reactive-bindings.ts
|
|
1568
|
+
function patternNames(node, out) {
|
|
1569
|
+
if (!node) return;
|
|
1570
|
+
switch (node.type) {
|
|
1571
|
+
case "Identifier":
|
|
1572
|
+
out.add(node.name);
|
|
1573
|
+
return;
|
|
1574
|
+
case "ObjectPattern":
|
|
1575
|
+
for (const p of node.properties ?? []) {
|
|
1576
|
+
if (p.type === "RestElement") patternNames(p.argument, out);
|
|
1577
|
+
else patternNames(p.value, out);
|
|
1578
|
+
}
|
|
1579
|
+
return;
|
|
1580
|
+
case "ArrayPattern":
|
|
1581
|
+
for (const e of node.elements ?? []) patternNames(e, out);
|
|
1582
|
+
return;
|
|
1583
|
+
case "AssignmentPattern":
|
|
1584
|
+
patternNames(node.left, out);
|
|
1585
|
+
return;
|
|
1586
|
+
case "RestElement":
|
|
1587
|
+
patternNames(node.argument, out);
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
function walk2(root, visit) {
|
|
1592
|
+
if (!root || typeof root !== "object" || typeof root.type !== "string") return;
|
|
1593
|
+
visit(root);
|
|
1594
|
+
for (const key of Object.keys(root)) {
|
|
1595
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
1596
|
+
const value = root[key];
|
|
1597
|
+
if (Array.isArray(value)) for (const item of value) walk2(item, visit);
|
|
1598
|
+
else if (value && typeof value === "object") walk2(value, visit);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
function calledPrimitive(node, bindings) {
|
|
1602
|
+
if (node.type !== "CallExpression") return void 0;
|
|
1603
|
+
const callee = node.callee;
|
|
1604
|
+
if (callee?.type !== "Identifier") return void 0;
|
|
1605
|
+
const name = callee.name;
|
|
1606
|
+
const imported = bindings.primitives.get(name);
|
|
1607
|
+
if (imported) return imported;
|
|
1608
|
+
const intrinsic = INTRINSICS[name];
|
|
1609
|
+
if (intrinsic && !bindings.shadowed.has(name)) return intrinsic.returns;
|
|
1610
|
+
return void 0;
|
|
1611
|
+
}
|
|
1612
|
+
function classify(id, returns, kinds) {
|
|
1613
|
+
if (Array.isArray(returns)) {
|
|
1614
|
+
if (id.type === "ArrayPattern") {
|
|
1615
|
+
id.elements.forEach((element, index) => {
|
|
1616
|
+
const kind = returns[index];
|
|
1617
|
+
if (element?.type === "Identifier" && kind) kinds.set(element.name, kind);
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
return;
|
|
1621
|
+
}
|
|
1622
|
+
if (id.type === "Identifier") kinds.set(id.name, returns);
|
|
1623
|
+
}
|
|
1624
|
+
function declaredNames(program) {
|
|
1625
|
+
const names = /* @__PURE__ */ new Set();
|
|
1626
|
+
walk2(program, (node) => {
|
|
1627
|
+
switch (node.type) {
|
|
1628
|
+
case "VariableDeclarator":
|
|
1629
|
+
patternNames(node.id, names);
|
|
1630
|
+
return;
|
|
1631
|
+
case "FunctionDeclaration":
|
|
1632
|
+
case "FunctionExpression":
|
|
1633
|
+
case "ArrowFunctionExpression":
|
|
1634
|
+
patternNames(node.id, names);
|
|
1635
|
+
for (const p of node.params ?? []) patternNames(p, names);
|
|
1636
|
+
return;
|
|
1637
|
+
case "ClassDeclaration":
|
|
1638
|
+
case "ClassExpression":
|
|
1639
|
+
patternNames(node.id, names);
|
|
1640
|
+
return;
|
|
1641
|
+
case "ImportSpecifier":
|
|
1642
|
+
case "ImportDefaultSpecifier":
|
|
1643
|
+
case "ImportNamespaceSpecifier":
|
|
1644
|
+
patternNames(node.local, names);
|
|
1645
|
+
return;
|
|
1646
|
+
case "CatchClause":
|
|
1647
|
+
patternNames(node.param, names);
|
|
1648
|
+
return;
|
|
1649
|
+
}
|
|
1650
|
+
});
|
|
1651
|
+
return names;
|
|
1652
|
+
}
|
|
1653
|
+
function resolveReactiveBindings(program) {
|
|
1654
|
+
const bindings = { kinds: /* @__PURE__ */ new Map(), primitives: /* @__PURE__ */ new Map(), shadowed: /* @__PURE__ */ new Set() };
|
|
1655
|
+
const root = program;
|
|
1656
|
+
walk2(root, (node) => {
|
|
1657
|
+
const names = /* @__PURE__ */ new Set();
|
|
1658
|
+
if (node.type === "VariableDeclarator") patternNames(node.id, names);
|
|
1659
|
+
else if (node.type === "FunctionDeclaration" || node.type === "ClassDeclaration") patternNames(node.id, names);
|
|
1660
|
+
else if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") patternNames(node.local, names);
|
|
1661
|
+
else if (node.type === "ImportSpecifier") patternNames(node.local, names);
|
|
1662
|
+
else if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration") {
|
|
1663
|
+
for (const p of node.params ?? []) patternNames(p, names);
|
|
1664
|
+
}
|
|
1665
|
+
for (const name of names) if (INTRINSICS[name]) bindings.shadowed.add(name);
|
|
1666
|
+
});
|
|
1667
|
+
for (const statement of root.body ?? []) {
|
|
1668
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
1669
|
+
if (!isPrimitiveModule(statement.source?.value)) continue;
|
|
1670
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
1671
|
+
if (specifier.type !== "ImportSpecifier") continue;
|
|
1672
|
+
const importedNode = specifier.imported;
|
|
1673
|
+
const imported = importedNode.type === "Identifier" ? importedNode.name : importedNode.value;
|
|
1674
|
+
const returns = RUNTIME_PRIMITIVES[imported];
|
|
1675
|
+
if (returns) bindings.primitives.set(specifier.local.name, returns);
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
walk2(root, (node) => {
|
|
1679
|
+
if (node.type !== "VariableDeclarator" || !node.init) return;
|
|
1680
|
+
const returns = calledPrimitive(node.init, bindings);
|
|
1681
|
+
if (returns) classify(node.id, returns, bindings.kinds);
|
|
1682
|
+
});
|
|
1683
|
+
return bindings;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
// src/transform/source-locations.ts
|
|
1687
|
+
var SOURCE_MARKER = "__fx_source";
|
|
1688
|
+
var COMPONENT_MARKER = "__fx_component";
|
|
1689
|
+
var HOLE_MARKER = "__fx_hole";
|
|
1690
|
+
var markHole = (accessor, line, column, name) => `(globalThis.${HOLE_MARKER} ?? ((f) => f))(${accessor},${line},${column}${name === void 0 ? "" : `,${JSON.stringify(name)}`})`;
|
|
1691
|
+
var PROPS_MARKER = "__fx_props";
|
|
1692
|
+
var markProps = (object, at) => `(globalThis.${PROPS_MARKER} ?? ((p) => p))(${object},${JSON.stringify(at)})`;
|
|
1693
|
+
var RUNTIME_PRIMITIVES2 = /* @__PURE__ */ new Set([
|
|
1694
|
+
"signal",
|
|
1695
|
+
"memo",
|
|
1696
|
+
"effect",
|
|
1697
|
+
"store",
|
|
1698
|
+
"resource",
|
|
1699
|
+
"watch",
|
|
1700
|
+
"createSignal",
|
|
1701
|
+
"createMemo",
|
|
1702
|
+
"createEffect",
|
|
1703
|
+
"createStore",
|
|
1704
|
+
"createResource"
|
|
1705
|
+
]);
|
|
1706
|
+
function mayHaveReactiveCall(code) {
|
|
1707
|
+
for (const name of RUNTIME_PRIMITIVES2) {
|
|
1708
|
+
if (code.includes(name)) return true;
|
|
1709
|
+
}
|
|
1710
|
+
return code.includes("$signal") || code.includes("$memo") || code.includes("$effect") || code.includes("$store");
|
|
1711
|
+
}
|
|
1712
|
+
var SINGLE_STATEMENT_PARENTS = /* @__PURE__ */ new Set([
|
|
1713
|
+
"IfStatement",
|
|
1714
|
+
"ForStatement",
|
|
1715
|
+
"ForInStatement",
|
|
1716
|
+
"ForOfStatement",
|
|
1717
|
+
"WhileStatement",
|
|
1718
|
+
"DoWhileStatement",
|
|
1719
|
+
"LabeledStatement",
|
|
1720
|
+
"WithStatement"
|
|
1721
|
+
]);
|
|
1722
|
+
function isLoneBranch(statement, parent) {
|
|
1723
|
+
if (!parent || statement.type === "BlockStatement") return false;
|
|
1724
|
+
return SINGLE_STATEMENT_PARENTS.has(parent.type);
|
|
1725
|
+
}
|
|
1726
|
+
function isExport(node) {
|
|
1727
|
+
return node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration";
|
|
1728
|
+
}
|
|
1729
|
+
function isStatement(node) {
|
|
1730
|
+
return node.type.endsWith("Statement") || node.type === "VariableDeclaration";
|
|
1731
|
+
}
|
|
1732
|
+
function walk3(node, visit, stack = []) {
|
|
1733
|
+
if (!node || typeof node.type !== "string") return;
|
|
1734
|
+
visit(node, stack);
|
|
1735
|
+
stack.push(node);
|
|
1736
|
+
for (const key of Object.keys(node)) {
|
|
1737
|
+
if (key === "loc" || key === "parent") continue;
|
|
1738
|
+
const value = node[key];
|
|
1739
|
+
if (Array.isArray(value)) {
|
|
1740
|
+
for (const child of value) walk3(child, visit, stack);
|
|
1741
|
+
} else if (value && typeof value.type === "string") {
|
|
1742
|
+
walk3(value, visit, stack);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
stack.pop();
|
|
1746
|
+
}
|
|
1747
|
+
function nameFor(stack) {
|
|
1748
|
+
const declarator = stack[stack.length - 1];
|
|
1749
|
+
if (declarator?.type !== "VariableDeclarator") return void 0;
|
|
1750
|
+
const id = declarator.id;
|
|
1751
|
+
if (id?.type === "Identifier") return id.name;
|
|
1752
|
+
if (id?.type === "ArrayPattern") {
|
|
1753
|
+
const first = id.elements?.[0];
|
|
1754
|
+
if (first?.type === "Identifier") return first.name;
|
|
1755
|
+
}
|
|
1756
|
+
return void 0;
|
|
1757
|
+
}
|
|
1758
|
+
function componentName(node) {
|
|
1759
|
+
if (node.type === "FunctionDeclaration") {
|
|
1760
|
+
const name = node.id?.name;
|
|
1761
|
+
return name && name[0] === name[0]?.toUpperCase() ? name : void 0;
|
|
1762
|
+
}
|
|
1763
|
+
if (node.type === "VariableDeclarator") {
|
|
1764
|
+
const id = node.id;
|
|
1765
|
+
const init = node.init;
|
|
1766
|
+
if (id?.type !== "Identifier" || !init) return void 0;
|
|
1767
|
+
if (init.type !== "ArrowFunctionExpression" && init.type !== "FunctionExpression") return void 0;
|
|
1768
|
+
const name = id.name;
|
|
1769
|
+
return name[0] === name[0]?.toUpperCase() ? name : void 0;
|
|
1770
|
+
}
|
|
1771
|
+
return void 0;
|
|
1772
|
+
}
|
|
1773
|
+
function collectSourceEdits(program, filename) {
|
|
1774
|
+
const edits = [];
|
|
1775
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
1776
|
+
const { primitives, shadowed } = resolveReactiveBindings(program);
|
|
1777
|
+
walk3(program, (node, stack) => {
|
|
1778
|
+
const component = componentName(node);
|
|
1779
|
+
if (component) {
|
|
1780
|
+
const at = node.loc?.start;
|
|
1781
|
+
let end2 = node.end;
|
|
1782
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
1783
|
+
const up = stack[i];
|
|
1784
|
+
if (up.type !== "VariableDeclaration" && up.type !== "ExportNamedDeclaration" && up.type !== "ExportDefaultDeclaration") {
|
|
1785
|
+
break;
|
|
1786
|
+
}
|
|
1787
|
+
end2 = up.end;
|
|
1788
|
+
}
|
|
1789
|
+
if (at) {
|
|
1790
|
+
edits.push({
|
|
1791
|
+
start: end2,
|
|
1792
|
+
end: end2,
|
|
1793
|
+
code: `;globalThis.${COMPONENT_MARKER}?.(${JSON.stringify(component)},${JSON.stringify(filename)},${at.line},${at.column});`
|
|
1794
|
+
});
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
if (node.type !== "CallExpression") return;
|
|
1798
|
+
const callee = node.callee;
|
|
1799
|
+
if (callee?.type !== "Identifier") return;
|
|
1800
|
+
const name = callee.name;
|
|
1801
|
+
const intrinsic = isIntrinsicName(name) && !shadowed.has(name) ? INTRINSICS[name] : void 0;
|
|
1802
|
+
const imported = primitives.has(name) || RUNTIME_PRIMITIVES2.has(name);
|
|
1803
|
+
if (!intrinsic && !imported) return;
|
|
1804
|
+
const position = node.loc?.start;
|
|
1805
|
+
if (!position) return;
|
|
1806
|
+
let statement;
|
|
1807
|
+
let parent;
|
|
1808
|
+
let index = -1;
|
|
1809
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
1810
|
+
if (isStatement(stack[i]) || isExport(stack[i])) {
|
|
1811
|
+
statement = stack[i];
|
|
1812
|
+
parent = stack[i - 1];
|
|
1813
|
+
index = i;
|
|
1814
|
+
break;
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
if (!statement) return;
|
|
1818
|
+
let start = statement.start;
|
|
1819
|
+
let end = statement.end;
|
|
1820
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
1821
|
+
if (!isExport(stack[i])) break;
|
|
1822
|
+
start = stack[i].start;
|
|
1823
|
+
end = stack[i].end;
|
|
1824
|
+
parent = stack[i - 1];
|
|
1825
|
+
}
|
|
1826
|
+
if (claimed.has(start)) return;
|
|
1827
|
+
claimed.add(start);
|
|
1828
|
+
const label = nameFor(stack);
|
|
1829
|
+
const args = [
|
|
1830
|
+
JSON.stringify(filename),
|
|
1831
|
+
String(position.line),
|
|
1832
|
+
String(position.column),
|
|
1833
|
+
label ? JSON.stringify(label) : void 0
|
|
1834
|
+
].filter((a) => a !== void 0).join(",");
|
|
1835
|
+
const mark = `globalThis.${SOURCE_MARKER}?.(${args});`;
|
|
1836
|
+
if (isLoneBranch(statement, parent)) {
|
|
1837
|
+
edits.push({ start, end: start, code: `{${mark}` });
|
|
1838
|
+
edits.push({ start: end, end, code: "}" });
|
|
1839
|
+
return;
|
|
1840
|
+
}
|
|
1841
|
+
edits.push({ start, end: start, code: mark });
|
|
1842
|
+
});
|
|
1843
|
+
return { edits, marked: edits.length };
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
// src/analyze/paren.ts
|
|
1847
|
+
function skipQuoted(src, start) {
|
|
1848
|
+
const quote = src[start];
|
|
1849
|
+
for (let i = start + 1; i < src.length; i++) {
|
|
1850
|
+
const c = src[i];
|
|
1851
|
+
if (c === "\\") i++;
|
|
1852
|
+
else if (c === quote) return i + 1;
|
|
1853
|
+
else if (c === "\n") return -1;
|
|
1854
|
+
}
|
|
1855
|
+
return -1;
|
|
1856
|
+
}
|
|
1857
|
+
function skipTemplate(src, start) {
|
|
1858
|
+
let i = start + 1;
|
|
1859
|
+
while (i < src.length) {
|
|
1860
|
+
const c = src[i];
|
|
1861
|
+
if (c === "\\") {
|
|
1862
|
+
i += 2;
|
|
1863
|
+
} else if (c === "`") {
|
|
1864
|
+
return i + 1;
|
|
1865
|
+
} else if (c === "$" && src[i + 1] === "{") {
|
|
1866
|
+
let depth = 1;
|
|
1867
|
+
i += 2;
|
|
1868
|
+
while (i < src.length && depth > 0) {
|
|
1869
|
+
const k = src[i];
|
|
1870
|
+
if (k === "\\") i += 2;
|
|
1871
|
+
else if (k === "`") {
|
|
1872
|
+
const end = skipTemplate(src, i);
|
|
1873
|
+
if (end < 0) return -1;
|
|
1874
|
+
i = end;
|
|
1875
|
+
} else if (k === '"' || k === "'") {
|
|
1876
|
+
const end = skipQuoted(src, i);
|
|
1877
|
+
if (end < 0) return -1;
|
|
1878
|
+
i = end;
|
|
1879
|
+
} else {
|
|
1880
|
+
if (k === "{") depth++;
|
|
1881
|
+
else if (k === "}") depth--;
|
|
1882
|
+
i++;
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
} else {
|
|
1886
|
+
i++;
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
return -1;
|
|
1890
|
+
}
|
|
1891
|
+
function skipRegex(src, start) {
|
|
1892
|
+
let inClass = false;
|
|
1893
|
+
for (let i = start + 1; i < src.length; i++) {
|
|
1894
|
+
const c = src[i];
|
|
1895
|
+
if (c === "\\") i++;
|
|
1896
|
+
else if (c === "[") inClass = true;
|
|
1897
|
+
else if (c === "]") inClass = false;
|
|
1898
|
+
else if (c === "\n") return -1;
|
|
1899
|
+
else if (c === "/" && !inClass) {
|
|
1900
|
+
let end = i + 1;
|
|
1901
|
+
while (end < src.length && /[a-z]/i.test(src[end])) end++;
|
|
1902
|
+
return end;
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
return -1;
|
|
1906
|
+
}
|
|
1907
|
+
function divides(prev) {
|
|
1908
|
+
return prev !== "" && /[A-Za-z0-9_$)\]]/.test(prev);
|
|
1909
|
+
}
|
|
1910
|
+
function topLevelComma(src) {
|
|
1911
|
+
let depth = 0;
|
|
1912
|
+
let prev = "";
|
|
1913
|
+
let i = 0;
|
|
1914
|
+
while (i < src.length) {
|
|
1915
|
+
const c = src[i];
|
|
1916
|
+
if (c === '"' || c === "'") {
|
|
1917
|
+
const end = skipQuoted(src, i);
|
|
1918
|
+
if (end < 0) return null;
|
|
1919
|
+
i = end;
|
|
1920
|
+
prev = c;
|
|
1921
|
+
} else if (c === "`") {
|
|
1922
|
+
const end = skipTemplate(src, i);
|
|
1923
|
+
if (end < 0) return null;
|
|
1924
|
+
i = end;
|
|
1925
|
+
prev = c;
|
|
1926
|
+
} else if (c === "/" && src[i + 1] === "/") {
|
|
1927
|
+
return null;
|
|
1928
|
+
} else if (c === "/" && src[i + 1] === "*") {
|
|
1929
|
+
const end = src.indexOf("*/", i + 2);
|
|
1930
|
+
if (end < 0) return null;
|
|
1931
|
+
i = end + 2;
|
|
1932
|
+
} else if (c === "/" && !divides(prev)) {
|
|
1933
|
+
const end = skipRegex(src, i);
|
|
1934
|
+
if (end < 0) return null;
|
|
1935
|
+
i = end;
|
|
1936
|
+
prev = "/";
|
|
1937
|
+
} else {
|
|
1938
|
+
if (c === "(" || c === "[" || c === "{") depth++;
|
|
1939
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
1940
|
+
depth--;
|
|
1941
|
+
if (depth < 0) return null;
|
|
1942
|
+
} else if (c === "," && depth === 0) return true;
|
|
1943
|
+
if (!/\s/.test(c)) prev = c;
|
|
1944
|
+
i++;
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
return depth === 0 ? false : null;
|
|
1948
|
+
}
|
|
1949
|
+
function paren(code, position) {
|
|
1950
|
+
const src = code.trim();
|
|
1951
|
+
if (src === "") return `(${code})`;
|
|
1952
|
+
if (position === "arrowBody" && src[0] === "{") return `(${code})`;
|
|
1953
|
+
return topLevelComma(src) === false ? code : `(${code})`;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
// src/codegen/backends/imperative.ts
|
|
1957
|
+
var CONTROL_COMPONENT = {
|
|
1958
|
+
show: "Show",
|
|
1959
|
+
for: "For",
|
|
1960
|
+
index: "Index",
|
|
1961
|
+
switch: "Switch",
|
|
1962
|
+
match: "Match",
|
|
1963
|
+
dynamic: "Dynamic",
|
|
1964
|
+
errorBoundary: "ErrorBoundary",
|
|
1965
|
+
suspense: "Suspense"
|
|
1966
|
+
};
|
|
1967
|
+
function isIdent(name) {
|
|
1968
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
1969
|
+
}
|
|
1970
|
+
function propKey(name) {
|
|
1971
|
+
return isIdent(name) ? name : JSON.stringify(name);
|
|
1972
|
+
}
|
|
1973
|
+
function propValue(p) {
|
|
1974
|
+
if (p.expr !== void 0) {
|
|
1975
|
+
if (p.name === "ref" && p.at)
|
|
1976
|
+
return markHole(paren(p.expr, "value"), p.at.line, p.at.column, p.expr);
|
|
1977
|
+
if (!p.reactive) return paren(p.expr, "value");
|
|
1978
|
+
const accessor = `() => ${paren(p.expr, "arrowBody")}`;
|
|
1979
|
+
return p.at ? markHole(accessor, p.at.line, p.at.column, p.directive) : accessor;
|
|
1980
|
+
}
|
|
1981
|
+
return JSON.stringify(p.literal ?? true);
|
|
1982
|
+
}
|
|
1983
|
+
function componentPropValue(p) {
|
|
1984
|
+
if (p.expr !== void 0) return paren(p.expr, "value");
|
|
1985
|
+
return JSON.stringify(p.literal ?? true);
|
|
1986
|
+
}
|
|
1987
|
+
function emitProps(props, childrenExpr, used) {
|
|
1988
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
1989
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
1990
|
+
const entries = normal.map((p) => `${propKey(p.name)}: ${propValue(p)}`);
|
|
1991
|
+
if (childrenExpr != null) entries.push(`children: ${childrenExpr}`);
|
|
1992
|
+
const obj = `{ ${entries.join(", ")} }`;
|
|
1993
|
+
if (spreads.length > 0) {
|
|
1994
|
+
used.add("mergeProps");
|
|
1995
|
+
return `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
1996
|
+
}
|
|
1997
|
+
return obj;
|
|
1998
|
+
}
|
|
1999
|
+
function markBind(code, at) {
|
|
2000
|
+
return at ? markHole(code, at.line, at.column) : code;
|
|
2001
|
+
}
|
|
2002
|
+
function emitChildren(children, used, templates) {
|
|
2003
|
+
if (children.length === 1) return emitNode(children[0], used, templates);
|
|
2004
|
+
return `[${children.map((c) => emitNode(c, used, templates)).join(", ")}]`;
|
|
2005
|
+
}
|
|
2006
|
+
function emitComponent(name, props, children, used, templates, at) {
|
|
2007
|
+
used.add("createMemo");
|
|
2008
|
+
used.add("createComponent");
|
|
2009
|
+
const spreads = props.filter((p) => p.kind === "spread");
|
|
2010
|
+
const normal = props.filter((p) => p.kind !== "spread");
|
|
2011
|
+
const entries = normal.map(
|
|
2012
|
+
(p) => `get ${propKey(p.name)}() { return ${componentPropValue(p)}; }`
|
|
2013
|
+
);
|
|
2014
|
+
if (children.length > 0) {
|
|
2015
|
+
entries.push(`get children() { return ${emitChildren(children, used, templates)}; }`);
|
|
2016
|
+
}
|
|
2017
|
+
let obj = `{ ${entries.join(", ")} }`;
|
|
2018
|
+
if (spreads.length > 0) {
|
|
2019
|
+
used.add("mergeProps");
|
|
2020
|
+
obj = `mergeProps(${spreads.map((s) => s.expr).join(", ")}, ${obj})`;
|
|
2021
|
+
}
|
|
2022
|
+
const propAt = {};
|
|
2023
|
+
for (const p of normal) if (p.at) propAt[p.name] = [p.at.line, p.at.column];
|
|
2024
|
+
if (Object.keys(propAt).length) obj = markProps(obj, propAt);
|
|
2025
|
+
const mark = at ? `globalThis.${SOURCE_MARKER}?.(${JSON.stringify(at.file)},${at.line},${at.column},${JSON.stringify(name)}), ` : "";
|
|
2026
|
+
return `createMemo(${paren(`${mark}() => createComponent(${name}, ${obj})`, "value")})`;
|
|
2027
|
+
}
|
|
2028
|
+
function staticAttrAssignment(el, prop) {
|
|
2029
|
+
const name = htmlAttrName(prop.name);
|
|
2030
|
+
const value = prop.literal;
|
|
2031
|
+
if (value === true || value === void 0) return `${el}.setAttribute(${JSON.stringify(name)}, "");`;
|
|
2032
|
+
if (value === false || value === null) return "";
|
|
2033
|
+
return `${el}.setAttribute(${JSON.stringify(name)}, ${JSON.stringify(String(value))});`;
|
|
2034
|
+
}
|
|
2035
|
+
var partial = false;
|
|
2036
|
+
function emitNode(node, used, templates) {
|
|
2037
|
+
switch (node.kind) {
|
|
2038
|
+
case "text":
|
|
2039
|
+
return JSON.stringify(node.value);
|
|
2040
|
+
case "expr": {
|
|
2041
|
+
if (!node.reactive) return paren(node.code, "value");
|
|
2042
|
+
const accessor = `() => ${paren(node.code, "arrowBody")}`;
|
|
2043
|
+
return node.at ? markHole(accessor, node.at.line, node.at.column) : accessor;
|
|
2044
|
+
}
|
|
2045
|
+
case "fragment":
|
|
2046
|
+
if (node.children.length === 0) return "null";
|
|
2047
|
+
return emitChildren(node.children, used, templates);
|
|
2048
|
+
case "component":
|
|
2049
|
+
return markBind(emitComponent(node.name, node.props, node.children, used, templates, node.at), node.bindAt ?? node.at);
|
|
2050
|
+
case "control": {
|
|
2051
|
+
const name = CONTROL_COMPONENT[node.control] ?? node.control;
|
|
2052
|
+
used.add(name);
|
|
2053
|
+
return markBind(emitComponent(name, node.props, node.children, used, templates), node.bindAt);
|
|
2054
|
+
}
|
|
2055
|
+
case "element": {
|
|
2056
|
+
if (templates && node.static && !node.svg && isSerializable(node)) {
|
|
2057
|
+
used.add("templateNode");
|
|
2058
|
+
const id = `_tmpl$${templates.length}`;
|
|
2059
|
+
templates.push({ id, html: serializeStatic(node), tag: node.tag, svg: false });
|
|
2060
|
+
const at = templatePositions(node);
|
|
2061
|
+
return at ? `templateNode(${id}, ${JSON.stringify(node.tag)}, false, false, ${JSON.stringify(at)})` : `templateNode(${id}, ${JSON.stringify(node.tag)})`;
|
|
2062
|
+
}
|
|
2063
|
+
if (templates && partial) {
|
|
2064
|
+
const plan = planPartialTemplate(node);
|
|
2065
|
+
if (plan) {
|
|
2066
|
+
used.add("templateNode");
|
|
2067
|
+
used.add("insert");
|
|
2068
|
+
used.add("holeEnd");
|
|
2069
|
+
used.add("holeContent");
|
|
2070
|
+
used.add("holeScope");
|
|
2071
|
+
const id = `_tmpl$${templates.length}`;
|
|
2072
|
+
templates.push({ id, html: plan.html, tag: plan.tag, svg: false });
|
|
2073
|
+
const at = templatePositions(node);
|
|
2074
|
+
const lines = [
|
|
2075
|
+
`const _el$ = templateNode(${id}, ${JSON.stringify(plan.tag)}, true${at ? `, false, ${JSON.stringify(at)}` : ""});`
|
|
2076
|
+
];
|
|
2077
|
+
for (const step of plan.steps) lines.push(`const ${step.ref} = ${step.expr};`);
|
|
2078
|
+
for (const hole of plan.holes) {
|
|
2079
|
+
lines.push(
|
|
2080
|
+
`insert(${hole.parentRef}, holeScope(${hole.startRef}, () => ${paren(emitNode(hole.node, used, templates), "arrowBody")}), ${hole.endRef}, holeContent(${hole.startRef}, ${hole.endRef}));`
|
|
2081
|
+
);
|
|
2082
|
+
}
|
|
2083
|
+
lines.push("return _el$;");
|
|
2084
|
+
return `(() => { ${lines.join(" ")} })()`;
|
|
2085
|
+
}
|
|
2086
|
+
}
|
|
2087
|
+
used.add("createNativeElement");
|
|
2088
|
+
const tag = JSON.stringify(node.tag);
|
|
2089
|
+
const el = "_el$";
|
|
2090
|
+
const parts = [];
|
|
2091
|
+
const createArgs = node.at ? `${tag}, ${node.svg}, ${JSON.stringify(node.at)}` : node.svg ? `${tag}, true` : tag;
|
|
2092
|
+
parts.push(`const ${el} = createNativeElement(${createArgs});`);
|
|
2093
|
+
if (node.props.length > 0) {
|
|
2094
|
+
if (!node.svg && node.props.every(isPlainLiteralAttr)) {
|
|
2095
|
+
for (const p of node.props) parts.push(staticAttrAssignment(el, p));
|
|
2096
|
+
} else {
|
|
2097
|
+
used.add("spread");
|
|
2098
|
+
const svgFlag = node.svg ? ", isSVG: true" : "";
|
|
2099
|
+
parts.push(`spread({ element: ${el}, props: ${emitProps(node.props, null, used)}${svgFlag} });`);
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
for (const child of node.children) {
|
|
2103
|
+
used.add("insert");
|
|
2104
|
+
const childCode = emitNode(child, used, templates);
|
|
2105
|
+
const isFn = child.kind === "expr" && child.reactive || child.kind === "component" || child.kind === "control";
|
|
2106
|
+
parts.push(isFn ? `insert(${el}, ${childCode}, null);` : `insert(${el}, ${childCode});`);
|
|
2107
|
+
}
|
|
2108
|
+
parts.push(`return ${el};`);
|
|
2109
|
+
return `(() => { ${parts.join(" ")} })()`;
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
function inlineTemplates(code, templates) {
|
|
2114
|
+
if (!templates || templates.length === 0) return code;
|
|
2115
|
+
const byId = new Map(templates.map((t) => [t.id, JSON.stringify(t.html)]));
|
|
2116
|
+
return code.replace(/_tmpl\$\d+/g, (id) => byId.get(id) ?? id);
|
|
2117
|
+
}
|
|
2118
|
+
var imperativeBackend = {
|
|
2119
|
+
name: "imperative",
|
|
2120
|
+
contract: RUNTIME_CONTRACT_V1,
|
|
2121
|
+
emit(node, options) {
|
|
2122
|
+
const used = /* @__PURE__ */ new Set();
|
|
2123
|
+
const templates = options?.templateClone !== false ? [] : void 0;
|
|
2124
|
+
partial = options?.partialTemplates === true;
|
|
2125
|
+
const code = emitNode(node, used, templates);
|
|
2126
|
+
return { code, imports: Array.from(used), templates };
|
|
2127
|
+
}
|
|
2128
|
+
};
|
|
2129
|
+
|
|
2130
|
+
// src/resolve/component-resolver.ts
|
|
2131
|
+
var isMapRule = (rule) => "components" in rule;
|
|
2132
|
+
function substitute(template, match) {
|
|
2133
|
+
return template.replace(/\$(\d+)/g, (whole, index) => {
|
|
2134
|
+
const group = match[Number(index)];
|
|
2135
|
+
return group ?? whole;
|
|
2136
|
+
});
|
|
2137
|
+
}
|
|
2138
|
+
function normalise(name, entry) {
|
|
2139
|
+
return typeof entry === "string" ? { module: entry, export: name } : entry;
|
|
2140
|
+
}
|
|
2141
|
+
function createComponentResolver(rules = []) {
|
|
2142
|
+
const exact = /* @__PURE__ */ new Map();
|
|
2143
|
+
const conflicts = [];
|
|
2144
|
+
const patterns = [];
|
|
2145
|
+
for (const rule of rules) {
|
|
2146
|
+
if (!isMapRule(rule)) {
|
|
2147
|
+
patterns.push(rule);
|
|
2148
|
+
continue;
|
|
2149
|
+
}
|
|
2150
|
+
for (const [name, entry] of Object.entries(rule.components)) {
|
|
2151
|
+
const resolved = normalise(name, entry);
|
|
2152
|
+
const existing = exact.get(name);
|
|
2153
|
+
if (existing && existing.module !== resolved.module) {
|
|
2154
|
+
const conflict = conflicts.find((c) => c.name === name);
|
|
2155
|
+
if (conflict) conflict.modules.push(resolved.module);
|
|
2156
|
+
else conflicts.push({ name, modules: [existing.module, resolved.module] });
|
|
2157
|
+
continue;
|
|
2158
|
+
}
|
|
2159
|
+
exact.set(name, resolved);
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
const cache = /* @__PURE__ */ new Map();
|
|
2163
|
+
const resolve = (name) => {
|
|
2164
|
+
if (cache.has(name)) return cache.get(name);
|
|
2165
|
+
let result = exact.get(name);
|
|
2166
|
+
if (!result) {
|
|
2167
|
+
for (const rule of patterns) {
|
|
2168
|
+
const match = name.match(new RegExp(rule.match.source, rule.match.flags.replace("g", "")));
|
|
2169
|
+
if (!match) continue;
|
|
2170
|
+
result = {
|
|
2171
|
+
module: substitute(rule.module, match),
|
|
2172
|
+
export: rule.export ? substitute(rule.export, match) : name
|
|
2173
|
+
};
|
|
2174
|
+
break;
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
cache.set(name, result);
|
|
2178
|
+
return result;
|
|
2179
|
+
};
|
|
2180
|
+
return {
|
|
2181
|
+
resolve,
|
|
2182
|
+
names: () => [...exact.keys()],
|
|
2183
|
+
conflicts: () => conflicts
|
|
2184
|
+
};
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
// src/lower/template.ts
|
|
2188
|
+
import {
|
|
2189
|
+
parseTemplate
|
|
2190
|
+
} from "@fluixi/template-parser";
|
|
2191
|
+
|
|
2192
|
+
// src/lower/text.ts
|
|
2193
|
+
function cleanTemplateText(value) {
|
|
2194
|
+
const lines = value.split(/\r\n|\n|\r/);
|
|
2195
|
+
let lastNonEmpty = 0;
|
|
2196
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2197
|
+
if (/[^ \t]/.test(lines[i])) lastNonEmpty = i;
|
|
2198
|
+
}
|
|
2199
|
+
let out = "";
|
|
2200
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2201
|
+
let line = lines[i].replace(/\t/g, " ");
|
|
2202
|
+
if (i !== 0) line = line.replace(/^ +/, "");
|
|
2203
|
+
if (i !== lines.length - 1) line = line.replace(/ +$/, "");
|
|
2204
|
+
if (!line) continue;
|
|
2205
|
+
if (i !== lastNonEmpty) line += " ";
|
|
2206
|
+
out += line;
|
|
2207
|
+
}
|
|
2208
|
+
return out;
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
// src/lower/template.ts
|
|
2212
|
+
var DELEGATED = /* @__PURE__ */ new Set([
|
|
2213
|
+
"click",
|
|
2214
|
+
"dblclick",
|
|
2215
|
+
"input",
|
|
2216
|
+
"change",
|
|
2217
|
+
"submit",
|
|
2218
|
+
"focus",
|
|
2219
|
+
"blur",
|
|
2220
|
+
"keydown",
|
|
2221
|
+
"keyup",
|
|
2222
|
+
"keypress",
|
|
2223
|
+
"mousedown",
|
|
2224
|
+
"mouseup"
|
|
2225
|
+
]);
|
|
2226
|
+
function escapeTemplate(s) {
|
|
2227
|
+
return s.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
|
|
2228
|
+
}
|
|
2229
|
+
function capitalize(s) {
|
|
2230
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
2231
|
+
}
|
|
2232
|
+
var Lowerer = class {
|
|
2233
|
+
constructor(holes, used, sourceFile, positionAt) {
|
|
2234
|
+
this.holes = holes;
|
|
2235
|
+
this.used = used;
|
|
2236
|
+
this.sourceFile = sourceFile;
|
|
2237
|
+
this.positionAt = positionAt;
|
|
2238
|
+
}
|
|
2239
|
+
/**
|
|
2240
|
+
* Where a tag is written, for the mark a component node carries.
|
|
2241
|
+
*
|
|
2242
|
+
* The parser reports offsets, and they are offsets into the file: the pieces it was given
|
|
2243
|
+
* carry the positions the front-end recorded. Only built when the host asked for source
|
|
2244
|
+
* locations, which is the only time either of these is set.
|
|
2245
|
+
*/
|
|
2246
|
+
tagAt(node) {
|
|
2247
|
+
const start = node.loc?.start;
|
|
2248
|
+
if (!this.sourceFile || !this.positionAt || start == null) return void 0;
|
|
2249
|
+
const { line, column } = this.positionAt(start);
|
|
2250
|
+
return { file: this.sourceFile, line, column };
|
|
2251
|
+
}
|
|
2252
|
+
hole(i) {
|
|
2253
|
+
return this.holes[i] ?? { code: "undefined", reactive: false };
|
|
2254
|
+
}
|
|
2255
|
+
/** Emit a nested IR subtree to code (for `Show` fallback / `For` children). */
|
|
2256
|
+
emit(node) {
|
|
2257
|
+
const { code, imports, templates } = imperativeBackend.emit(node, {});
|
|
2258
|
+
for (const s of imports) this.used.add(s);
|
|
2259
|
+
return inlineTemplates(code, templates);
|
|
2260
|
+
}
|
|
2261
|
+
// --- content ------------------------------------------------------------
|
|
2262
|
+
lowerRoot(children) {
|
|
2263
|
+
const nodes = this.lowerChildren(children);
|
|
2264
|
+
if (nodes.length === 1) return nodes[0];
|
|
2265
|
+
return { kind: "fragment", children: nodes };
|
|
2266
|
+
}
|
|
2267
|
+
/** Lower a sibling list, handling text-drop and if/else/each restructuring. */
|
|
2268
|
+
lowerChildren(children) {
|
|
2269
|
+
const out = [];
|
|
2270
|
+
for (let i = 0; i < children.length; i++) {
|
|
2271
|
+
const child = children[i];
|
|
2272
|
+
if (child.kind === "Element" || child.kind === "Component") {
|
|
2273
|
+
const ifDir = child.attributes.find((a) => a.kind === "IfDirective");
|
|
2274
|
+
if (ifDir && ifDir.kind === "IfDirective") {
|
|
2275
|
+
let elseIdx = i + 1;
|
|
2276
|
+
if (elseIdx < children.length && this.isBlankText(children[elseIdx])) elseIdx++;
|
|
2277
|
+
const elseNode = children[elseIdx];
|
|
2278
|
+
const hasElse = elseNode && (elseNode.kind === "Element" || elseNode.kind === "Component") && elseNode.attributes.some((a) => a.kind === "ElseDirective");
|
|
2279
|
+
out.push(this.lowerIf(child, ifDir.hole, hasElse ? elseNode : null));
|
|
2280
|
+
if (hasElse) i = elseIdx;
|
|
2281
|
+
continue;
|
|
2282
|
+
}
|
|
2283
|
+
if (child.attributes.some((a) => a.kind === "EachDirective")) {
|
|
2284
|
+
out.push(this.lowerEach(child));
|
|
2285
|
+
continue;
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
const node = this.lowerNode(child);
|
|
2289
|
+
if (node) out.push(node);
|
|
2290
|
+
}
|
|
2291
|
+
return out;
|
|
2292
|
+
}
|
|
2293
|
+
isBlankText(n2) {
|
|
2294
|
+
return n2.kind === "Text" && !n2.raw && cleanTemplateText(n2.value) === "";
|
|
2295
|
+
}
|
|
2296
|
+
lowerNode(node) {
|
|
2297
|
+
switch (node.kind) {
|
|
2298
|
+
case "Text": {
|
|
2299
|
+
if (node.raw) return { kind: "text", value: node.value };
|
|
2300
|
+
const cleaned = cleanTemplateText(node.value);
|
|
2301
|
+
return cleaned ? { kind: "text", value: cleaned } : null;
|
|
2302
|
+
}
|
|
2303
|
+
case "Comment":
|
|
2304
|
+
return null;
|
|
2305
|
+
// dropped, like JSX comments
|
|
2306
|
+
case "Expression": {
|
|
2307
|
+
const h = this.hole(node.hole);
|
|
2308
|
+
return { kind: "expr", code: h.code, reactive: h.reactive, ...h.at ? { at: h.at } : {} };
|
|
2309
|
+
}
|
|
2310
|
+
case "Fragment":
|
|
2311
|
+
return { kind: "fragment", children: this.lowerChildren(node.children) };
|
|
2312
|
+
case "Element":
|
|
2313
|
+
return this.lowerElement(node);
|
|
2314
|
+
case "Component":
|
|
2315
|
+
return this.lowerComponent(node);
|
|
2316
|
+
default:
|
|
2317
|
+
return null;
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
lowerElement(el) {
|
|
2321
|
+
const isAttr = el.attributes.find((a) => a.kind === "Attribute" && a.name === "is");
|
|
2322
|
+
if (el.tag === "component" && isAttr && isAttr.value && isAttr.value.kind === "hole") {
|
|
2323
|
+
const rest = el.attributes.filter((a) => a !== isAttr);
|
|
2324
|
+
const node = this.lowerComponent({ ...el, kind: "Component", tag: "Dynamic", tagHole: null, attributes: rest });
|
|
2325
|
+
node.props.unshift({ name: "component", kind: "attr", expr: `() => ${paren(this.hole(isAttr.value.hole).code, "arrowBody")}` });
|
|
2326
|
+
return node;
|
|
2327
|
+
}
|
|
2328
|
+
const at = this.tagAt(el);
|
|
2329
|
+
return {
|
|
2330
|
+
kind: "element",
|
|
2331
|
+
tag: el.tag,
|
|
2332
|
+
svg: el.namespace === "svg",
|
|
2333
|
+
props: this.lowerAttributes(el.attributes),
|
|
2334
|
+
children: this.lowerChildren(el.children),
|
|
2335
|
+
static: false,
|
|
2336
|
+
...at ? { at } : {}
|
|
2337
|
+
};
|
|
2338
|
+
}
|
|
2339
|
+
lowerComponent(el) {
|
|
2340
|
+
const name = el.tagHole != null ? this.hole(el.tagHole).code : el.tag;
|
|
2341
|
+
const props = this.lowerAttributes(el.attributes);
|
|
2342
|
+
const { slots, rest } = this.partitionSlots(el.children);
|
|
2343
|
+
for (const [slotName, nodes] of slots) {
|
|
2344
|
+
const ir = nodes.length === 1 ? nodes[0] : { kind: "fragment", children: nodes };
|
|
2345
|
+
const slotProp = {
|
|
2346
|
+
name: slotName,
|
|
2347
|
+
kind: "attr",
|
|
2348
|
+
expr: this.emit(ir),
|
|
2349
|
+
jsxElement: true
|
|
2350
|
+
};
|
|
2351
|
+
const clash = props.findIndex((p) => p.name === slotName);
|
|
2352
|
+
if (clash >= 0) props[clash] = slotProp;
|
|
2353
|
+
else props.push(slotProp);
|
|
2354
|
+
}
|
|
2355
|
+
const loadAttr = el.attributes.find((a) => a.kind === "LoadDirective");
|
|
2356
|
+
const load = loadAttr ? parseLoadDirective(`load:${loadAttr.strategy}`, loadAttr.modifier) : void 0;
|
|
2357
|
+
const bindAt = props.find((p) => p.at && p.reactive)?.at;
|
|
2358
|
+
const at = this.tagAt(el);
|
|
2359
|
+
return {
|
|
2360
|
+
kind: "component",
|
|
2361
|
+
name,
|
|
2362
|
+
props,
|
|
2363
|
+
children: this.lowerChildren(rest),
|
|
2364
|
+
...at ? { at } : {},
|
|
2365
|
+
...bindAt ? { bindAt } : {},
|
|
2366
|
+
...load ? { load } : {}
|
|
2367
|
+
};
|
|
2368
|
+
}
|
|
2369
|
+
/** Split a component's children into named slots (`slot="x"`) and the rest. */
|
|
2370
|
+
partitionSlots(children) {
|
|
2371
|
+
const slots = /* @__PURE__ */ new Map();
|
|
2372
|
+
const rest = [];
|
|
2373
|
+
for (const child of children) {
|
|
2374
|
+
if (child.kind === "Element" || child.kind === "Component") {
|
|
2375
|
+
const slot = child.attributes.find(
|
|
2376
|
+
(a) => a.kind === "Attribute" && a.name === "slot"
|
|
2377
|
+
);
|
|
2378
|
+
if (slot && slot.value && slot.value.kind === "static") {
|
|
2379
|
+
const stripped = { ...child, attributes: child.attributes.filter((a) => a !== slot) };
|
|
2380
|
+
const ir = stripped.kind === "Element" ? this.lowerElement(stripped) : this.lowerComponent(stripped);
|
|
2381
|
+
const bucket = slots.get(slot.value.value) ?? [];
|
|
2382
|
+
bucket.push(ir);
|
|
2383
|
+
slots.set(slot.value.value, bucket);
|
|
2384
|
+
continue;
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
rest.push(child);
|
|
2388
|
+
}
|
|
2389
|
+
return { slots, rest };
|
|
2390
|
+
}
|
|
2391
|
+
// --- control-flow sugar -------------------------------------------------
|
|
2392
|
+
/** `<el if=${cond}>…</el>` (+ optional `<el else>`) → `<Show when fallback>`. */
|
|
2393
|
+
lowerIf(el, condHole, elseEl) {
|
|
2394
|
+
const cond = this.hole(condHole);
|
|
2395
|
+
const props = [
|
|
2396
|
+
{ name: "when", kind: "attr", expr: cond.code, reactive: cond.reactive, ...cond.at ? { at: cond.at } : {} }
|
|
2397
|
+
];
|
|
2398
|
+
if (elseEl) {
|
|
2399
|
+
const elseIR = this.stripAndLower(elseEl, (a) => a.kind === "ElseDirective");
|
|
2400
|
+
props.push({ name: "fallback", kind: "attr", expr: this.emit(elseIR), jsxElement: true });
|
|
2401
|
+
}
|
|
2402
|
+
const child = this.stripAndLower(el, (a) => a.kind === "IfDirective");
|
|
2403
|
+
const at = this.tagAt(el);
|
|
2404
|
+
return { kind: "component", name: "Show", props, children: [child], ...at ? { at } : {}, ...cond.at ? { bindAt: cond.at } : {} };
|
|
2405
|
+
}
|
|
2406
|
+
/** `<el each=${items} key?>${item => …}</el>` → `<For each>{item => <el>…</el>}</For>`. */
|
|
2407
|
+
lowerEach(el) {
|
|
2408
|
+
const eachDir = el.attributes.find((a) => a.kind === "EachDirective");
|
|
2409
|
+
if (!eachDir || eachDir.kind !== "EachDirective") return this.lowerNode(el);
|
|
2410
|
+
const each = this.hole(eachDir.hole);
|
|
2411
|
+
const props = [{ name: "each", kind: "attr", expr: each.code, reactive: each.reactive, ...each.at ? { at: each.at } : {} }];
|
|
2412
|
+
if (eachDir.key) {
|
|
2413
|
+
const by = "static" in eachDir.key ? `(item) => item[${JSON.stringify(eachDir.key.static)}]` : this.hole(eachDir.key.hole).code;
|
|
2414
|
+
props.push({ name: "by", kind: "attr", expr: by });
|
|
2415
|
+
}
|
|
2416
|
+
const arrow = this.itemArrow(el);
|
|
2417
|
+
let childrenCode;
|
|
2418
|
+
if (arrow) {
|
|
2419
|
+
const bodyIR = { kind: "expr", code: arrow.body, reactive: arrow.bodyReactive };
|
|
2420
|
+
const repeated = this.rebuildWithChildren(el, [bodyIR]);
|
|
2421
|
+
childrenCode = `(${arrow.params.join(", ")}) => ${paren(this.emit(repeated), "arrowBody")}`;
|
|
2422
|
+
} else {
|
|
2423
|
+
const repeated = this.stripAndLower(el, (a) => a.kind === "EachDirective");
|
|
2424
|
+
childrenCode = `() => ${paren(this.emit(repeated), "arrowBody")}`;
|
|
2425
|
+
}
|
|
2426
|
+
const children = [{ kind: "expr", code: childrenCode, reactive: false }];
|
|
2427
|
+
const at = this.tagAt(el);
|
|
2428
|
+
return { kind: "component", name: "For", props, children, ...at ? { at } : {}, ...each.at ? { bindAt: each.at } : {} };
|
|
2429
|
+
}
|
|
2430
|
+
/** The single `${item => expr}` child arrow, if that's the element's content. */
|
|
2431
|
+
itemArrow(el) {
|
|
2432
|
+
const kids = el.children.filter((c) => !this.isBlankText(c));
|
|
2433
|
+
if (kids.length !== 1 || kids[0].kind !== "Expression") return null;
|
|
2434
|
+
return this.hole(kids[0].hole).arrow ?? null;
|
|
2435
|
+
}
|
|
2436
|
+
/** Lower an element to IR, dropping the directive attribute matched by `drop`. */
|
|
2437
|
+
stripAndLower(el, drop) {
|
|
2438
|
+
const clone = { ...el, attributes: el.attributes.filter((a) => !drop(a)) };
|
|
2439
|
+
return clone.kind === "Element" ? this.lowerElement(clone) : this.lowerComponent(clone);
|
|
2440
|
+
}
|
|
2441
|
+
/** Same element (minus each/key) but with explicit IR children. */
|
|
2442
|
+
rebuildWithChildren(el, children) {
|
|
2443
|
+
const props = this.lowerAttributes(
|
|
2444
|
+
el.attributes.filter((a) => a.kind !== "EachDirective")
|
|
2445
|
+
);
|
|
2446
|
+
if (el.kind === "Element") {
|
|
2447
|
+
const at = this.tagAt(el);
|
|
2448
|
+
return {
|
|
2449
|
+
kind: "element",
|
|
2450
|
+
tag: el.tag,
|
|
2451
|
+
svg: el.namespace === "svg",
|
|
2452
|
+
props,
|
|
2453
|
+
children,
|
|
2454
|
+
static: false,
|
|
2455
|
+
...at ? { at } : {}
|
|
2456
|
+
};
|
|
2457
|
+
}
|
|
2458
|
+
return { kind: "component", name: el.tag, props, children };
|
|
2459
|
+
}
|
|
2460
|
+
// --- attributes ---------------------------------------------------------
|
|
2461
|
+
lowerAttributes(attrs) {
|
|
2462
|
+
const props = [];
|
|
2463
|
+
const classEntries = [];
|
|
2464
|
+
let classReactive = false;
|
|
2465
|
+
let classAt;
|
|
2466
|
+
const styleEntries = [];
|
|
2467
|
+
let styleReactive = false;
|
|
2468
|
+
let styleAt;
|
|
2469
|
+
const usePairs = [];
|
|
2470
|
+
for (const a of attrs) {
|
|
2471
|
+
switch (a.kind) {
|
|
2472
|
+
case "IfDirective":
|
|
2473
|
+
case "EachDirective":
|
|
2474
|
+
case "ElseDirective":
|
|
2475
|
+
break;
|
|
2476
|
+
// handled as control-flow at the parent level
|
|
2477
|
+
case "Attribute":
|
|
2478
|
+
props.push(this.plainAttr(a));
|
|
2479
|
+
break;
|
|
2480
|
+
case "PropertyBinding":
|
|
2481
|
+
props.push({
|
|
2482
|
+
name: a.name,
|
|
2483
|
+
kind: "prop",
|
|
2484
|
+
expr: this.hole(a.hole).code,
|
|
2485
|
+
reactive: this.hole(a.hole).reactive,
|
|
2486
|
+
...this.hole(a.hole).at ? { at: this.hole(a.hole).at } : {}
|
|
2487
|
+
});
|
|
2488
|
+
break;
|
|
2489
|
+
case "EventBinding":
|
|
2490
|
+
props.push(this.eventProp(a));
|
|
2491
|
+
break;
|
|
2492
|
+
case "RefBinding":
|
|
2493
|
+
props.push({
|
|
2494
|
+
name: "ref",
|
|
2495
|
+
kind: "ref",
|
|
2496
|
+
expr: this.hole(a.hole).code,
|
|
2497
|
+
reactive: this.hole(a.hole).reactive,
|
|
2498
|
+
// Which variable ends up holding the element, and where it was written.
|
|
2499
|
+
...this.hole(a.hole).at ? { at: this.hole(a.hole).at } : {}
|
|
2500
|
+
});
|
|
2501
|
+
break;
|
|
2502
|
+
case "Spread":
|
|
2503
|
+
props.push({ name: "", kind: "spread", expr: this.hole(a.hole).code });
|
|
2504
|
+
break;
|
|
2505
|
+
case "ClassDirective": {
|
|
2506
|
+
const h = this.hole(a.hole);
|
|
2507
|
+
classEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
|
|
2508
|
+
classReactive = classReactive || h.reactive;
|
|
2509
|
+
classAt ??= h.at;
|
|
2510
|
+
break;
|
|
2511
|
+
}
|
|
2512
|
+
case "StyleDirective": {
|
|
2513
|
+
const h = this.hole(a.hole);
|
|
2514
|
+
styleEntries.push(`${JSON.stringify(a.name)}: ${h.code}`);
|
|
2515
|
+
styleReactive = styleReactive || h.reactive;
|
|
2516
|
+
styleAt ??= h.at;
|
|
2517
|
+
break;
|
|
2518
|
+
}
|
|
2519
|
+
case "BindDirective":
|
|
2520
|
+
props.push(...this.bindProps(a.name, this.hole(a.hole)));
|
|
2521
|
+
break;
|
|
2522
|
+
case "UseDirective": {
|
|
2523
|
+
const dir = a.name ?? (a.hole != null ? this.hole(a.hole).code : null);
|
|
2524
|
+
if (!dir) break;
|
|
2525
|
+
usePairs.push(a.name != null && a.hole != null ? `[${dir}, () => ${paren(this.hole(a.hole).code, "arrowBody")}]` : `[${dir}]`);
|
|
2526
|
+
break;
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
if (classEntries.length > 0) {
|
|
2531
|
+
props.push({
|
|
2532
|
+
name: "classList",
|
|
2533
|
+
kind: "attr",
|
|
2534
|
+
expr: `{ ${classEntries.join(", ")} }`,
|
|
2535
|
+
reactive: classReactive,
|
|
2536
|
+
...classAt ? { at: classAt } : {}
|
|
2537
|
+
});
|
|
2538
|
+
}
|
|
2539
|
+
if (styleEntries.length > 0) {
|
|
2540
|
+
props.push({
|
|
2541
|
+
name: "style",
|
|
2542
|
+
kind: "attr",
|
|
2543
|
+
expr: `{ ${styleEntries.join(", ")} }`,
|
|
2544
|
+
reactive: styleReactive,
|
|
2545
|
+
...styleAt ? { at: styleAt } : {}
|
|
2546
|
+
});
|
|
2547
|
+
}
|
|
2548
|
+
if (usePairs.length > 0) {
|
|
2549
|
+
props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
|
|
2550
|
+
}
|
|
2551
|
+
return props;
|
|
2552
|
+
}
|
|
2553
|
+
/**
|
|
2554
|
+
* Lower an event binding. Plain `@click`/`onClick` (no modifiers) stays a
|
|
2555
|
+
* DELEGATED handler (unchanged, byte-identical with JSX). `on:click` or any
|
|
2556
|
+
* modifier (`.capture`/`.once`/`.passive`/`.prevent`/`.stop`/`.self`) emits a
|
|
2557
|
+
* NATIVE `on:name` prop the runtime attaches via addEventListener.
|
|
2558
|
+
*/
|
|
2559
|
+
eventProp(a) {
|
|
2560
|
+
const dom = a.name.toLowerCase();
|
|
2561
|
+
const handler = this.hole(a.hole).code;
|
|
2562
|
+
const native = a.syntax === "colon" || a.modifiers.length > 0;
|
|
2563
|
+
if (!native) {
|
|
2564
|
+
return {
|
|
2565
|
+
name: "on" + capitalize(a.name),
|
|
2566
|
+
kind: "event",
|
|
2567
|
+
event: { name: dom, delegated: DELEGATED.has(dom) },
|
|
2568
|
+
expr: handler
|
|
2569
|
+
};
|
|
2570
|
+
}
|
|
2571
|
+
const wrapped = this.wrapHandler(handler, a.modifiers);
|
|
2572
|
+
const options = this.eventOptions(a.modifiers);
|
|
2573
|
+
const value = options ? `[${wrapped}, ${options}]` : wrapped;
|
|
2574
|
+
return { name: "on:" + dom, kind: "attr", expr: value };
|
|
2575
|
+
}
|
|
2576
|
+
/** Wrap a handler for `.prevent`/`.stop`/`.self` (no wrap when none apply). */
|
|
2577
|
+
wrapHandler(handler, mods) {
|
|
2578
|
+
const guard = mods.includes("self") ? "if (e.target !== e.currentTarget) return; " : "";
|
|
2579
|
+
const pre = [];
|
|
2580
|
+
if (mods.includes("prevent")) pre.push("e.preventDefault();");
|
|
2581
|
+
if (mods.includes("stop")) pre.push("e.stopPropagation();");
|
|
2582
|
+
if (!guard && pre.length === 0) return handler;
|
|
2583
|
+
return `(e) => { ${guard}${pre.join(" ")} return (${handler})(e); }`;
|
|
2584
|
+
}
|
|
2585
|
+
/** addEventListener options object from `.capture`/`.once`/`.passive`, or null. */
|
|
2586
|
+
eventOptions(mods) {
|
|
2587
|
+
const opts = [];
|
|
2588
|
+
if (mods.includes("capture")) opts.push("capture: true");
|
|
2589
|
+
if (mods.includes("once")) opts.push("once: true");
|
|
2590
|
+
if (mods.includes("passive")) opts.push("passive: true");
|
|
2591
|
+
return opts.length ? `{ ${opts.join(", ")} }` : null;
|
|
2592
|
+
}
|
|
2593
|
+
/** `bind:value=${sig}` → `value={read()}` + `onInput={e => write(e…value)}`. */
|
|
2594
|
+
bindProps(prop, h) {
|
|
2595
|
+
const sigCode = h.code;
|
|
2596
|
+
const checked = prop === "checked";
|
|
2597
|
+
const evt = checked ? "change" : "input";
|
|
2598
|
+
const accessor = checked ? "checked" : "value";
|
|
2599
|
+
this.used.add("bindPair");
|
|
2600
|
+
const sig = `bindPair(${sigCode})`;
|
|
2601
|
+
return [
|
|
2602
|
+
{
|
|
2603
|
+
name: prop,
|
|
2604
|
+
kind: "attr",
|
|
2605
|
+
expr: `${sig}[0]()`,
|
|
2606
|
+
reactive: true,
|
|
2607
|
+
directive: `bind:${prop}`,
|
|
2608
|
+
...h.at ? { at: h.at } : {}
|
|
2609
|
+
},
|
|
2610
|
+
{
|
|
2611
|
+
name: "on" + capitalize(evt),
|
|
2612
|
+
kind: "event",
|
|
2613
|
+
event: { name: evt, delegated: DELEGATED.has(evt) },
|
|
2614
|
+
// delegated handler: `target` is the bound element (currentTarget is the root)
|
|
2615
|
+
expr: `(e) => ${sig}[1](e.target.${accessor})`
|
|
2616
|
+
}
|
|
2617
|
+
];
|
|
2618
|
+
}
|
|
2619
|
+
/** A plain attribute (incl. lit `?bool`, mixed values), old `mkProp` behavior. */
|
|
2620
|
+
plainAttr(a) {
|
|
2621
|
+
const v = a.value;
|
|
2622
|
+
let name = a.name;
|
|
2623
|
+
if (a.name === "class") {
|
|
2624
|
+
name = v && v.kind === "hole" && this.hole(v.hole).object ? "classList" : "className";
|
|
2625
|
+
} else if (a.name === "html") {
|
|
2626
|
+
name = "innerHTML";
|
|
2627
|
+
}
|
|
2628
|
+
const kind = "attr";
|
|
2629
|
+
const base = { name, kind };
|
|
2630
|
+
if (v == null) {
|
|
2631
|
+
base.literal = true;
|
|
2632
|
+
return base;
|
|
2633
|
+
}
|
|
2634
|
+
if (v.kind === "static") {
|
|
2635
|
+
base.literal = v.value;
|
|
2636
|
+
return base;
|
|
2637
|
+
}
|
|
2638
|
+
if (v.kind === "hole") {
|
|
2639
|
+
const h = this.hole(v.hole);
|
|
2640
|
+
base.expr = h.code;
|
|
2641
|
+
base.reactive = h.reactive;
|
|
2642
|
+
if (h.at) base.at = h.at;
|
|
2643
|
+
return base;
|
|
2644
|
+
}
|
|
2645
|
+
let reactive = false;
|
|
2646
|
+
let at;
|
|
2647
|
+
const body = v.parts.map((p) => {
|
|
2648
|
+
if ("text" in p) return escapeTemplate(p.text);
|
|
2649
|
+
const h = this.hole(p.hole);
|
|
2650
|
+
reactive = reactive || h.reactive;
|
|
2651
|
+
at ??= h.at;
|
|
2652
|
+
return "${" + h.code + "}";
|
|
2653
|
+
}).join("");
|
|
2654
|
+
base.expr = "`" + body + "`";
|
|
2655
|
+
base.reactive = reactive;
|
|
2656
|
+
if (at) base.at = at;
|
|
2657
|
+
return base;
|
|
2658
|
+
}
|
|
2659
|
+
};
|
|
2660
|
+
function lowerTemplate(pieces, holes, used, opts = {}) {
|
|
2661
|
+
const { root } = parseTemplate(pieces, { svg: opts.svg });
|
|
2662
|
+
return new Lowerer(holes, used, opts.sourceFile, opts.positionAt).lowerRoot(root.children);
|
|
2663
|
+
}
|
|
2664
|
+
|
|
2665
|
+
// src/lower/compile-template.ts
|
|
2666
|
+
function compilePieces(pieces, holes, options = {}) {
|
|
2667
|
+
const used = /* @__PURE__ */ new Set();
|
|
2668
|
+
const ir = lowerTemplate(pieces, [...holes], used, {
|
|
2669
|
+
svg: options.svg,
|
|
2670
|
+
...options.sourceFile ? { sourceFile: options.sourceFile } : {},
|
|
2671
|
+
...options.positionAt ? { positionAt: options.positionAt } : {}
|
|
2672
|
+
});
|
|
2673
|
+
analyzeStatic(ir);
|
|
2674
|
+
resolveComponentSources(ir, {
|
|
2675
|
+
resolver: createComponentResolver(options.resolve ?? []),
|
|
2676
|
+
modules: {
|
|
2677
|
+
controlFlowModule: options.controlFlowModule ?? "@fluixi/dom",
|
|
2678
|
+
coreModule: options.coreModule ?? "@fluixi/core",
|
|
2679
|
+
routerModule: options.routerModule ?? "@fluixi/core/router"
|
|
2680
|
+
},
|
|
2681
|
+
strategyFor: (node) => node.load
|
|
2682
|
+
});
|
|
2683
|
+
collapseStrategies(ir);
|
|
2684
|
+
const { code, imports, templates } = imperativeBackend.emit(ir, {
|
|
2685
|
+
templateClone: options.templateClone,
|
|
2686
|
+
partialTemplates: options.partialTemplates
|
|
2687
|
+
});
|
|
2688
|
+
for (const symbol of imports) used.add(symbol);
|
|
2689
|
+
if (options.hoistTemplates) return { code, imports: [...used], ir, templates };
|
|
2690
|
+
return { code: inlineTemplates(code, templates), imports: [...used], ir };
|
|
2691
|
+
}
|
|
2692
|
+
|
|
2693
|
+
// src/analyze/reactive-expr.ts
|
|
2694
|
+
var STABLE_MEMBER = "children";
|
|
2695
|
+
function isReactiveShape(shape) {
|
|
2696
|
+
switch (shape.kind) {
|
|
2697
|
+
case "call":
|
|
2698
|
+
return true;
|
|
2699
|
+
case "member":
|
|
2700
|
+
return shape.property !== STABLE_MEMBER;
|
|
2701
|
+
case "compound":
|
|
2702
|
+
return shape.parts.some(isReactiveShape);
|
|
2703
|
+
case "opaque":
|
|
2704
|
+
return false;
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
// src/analyze/expr-shape.ts
|
|
2709
|
+
var CALL = /* @__PURE__ */ new Set(["CallExpression", "OptionalCallExpression"]);
|
|
2710
|
+
var MEMBER = /* @__PURE__ */ new Set(["MemberExpression", "OptionalMemberExpression"]);
|
|
2711
|
+
function shapeOf(node) {
|
|
2712
|
+
if (!node) return { kind: "opaque" };
|
|
2713
|
+
if (CALL.has(node.type)) return { kind: "call" };
|
|
2714
|
+
if (MEMBER.has(node.type)) {
|
|
2715
|
+
const property = node.property;
|
|
2716
|
+
const computed = node.computed === true;
|
|
2717
|
+
return {
|
|
2718
|
+
kind: "member",
|
|
2719
|
+
property: !computed && property?.type === "Identifier" ? property.name : null
|
|
2720
|
+
};
|
|
2721
|
+
}
|
|
2722
|
+
if (node.type === "ConditionalExpression") {
|
|
2723
|
+
return { kind: "compound", parts: [node.test, node.consequent, node.alternate].map(part) };
|
|
2724
|
+
}
|
|
2725
|
+
if (node.type === "LogicalExpression" || node.type === "BinaryExpression") {
|
|
2726
|
+
return { kind: "compound", parts: [node.left, node.right].map(part) };
|
|
2727
|
+
}
|
|
2728
|
+
if (node.type === "TemplateLiteral") {
|
|
2729
|
+
return { kind: "compound", parts: node.expressions.map(part) };
|
|
2730
|
+
}
|
|
2731
|
+
if (node.type === "ObjectExpression") {
|
|
2732
|
+
const props = node.properties.filter(
|
|
2733
|
+
(p) => (p.type === "ObjectProperty" || p.type === "Property") && p.computed !== true
|
|
2734
|
+
);
|
|
2735
|
+
return { kind: "compound", parts: props.map((p) => part(p.value)) };
|
|
2736
|
+
}
|
|
2737
|
+
if (node.type === "ArrayExpression") {
|
|
2738
|
+
const items = node.elements.filter(
|
|
2739
|
+
(e) => e != null && e.type !== "SpreadElement"
|
|
2740
|
+
);
|
|
2741
|
+
return { kind: "compound", parts: items.map(part) };
|
|
2742
|
+
}
|
|
2743
|
+
return { kind: "opaque" };
|
|
2744
|
+
}
|
|
2745
|
+
var part = (n2) => shapeOf(n2);
|
|
2746
|
+
|
|
2747
|
+
// src/lower/jsx.ts
|
|
2748
|
+
var SVG_TAGS = /* @__PURE__ */ new Set([
|
|
2749
|
+
"svg",
|
|
2750
|
+
"path",
|
|
2751
|
+
"circle",
|
|
2752
|
+
"rect",
|
|
2753
|
+
"line",
|
|
2754
|
+
"polygon",
|
|
2755
|
+
"polyline",
|
|
2756
|
+
"ellipse",
|
|
2757
|
+
"g",
|
|
2758
|
+
"defs",
|
|
2759
|
+
"clipPath",
|
|
2760
|
+
"text"
|
|
2761
|
+
]);
|
|
2762
|
+
var EVENT_RE = /^on[A-Z]/;
|
|
2763
|
+
var DELEGATED2 = /* @__PURE__ */ new Set([
|
|
2764
|
+
"click",
|
|
2765
|
+
"dblclick",
|
|
2766
|
+
"input",
|
|
2767
|
+
"change",
|
|
2768
|
+
"submit",
|
|
2769
|
+
"focus",
|
|
2770
|
+
"blur",
|
|
2771
|
+
"keydown",
|
|
2772
|
+
"keyup",
|
|
2773
|
+
"keypress",
|
|
2774
|
+
"mousedown",
|
|
2775
|
+
"mouseup"
|
|
2776
|
+
]);
|
|
2777
|
+
var isReactive = (node) => isReactiveShape(shapeOf(node));
|
|
2778
|
+
var posOf = (node, ctx) => {
|
|
2779
|
+
if (!ctx.sourceFile) return void 0;
|
|
2780
|
+
const at = node.loc?.start;
|
|
2781
|
+
return at ? { line: at.line, column: at.column } : void 0;
|
|
2782
|
+
};
|
|
2783
|
+
function literalOf(node) {
|
|
2784
|
+
if (node.type === "StringLiteral" || node.type === "NumericLiteral" || node.type === "BooleanLiteral") {
|
|
2785
|
+
return node.value;
|
|
2786
|
+
}
|
|
2787
|
+
if (node.type === "Literal" && (typeof node.value === "string" || typeof node.value === "number" || typeof node.value === "boolean")) {
|
|
2788
|
+
return node.value;
|
|
2789
|
+
}
|
|
2790
|
+
return void 0;
|
|
2791
|
+
}
|
|
2792
|
+
var isString = (node) => !!node && typeof literalOf(node) === "string";
|
|
2793
|
+
function tagInfo(name, ctx) {
|
|
2794
|
+
if (name.type === "JSXIdentifier") {
|
|
2795
|
+
return { tag: name.name, component: name.name[0] !== name.name[0].toLowerCase() };
|
|
2796
|
+
}
|
|
2797
|
+
if (name.type === "JSXMemberExpression") return { tag: ctx.code(name), component: true };
|
|
2798
|
+
if (name.type === "JSXNamespacedName") {
|
|
2799
|
+
return { tag: `${name.namespace.name}:${name.name.name}`, component: false };
|
|
2800
|
+
}
|
|
2801
|
+
return { tag: "div", component: false };
|
|
2802
|
+
}
|
|
2803
|
+
function attrName(name) {
|
|
2804
|
+
if (name.type === "JSXIdentifier") return name.name === "class" ? "className" : name.name;
|
|
2805
|
+
if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
|
|
2806
|
+
return "unknown";
|
|
2807
|
+
}
|
|
2808
|
+
function buildAttr(attr, ctx) {
|
|
2809
|
+
const name = attrName(attr.name);
|
|
2810
|
+
const isEvent = EVENT_RE.test(name);
|
|
2811
|
+
const isHandler = isEvent || name.startsWith("on:") || name === "ref";
|
|
2812
|
+
const kind = isEvent ? "event" : "attr";
|
|
2813
|
+
const base = { name, kind };
|
|
2814
|
+
if (isEvent) {
|
|
2815
|
+
const evt = name.slice(2).toLowerCase();
|
|
2816
|
+
base.event = { name: evt, delegated: DELEGATED2.has(evt) };
|
|
2817
|
+
}
|
|
2818
|
+
const v = attr.value;
|
|
2819
|
+
if (v == null) {
|
|
2820
|
+
base.literal = true;
|
|
2821
|
+
base.at = posOf(attr, ctx);
|
|
2822
|
+
return base;
|
|
2823
|
+
}
|
|
2824
|
+
if (isString(v)) {
|
|
2825
|
+
base.literal = literalOf(v);
|
|
2826
|
+
base.at = posOf(v, ctx);
|
|
2827
|
+
return base;
|
|
2828
|
+
}
|
|
2829
|
+
if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
|
|
2830
|
+
const expr = v.expression;
|
|
2831
|
+
const literal = literalOf(expr);
|
|
2832
|
+
if (literal !== void 0) {
|
|
2833
|
+
base.literal = literal;
|
|
2834
|
+
base.at = posOf(expr, ctx);
|
|
2835
|
+
return base;
|
|
2836
|
+
}
|
|
2837
|
+
base.expr = ctx.code(expr);
|
|
2838
|
+
base.reactive = isHandler ? false : isReactive(expr);
|
|
2839
|
+
base.at = posOf(expr, ctx);
|
|
2840
|
+
if (expr.type === "JSXElement" || expr.type === "JSXFragment") base.jsxElement = true;
|
|
2841
|
+
return base;
|
|
2842
|
+
}
|
|
2843
|
+
base.literal = true;
|
|
2844
|
+
return base;
|
|
2845
|
+
}
|
|
2846
|
+
function attrExprCode(attr, ctx) {
|
|
2847
|
+
const v = attr.value;
|
|
2848
|
+
if (v == null) return null;
|
|
2849
|
+
if (isString(v)) return JSON.stringify(literalOf(v));
|
|
2850
|
+
if (v.type === "JSXExpressionContainer" && v.expression?.type !== "JSXEmptyExpression") {
|
|
2851
|
+
return ctx.code(v.expression);
|
|
2852
|
+
}
|
|
2853
|
+
return null;
|
|
2854
|
+
}
|
|
2855
|
+
function buildProps(attrs, ctx) {
|
|
2856
|
+
const props = [];
|
|
2857
|
+
const usePairs = [];
|
|
2858
|
+
for (const a of attrs) {
|
|
2859
|
+
if (a.type === "JSXSpreadAttribute") {
|
|
2860
|
+
props.push({ name: "", kind: "spread", expr: ctx.code(a.argument) });
|
|
2861
|
+
continue;
|
|
2862
|
+
}
|
|
2863
|
+
if (a.type !== "JSXAttribute") continue;
|
|
2864
|
+
if (isLoadDirective(attrName(a.name))) continue;
|
|
2865
|
+
const ns = a.name.type === "JSXNamespacedName" ? a.name.namespace.name : null;
|
|
2866
|
+
if (ns === "use") {
|
|
2867
|
+
const dir = a.name.name.name;
|
|
2868
|
+
ctx.used.add(dir);
|
|
2869
|
+
const opts = attrExprCode(a, ctx);
|
|
2870
|
+
usePairs.push(opts != null ? `[${dir}, () => ${paren(opts, "arrowBody")}]` : `[${dir}]`);
|
|
2871
|
+
continue;
|
|
2872
|
+
}
|
|
2873
|
+
if (ns === "oncapture") {
|
|
2874
|
+
const evt = a.name.name.name.toLowerCase();
|
|
2875
|
+
const h = attrExprCode(a, ctx) ?? "undefined";
|
|
2876
|
+
props.push({ name: "on:" + evt, kind: "attr", expr: `[${h}, { capture: true }]` });
|
|
2877
|
+
continue;
|
|
2878
|
+
}
|
|
2879
|
+
props.push(buildAttr(a, ctx));
|
|
2880
|
+
}
|
|
2881
|
+
if (usePairs.length > 0) props.push({ name: "use", kind: "attr", expr: `[${usePairs.join(", ")}]` });
|
|
2882
|
+
return props;
|
|
2883
|
+
}
|
|
2884
|
+
function buildChildren(children, ctx) {
|
|
2885
|
+
const out = [];
|
|
2886
|
+
for (const child of children) {
|
|
2887
|
+
if (child.type === "JSXText") {
|
|
2888
|
+
const text = cleanTemplateText(child.value);
|
|
2889
|
+
if (text) out.push({ kind: "text", value: text });
|
|
2890
|
+
} else if (child.type === "JSXExpressionContainer") {
|
|
2891
|
+
if (child.expression?.type !== "JSXEmptyExpression") {
|
|
2892
|
+
const expr = child.expression;
|
|
2893
|
+
const reactive = isReactive(expr);
|
|
2894
|
+
out.push({
|
|
2895
|
+
kind: "expr",
|
|
2896
|
+
code: ctx.code(expr),
|
|
2897
|
+
reactive,
|
|
2898
|
+
...reactive ? { at: posOf(expr, ctx) } : {}
|
|
2899
|
+
});
|
|
2900
|
+
}
|
|
2901
|
+
} else if (child.type === "JSXElement" || child.type === "JSXFragment") {
|
|
2902
|
+
out.push(buildNode(child, ctx));
|
|
2903
|
+
} else if (child.type === "JSXSpreadChild") {
|
|
2904
|
+
out.push({ kind: "expr", code: ctx.code(child.expression), reactive: false });
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
return out;
|
|
2908
|
+
}
|
|
2909
|
+
function readLoad(attrs) {
|
|
2910
|
+
for (const a of attrs) {
|
|
2911
|
+
if (a.type !== "JSXAttribute") continue;
|
|
2912
|
+
const name = attrName(a.name);
|
|
2913
|
+
if (!isLoadDirective(name)) continue;
|
|
2914
|
+
if (a.value && !isString(a.value)) {
|
|
2915
|
+
throw new LoadDirectiveError(
|
|
2916
|
+
`'${name}' needs a literal value, not an expression — the strategy is compile-time.`
|
|
2917
|
+
);
|
|
2918
|
+
}
|
|
2919
|
+
return parseLoadDirective(name, a.value ? literalOf(a.value) : null);
|
|
2920
|
+
}
|
|
2921
|
+
return void 0;
|
|
2922
|
+
}
|
|
2923
|
+
function buildNode(node, ctx) {
|
|
2924
|
+
if (node.type === "JSXFragment") {
|
|
2925
|
+
return { kind: "fragment", children: buildChildren(node.children, ctx) };
|
|
2926
|
+
}
|
|
2927
|
+
const { tag, component } = tagInfo(node.openingElement.name, ctx);
|
|
2928
|
+
const props = buildProps(node.openingElement.attributes, ctx);
|
|
2929
|
+
const children = buildChildren(node.children, ctx);
|
|
2930
|
+
const at = ctx.sourceFile && node.loc?.start ? { file: ctx.sourceFile, line: node.loc.start.line, column: node.loc.start.column } : void 0;
|
|
2931
|
+
if (component) {
|
|
2932
|
+
const load = readLoad(node.openingElement.attributes);
|
|
2933
|
+
return { kind: "component", name: tag, props, children, ...load ? { load } : {}, ...at ? { at } : {} };
|
|
2934
|
+
}
|
|
2935
|
+
return { kind: "element", tag, svg: SVG_TAGS.has(tag), props, children, static: false, ...at ? { at } : {} };
|
|
2936
|
+
}
|
|
2937
|
+
function buildJsxIR(node, ctx) {
|
|
2938
|
+
const ir = buildNode(node, ctx);
|
|
2939
|
+
analyzeStatic(ir);
|
|
2940
|
+
return ir;
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
// src/transform/bindings.ts
|
|
2944
|
+
function patternNames2(node, out) {
|
|
2945
|
+
if (!node) return;
|
|
2946
|
+
switch (node.type) {
|
|
2947
|
+
case "Identifier":
|
|
2948
|
+
out.add(node.name);
|
|
2949
|
+
return;
|
|
2950
|
+
case "ObjectPattern":
|
|
2951
|
+
for (const p of node.properties) {
|
|
2952
|
+
if (p.type === "RestElement") patternNames2(p.argument, out);
|
|
2953
|
+
else patternNames2(p.value, out);
|
|
2954
|
+
}
|
|
2955
|
+
return;
|
|
2956
|
+
case "ArrayPattern":
|
|
2957
|
+
for (const e of node.elements) patternNames2(e, out);
|
|
2958
|
+
return;
|
|
2959
|
+
case "AssignmentPattern":
|
|
2960
|
+
patternNames2(node.left, out);
|
|
2961
|
+
return;
|
|
2962
|
+
case "RestElement":
|
|
2963
|
+
patternNames2(node.argument, out);
|
|
2964
|
+
return;
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
function declared(node, out) {
|
|
2968
|
+
switch (node.type) {
|
|
2969
|
+
case "ImportDeclaration":
|
|
2970
|
+
for (const s of node.specifiers) patternNames2(s.local, out);
|
|
2971
|
+
return;
|
|
2972
|
+
case "VariableDeclaration":
|
|
2973
|
+
for (const d of node.declarations) patternNames2(d.id, out);
|
|
2974
|
+
return;
|
|
2975
|
+
case "FunctionDeclaration":
|
|
2976
|
+
case "ClassDeclaration":
|
|
2977
|
+
patternNames2(node.id, out);
|
|
2978
|
+
return;
|
|
2979
|
+
case "ExportNamedDeclaration":
|
|
2980
|
+
case "ExportDefaultDeclaration":
|
|
2981
|
+
if (node.declaration) declared(node.declaration, out);
|
|
2982
|
+
return;
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
function moduleBindings(program) {
|
|
2986
|
+
const names = /* @__PURE__ */ new Set();
|
|
2987
|
+
for (const statement of program.body) declared(statement, names);
|
|
2988
|
+
return names;
|
|
2989
|
+
}
|
|
2990
|
+
|
|
2991
|
+
// src/transform/props.ts
|
|
2992
|
+
import { parse } from "@babel/parser";
|
|
2993
|
+
|
|
2994
|
+
// src/analyze/props-destructure.ts
|
|
2995
|
+
function patternNames3(node, out) {
|
|
2996
|
+
if (!node) return;
|
|
2997
|
+
switch (node.type) {
|
|
2998
|
+
case "Identifier":
|
|
2999
|
+
out.add(node.name);
|
|
3000
|
+
return;
|
|
3001
|
+
case "ObjectPattern":
|
|
3002
|
+
for (const p of node.properties) {
|
|
3003
|
+
if (p.type === "RestElement") patternNames3(p.argument, out);
|
|
3004
|
+
else patternNames3(p.value, out);
|
|
3005
|
+
}
|
|
3006
|
+
return;
|
|
3007
|
+
case "ArrayPattern":
|
|
3008
|
+
for (const e of node.elements) patternNames3(e, out);
|
|
3009
|
+
return;
|
|
3010
|
+
case "AssignmentPattern":
|
|
3011
|
+
patternNames3(node.left, out);
|
|
3012
|
+
return;
|
|
3013
|
+
case "RestElement":
|
|
3014
|
+
patternNames3(node.argument, out);
|
|
3015
|
+
return;
|
|
3016
|
+
}
|
|
3017
|
+
}
|
|
3018
|
+
function isSafeDefault(node) {
|
|
3019
|
+
switch (node.type) {
|
|
3020
|
+
case "StringLiteral":
|
|
3021
|
+
case "NumericLiteral":
|
|
3022
|
+
case "BooleanLiteral":
|
|
3023
|
+
case "NullLiteral":
|
|
3024
|
+
case "BigIntLiteral":
|
|
3025
|
+
case "RegExpLiteral":
|
|
3026
|
+
case "Identifier":
|
|
3027
|
+
return true;
|
|
3028
|
+
// ESTree spells every literal as `Literal`; both shapes reach this analysis.
|
|
3029
|
+
case "Literal":
|
|
3030
|
+
return true;
|
|
3031
|
+
case "TemplateLiteral":
|
|
3032
|
+
return node.expressions.every(isSafeDefault);
|
|
3033
|
+
case "UnaryExpression":
|
|
3034
|
+
return isSafeDefault(node.argument);
|
|
3035
|
+
case "MemberExpression":
|
|
3036
|
+
return false;
|
|
3037
|
+
default:
|
|
3038
|
+
return false;
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
function walk4(root, visit) {
|
|
3042
|
+
if (!root || typeof root !== "object") return;
|
|
3043
|
+
visit(root);
|
|
3044
|
+
for (const key of Object.keys(root)) {
|
|
3045
|
+
if (key === "loc" || key === "range" || key === "leadingComments" || key === "trailingComments") continue;
|
|
3046
|
+
const value = root[key];
|
|
3047
|
+
if (Array.isArray(value)) {
|
|
3048
|
+
for (const item of value) if (item && typeof item === "object") walk4(item, visit);
|
|
3049
|
+
} else if (value && typeof value === "object" && typeof value.type === "string") {
|
|
3050
|
+
walk4(value, visit);
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
function reassignedNames(body) {
|
|
3055
|
+
const written = /* @__PURE__ */ new Set();
|
|
3056
|
+
walk4(body, (node) => {
|
|
3057
|
+
if (node.type === "AssignmentExpression") patternNames3(node.left, written);
|
|
3058
|
+
else if (node.type === "UpdateExpression") {
|
|
3059
|
+
const arg = node.argument;
|
|
3060
|
+
if (arg?.type === "Identifier") written.add(arg.name);
|
|
3061
|
+
}
|
|
3062
|
+
});
|
|
3063
|
+
return written;
|
|
3064
|
+
}
|
|
3065
|
+
function shadowedNames(body, candidates) {
|
|
3066
|
+
const shadowed = /* @__PURE__ */ new Set();
|
|
3067
|
+
const note = (node) => {
|
|
3068
|
+
const names = /* @__PURE__ */ new Set();
|
|
3069
|
+
patternNames3(node, names);
|
|
3070
|
+
for (const n2 of names) if (candidates.has(n2)) shadowed.add(n2);
|
|
3071
|
+
};
|
|
3072
|
+
walk4(body, (node) => {
|
|
3073
|
+
switch (node.type) {
|
|
3074
|
+
case "VariableDeclarator":
|
|
3075
|
+
note(node.id);
|
|
3076
|
+
return;
|
|
3077
|
+
case "FunctionDeclaration":
|
|
3078
|
+
case "FunctionExpression":
|
|
3079
|
+
case "ArrowFunctionExpression":
|
|
3080
|
+
note(node.id);
|
|
3081
|
+
for (const p of node.params ?? []) note(p);
|
|
3082
|
+
return;
|
|
3083
|
+
case "CatchClause":
|
|
3084
|
+
note(node.param);
|
|
3085
|
+
return;
|
|
3086
|
+
case "ClassDeclaration":
|
|
3087
|
+
case "ClassExpression":
|
|
3088
|
+
note(node.id);
|
|
3089
|
+
return;
|
|
3090
|
+
}
|
|
3091
|
+
});
|
|
3092
|
+
return shadowed;
|
|
3093
|
+
}
|
|
3094
|
+
function collect(pattern, path, plan) {
|
|
3095
|
+
for (const property of pattern.properties ?? []) {
|
|
3096
|
+
if (property.type === "RestElement") {
|
|
3097
|
+
const argument = property.argument;
|
|
3098
|
+
if (argument?.type !== "Identifier" || path.length > 0) {
|
|
3099
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
3100
|
+
patternNames3(argument, names2);
|
|
3101
|
+
for (const name of names2) plan.bails.push({ name, reason: "rest" });
|
|
3102
|
+
continue;
|
|
3103
|
+
}
|
|
3104
|
+
plan.rest = { name: argument.name, keys: [] };
|
|
3105
|
+
continue;
|
|
3106
|
+
}
|
|
3107
|
+
if (property.computed) {
|
|
3108
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
3109
|
+
patternNames3(property.value, names2);
|
|
3110
|
+
for (const name of names2) plan.bails.push({ name, reason: "computed" });
|
|
3111
|
+
continue;
|
|
3112
|
+
}
|
|
3113
|
+
const key = property.key;
|
|
3114
|
+
const propertyName = key.type === "Identifier" ? key.name : key.value;
|
|
3115
|
+
let value = property.value;
|
|
3116
|
+
let fallback;
|
|
3117
|
+
if (value.type === "AssignmentPattern") {
|
|
3118
|
+
fallback = value.right;
|
|
3119
|
+
value = value.left;
|
|
3120
|
+
}
|
|
3121
|
+
const here = [...path, propertyName];
|
|
3122
|
+
if (value.type === "Identifier") {
|
|
3123
|
+
if (fallback && !isSafeDefault(fallback)) {
|
|
3124
|
+
plan.bails.push({ name: value.name, reason: "unsafe-default" });
|
|
3125
|
+
continue;
|
|
3126
|
+
}
|
|
3127
|
+
plan.reads.push({ name: value.name, path: here, ...fallback ? { fallback } : {} });
|
|
3128
|
+
continue;
|
|
3129
|
+
}
|
|
3130
|
+
if (value.type === "ObjectPattern") {
|
|
3131
|
+
if (fallback) {
|
|
3132
|
+
const names2 = /* @__PURE__ */ new Set();
|
|
3133
|
+
patternNames3(value, names2);
|
|
3134
|
+
for (const name of names2) plan.bails.push({ name, reason: "unsafe-default" });
|
|
3135
|
+
continue;
|
|
3136
|
+
}
|
|
3137
|
+
collect(value, here, plan);
|
|
3138
|
+
continue;
|
|
3139
|
+
}
|
|
3140
|
+
const names = /* @__PURE__ */ new Set();
|
|
3141
|
+
patternNames3(value, names);
|
|
3142
|
+
for (const name of names) plan.bails.push({ name, reason: "computed" });
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
function planPropsDestructure(pattern, body) {
|
|
3146
|
+
const plan = { reads: [], bails: [] };
|
|
3147
|
+
if (pattern?.type !== "ObjectPattern") return plan;
|
|
3148
|
+
collect(pattern, [], plan);
|
|
3149
|
+
if (plan.rest) {
|
|
3150
|
+
if (body?.type !== "BlockStatement") {
|
|
3151
|
+
plan.bails.push({ name: plan.rest.name, reason: "rest" });
|
|
3152
|
+
delete plan.rest;
|
|
3153
|
+
} else {
|
|
3154
|
+
plan.rest.keys = (pattern.properties ?? []).filter((p) => p.type !== "RestElement" && !p.computed).map((p) => {
|
|
3155
|
+
const key = p.key;
|
|
3156
|
+
return key.type === "Identifier" ? key.name : key.value;
|
|
3157
|
+
});
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
if (plan.reads.length === 0 && !plan.rest) return plan;
|
|
3161
|
+
const candidates = new Set(plan.reads.map((r) => r.name));
|
|
3162
|
+
if (plan.rest) candidates.add(plan.rest.name);
|
|
3163
|
+
const written = reassignedNames(body);
|
|
3164
|
+
const shadowed = shadowedNames(body, candidates);
|
|
3165
|
+
const kept = [];
|
|
3166
|
+
for (const read of plan.reads) {
|
|
3167
|
+
if (written.has(read.name)) plan.bails.push({ name: read.name, reason: "reassigned" });
|
|
3168
|
+
else if (shadowed.has(read.name)) plan.bails.push({ name: read.name, reason: "shadowed" });
|
|
3169
|
+
else kept.push(read);
|
|
3170
|
+
}
|
|
3171
|
+
plan.reads = kept;
|
|
3172
|
+
if (plan.rest && written.has(plan.rest.name)) {
|
|
3173
|
+
plan.bails.push({ name: plan.rest.name, reason: "reassigned" });
|
|
3174
|
+
delete plan.rest;
|
|
3175
|
+
} else if (plan.rest && shadowed.has(plan.rest.name)) {
|
|
3176
|
+
plan.bails.push({ name: plan.rest.name, reason: "shadowed" });
|
|
3177
|
+
delete plan.rest;
|
|
3178
|
+
}
|
|
3179
|
+
return plan;
|
|
3180
|
+
}
|
|
3181
|
+
function explainBail(reason) {
|
|
3182
|
+
switch (reason) {
|
|
3183
|
+
case "rest":
|
|
3184
|
+
return "this rest element cannot be served by splitProps, and copying the props into a plain object would lose the getters";
|
|
3185
|
+
case "computed":
|
|
3186
|
+
return "the property is not known until it runs";
|
|
3187
|
+
case "reassigned":
|
|
3188
|
+
return "the binding is assigned to, and props are read-only";
|
|
3189
|
+
case "shadowed":
|
|
3190
|
+
return "an inner scope binds the same name";
|
|
3191
|
+
case "unsafe-default":
|
|
3192
|
+
return "the default would have to run again on every read";
|
|
3193
|
+
}
|
|
3194
|
+
}
|
|
3195
|
+
|
|
3196
|
+
// src/transform/props.ts
|
|
3197
|
+
function walk5(root, visit, parent = null) {
|
|
3198
|
+
if (!root || typeof root !== "object" || typeof root.type !== "string") return;
|
|
3199
|
+
if (root.type.startsWith("TS")) return;
|
|
3200
|
+
if (visit(root, parent) === false) return;
|
|
3201
|
+
for (const key of Object.keys(root)) {
|
|
3202
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
3203
|
+
const value = root[key];
|
|
3204
|
+
if (Array.isArray(value)) {
|
|
3205
|
+
for (const item of value) walk5(item, visit, root);
|
|
3206
|
+
} else if (value && typeof value === "object") {
|
|
3207
|
+
walk5(value, visit, root);
|
|
3208
|
+
}
|
|
3209
|
+
}
|
|
3210
|
+
}
|
|
3211
|
+
function isComponentName(name) {
|
|
3212
|
+
return !!name && name[0] >= "A" && name[0] <= "Z";
|
|
3213
|
+
}
|
|
3214
|
+
function componentFunctions(program) {
|
|
3215
|
+
const found = [];
|
|
3216
|
+
walk5(program, (node) => {
|
|
3217
|
+
if (node.type === "FunctionDeclaration" && isComponentName(node.id?.name)) {
|
|
3218
|
+
found.push(node);
|
|
3219
|
+
return;
|
|
3220
|
+
}
|
|
3221
|
+
if (node.type === "VariableDeclarator" && isComponentName(node.id?.name)) {
|
|
3222
|
+
const init = node.init;
|
|
3223
|
+
if (init && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression")) found.push(init);
|
|
3224
|
+
}
|
|
3225
|
+
});
|
|
3226
|
+
return found;
|
|
3227
|
+
}
|
|
3228
|
+
function usedNames(fn) {
|
|
3229
|
+
const names = /* @__PURE__ */ new Set();
|
|
3230
|
+
walk5(fn, (node) => {
|
|
3231
|
+
if (node.type === "Identifier") names.add(node.name);
|
|
3232
|
+
});
|
|
3233
|
+
return names;
|
|
3234
|
+
}
|
|
3235
|
+
function isReferencePosition(node, parent) {
|
|
3236
|
+
if (!parent) return true;
|
|
3237
|
+
switch (parent.type) {
|
|
3238
|
+
case "MemberExpression":
|
|
3239
|
+
case "OptionalMemberExpression":
|
|
3240
|
+
return !(parent.property === node && !parent.computed);
|
|
3241
|
+
case "ObjectProperty":
|
|
3242
|
+
case "ObjectMethod":
|
|
3243
|
+
case "ClassProperty":
|
|
3244
|
+
case "ClassMethod":
|
|
3245
|
+
return !(parent.key === node && !parent.computed);
|
|
3246
|
+
case "LabeledStatement":
|
|
3247
|
+
case "BreakStatement":
|
|
3248
|
+
case "ContinueStatement":
|
|
3249
|
+
return parent.label !== node;
|
|
3250
|
+
case "ImportSpecifier":
|
|
3251
|
+
case "ExportSpecifier":
|
|
3252
|
+
return false;
|
|
3253
|
+
default:
|
|
3254
|
+
return true;
|
|
3255
|
+
}
|
|
3256
|
+
}
|
|
3257
|
+
function collectPropsEdits(program, code, options = {}) {
|
|
3258
|
+
const edits = [];
|
|
3259
|
+
const diagnostics = [];
|
|
3260
|
+
const used = /* @__PURE__ */ new Set();
|
|
3261
|
+
for (const fn of componentFunctions(program)) {
|
|
3262
|
+
const pattern = fn.params?.[0];
|
|
3263
|
+
if (pattern?.type !== "ObjectPattern") continue;
|
|
3264
|
+
const plan = planPropsDestructure(pattern, fn.body);
|
|
3265
|
+
for (const b of plan.bails) {
|
|
3266
|
+
diagnostics.push({ name: b.name, reason: b.reason, message: explainBail(b.reason), start: pattern.start });
|
|
3267
|
+
}
|
|
3268
|
+
if (plan.bails.length > 0) continue;
|
|
3269
|
+
if (plan.reads.length === 0 && !plan.rest) continue;
|
|
3270
|
+
const taken = usedNames(fn);
|
|
3271
|
+
let parameter = options.parameterName ?? "props";
|
|
3272
|
+
while (taken.has(parameter)) parameter = `_${parameter}`;
|
|
3273
|
+
const replacement = /* @__PURE__ */ new Map();
|
|
3274
|
+
for (const read of plan.reads) {
|
|
3275
|
+
const access = `${parameter}.${read.path.join(".")}`;
|
|
3276
|
+
const fallback = read.fallback;
|
|
3277
|
+
replacement.set(
|
|
3278
|
+
read.name,
|
|
3279
|
+
fallback ? `(${access} === undefined ? ${code.slice(fallback.start, fallback.end)} : ${access})` : access
|
|
3280
|
+
);
|
|
3281
|
+
}
|
|
3282
|
+
const annotation = pattern.typeAnnotation;
|
|
3283
|
+
edits.push({ start: pattern.start, end: annotation?.start ?? pattern.end, code: parameter });
|
|
3284
|
+
if (plan.rest) {
|
|
3285
|
+
const keys = plan.rest.keys.map((k) => JSON.stringify(k)).join(", ");
|
|
3286
|
+
const body = fn.body;
|
|
3287
|
+
edits.push({
|
|
3288
|
+
start: body.start + 1,
|
|
3289
|
+
end: body.start + 1,
|
|
3290
|
+
code: `
|
|
3291
|
+
const [, ${plan.rest.name}] = splitProps(${parameter}, [${keys}]);`
|
|
3292
|
+
});
|
|
3293
|
+
used.add("splitProps");
|
|
3294
|
+
}
|
|
3295
|
+
const matches = [];
|
|
3296
|
+
walk5(fn.body, (node, parent) => {
|
|
3297
|
+
if (node.type !== "Identifier") return void 0;
|
|
3298
|
+
const text = replacement.get(node.name);
|
|
3299
|
+
if (!text) return void 0;
|
|
3300
|
+
const shorthand = parent?.type === "ObjectProperty" && parent.shorthand && parent.value === node;
|
|
3301
|
+
if (!shorthand && !isReferencePosition(node, parent)) return void 0;
|
|
3302
|
+
const path = plan.reads.find((r) => r.name === node.name).path;
|
|
3303
|
+
matches.push({ node, parent: shorthand ? parent : null, text, property: path[path.length - 1] });
|
|
3304
|
+
return void 0;
|
|
3305
|
+
});
|
|
3306
|
+
for (const match of matches) {
|
|
3307
|
+
if (match.parent) {
|
|
3308
|
+
edits.push({ start: match.parent.start, end: match.parent.end, code: `${match.node.name}: ${match.text}` });
|
|
3309
|
+
} else {
|
|
3310
|
+
edits.push({ start: match.node.start, end: match.node.end, code: match.text });
|
|
3311
|
+
}
|
|
3312
|
+
asMemberRead(match.node, parameter, match.property);
|
|
3313
|
+
}
|
|
3314
|
+
}
|
|
3315
|
+
return { edits, diagnostics, used };
|
|
3316
|
+
}
|
|
3317
|
+
function asMemberRead(node, parameter, property) {
|
|
3318
|
+
node.type = "MemberExpression";
|
|
3319
|
+
node.computed = false;
|
|
3320
|
+
node.optional = false;
|
|
3321
|
+
node.object = { type: "Identifier", name: parameter, start: node.start, end: node.start };
|
|
3322
|
+
node.property = { type: "Identifier", name: property, start: node.end, end: node.end };
|
|
3323
|
+
delete node["name"];
|
|
3324
|
+
}
|
|
3325
|
+
function transformProps(code, filename = "module.tsx", options = {}) {
|
|
3326
|
+
const ast = parse(code, {
|
|
3327
|
+
sourceType: "module",
|
|
3328
|
+
plugins: ["typescript", "jsx"],
|
|
3329
|
+
errorRecovery: true
|
|
3330
|
+
});
|
|
3331
|
+
const { edits, diagnostics, used } = collectPropsEdits(ast.program, code, options);
|
|
3332
|
+
const source = new MagicString(code);
|
|
3333
|
+
for (const e of edits) {
|
|
3334
|
+
if (e.start === e.end) source.appendLeft(e.start, e.code);
|
|
3335
|
+
else source.overwrite(e.start, e.end, e.code);
|
|
3336
|
+
}
|
|
3337
|
+
if (used.has("splitProps") && !moduleBindings(ast.program).has("splitProps")) {
|
|
3338
|
+
source.prepend(`import { splitProps } from ${JSON.stringify(options.runtimeModule ?? "@fluixi/dom")};
|
|
3339
|
+
`);
|
|
3340
|
+
}
|
|
3341
|
+
return {
|
|
3342
|
+
code: source.toString(),
|
|
3343
|
+
map: source.generateMap({ source: filename, includeContent: true, hires: true }),
|
|
3344
|
+
diagnostics,
|
|
3345
|
+
rewritten: edits.filter((e) => e.code !== (options.parameterName ?? "props")).length
|
|
3346
|
+
};
|
|
3347
|
+
}
|
|
3348
|
+
|
|
3349
|
+
// src/transform/intrinsics.ts
|
|
3350
|
+
function walk6(root, visit) {
|
|
3351
|
+
if (!root || typeof root !== "object" || typeof root.type !== "string") return;
|
|
3352
|
+
visit(root);
|
|
3353
|
+
for (const key of Object.keys(root)) {
|
|
3354
|
+
if (key === "loc" || key === "leadingComments" || key === "trailingComments") continue;
|
|
3355
|
+
const value = root[key];
|
|
3356
|
+
if (Array.isArray(value)) for (const item of value) walk6(item, visit);
|
|
3357
|
+
else if (value && typeof value === "object") walk6(value, visit);
|
|
3358
|
+
}
|
|
3359
|
+
}
|
|
3360
|
+
function collectIntrinsicEdits(program) {
|
|
3361
|
+
const edits = [];
|
|
3362
|
+
const diagnostics = [];
|
|
3363
|
+
const imports = /* @__PURE__ */ new Map();
|
|
3364
|
+
const reported = /* @__PURE__ */ new Set();
|
|
3365
|
+
const { shadowed } = resolveReactiveBindings(program);
|
|
3366
|
+
walk6(program, (node) => {
|
|
3367
|
+
if (node.type !== "CallExpression") return;
|
|
3368
|
+
const callee = node.callee;
|
|
3369
|
+
if (callee?.type !== "Identifier") return;
|
|
3370
|
+
const name = callee.name;
|
|
3371
|
+
if (!isIntrinsicName(name)) return;
|
|
3372
|
+
if (shadowed.has(name)) {
|
|
3373
|
+
if (!reported.has(name)) {
|
|
3374
|
+
reported.add(name);
|
|
3375
|
+
diagnostics.push({
|
|
3376
|
+
name,
|
|
3377
|
+
message: `${name} is reserved for the compiler, and this module binds it. The call keeps your binding's meaning; rename it to use the intrinsic.`,
|
|
3378
|
+
start: callee.start
|
|
3379
|
+
});
|
|
3380
|
+
}
|
|
3381
|
+
return;
|
|
3382
|
+
}
|
|
3383
|
+
const intrinsic = INTRINSICS[name];
|
|
3384
|
+
edits.push({ start: callee.start, end: callee.end, code: intrinsic.export });
|
|
3385
|
+
const forModule = imports.get(intrinsic.module) ?? /* @__PURE__ */ new Set();
|
|
3386
|
+
forModule.add(intrinsic.export);
|
|
3387
|
+
imports.set(intrinsic.module, forModule);
|
|
3388
|
+
});
|
|
3389
|
+
return { edits, diagnostics, imports };
|
|
3390
|
+
}
|
|
3391
|
+
function intrinsicImports(result, bound) {
|
|
3392
|
+
const lines = [];
|
|
3393
|
+
for (const [module, names] of [...result.imports].sort(([a], [b]) => a.localeCompare(b))) {
|
|
3394
|
+
const wanted = [...names].filter((name) => !bound.has(name)).sort();
|
|
3395
|
+
if (wanted.length) lines.push(`import { ${wanted.join(", ")} } from ${JSON.stringify(module)};`);
|
|
3396
|
+
}
|
|
3397
|
+
return lines.length ? `${lines.join("\n")}
|
|
3398
|
+
` : "";
|
|
3399
|
+
}
|
|
3400
|
+
|
|
3401
|
+
// src/transform/templates.ts
|
|
3402
|
+
var CONTROL_FLOW = new Set(DOM_CONTROL_FLOW);
|
|
3403
|
+
var CORE_ONLY = new Set(CORE_CONTROL_FLOW);
|
|
3404
|
+
var ROUTER = new Set(ROUTER_COMPONENTS);
|
|
3405
|
+
var REACTIVE = /* @__PURE__ */ new Set([
|
|
3406
|
+
"createMemo",
|
|
3407
|
+
"createEffect",
|
|
3408
|
+
"createRenderEffect",
|
|
3409
|
+
"createRoot",
|
|
3410
|
+
"createSignal",
|
|
3411
|
+
"onCleanup",
|
|
3412
|
+
"batch",
|
|
3413
|
+
"untrack"
|
|
3414
|
+
]);
|
|
3415
|
+
function walk7(node, visit, parent = null, key = "") {
|
|
3416
|
+
if (!node || typeof node !== "object") return;
|
|
3417
|
+
if (Array.isArray(node)) {
|
|
3418
|
+
for (const child of node) walk7(child, visit, parent, key);
|
|
3419
|
+
return;
|
|
3420
|
+
}
|
|
3421
|
+
if (typeof node.type === "string") visit(node, parent, key);
|
|
3422
|
+
for (const k of Object.keys(node)) {
|
|
3423
|
+
if (k === "loc" || k === "leadingComments" || k === "trailingComments") continue;
|
|
3424
|
+
walk7(node[k], visit, typeof node.type === "string" ? node : parent, k);
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
var isJsx = (node) => node.type === "JSXElement" || node.type === "JSXFragment";
|
|
3428
|
+
function isJsxRoot(node, parent, key) {
|
|
3429
|
+
if (!isJsx(node)) return false;
|
|
3430
|
+
return !(parent && isJsx(parent) && key === "children");
|
|
3431
|
+
}
|
|
3432
|
+
function isTemplateTag(node, litTag) {
|
|
3433
|
+
return node.type === "TaggedTemplateExpression" && node.tag?.type === "Identifier" && (node.tag.name === litTag || node.tag.name === "svg");
|
|
3434
|
+
}
|
|
3435
|
+
function outermost(list) {
|
|
3436
|
+
return list.filter(
|
|
3437
|
+
(e) => !list.some(
|
|
3438
|
+
(o) => o !== e && o.start <= e.start && o.end >= e.end && o.end - o.start > e.end - e.start && // An insertion at either edge of the container sits beside it, not inside it, so
|
|
3439
|
+
// it is not something the container's code already folded in. A statement can
|
|
3440
|
+
// start where its callee starts (`$effect(…)`) and a declarator can end where its
|
|
3441
|
+
// JSX ends (`const Row = () => <b/>`).
|
|
3442
|
+
!(e.start === e.end && (e.start === o.start || e.start === o.end))
|
|
3443
|
+
)
|
|
3444
|
+
);
|
|
3445
|
+
}
|
|
3446
|
+
function slice(code, start, end, edits) {
|
|
3447
|
+
const inside = outermost(edits.filter((e) => e.start >= start && e.end <= end)).sort((a, b) => b.start - a.start);
|
|
3448
|
+
let out = code.slice(start, end);
|
|
3449
|
+
for (const e of inside) out = out.slice(0, e.start - start) + e.code + out.slice(e.end - start);
|
|
3450
|
+
return out;
|
|
3451
|
+
}
|
|
3452
|
+
function bindHole(code, expr, edits, keepPosition = false) {
|
|
3453
|
+
const at = keepPosition ? expr.loc?.start : void 0;
|
|
3454
|
+
const hole = {
|
|
3455
|
+
code: slice(code, expr.start, expr.end, edits),
|
|
3456
|
+
reactive: isReactiveShape(shapeOf(expr)),
|
|
3457
|
+
// Where the `${…}` is, so a binding made from it can say so. The source map cannot:
|
|
3458
|
+
// the whole template is one overwrite. Only when source locations were asked for.
|
|
3459
|
+
...at ? { at: { line: at.line, column: at.column } } : {}
|
|
3460
|
+
};
|
|
3461
|
+
if (expr.type === "ArrowFunctionExpression" && expr.body?.type !== "BlockStatement") {
|
|
3462
|
+
hole.arrow = {
|
|
3463
|
+
params: expr.params.map((p) => slice(code, p.start, p.end, edits)),
|
|
3464
|
+
body: slice(code, expr.body.start, expr.body.end, edits),
|
|
3465
|
+
bodyReactive: isReactiveShape(shapeOf(expr.body))
|
|
3466
|
+
};
|
|
3467
|
+
}
|
|
3468
|
+
if (expr.type === "ObjectExpression") hole.object = true;
|
|
3469
|
+
return hole;
|
|
3470
|
+
}
|
|
3471
|
+
function adoptTemplates(code, templates, hoisted) {
|
|
3472
|
+
if (!templates || templates.length === 0) return code;
|
|
3473
|
+
const rename = /* @__PURE__ */ new Map();
|
|
3474
|
+
for (const tpl of templates) {
|
|
3475
|
+
const key = `${tpl.svg ? "svg:" : ""}${tpl.html}`;
|
|
3476
|
+
let name = hoisted.get(key);
|
|
3477
|
+
if (!name) {
|
|
3478
|
+
name = `_fxTmpl$${hoisted.size}`;
|
|
3479
|
+
hoisted.set(key, name);
|
|
3480
|
+
}
|
|
3481
|
+
rename.set(tpl.id, name);
|
|
3482
|
+
}
|
|
3483
|
+
return code.replace(/_tmpl\$\d+/g, (id) => rename.get(id) ?? id);
|
|
3484
|
+
}
|
|
3485
|
+
function collectNeeds(node, out) {
|
|
3486
|
+
if (node.kind === "component" && node.load && !node.source && isDeferred(node.load)) {
|
|
3487
|
+
if (!out.ignoredLoad.has(node.name)) out.ignoredLoad.set(node.name, node.load);
|
|
3488
|
+
}
|
|
3489
|
+
if (node.kind === "component" && node.source) {
|
|
3490
|
+
const src = node.source;
|
|
3491
|
+
if (src.origin === "builtin") out.builtins.add(node.name);
|
|
3492
|
+
else if (isDeferred(src.loading) && !out.resolved.has(node.name)) out.deferred.set(node.name, src);
|
|
3493
|
+
else {
|
|
3494
|
+
out.deferred.delete(node.name);
|
|
3495
|
+
out.resolved.set(node.name, src);
|
|
3496
|
+
}
|
|
3497
|
+
}
|
|
3498
|
+
if ("props" in node && node.props) {
|
|
3499
|
+
for (const p of node.props) {
|
|
3500
|
+
if (p.kind === "event" && p.event?.delegated) out.delegatedEvents.add(p.event.name);
|
|
3501
|
+
}
|
|
3502
|
+
}
|
|
3503
|
+
if ("children" in node && node.children) for (const c of node.children) collectNeeds(c, out);
|
|
3504
|
+
}
|
|
3505
|
+
function deferrableImport(program, name) {
|
|
3506
|
+
for (const statement of program.body ?? []) {
|
|
3507
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
3508
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
3509
|
+
if (specifier.local?.name !== name) continue;
|
|
3510
|
+
if (specifier.type === "ImportNamespaceSpecifier") return void 0;
|
|
3511
|
+
const exported = specifier.type === "ImportDefaultSpecifier" ? "default" : specifier.imported?.name ?? specifier.imported?.value;
|
|
3512
|
+
return { module: statement.source.value, export: exported, declaration: statement, specifier };
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
return void 0;
|
|
3516
|
+
}
|
|
3517
|
+
function hasNamespaceImport(program, name) {
|
|
3518
|
+
for (const statement of program.body ?? []) {
|
|
3519
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
3520
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
3521
|
+
if (specifier.type === "ImportNamespaceSpecifier" && specifier.local?.name === name) return true;
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
return false;
|
|
3525
|
+
}
|
|
3526
|
+
function usedAsValue(program, name, specifier) {
|
|
3527
|
+
let seen = false;
|
|
3528
|
+
walk7(program, (node) => {
|
|
3529
|
+
if (seen || node.type !== "Identifier" || node.name !== name) return;
|
|
3530
|
+
if (node.start === specifier.local?.start && node.end === specifier.local?.end) return;
|
|
3531
|
+
seen = true;
|
|
3532
|
+
});
|
|
3533
|
+
return seen;
|
|
3534
|
+
}
|
|
3535
|
+
function strategyLiteral(s) {
|
|
3536
|
+
const parts = [`kind: ${JSON.stringify(s.kind)}`];
|
|
3537
|
+
if (s.kind === "visible" && s.rootMargin) parts.push(`rootMargin: ${JSON.stringify(s.rootMargin)}`);
|
|
3538
|
+
if (s.kind === "interaction") parts.push(`events: ${JSON.stringify(s.events)}`);
|
|
3539
|
+
if (s.kind === "media") parts.push(`query: ${JSON.stringify(s.query)}`);
|
|
3540
|
+
return `{ ${parts.join(", ")} }`;
|
|
3541
|
+
}
|
|
3542
|
+
function positionsIn(code) {
|
|
3543
|
+
const starts = [0];
|
|
3544
|
+
for (let i = 0; i < code.length; i++) if (code.charCodeAt(i) === 10) starts.push(i + 1);
|
|
3545
|
+
return (offset) => {
|
|
3546
|
+
let low = 0;
|
|
3547
|
+
let high = starts.length - 1;
|
|
3548
|
+
while (low < high) {
|
|
3549
|
+
const mid = low + high + 1 >> 1;
|
|
3550
|
+
if (starts[mid] <= offset) low = mid;
|
|
3551
|
+
else high = mid - 1;
|
|
3552
|
+
}
|
|
3553
|
+
return { line: low + 1, column: offset - starts[low] };
|
|
3554
|
+
};
|
|
3555
|
+
}
|
|
3556
|
+
function toPieces(node) {
|
|
3557
|
+
const pieces = [];
|
|
3558
|
+
node.quasi.quasis.forEach((q, i) => {
|
|
3559
|
+
pieces.push({ kind: "static", text: q.value.cooked ?? q.value.raw, start: q.start });
|
|
3560
|
+
if (i < node.quasi.expressions.length) {
|
|
3561
|
+
const e = node.quasi.expressions[i];
|
|
3562
|
+
pieces.push({ kind: "hole", index: i, start: e.start, end: e.end });
|
|
3563
|
+
}
|
|
3564
|
+
});
|
|
3565
|
+
return pieces;
|
|
3566
|
+
}
|
|
3567
|
+
function relativeToRoot(id, root) {
|
|
3568
|
+
if (!root) return id;
|
|
3569
|
+
const base = root.endsWith("/") ? root : `${root}/`;
|
|
3570
|
+
return id.startsWith(base) ? id.slice(base.length) : id;
|
|
3571
|
+
}
|
|
3572
|
+
function transformTemplates(code, id, options = {}) {
|
|
3573
|
+
const litTag = options.litTag ?? "html";
|
|
3574
|
+
const format = options.format ?? "both";
|
|
3575
|
+
const wantsLit = format !== "jsx";
|
|
3576
|
+
const wantsJsx = format !== "lit";
|
|
3577
|
+
const mayHaveTemplate = wantsLit && (code.includes(`${litTag}\``) || code.includes("svg`"));
|
|
3578
|
+
const wantsIntrinsics = options.intrinsics !== false && mayHaveIntrinsic(code);
|
|
3579
|
+
const wantsSources = options.sourceLocations === true && mayHaveReactiveCall(code);
|
|
3580
|
+
const sourceName = relativeToRoot(id, options.sourceRoot);
|
|
3581
|
+
if (!mayHaveTemplate && !(wantsJsx && code.includes("<")) && !wantsIntrinsics && !wantsSources) return null;
|
|
3582
|
+
const ast = parse2(code, {
|
|
3583
|
+
sourceType: "module",
|
|
3584
|
+
plugins: ["jsx", "typescript"],
|
|
3585
|
+
errorRecovery: true
|
|
3586
|
+
});
|
|
3587
|
+
const found = [];
|
|
3588
|
+
walk7(ast.program, (n2, parent, key) => {
|
|
3589
|
+
if (wantsLit && isTemplateTag(n2, litTag)) found.push(n2);
|
|
3590
|
+
else if (wantsJsx && isJsxRoot(n2, parent, key)) found.push(n2);
|
|
3591
|
+
});
|
|
3592
|
+
const props = options.propsDestructure === false ? { edits: [], diagnostics: [], used: /* @__PURE__ */ new Set() } : collectPropsEdits(ast.program, code, { runtimeModule: options.runtimeModule });
|
|
3593
|
+
const intrinsics = options.intrinsics === false ? { edits: [], diagnostics: [], imports: /* @__PURE__ */ new Map() } : collectIntrinsicEdits(ast.program);
|
|
3594
|
+
const sources = options.sourceLocations === true ? collectSourceEdits(ast.program, sourceName) : { edits: [], marked: 0 };
|
|
3595
|
+
const silent = found.length === 0 && props.edits.length === 0 && sources.edits.length === 0 && intrinsics.edits.length === 0 && intrinsics.diagnostics.length === 0 && props.diagnostics.length === 0;
|
|
3596
|
+
if (silent) return null;
|
|
3597
|
+
found.sort((a, b) => b.start - a.start || a.end - b.end);
|
|
3598
|
+
const used = new Set(props.used);
|
|
3599
|
+
const needs = {
|
|
3600
|
+
builtins: /* @__PURE__ */ new Set(),
|
|
3601
|
+
resolved: /* @__PURE__ */ new Map(),
|
|
3602
|
+
deferred: /* @__PURE__ */ new Map(),
|
|
3603
|
+
delegatedEvents: /* @__PURE__ */ new Set(),
|
|
3604
|
+
ignoredLoad: /* @__PURE__ */ new Map(),
|
|
3605
|
+
unresolved: /* @__PURE__ */ new Set(),
|
|
3606
|
+
declared: declaredNames(ast.program)
|
|
3607
|
+
};
|
|
3608
|
+
const hoisted = /* @__PURE__ */ new Map();
|
|
3609
|
+
const edits = [...props.edits, ...intrinsics.edits, ...sources.edits];
|
|
3610
|
+
for (const node of found) {
|
|
3611
|
+
const nested = found.some(
|
|
3612
|
+
(o) => o !== node && o.start <= node.start && o.end >= node.end && o.end - o.start > node.end - node.start
|
|
3613
|
+
);
|
|
3614
|
+
if (isJsx(node)) {
|
|
3615
|
+
edits.push({ start: node.start, end: node.end, code: compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceName) });
|
|
3616
|
+
continue;
|
|
3617
|
+
}
|
|
3618
|
+
const holes = node.quasi.expressions.map(
|
|
3619
|
+
(e) => bindHole(code, e, edits, options.sourceLocations === true)
|
|
3620
|
+
);
|
|
3621
|
+
const result = compilePieces(toPieces(node), holes, {
|
|
3622
|
+
...options,
|
|
3623
|
+
hoistTemplates: true,
|
|
3624
|
+
// A tag inside a template gets the same mark a JSX tag does. Without one, the nodes it
|
|
3625
|
+
// makes are the only ones in the module carrying no mark, and they take whichever was
|
|
3626
|
+
// left behind by the last declaration that did.
|
|
3627
|
+
...options.sourceLocations === true ? { sourceFile: sourceName, positionAt: positionsIn(code) } : {},
|
|
3628
|
+
// Same defaults as the plugin, so moving a build over is not a rendering change.
|
|
3629
|
+
templateClone: options.templateClone ?? true,
|
|
3630
|
+
partialTemplates: nested ? false : options.partialTemplates ?? true,
|
|
3631
|
+
svg: node.tag.name === "svg"
|
|
3632
|
+
});
|
|
3633
|
+
for (const symbol of result.imports) used.add(symbol);
|
|
3634
|
+
collectNeeds(result.ir, needs);
|
|
3635
|
+
edits.push({ start: node.start, end: node.end, code: adoptTemplates(result.code, result.templates, hoisted) });
|
|
3636
|
+
}
|
|
3637
|
+
const out = new MagicString(code);
|
|
3638
|
+
for (const e of outermost(edits)) {
|
|
3639
|
+
if (e.start === e.end) out.appendLeft(e.start, e.code);
|
|
3640
|
+
else out.overwrite(e.start, e.end, e.code);
|
|
3641
|
+
}
|
|
3642
|
+
const bound = moduleBindings(ast.program);
|
|
3643
|
+
const why = /* @__PURE__ */ new Map();
|
|
3644
|
+
for (const [name] of needs.ignoredLoad) {
|
|
3645
|
+
if (!bound.has(name)) why.set(name, "unsupplied");
|
|
3646
|
+
}
|
|
3647
|
+
for (const [name, strategy] of [...needs.ignoredLoad]) {
|
|
3648
|
+
if (!bound.has(name)) continue;
|
|
3649
|
+
const found2 = deferrableImport(ast.program, name);
|
|
3650
|
+
if (!found2) {
|
|
3651
|
+
why.set(name, hasNamespaceImport(ast.program, name) ? "namespace" : "local");
|
|
3652
|
+
continue;
|
|
3653
|
+
}
|
|
3654
|
+
if (usedAsValue(ast.program, name, found2.specifier)) {
|
|
3655
|
+
why.set(name, "value");
|
|
3656
|
+
continue;
|
|
3657
|
+
}
|
|
3658
|
+
const rest = found2.declaration.specifiers.filter((sp) => sp !== found2.specifier);
|
|
3659
|
+
const end = rest.length === 0 && code[found2.declaration.end] === "\n" ? found2.declaration.end + 1 : found2.declaration.end;
|
|
3660
|
+
out.overwrite(
|
|
3661
|
+
found2.declaration.start,
|
|
3662
|
+
end,
|
|
3663
|
+
rest.length === 0 ? "" : `import { ${rest.map((sp) => code.slice(sp.start, sp.end)).join(", ")} } from ${JSON.stringify(found2.declaration.source.value)};`
|
|
3664
|
+
);
|
|
3665
|
+
needs.deferred.set(name, { module: found2.module, export: found2.export, origin: "rule", loading: strategy });
|
|
3666
|
+
bound.delete(name);
|
|
3667
|
+
needs.ignoredLoad.delete(name);
|
|
3668
|
+
}
|
|
3669
|
+
const prologue = buildPrologue(used, needs, hoisted, bound, options);
|
|
3670
|
+
if (prologue) out.prepend(prologue);
|
|
3671
|
+
const runtimeImports = intrinsicImports(intrinsics, bound);
|
|
3672
|
+
if (runtimeImports) out.prepend(runtimeImports);
|
|
3673
|
+
const reasons = {
|
|
3674
|
+
value: (name) => `${name} is used as a value here, not only as a tag, so its import has to stay — deferring would hand those references a lazy wrapper instead of the component. Defer it at the point it is rendered, or keep it eager.`,
|
|
3675
|
+
namespace: (name) => `${name} came in through a namespace import, which has no single export to defer. Import ${name} by name.`,
|
|
3676
|
+
local: (name) => `${name} is declared in this file, so there is no module to load separately. Move it to its own file and import it.`,
|
|
3677
|
+
unsupplied: (name) => `nothing supplies ${name}, so there is no import to defer. Import it, or add a \`resolve\` rule for it.`
|
|
3678
|
+
};
|
|
3679
|
+
const loadDiagnostics = [...needs.ignoredLoad].map(([name, strategy]) => ({
|
|
3680
|
+
name,
|
|
3681
|
+
strategy,
|
|
3682
|
+
message: `load:${strategy.kind} has no effect on <${name}>: ${reasons[why.get(name) ?? "unsupplied"](name)}`
|
|
3683
|
+
}));
|
|
3684
|
+
const unresolvedDiagnostics = [...needs.unresolved].map((name) => ({
|
|
3685
|
+
name,
|
|
3686
|
+
message: `<${name}> is not defined: nothing imports it and no \`resolve\` rule supplies it. This compiles to a reference to a name that does not exist, so the component renders nothing. Import ${name}, or add a \`resolve\` rule for it.`
|
|
3687
|
+
}));
|
|
3688
|
+
return {
|
|
3689
|
+
code: out.toString(),
|
|
3690
|
+
map: out.generateMap({ source: id, includeContent: true, hires: true }),
|
|
3691
|
+
...loadDiagnostics.length ? { loadDiagnostics } : {},
|
|
3692
|
+
...unresolvedDiagnostics.length ? { unresolvedDiagnostics } : {},
|
|
3693
|
+
...props.diagnostics.length ? { propsDiagnostics: props.diagnostics } : {},
|
|
3694
|
+
...intrinsics.diagnostics.length ? { intrinsicDiagnostics: intrinsics.diagnostics } : {}
|
|
3695
|
+
};
|
|
3696
|
+
}
|
|
3697
|
+
function compileJsx(code, node, edits, nested, used, needs, hoisted, options, sourceFile) {
|
|
3698
|
+
const ir = buildJsxIR(node, {
|
|
3699
|
+
code: (n2) => slice(code, n2.start, n2.end, edits),
|
|
3700
|
+
used,
|
|
3701
|
+
...options.sourceLocations === true && sourceFile ? { sourceFile } : {}
|
|
3702
|
+
});
|
|
3703
|
+
analyzeStatic(ir);
|
|
3704
|
+
const report = resolveComponentSources(ir, {
|
|
3705
|
+
resolver: createComponentResolver(options.resolve ?? []),
|
|
3706
|
+
// Without this the pass calls every author-imported component unresolved, and the
|
|
3707
|
+
// report is unusable for diagnostics.
|
|
3708
|
+
isBound: (name) => needs.declared.has(name),
|
|
3709
|
+
modules: {
|
|
3710
|
+
controlFlowModule: options.controlFlowModule ?? options.runtimeModule ?? "@fluixi/dom",
|
|
3711
|
+
coreModule: options.coreModule ?? "@fluixi/core",
|
|
3712
|
+
routerModule: options.routerModule ?? "@fluixi/core/router"
|
|
3713
|
+
},
|
|
3714
|
+
strategyFor: (n2) => n2.load
|
|
3715
|
+
});
|
|
3716
|
+
for (const name of report.unresolved) needs.unresolved.add(name);
|
|
3717
|
+
collapseStrategies(ir);
|
|
3718
|
+
const emitted = imperativeBackend.emit(ir, {
|
|
3719
|
+
templateClone: options.templateClone ?? true,
|
|
3720
|
+
partialTemplates: nested ? false : options.partialTemplates ?? true
|
|
3721
|
+
});
|
|
3722
|
+
for (const symbol of emitted.imports) used.add(symbol);
|
|
3723
|
+
collectNeeds(ir, needs);
|
|
3724
|
+
return adoptTemplates(emitted.code, emitted.templates, hoisted);
|
|
3725
|
+
}
|
|
3726
|
+
function buildPrologue(used, needs, hoisted, bound, options) {
|
|
3727
|
+
const runtime = options.runtimeModule ?? "@fluixi/dom";
|
|
3728
|
+
const core = options.coreModule ?? "@fluixi/core";
|
|
3729
|
+
const groups = /* @__PURE__ */ new Map();
|
|
3730
|
+
const add = (mod, name) => groups.set(mod, [...groups.get(mod) ?? [], name]);
|
|
3731
|
+
const wanted = new Set(used);
|
|
3732
|
+
for (const name of needs.builtins) wanted.add(name);
|
|
3733
|
+
if (needs.delegatedEvents.size > 0) wanted.add("delegateEvents");
|
|
3734
|
+
for (const name of [...wanted].sort()) {
|
|
3735
|
+
if (bound.has(name)) continue;
|
|
3736
|
+
if (REACTIVE.has(name)) add(options.reactiveModule ?? "@fluixi/reactive/signal", name);
|
|
3737
|
+
else if (ROUTER.has(name)) add(options.routerModule ?? "@fluixi/core/router", name);
|
|
3738
|
+
else if (CORE_ONLY.has(name)) add(core, name);
|
|
3739
|
+
else if (CONTROL_FLOW.has(name)) add(options.controlFlowModule ?? runtime, name);
|
|
3740
|
+
else add(runtime, name);
|
|
3741
|
+
}
|
|
3742
|
+
for (const [name, src] of needs.resolved) {
|
|
3743
|
+
if (!bound.has(name)) add(src.module, src.export === "default" ? `default as ${name}` : name === src.export ? name : `${src.export} as ${name}`);
|
|
3744
|
+
}
|
|
3745
|
+
const lines = [];
|
|
3746
|
+
for (const [mod, names] of groups) {
|
|
3747
|
+
lines.push(`import { ${names.join(", ")} } from ${JSON.stringify(mod)};`);
|
|
3748
|
+
}
|
|
3749
|
+
const lazy = [...needs.deferred].filter(([name]) => !bound.has(name));
|
|
3750
|
+
if (lazy.length > 0) {
|
|
3751
|
+
if (!bound.has("deferred")) lines.push(`import { deferred } from ${JSON.stringify(core)};`);
|
|
3752
|
+
for (const [name, src] of lazy) {
|
|
3753
|
+
lines.push(
|
|
3754
|
+
`const ${name} = deferred(() => import(${JSON.stringify(src.module)}), { strategy: ${strategyLiteral(src.loading)}, export: ${JSON.stringify(src.export)} });`
|
|
3755
|
+
);
|
|
3756
|
+
}
|
|
3757
|
+
}
|
|
3758
|
+
for (const [key, name] of hoisted) {
|
|
3759
|
+
lines.push(`const ${name} = ${JSON.stringify(key.startsWith("svg:") ? key.slice(4) : key)};`);
|
|
3760
|
+
}
|
|
3761
|
+
if (needs.delegatedEvents.size > 0) {
|
|
3762
|
+
lines.push(`delegateEvents(${JSON.stringify([...needs.delegatedEvents])});`);
|
|
3763
|
+
}
|
|
3764
|
+
return lines.length > 0 ? lines.join("\n") + "\n" : null;
|
|
3765
|
+
}
|
|
3766
|
+
export {
|
|
3767
|
+
explainBail,
|
|
3768
|
+
moduleBindings,
|
|
3769
|
+
planPropsDestructure,
|
|
3770
|
+
shapeOf,
|
|
3771
|
+
transformProps,
|
|
3772
|
+
transformTemplates
|
|
3773
|
+
};
|