xhs-mp-sketch-loader 2.0.16 → 2.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("./consts.js"),t=require("xhs-mp-pack"),r=require("xhs-mp-compiler-utils"),n=require("xhs-mp-ml-parser"),i=require("lodash");class o extends n.BaseValidator{constructor(e,t){super(),this.source=e,this.filePath=t}validateBase(e){if(n.isImport(e)||n.isTemplateConsume(e)||n.isTemplateDefine(e))throw new t.ParserError(e.start,"骨架文件:不支持 template、import 标签");if(n.isInclude(e))throw new t.ParserError(e.start,"骨架文件:不支持 include 标签");if(n.isSlotGet(e)||n.isSlotPass(e))throw new t.ParserError(e.start,"骨架文件:不支持 插槽 语法");if(n.isSJS(e))throw new t.ParserError(e.start,"骨架文件:不支持 sjs 标签")}validate(e){var t;this.validateBase(e),n.isLoopNode(e)&&this.validateLoop(e),n.isConditionNode(e)&&this.validateCondition(e),e.is(n.ElementType.Element)&&"block"===e.name&&n.
|
|
1
|
+
"use strict";var e=require("./consts.js"),t=require("xhs-mp-pack"),r=require("xhs-mp-compiler-utils"),n=require("xhs-mp-ml-parser"),i=require("lodash");class o extends n.BaseValidator{constructor(e,t){super(),this.source=e,this.filePath=t}validateBase(e){if(n.isImport(e)||n.isTemplateConsume(e)||n.isTemplateDefine(e))throw new t.ParserError(e.start,"骨架文件:不支持 template、import 标签");if(n.isInclude(e))throw new t.ParserError(e.start,"骨架文件:不支持 include 标签");if(n.isSlotGet(e)||n.isSlotPass(e))throw new t.ParserError(e.start,"骨架文件:不支持 插槽 语法");if(n.isSJS(e))throw new t.ParserError(e.start,"骨架文件:不支持 sjs 标签")}validate(e){var t;this.validateBase(e),n.isLoopNode(e)&&this.validateLoop(e),n.isConditionNode(e)&&this.validateCondition(e),e.is(n.ElementType.Element)&&"block"===e.name&&n.fixBlock(e),e.children.length&&(e.children.forEach((e=>{this.validate(e)})),(null===(t=e.children)||void 0===t?void 0:t.some(n.isConditionNode))&&this.validateConditionStack(e.children))}}class s{static addExpr(t){const r=i.last(s._list);if(r){r.exprs.push(t);const n=r.exprs.length-1;return t.loopContext.length?`${e.EXPR_TRY_CATCH}(${n}, { ${t.loopContext.join(",")}} )`:`${e.EXPR_TRY_CATCH}(${n})`}return""}static renderExpr(){var t;const r=i.last(s._list);if(!(null===(t=null==r?void 0:r.exprs)||void 0===t?void 0:t.length))return"";const n=r.exprs.map(((t,r)=>{let n="";return n=t.loopContext.length?`{\n const { ${t.loopContext.join(",")} } = ${e.EXPR_TRY_CATCH_CTX}\n return ${t.value};\n }\n `:`{ return ${t.value}; }`,{code:n,idx:r}})),o=i.groupBy(n,"code"),l=Object.keys(o).map((e=>`${o[e].map((({idx:e})=>`case ${e}:`)).join("")}${e}`)).join("\n");return`\n const ${e.EXPR_TRY_CATCH} = (${e.EXPR_TRY_CATCH_INDEX}, ${e.EXPR_TRY_CATCH_CTX}) => {\n try{\n switch(${e.EXPR_TRY_CATCH_INDEX}) {\n ${l} \n }\n }catch(error){\n // console.warn(\`[xhs warning]: \${error.message}\`)\n }\n }\n `}static addHelper(e){const t=i.last(s._list);t&&(t[e]=!0)}static renderListHelper(){const t=i.last(s._list);return(null==t?void 0:t[e.HELPERS.RENDER_LIST])?`const ${e.HELPERS.RENDER_LIST} = (list, renderItem) => {\n if(Array.isArray(list)){ return list.map(renderItem).filter(Boolean) }\n if(typeof list === 'object' && list != null ){ return Object.entries(list).map(([k, v]) => renderItem(v, k)).filter(Boolean) } \n if(typeof list === 'string'){ return list.split("").map(renderItem).filter(Boolean) } \n return []\n }`:""}static renderElementHelper(){const t=i.last(s._list);return(null==t?void 0:t[e.HELPERS.RENDER_ELEMENT])?(s.addHelper(e.HELPERS.FLAT_CHILDREN),`const ${e.HELPERS.RENDER_ELEMENT} = (tagName, attributes, childNodes) => ({ type: ${e.NodeType.Element}, tagName, attributes, childNodes: ${e.HELPERS.FLAT_CHILDREN}(childNodes) })`):""}static renderTextHelper(){const t=i.last(s._list);return(null==t?void 0:t[e.HELPERS.RENDER_TEXT])?`const ${e.HELPERS.RENDER_TEXT} = (textContent) => ({ type: ${e.NodeType.Text} , textContent })`:""}static renderCommentHelper(){const t=i.last(s._list);return(null==t?void 0:t[e.HELPERS.RENDER_COMMENT])?`const ${e.HELPERS.RENDER_COMMENT} = (textContent) => ({ type: ${e.NodeType.Comment} , textContent })`:""}static renderFlatChildrenHelper(){const t=i.last(s._list);if(null==t?void 0:t[e.HELPERS.FLAT_CHILDREN])return`function ${e.HELPERS.FLAT_CHILDREN}(l) { return Array.isArray(l) ? l.flat(Infinity).filter(Boolean) : [] }`}static startHoisting(){s._list.push({exprs:[],[e.HELPERS.RENDER_LIST]:!1,[e.HELPERS.RENDER_ELEMENT]:!1,[e.HELPERS.RENDER_TEXT]:!1,[e.HELPERS.RENDER_COMMENT]:!1,[e.HELPERS.FLAT_CHILDREN]:!1})}static popHoisting(){const e=[s.renderExpr(),s.renderCommentHelper(),s.renderTextHelper(),s.renderElementHelper(),s.renderListHelper(),s.renderFlatChildrenHelper()].filter(Boolean).join("\n");return s._list.pop(),e}static clear(){s._list=[]}}s._list=[];const l=(r,i,o,l)=>{try{if("expression"===o||"text"===o){return("expression"===o?r.trim():r).split(/({{.*?}})/).filter(Boolean).map((t=>{var r,l;if(null===(r=n.matchMustache(t))||void 0===r?void 0:r.length){const r=t.replace(/{{(.*?)}}/g,((t,r)=>((t,r)=>{let i=!1;const o=new Set,l=n.parseExpression(t).map((t=>{var s,l;if("string"==typeof t)return t;const a=null===(s=t.source)||void 0===s?void 0:s.trim();return n.LiteralInML.LITERALS.includes(a)?a:n.LiteralInML.GLOBALS.includes(a)?(i=!0,"({})"):(null===(l=r.loopContext)||void 0===l?void 0:l.includes(a))?(i=!0,o.add(a),a):(i=!0,`${e.SKETCH_CTX}.${a}`)})).join("");return i?s.addExpr({value:l,loopContext:[...o].sort()}):l})(r,i)));return`(${r})`}return"expression"===o?JSON.stringify(t):(l=t).includes("&")?JSON.stringify(n.decodeHtmlEntity(l,!1)):JSON.stringify(l)})).join("+")}}catch(e){const o=l instanceof n.XhsAttr?l.name:"";let s=e.message+` in \`${r}\``;throw o?(s=`${s} at the \`${o}\` attribute`,i instanceof n.XhsElement&&(s=`${s} of the ${i.name} tag`)):l instanceof n.XhsText&&(s=`${s} at text block`),new t.ParserError((null==l?void 0:l.start)||(null==i?void 0:i.start)||0,s)}throw new Error(`${r} 未实现模式${o}`)};function a(e,t,r,i="expression"){var o;return(null===(o=n.matchMustache(t))||void 0===o?void 0:o.length)?l(t,e,i,r)||"":JSON.stringify(t)}class E{constructor(e){var t;this.filePath="",this.shortPath="",this.removeComentNode=!0,this.filePath=e.filePath,this.removeComentNode=null===(t=e.removeComentNode)||void 0===t||t,this.ast=e.ast}handleAttr(e){const t=[];return Object.values(e.attrsMap).forEach((r=>{const{name:i,value:o}=r;n.XhsOperations.includes(i)||("boolean"!=typeof o?t.push(`${JSON.stringify(i)}: ${a(e,o,r)}`):t.push(`${JSON.stringify(i)}: ${o}`))})),`{ ${t.filter(Boolean).join(",")} }`}renderFor(t){const{forBody:r,forItem:i,forIndex:o,forKey:l,forBodyAttr:E,forKeyAttr:d}=n.getLoop(t),c=a(t,r,E),p=i||"item",h=o||"index";if(n.isDefined(l)){let e="";e=l===p?p:l===h?h:(null==l?void 0:l.startsWith(`${p}.`))?l:["*this","*item",void 0].includes(l)?p:n.matchMustache(l)?l||'""':`${p}["${l}"]`,d&&(d.value=e)}return t.loopContext=[...t.loopContext,p,h],n.broadcastContext({nodes:t.children,loopContext:[p,h]}),s.addHelper(e.HELPERS.RENDER_LIST),`${e.HELPERS.RENDER_LIST}(\n ${c},\n (${p}, ${h}) => ${t.operation["xhs:if"]?this.renderIf(t):this.renderElement(t)}\n)`}renderIf(e){var t,r,i;const o=[e],s=null===(r=null===(t=e.parent)||void 0===t?void 0:t.children)||void 0===r?void 0:r.findIndex((t=>t===e));if(void 0!==s){const t=null===(i=e.parent)||void 0===i?void 0:i.children;if(t)for(let e=s+1;e<t.length;e++){const r=t[e];if(r.operation["xhs:elif"])o.push(r);else if(!r.is(n.ElementType.COMMENT)){if(r.operation["xhs:else"]){o.push(r);break}break}}}const l=!o.find((e=>e.operation["xhs:else"]));let E="";return o.forEach((t=>{const{ifState:r,elifState:i,elseState:o}=n.getCondition(t);if(n.isDefined(r)||n.isDefined(i)){const n=a(e,`${r||i}`,e.operation["xhs:if"]||e.operation["xhs:elif"]);E+=`${n} ? ${this.renderElement(t)} :`}else o&&(E+=this.renderElement(t))})),l&&(E+="null"),E}renderElement(t){const r=t.children.map((e=>this.renderNode(e))).filter(Boolean).join(",");let i=n.tagTransformMap[t.name]?`xhs-${t.name}`:t.name;return s.addHelper(e.HELPERS.RENDER_ELEMENT),`${e.HELPERS.RENDER_ELEMENT}('${i}', ${this.handleAttr(t)}, [${r}])`}renderNode(t){if(t.is(n.ElementType.COMMENT)&&!this.removeComentNode)return s.addHelper(e.HELPERS.RENDER_COMMENT),`${e.HELPERS.RENDER_COMMENT}(${JSON.stringify(t.content)})`;if(t.is(n.ElementType.Element)){if(i.isEmpty(t.operation))return this.renderElement(t);if(t.operation["xhs:for"])return this.renderFor(t);if(t.operation["xhs:if"])return this.renderIf(t)}else if(t.is(n.ElementType.TEXT)){const r=a(t,t.content,t,"text");return s.addHelper(e.HELPERS.RENDER_TEXT),`${e.HELPERS.RENDER_TEXT}(${r})`}return""}renderSketch(){s.startHoisting(),s.addHelper(e.HELPERS.FLAT_CHILDREN);const t=this.ast.children.map((e=>this.renderNode(e))).filter(Boolean);return`function(${e.SKETCH_CTX}){\n ${s.popHoisting()}\n return ${e.HELPERS.FLAT_CHILDREN}([${t.join(",")}]);\n }`}}module.exports=function(i){return e.__awaiter(this,void 0,void 0,(function*(){const e=this.async();try{const{resourcePath:l,_compiler:a,resourceQuery:d}=this,{miniprogramDir:c,appJSON:p={}}=(null==a?void 0:a.project)||{},h=r.getShortPath(c,l);t.initErrorContext(i,l),s.clear();const u=n.parseML(i,l);new o(i,l).validate(u);const f=new E({ast:u,filePath:l,shortPath:h});e(null,`export default ${f.renderSketch()}`)}catch(t){e(t)}}))};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xhs-mp-sketch-loader",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.17",
|
|
4
4
|
"description": "sketch loader",
|
|
5
5
|
"category": "esm",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@swc/core": "^1.3.83",
|
|
31
31
|
"webpack": "^5.88.2",
|
|
32
|
-
"xhs-mp-pack": "2.0.
|
|
33
|
-
"xhs-mp-compiler-utils": "2.0.
|
|
34
|
-
"xhs-mp-shared": "2.0.
|
|
35
|
-
"xhs-mp-ml-parser": "2.0.
|
|
32
|
+
"xhs-mp-pack": "2.0.17",
|
|
33
|
+
"xhs-mp-compiler-utils": "2.0.17",
|
|
34
|
+
"xhs-mp-shared": "2.0.17",
|
|
35
|
+
"xhs-mp-ml-parser": "2.0.17"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@rollup/plugin-commonjs": "^25.0.7",
|
|
@@ -53,10 +53,10 @@
|
|
|
53
53
|
"tslib": "^2.6.2",
|
|
54
54
|
"typescript": "^5.4.3",
|
|
55
55
|
"webpack": "^5.88.2",
|
|
56
|
-
"xhs-mp-pack": "2.0.
|
|
57
|
-
"xhs-mp-compiler-utils": "2.0.
|
|
58
|
-
"xhs-mp-shared": "2.0.
|
|
59
|
-
"xhs-mp-ml-parser": "2.0.
|
|
56
|
+
"xhs-mp-pack": "2.0.17",
|
|
57
|
+
"xhs-mp-compiler-utils": "2.0.17",
|
|
58
|
+
"xhs-mp-shared": "2.0.17",
|
|
59
|
+
"xhs-mp-ml-parser": "2.0.17"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"watch": "npm run prebuild && rollup -c -w --sourcemap true",
|