const { spawnSync } = require("child_process"); const fs = require("fs"); const path = require("path"); const nfs = require("fs"); const os = require("os"); const DEBUG_OUTPUT = process.env["WX_DEBUG_COMPILER_OUTPUT"]; function parseDeps(source, x, pageConfig, funcName) { // 插入引用信息 let dep_x = `var x=['`; let dep_gz = ""; let dep_d_ = ""; let t_x = []; if (funcName !== "$gwx") { // TODO: 可能存在自定义目录无法识别的问题? // 非标准函数名 t_x = x.filter( (ele) => ele.startsWith("./miniprogram_npm") || ele.startsWith("../") || ele.startsWith("./page") ); } for (let key in pageConfig) { if (pageConfig[key].deps.length > 0) { for (let dep of pageConfig[key].deps) { let index = x.indexOf(dep); if (index < 0) { continue; } // x if (!t_x.includes(dep)) t_x.push(dep); } } } let i = 1; for (let dep of t_x) { if (dep.startsWith("../")) continue; // d_ const d_Exp = `d_\\[x\\[${x.indexOf( dep )}\\]\\]={}\n[\\s\\S]*?e_\\[x\\[${x.indexOf( dep )}\\]\\]={f:m\\d+,j:\\[.*?\\],i:\\[.*?\\],ti:\\[(.*?)\\],ic:\\[.*?\\]}`; let m0 = source.match(new RegExp(d_Exp)); let m0_str = m0[0]; const depIndex = m0[1].match(/\d+/g); if (depIndex) { for (let index of depIndex) { m0_str = m0_str.replace( new RegExp(`x\\[${index}\\]`, "g"), `x[${t_x.indexOf(x[index])}]` ); } } // process.stderr.write(dep + '=====' + x.indexOf(dep) + "-" + t_x.indexOf(dep) + '\n') dep_d_ += m0_str .replace( new RegExp(`x\\[${x.indexOf(dep)}\\]`, "g"), `x[${t_x.indexOf(dep)}]` ) .replace( new RegExp(`\\${funcName}\\_\\d+`, "g"), `${funcName}_${i}` ) .replace(/m\d+/g, `m${i - 1}`) + "\n"; let gz_name = m0_str.match(/var z=gz(.*?)\(\)/)[1]; // process.stderr.write("get funcName - " + t[0].match(/var z=gz(.*?)\(\)/)[1] + "\n") // gz const exp = `function gz\\${gz_name}\\(\\)\\{[\\s\\S]*?__WXML_GLOBAL__\\.ops_cached\\.\\${gz_name}\n}`; DEBUG_OUTPUT && process.stderr.write("COMMON=====" + dep + "----" + exp + "\n"); const gz = source.match(new RegExp(exp)); dep_gz += gz[0].replace( new RegExp(`\\${funcName}\\_\\d+`, "g"), `${funcName}_${i}` ) + "\n"; i++; } dep_x += `${t_x.join("','")}'];`; return { dep_x, dep_gz, dep_d_, }; } function genFunctionContent_LL(wxmlName, config = {}, source, funcName, x) { // 起始 let content = `${config.funcName}` + "=function(_,_v,_n,_p,_s,_wp,_wl,$gwn,$gwl,$gwh,wh,$gstack,$gwrt,gra,grb,TestTest,wfor,_ca,_da,_r,_rz,_o,_oz,_1,_1z,_2,_2z,_m,_mz,nv_getDate,nv_getRegExp,nv_console,nv_parseInt,nv_parseFloat,nv_isNaN,nv_isFinite,nv_decodeURI,nv_decodeURIComponent,nv_encodeURI,nv_encodeURIComponent,$gdc,nv_JSON,_af,_gv,_ai,_grp,_gd,_gapi,$ixc,_ic,_w,_ev,_tsd){return function(path,global){\u000aif(typeof global==='undefined'){if (typeof __GWX_GLOBAL__==='undefined')global={};else global=__GWX_GLOBAL__;}if(typeof __WXML_GLOBAL__ === 'undefined') {__WXML_GLOBAL__={};\u000a}__WXML_GLOBAL__.modules = __WXML_GLOBAL__.modules || {};\u000a"; // content += "var e_={}\u000aif(typeof(global.entrys)==='undefined')global.entrys={};e_=global.entrys;\u000avar d_={}\u000aif(typeof(global.defines)==='undefined')global.defines={};d_=global.defines;\u000avar f_={}\u000aif(typeof(global.modules)==='undefined')global.modules={};f_=global.modules || {};\u000avar p_={}\u000a"; // cs const cs = source.match(/var cs([\s\S]*?)function gz/); content += "var cs" + cs[1].replace(new RegExp(`\\${funcName}`, "g"), config.funcName); // gz函数 const exp = `function gz\\${funcName}_${ config.num + 1 }\\(\\)\\{[\\s\\S]*_WXML_GLOBAL__\\.ops_cached\\.\\${funcName}_${ config.num + 1 }\n}`; const gz = source.match(new RegExp(exp))[0]; content += gz; // debug let debug = source.match( new RegExp( `\\n__WXML_GLOBAL__\\.ops_set\\.\\${funcName}=[\\s\\S]*?\\nvar` ) ); debug = debug[0] .substring(0, debug[0].length - 3) .replace(new RegExp(`\\${funcName}`, "g"), config.funcName); content += debug; content += `var x=['./${wxmlName}.wxml'`; // m0 --- m{num} const m0Exp = `var m${config.num}=function\\(e,s,r,gg\\){[\\s\\S]*e_\\[x\\[(\\d+)\\]\\]={f:m${config.num},j:\\[.*?\\],i:\\[.*?\\],ti:\\[(.*?)\\],ic:\\[.*?\\]}`; const m0 = source.match(new RegExp(m0Exp)); let m0_str = m0[0].replace(new RegExp(`m${config.num}`, "g"), "m0"); const cur_x = m0[1]; // cur_x与config.num不一定相同 m0_str = m0_str.replace(new RegExp(`x\\[${cur_x}\\]`, "g"), "x[0]"); const depIndex = m0[2].match(/\d+/g); if (depIndex) { let i = 1; for (let index of depIndex) { content += `,'${x[index]}'`; m0_str = m0_str.replace( new RegExp(`x\\[${index}\\]`, "g"), `x[${i++}]` ); } } content += `];d_[x[0]]={}\u000a`; m0_str = m0_str.replace(new RegExp(`x\\[${config.num}\\]`, "g"), "x[0]"); content += m0_str.replace( new RegExp(`\\${funcName}\\_\\d+`, "g"), `${config.funcName}_1` ); // path content += `\u000aif(path&&e_[path]){\u000awindow.__wxml_comp_version__=0.02\u000areturn function(env,dd,global){$gwxc=0;var root={\u0022tag\u0022:\u0022wx-page\u0022};root.children=[]\u000a;g=\u0022${config.funcName}\u0022;var main=e_[path].f\u000acs=[]\u000aif (typeof global===\u0022undefined\u0022)global={};global.f=$gdc(f_[path],\u0022\u0022,1);\u000aif(typeof(window.__webview_engine_version__)!='undefined'&&window.__webview_engine_version__+1e-6>=0.02+1e-6&&window.__mergeData__)\u000a{\u000aenv=window.__mergeData__(env,dd);\u000a}\u000atry{\u000amain(env,{},root,global);\u000a_tsd(root)\u000aif(typeof(window.__webview_engine_version__)=='undefined'|| window.__webview_engine_version__+1e-6<0.01+1e-6){return _ev(root);}\u000a}catch(err){\u000aconsole.log(cs, env);\u000aconsole.log(err)\u000athrow err\u000a}\u000a;g=\u0022\u0022;\u000areturn root;\u000a}\u000a}\u000a}\u000a}`; // end content += `(__g.a,__g.b,__g.c,__g.d,__g.e,__g.f,__g.g,__g.h,__g.i,__g.j,__g.k,__g.l,__g.m,__g.n,__g.o,__g.p,__g.q,__g.r,__g.s,__g.t,__g.u,__g.v,__g.w,__g.x,__g.y,__g.z,__g.A,__g.B,__g.C,__g.D,__g.E,__g.F,__g.G,__g.H,__g.I,__g.J,__g.K,__g.L,__g.M,__g.N,__g.O,__g.P,__g.Q,__g.R,__g.S,__g.T,__g.U,__g.V,__g.W,__g.X,__g.Y,__g.Z,__g.aa);if(__vd_version_info__.delayedGwx||false)${config.funcName}();`; content = content.replace( new RegExp(`\\${funcName}_${config.num + 1}`, "g"), `${config.funcName}_1` ); // console.log(content) return content; } function genCommonContent_LL(source, funcName, pageConfig, x) { let common = "var __wxAppData=__wxAppData||{};var __wxAppCode__=__wxAppCode__||{};var global=global||{};var __WXML_GLOBAL__=__WXML_GLOBAL__||{entrys:{},defines:{},modules:{},ops:[],wxs_nf_init:undefined,total_ops:0};var Component=Component||function(){};var definePlugin=definePlugin||function(){};var requirePlugin=requirePlugin||function(){};var Behavior=Behavior||function(){};var __vd_version_info__=__vd_version_info__||{};var __GWX_GLOBAL__=__GWX_GLOBAL__||{};if(this&&this.__g===undefined)Object.defineProperty(this,\u0022__g\u0022,{configurable:false,enumerable:false,writable:false,value:function(){function D(e,t){if(typeof t!=\u0022undefined\u0022)e.children.push(t)}function S(e){if(typeof e!=\u0022undefined\u0022)return{tag:\u0022virtual\u0022,wxKey:e,children:[]};return{tag:\u0022virtual\u0022,children:[]}}function v(e){$gwxc++;if($gwxc>=16e3){throw\u0022Dom limit exceeded, please check if there's any mistake you've made.\u0022}return{tag:\u0022wx-\u0022+e,attr:{},children:[],n:[],raw:{},generics:{}}}function e(e,t){t&&e.properities.push(t)}function t(e,t,r){return typeof e[r]!=\u0022undefined\u0022?e[r]:t[r]}function u(e){console.warn(\u0022WXMLRT_\u0022+g+\u0022:\u0022+e)}function r(e,t){u(t+\u0022:-1:-1:-1: Template `\u0022+e+\u0022` is being called recursively, will be stop.\u0022)}var s=console.warn;var n=console.log;function o(){function e(){}e.prototype={hn:function(e,t){if(typeof e==\u0022object\u0022){var r=0;var n=false,o=false;for(var a in e){n=n|a===\u0022__value__\u0022;o=o|a===\u0022__wxspec__\u0022;r++;if(r>2)break}return r==2&&n&&o&&(t||e.__wxspec__!==\u0022m\u0022||this.hn(e.__value__)===\u0022h\u0022)?\u0022h\u0022:\u0022n\u0022}return\u0022n\u0022},nh:function(e,t){return{__value__:e,__wxspec__:t?t:true}},rv:function(e){return this.hn(e,true)===\u0022n\u0022?e:this.rv(e.__value__)},hm:function(e){if(typeof e==\u0022object\u0022){var t=0;var r=false,n=false;for(var o in e){r=r|o===\u0022__value__\u0022;n=n|o===\u0022__wxspec__\u0022;t++;if(t>2)break}return t==2&&r&&n&&(e.__wxspec__===\u0022m\u0022||this.hm(e.__value__))}return false}};return new e}var A=o();function T(e){var t=e.split(\u0022\u005cn \u0022+\u0022 \u0022+\u0022 \u0022+\u0022 \u0022);for(var r=0;r=\u0022:case\u0022<=\u0022:case\u0022>\u0022:case\u0022<\u0022:case\u0022<<\u0022:case\u0022>>\u0022:p=x(e[1],t,r,n,o,a);u=x(e[2],t,r,n,o,a);l=M&&(A.hn(p)===\u0022h\u0022||A.hn(u)===\u0022h\u0022);switch(i){case\u0022+\u0022:f=A.rv(p)+A.rv(u);break;case\u0022*\u0022:f=A.rv(p)*A.rv(u);break;case\u0022/\u0022:f=A.rv(p)/A.rv(u);break;case\u0022%\u0022:f=A.rv(p)%A.rv(u);break;case\u0022|\u0022:f=A.rv(p)|A.rv(u);break;case\u0022^\u0022:f=A.rv(p)^A.rv(u);break;case\u0022&\u0022:f=A.rv(p)&A.rv(u);break;case\u0022===\u0022:f=A.rv(p)===A.rv(u);break;case\u0022==\u0022:f=A.rv(p)==A.rv(u);break;case\u0022!=\u0022:f=A.rv(p)!=A.rv(u);break;case\u0022!==\u0022:f=A.rv(p)!==A.rv(u);break;case\u0022>=\u0022:f=A.rv(p)>=A.rv(u);break;case\u0022<=\u0022:f=A.rv(p)<=A.rv(u);break;case\u0022>\u0022:f=A.rv(p)>A.rv(u);break;case\u0022<\u0022:f=A.rv(p)>\u0022:f=A.rv(p)>>A.rv(u);break;default:break}return l?A.nh(f,\u0022c\u0022):f;break;case\u0022-\u0022:p=e.length===3?x(e[1],t,r,n,o,a):0;u=e.length===3?x(e[2],t,r,n,o,a):x(e[1],t,r,n,o,a);l=M&&(A.hn(p)===\u0022h\u0022||A.hn(u)===\u0022h\u0022);f=l?A.rv(p)-A.rv(u):p-u;return l?A.nh(f,\u0022c\u0022):f;break;case\u0022!\u0022:p=x(e[1],t,r,n,o,a);l=M&&A.hn(p)==\u0022h\u0022;f=!A.rv(p);return l?A.nh(f,\u0022c\u0022):f;case\u0022~\u0022:p=x(e[1],t,r,n,o,a);l=M&&A.hn(p)==\u0022h\u0022;f=~A.rv(p);return l?A.nh(f,\u0022c\u0022):f;default:s(\u0022unrecognized op\u0022+i)}}function x(e,t,r,n,o,a){var i=e[0];var p=false;if(typeof a!==\u0022undefined\u0022)o.ap=a;if(typeof i===\u0022object\u0022){var u=i[0];var l,f,v,c,s,y,b,d,h,_,g;switch(u){case 2:return m(e,t,r,n,o);break;case 4:return x(e[1],t,r,n,o,p);break;case 5:switch(e.length){case 2:l=x(e[1],t,r,n,o,p);return M?[l]:[A.rv(l)];return[l];break;case 1:return[];break;default:l=x(e[1],t,r,n,o,p);v=x(e[2],t,r,n,o,p);l.push(M?v:A.rv(v));return l;break}break;case 6:l=x(e[1],t,r,n,o);var w=o.ap;h=A.hn(l)===\u0022h\u0022;f=h?A.rv(l):l;o.is_affected|=h;if(M){if(f===null||typeof f===\u0022undefined\u0022){return h?A.nh(undefined,\u0022e\u0022):undefined}v=x(e[2],t,r,n,o,p);_=A.hn(v)===\u0022h\u0022;c=_?A.rv(v):v;o.ap=w;o.is_affected|=_;if(c===null||typeof c===\u0022undefined\u0022||c===\u0022__proto__\u0022||c===\u0022prototype\u0022||c===\u0022caller\u0022){return h||_?A.nh(undefined,\u0022e\u0022):undefined}y=f[c];if(typeof y===\u0022function\u0022&&!w)y=undefined;g=A.hn(y)===\u0022h\u0022;o.is_affected|=g;return h||_?g?y:A.nh(y,\u0022e\u0022):y}else{if(f===null||typeof f===\u0022undefined\u0022){return undefined}v=x(e[2],t,r,n,o,p);_=A.hn(v)===\u0022h\u0022;c=_?A.rv(v):v;o.ap=w;o.is_affected|=_;if(c===null||typeof c===\u0022undefined\u0022||c===\u0022__proto__\u0022||c===\u0022prototype\u0022||c===\u0022caller\u0022){return undefined}y=f[c];if(typeof y===\u0022function\u0022&&!w)y=undefined;g=A.hn(y)===\u0022h\u0022;o.is_affected|=g;return g?A.rv(y):y}case 7:switch(e[1][0]){case 11:o.is_affected|=A.hn(n)===\u0022h\u0022;return n;case 3:b=A.rv(r);d=A.rv(t);v=e[1][1];if(n&&n.f&&n.f.hasOwnProperty(v)){l=n.f;o.ap=true}else{l=b&&b.hasOwnProperty(v)?r:d&&d.hasOwnProperty(v)?t:undefined}if(M){if(l){h=A.hn(l)===\u0022h\u0022;f=h?A.rv(l):l;y=f[v];g=A.hn(y)===\u0022h\u0022;o.is_affected|=h||g;y=h&&!g?A.nh(y,\u0022e\u0022):y;return y}}else{if(l){h=A.hn(l)===\u0022h\u0022;f=h?A.rv(l):l;y=f[v];g=A.hn(y)===\u0022h\u0022;o.is_affected|=h||g;return A.rv(y)}}return undefined}break;case 8:l={};l[e[1]]=x(e[2],t,r,n,o,p);return l;break;case 9:l=x(e[1],t,r,n,o,p);v=x(e[2],t,r,n,o,p);function O(e,t,r){var n,o;h=A.hn(e)===\u0022h\u0022;_=A.hn(t)===\u0022h\u0022;f=A.rv(e);c=A.rv(t);for(var a in c){if(r||!f.hasOwnProperty(a)){f[a]=M?_?A.nh(c[a],\u0022e\u0022):c[a]:A.rv(c[a])}}return e}var s=l;var j=true;if(typeof e[1][0]===\u0022object\u0022&&e[1][0][0]===10){l=v;v=s;j=false}if(typeof e[1][0]===\u0022object\u0022&&e[1][0][0]===10){var P={};return O(O(P,l,j),v,j)}else return O(l,v,j);break;case 10:l=x(e[1],t,r,n,o,p);l=M?l:A.rv(l);return l;break;case 12:var P;l=x(e[1],t,r,n,o);if(!o.ap){return M&&A.hn(l)===\u0022h\u0022?A.nh(P,\u0022f\u0022):P}var w=o.ap;v=x(e[2],t,r,n,o,p);o.ap=w;h=A.hn(l)===\u0022h\u0022;_=N(v);f=A.rv(l);c=A.rv(v);snap_bb=K(c,\u0022nv_\u0022);try{P=typeof f===\u0022function\u0022?K(f.apply(null,snap_bb)):undefined}catch(t){t.message=t.message.replace(/nv_/g,\u0022\u0022);t.stack=t.stack.substring(0,t.stack.indexOf(\u0022\u005cn\u0022,t.stack.lastIndexOf(\u0022at nv_\u0022)));t.stack=t.stack.replace(/\u005csnv_/g,\u0022 \u0022);t.stack=T(t.stack);if(n.debugInfo){t.stack+=\u0022\u005cn \u0022+\u0022 \u0022+\u0022 \u0022+\u0022 at \u0022+n.debugInfo[0]+\u0022:\u0022+n.debugInfo[1]+\u0022:\u0022+n.debugInfo[2];console.error(t)}P=undefined}return M&&(_||h)?A.nh(P,\u0022f\u0022):P}}else{if(i===3||i===1)return e[1];else if(i===11){var l=\u0022\u0022;for(var D=1;D=0;o--){if(r[e].i[o]&&n[r[e].i[o]][t])return n[r[e].i[o]][t]}for(var o=r[e].ti.length-1;o>=0;o--){var a=ne(r[e].ti[o],r,e);if(a&&n[a][t])return n[a][t]}var i=ae(r,e);for(var o=0;o=0;p--)if(r[e].j[p]){for(var a=r[r[e].j[p]].ti.length-1;a>=0;a--){var u=ne(r[r[e].j[p]].ti[a],r,e);if(u&&n[u][t]){return n[u][t]}}}}function ae(e,t){if(!t)return[];if($gaic[t]){return $gaic[t]}var r=[],n=[],o=0,a=0,i={},p={};n.push(t);p[t]=true;a++;while(o=0.02+1e-6&&window.__mergeData__)\u000a{\u000aenv=window.__mergeData__(env,dd);\u000a}\u000atry{\u000amain(env,{},root,global);\u000a_tsd(root)\u000aif(typeof(window.__webview_engine_version__)=='undefined'|| window.__webview_engine_version__+1e-6<0.01+1e-6){return _ev(root);}\u000a}catch(err){\u000aconsole.log(cs, env);\u000aconsole.log(err)\u000athrow err\u000a}\u000a;g=\u0022\u0022;\u000areturn root;\u000a}\u000a}\u000a}\u000a}(__g.a,__g.b,__g.c,__g.d,__g.e,__g.f,__g.g,__g.h,__g.i,__g.j,__g.k,__g.l,__g.m,__g.n,__g.o,__g.p,__g.q,__g.r,__g.s,__g.t,__g.u,__g.v,__g.w,__g.x,__g.y,__g.z,__g.A,__g.B,__g.C,__g.D,__g.E,__g.F,__g.G,__g.H,__g.I,__g.J,__g.K,__g.L,__g.M,__g.N,__g.O,__g.P,__g.Q,__g.R,__g.S,__g.T,__g.U,__g.V,__g.W,__g.X,__g.Y,__g.Z,__g.aa);if(__vd_version_info__.delayedGwx||true)" + funcName + "();;var __WXML_DEP__=__WXML_DEP__||{};"; // 追加引用 let common_end = ""; for (let key in pageConfig) { if (pageConfig[key].deps.length > 0) { common_end += `__WXML_DEP__["./${key}.wxml"]=["${pageConfig[ key ].deps.join('","')}",];`; } } common += common_end; return common; } /** * 获取 wxml 编译器路径 */ let wxmlParserPath = ""; function getWXMLParsePath() { if (wxmlParserPath) return wxmlParserPath; const fileName = process.platform === "darwin" ? "../bin/mac/wcc" : process.platform === "linux" ? "../bin/linux/wcc" : "../bin/windows/wcc.exe"; wxmlParserPath = path.join(__dirname, fileName); // 尝试修改权限 try { fs.chmodSync(wxmlParserPath, 0o777); } catch (err) { // ignore } return wxmlParserPath; } /** * 获取自定义组件编译参数 */ function getComponentArgs(files) { let args = []; let count = 0; files.forEach((file) => { const fileJson = file.fileJson; if (fileJson.usingComponents) { args.push(file.pagePath); args.push(Object.keys(fileJson.usingComponents).length); args = args.concat(Object.keys(fileJson.usingComponents)); count++; } }); args.unshift(count); return args; } /** * 获取完整文件列表,包括自定义组件 */ function getAllFiles(rootPath, files) { const ret = []; const hasCheckMap = {}; for (let i = 0, len = files.length; i < len; i++) { const file = files[i]; let fileJson = {}; const realPath = path.join(rootPath, file); if (hasCheckMap[realPath]) continue; hasCheckMap[realPath] = true; try { fileJson = require(`${realPath}.json`); } catch (err) { // ignore } // 自定义组件 if (fileJson.usingComponents) { Object.keys(fileJson.usingComponents).forEach((subFileKey) => { const subFile = fileJson.usingComponents[subFileKey]; len++; let relativePath = path.relative( rootPath, path.join(path.dirname(realPath), subFile) ); relativePath = relativePath.replace(/\\/g, "/"); files.push(relativePath); }); } ret.push({ pagePath: `${file}.wxml`, jsonPath: `${file}.json`, fileJson, }); } return ret; } /** * 入口 * 编译 wxml 到 js * files * * @param {*} options 配置选项 * @returns String or Object with lazyload */ function wxmlToJS(options = {}) { // 创建临时目录 const tmp = nfs.mkdtempSync(path.join(os.tmpdir(), "wcc_")); // 判断是否replace,是写入replace,否则拷贝文件到临时目录 for (let file of options.files) { if (typeof options.replaceContent[file] === "string") { // 写入替换内容 nfs.mkdirSync(path.dirname(path.resolve(tmp, file)), { recursive: true, }); nfs.writeFileSync( path.resolve(tmp, file), options.replaceContent[file] ); } else { // 复制原文件 nfs.mkdirSync(path.dirname(path.resolve(tmp, file)), { recursive: true, }); nfs.copyFileSync( path.resolve(options.cwd, file), path.resolve(tmp, file) ); } } // 使用临时目录执行wcc options.cwd = tmp; let rootPath = options.cwd; let files = options.files; let cut = options.isCut; const type = cut ? "-xc" : "-cc"; // files = getAllFiles(rootPath, files) // @TODO,如果遇到参数过长被操作系统干掉的情况,可以使用 --config-path FILE 配置,参数空格换成空行 // const componentArgs = getComponentArgs(files), componentArgs.join(' ') let args = [ "-d", "--split", options.wxmlCompileConfigSplit, type, options.wxmlCompileConfig, ] .concat(files) .concat(["-gn", options.genfuncname || "$gwx"]); // wxs调试 if (options.debugWXS) args.unshift("-ds"); // 懒加载 // if(options.lazyload)args=args.concat(['-ll', options.lazyloadConfig]) // wxmlParserPath 二进制可执行文件路径 const wxmlParserPath = getWXMLParsePath(); // console.warn('wcc args:', args) const wcc = spawnSync(wxmlParserPath, args, { cwd: rootPath }); nfs.rmSync(tmp, { recursive: true, force: true }); if (wcc.status === 0) { let result = wcc.stdout.toString(); DEBUG_OUTPUT && fs.writeFileSync(`${DEBUG_OUTPUT}/linux_output.js`, result); if (options.lazyload) { // 懒加载处理 const str = result; const resultObj = { generateFunctionName: { __COMMON__: options.genfuncname, }, generateFunctionContent: { __COMMON__: null, }, }; const needArr = options.wxmlCompileConfig.split( options.wxmlCompileConfigSplit ); const indexArr = eval(str.match(/var x=(\[.*\]);/)[1]); const pageConfig = {}; let i = 0; indexArr.forEach((ele, index) => { if (!ele.startsWith("../")) { pageConfig[ele.substring(2, ele.length - 5)] = { funcName: `${options.genfuncname}_XC_${i}`, num: i, deps: [], }; i++; } }); // 引入解析 const deps = str.matchAll( /_ai\(.*?,x\[(\d+)\],.*?,x\[(\d+)\],\d+,\d+\)/g ); for (let dep of deps) { // console.log(dep[1]) // 被引用文件下标 // console.log(dep[2]) // 引用文件下标 const target = indexArr[dep[2]]; pageConfig[target.substring(2, target.length - 5)].deps.unshift( "./" + path.join(path.dirname(target), indexArr[dep[1]]) ); } for (let key in pageConfig) { if (needArr.includes(`./${key}.wxml`)) { resultObj.generateFunctionName[key] = pageConfig[key].funcName; resultObj.generateFunctionContent[key] = genFunctionContent_LL( key, pageConfig[key], str, options.genfuncname, indexArr ); } } resultObj.generateFunctionContent.__COMMON__ = genCommonContent_LL( str, options.genfuncname, pageConfig, indexArr ); // console.log(pageConfig) result = resultObj; } return result; } else { throw new Error(`编译 .wxml 文件错误:${wcc.stderr.toString()}`); } } module.exports = wxmlToJS;