xhs-mp-project 2.1.1 → 2.1.2

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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("path"),e=require("fs-extra"),n=require("xhs-mp-shared"),o=require("json-to-ast"),s=require("lodash"),r=require("events"),i=require("process");function a(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,o.get?o:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var c=a(t),p=a(e);require("crypto"),require("net");const l=(t,e=100)=>{let n=null;return function(...o){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,o))}},u=(t="")=>t.split(c.sep).join(c.posix.sep),h=(...t)=>u(c.join(...t)),g=(...t)=>u(c.resolve(...t)),d=c.posix.sep,m=t=>{if(!u(t).includes(d))return"/";const e=u(t).split(d).filter(Boolean);return e.slice(0,e.length-1).join(d)},f="baba:changeExtJsonPath",P="baba:changeExtJson",b="miniprogram",j="minigame",v="miniwidget";require("util").inspect.custom;const y=(t,e)=>{const n=new Error(["\n<xhsml!",`[error:文件内容错误]:[file:${null==t?void 0:t.trim()}]`,e,"!xhsml>\n"].join("\n"));throw n.stack=void 0,n};function k(t,e){if(!Array.isArray(t))return e("pages 字段需为 array");for(let n=0;n<t.length;n++){const o=t[n];if(o.startsWith("/"))return e(`pages[${n}] 不应该以 '/' 开头`);if(o.startsWith("."))return e(`pages[${n}] 不应该以 '.' 开头`)}}const $=(t,e)=>t===e||(!(!t.startsWith(e)||!t.replace(e,"").startsWith("/"))||!(!e.startsWith(t)||!e.replace(t,"").startsWith("/")));function J(t,e,n){var o,s;if(!Array.isArray(t))return n("subPackages 字段需为 array");const r=[],i=[];for(let e=0;e<t.length;e++){if("string"!=typeof(null===(s=null===(o=t[e].root)||void 0===o?void 0:o.trim)||void 0===s?void 0:s.call(o)))return n(`subPackages[${e}].root 字段类型需为 string`);const a=t[e].root;if(a.startsWith("/"))return n(`subPackages[${e}].root 不应该以 '/' 开头`);if(a.startsWith("."))return n(`subPackages[${e}].root 不应该以 '.' 开头`);if(!Array.isArray(t[e].pages))return n(`subPackages[${e}].pages 类型需为array`);t[e].pages.length;for(let o=0;o<t[e].pages.length;o++){const s=t[e].pages[o];if(s.startsWith("/"))return n(`subPackages[${e}].pages[${o}] 不应该以 '/' 开头`);if(s.startsWith("."))return n(`subPackages[${e}].pages[${o}] 不应该以 '.' 开头`)}if(t[e].name){const o=i.indexOf(t[e].name);if(o>-1)return n(`subPackages[${e}] 与 subPackages[${o}] 的 name 相同`);i.push(t[e].name)}r.push(t[e].root)}for(let t=0;t<r.length;t++){const o=e.findIndex((e=>$(e,r[t])));if(o>-1)return n(`pages[${o}] 与 subPackages[${t}] 存在包含关系`);for(let e=t+1;e<r.length;e++)if($(r[t],r[e]))return n(`subPackages[${t}] 与 subPackages[${e}] 存在包含关系`)}}function C(t,e){return _(t)?null==t.navigationBarTextStyle||["black","white"].includes(t.navigationBarTextStyle)?null==t.backgroundTextStyle||["dark","light"].includes(t.backgroundTextStyle)?null==t.navigationStyle||["default","custom"].includes(t.navigationStyle)?void 0:e("window.navigationStyle 错误: 仅支持 default / custom"):e("window.backgroundTextStyle 错误: 仅支持 dark / light"):e("window.navigationBarTextStyle 错误: 仅支持 black / white"):e("window 字段需为 object")}function x(t,e){var n;if(!_(t))return e("tabBar 字段需为 object");if(!Array.isArray(null==t?void 0:t.list))return e("tabBar.list 字段需为 array");if(!(null===(n=null==t?void 0:t.list)||void 0===n?void 0:n.length))return e("tabBar.list 不能为空");if(t.list.length<2)return e("tabBar.list 错误: tabBar.list 需至少包含 2 项");if(t.list.length>5)return e("tabBar.list 错误: tabBar.list 不能超过 5 项");for(let n=0;n<t.list.length;n++){const{pagePath:o}=t.list[n];if(!o)return e(`tabBar.list[${n}] 错误: "pagePath" 不能为空`);if(o.startsWith("/"))return e(`tabBar.list[${n}].pagePath 不应该以 '/' 开头`);if(o.startsWith("."))return e(`tabBar.list[${n}].pagePath 不应该以 '.' 开头`)}}function S(t,e){if(!_(t))return e("usingComponents 字段需为 object")}function w(t,e){if(!_(t))return e("networkTimeout 字段需为 object");const n=["request","connectSocket","uploadFile","downloadFile"];for(const o of n)if(null!=t[o]&&"number"!=typeof t[o])return e(`networkTimeout.${o} 字段需为 number`)}function _(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const O=["window","networkTimeout","tabBar"],E=(t,e,n)=>{if(!((t,e,n)=>{var o,s,r,i,a,c;const p=t=>y(n,t),l=t.pages,u=t.subpackages||t.subPackages||[],g=t.usingComponents||{};if("pages"in e){k(e.pages,p);for(let t=0;t<e.pages.length;t++)if(!l.includes(e.pages[t]))return p(`pages[${t}]: "${e.pages[t]}" 不在 app.json pages 内`)}if("subpackages"in e||"subPackages"in e){J(e.subpackages||e.subPackages,e.pages?e.pages:l,p);const t=e.subpackages||e.subPackages||[];for(let e=0;e<t.length;e++){const n=t[e],s=u.find((t=>t.root===n.root));if(!s)return p(`subPackages[${e}].root: "${n.root}" 不在 app.json subPackages 内`);for(let t=0;t<n.pages.length;t++){const r=n.pages[t];if(!(null===(o=s.pages)||void 0===o?void 0:o.includes(r)))return p(`subPackages[${e}].pages[${t}]: "${r}" 不在 app.json subPackages 内`)}}}if("extPages"in e){if(!_(e.extPages))return p("extPages 字段需为 object");const t=u.flatMap((t=>t.pages.map((e=>h(t.root,e)))));for(const n of Object.keys(e.extPages))if(!l.includes(n)&&!t.includes(n))return p(`extPages["${n}"] 不在 app.json pages | subPackages 内`)}if("tabBar"in e){if(!_(e.tabBar))return p("tabBar 字段需为 object");if("list"in e.tabBar){x(e.tabBar,p);for(const{pagePath:t}of e.tabBar.list)if(null===(s=e.pages)||void 0===s?void 0:s.length){if(!e.pages.includes(t))return p(`tabBar.list 错误: "${t}" 不在 ext.json pages 内`)}else if(!l.includes(t))return p(`tabBar.list 错误: "${t}" 不在 app.json pages 内`)}}if((null===(r=e.pages)||void 0===r?void 0:r.length)&&!(null===(a=null===(i=e.tabBar)||void 0===i?void 0:i.list)||void 0===a?void 0:a.length)&&t.tabBar)for(let n=0;n<(null===(c=t.tabBar.list)||void 0===c?void 0:c.length);n++){const o=t.tabBar.list[n];if(!e.pages.includes(o.pagePath))return p(`ext.json pages 未包含 "${o.pagePath}",此页面在 app.json tabBar.list[${n}] 中被声明`)}if("window"in e&&C(e.window,p),"networkTimeout"in e&&w(e.networkTimeout,p),"usingComponents"in e){S(e.usingComponents,p);for(const t in e.usingComponents)if(!g[t])return p(`usingComponents["${t}"] 不在 app.json usingComponents 内`)}return!0})(t,e,n))return t;for(const[n,o]of Object.entries(e))if("extPages"!==n)if("object"==typeof o&&O.includes(n)){const e=Array.isArray(o);t[n]=t[n]||(e?[]:{}),t[n]=e?[...new Set([...t[n],...o])]:{...t[n],...o}}else t[n]=o;return t},D=t=>{try{const n=e.readFileSync(`${t}`,"utf-8");return o(n,{loc:!0,source:t}),JSON.parse(n)}catch(e){return y(t||e.source,e.message),{pages:[]}}};let B=1;function M(t,n="Component"){return!(!e.existsSync(`${t}.js`)&&!e.existsSync(`${t}.ts`))&&("Component"!==n||e.existsSync(`${t}.json`))}const N=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,T=(t,e)=>t.replace(e.endsWith("/")?e:`${e}/`,""),W=(n,o,s)=>{if(o.startsWith("plugin://"))return y(s,`小红书小程序暂不支持插件:${o}`);let r="";if(o.startsWith("/")&&M(r=h(n,o)))return T(r,n);if(o.startsWith(".")&&M(r=g(t.dirname(s),o)))return T(r,n);if(M(r=g(n,o)))return T(r,n);if(M(r=g(t.dirname(s),o)))return T(r,n);const i=((t,n)=>{try{const o=`${t}/package.json`;if(e.existsSync(o)){const s=D(o),r=Object.keys(s.dependencies||[]).find((t=>n.includes(t)))||"",i=`${t}/node_modules/${r}/package.json`;if(e.existsSync(i)){const e=n.replace(r,"").replace(/^\//,""),o=D(i);let s=o.miniprogram||o.main||"";s=/\.(ts|js)$/.test(s)?s.replace(/\.(js|ts)$/,""):s;let a=(e||s||"").trim();const c=h(t,"node_modules",r,a).replace(`${t}/`,"");if(M(`${t}/${c}`))return c;if(M(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(n,o);return i||y(s,`${N("Component")}:${o}`)},A=/\.|\/|\.\/|\.\.\//g;var q;!function(t){t.SKETCH="骨架屏",t.COMPONENT="组件"}(q||(q={}));const R=t=>t.endsWith("/")?t:`${t}/`,F=(t,e,n,o,s,r)=>{if((null==r?void 0:r.independent)&&(null==r?void 0:r.root)&&!t.startsWith(R(r.root)))return y(o,`独立分包${r.root}只可引用${r.root}目录下的${s}:${e}`);if((null==r?void 0:r.root)&&!t.startsWith(R(r.root))){for(const i of n.subPackages)if((null==r?void 0:r.root)!==i.root&&t.startsWith(R(i.root)))return y(o,`分包${null==r?void 0:r.root}不可引用分包${i.root}目录下的${s}:${e}`)}else if(!r)for(const r of n.subPackages)if(t.startsWith(R(r.root)))return y(o,`主包不可引用分包${r.root}目录下的${s}:${e}`)},z=(t,e,n,o,s)=>{const r={};for(const[i,a]of Object.entries(t)){let t;A.test(a)?(t=W(n,a,o),I(e,n,t,s)):t=a,r[i]=t}return r},V=(t,e,n,o,s)=>{const r={};for(const[i,a]of Object.entries(t)){const t=W(n,a,o);r[i]=t,F(t,a,e,o,q.COMPONENT,s),I(e,n,t,s)}return r},L=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),I=(t,e,n,o)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=g(e,`${n}.json`),r=D(s);t.componentsMap[n]=r,t.componentsMap[n].index=B++,L(r.componentPlaceholder)?r.componentPlaceholder=z(r.componentPlaceholder,t,e,s,o):Reflect.deleteProperty(r,"componentPlaceholder"),L(r.usingComponents)?r.usingComponents=V(r.usingComponents,t,e,s,o):Reflect.deleteProperty(r,"usingComponents")},G=(o,s,r,i,a)=>o.map((o=>{var c,p;let l={path:o};const u=g(s,`${o}.json`);if(e.existsSync(u)){let d,m=D(u);l={...l,...m},(d=null==i?void 0:i[o])&&(l={...l,...d}),l.path=o,L(l.componentPlaceholder)?l.componentPlaceholder=z(l.componentPlaceholder,r,s,u,a):Reflect.deleteProperty(l,"componentPlaceholder"),L(l.usingComponents)?l.usingComponents=V(l.usingComponents,r,s,u,a):Reflect.deleteProperty(l,"usingComponents");const f=null===(c=null==m?void 0:m.skeleton)||void 0===c?void 0:c.path;if(L(m.skeleton)&&f&&"string"==typeof f){let o="";if(o=f.startsWith("/")?h(s,f):g(t.dirname(u),f),!n.SKETCH_ML_REG.test(o))return y(u,`骨架屏后缀需为 ${n.SKETCH_ML_REG} 格式:${f}`);if(!e.existsSync(o))return y(u,`骨架屏不存在:${f}`);const i=T(o,s);F(i,f,r,u,q.SKETCH,a),l.skeleton={path:i.replace(n.ML_COMMON_EXT,""),options:(null===(p=m.skeleton)||void 0===p?void 0:p.options)||{}}}else Reflect.deleteProperty(l,"skeleton")}return l})),H=(t,e,n,o)=>{B=1;const s=g(e,"app.json");let r=D(s);r.subPackages=r.subPackages||r.subpackages||[],r=((t,e,n)=>{var o,s;const r=t=>y(e,t);if(!("pages"in t))return r("缺少 pages 字段");if(!Array.isArray(t.pages))return r("pages 字段需为 array");if(t.pages.length<=0)return r("pages 不能为空");if(k(t.pages,r),n===v){if(1!==(null===(o=t.pages)||void 0===o?void 0:o.length))return r("小组件有且仅有一个页面");if((null===(s=(null==t?void 0:t.subPackages)||(null==t?void 0:t.subpackages))||void 0===s?void 0:s.length)>0)return r("小组件不允许存在分包,请删除 subpackages 或 subPackages 字段");if((null==t?void 0:t.tabBar)&&Object.keys(t.tabBar).length>0)return r("小组件不允许存在 tabBar,请删除 tabBar 字段")}if("tabBar"in t){x(t.tabBar,r);for(const{pagePath:e}of t.tabBar.list)if(!t.pages.includes(e))return r(`tabBar.list 错误: "${e}" 不在 "pages" 内`)}return("subpackages"in t||"subPackages"in t)&&J(t.subPackages||t.subpackages,t.pages,r),"window"in t&&C(t.window,r),"networkTimeout"in t&&w(t.networkTimeout,r),"usingComponents"in t&&S(t.usingComponents,r),t})(r,s,o),r.compileType=o;const i=`${n}/ext.json`,a=n?D(i):{};let c={};return((null==a?void 0:a.extEnable)&&(null==a?void 0:a.directCommit)||(null==a?void 0:a.extEnable))&&(r=E(r,a,i),c=a.extPages||{}),r.componentsMap={},L(r.usingComponents)?r.usingComponents=V(r.usingComponents,r,e,s):Reflect.deleteProperty(r,"usingComponents"),r.pages.map(((t,n)=>{if(!M(g(e,t),"Page"))return y(s,`${N("Page")}:${t} in pages[${n}]`)})),r.pages=G(r.pages,e,r,c),r.subPackages=r.subPackages.map(((t,n)=>{const o=t.pages.map(((o,r)=>{const i=h(t.root,o);return M(g(e,i),"Page")?i:y(s,`${N("Page")}:${o} in subPackages[${n}].pages[${r}]`)}));return{...t,pages:G(o,e,r,c,t)}})),r.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0},{appJSON:r}},K=t=>{t=u(t);const n=g(t,"game.json");let o=D(n);return o.subPackages=o.subPackages||o.subpackages||[],o.pages=[],o.subPackages=o.subPackages.map((t=>{let e=u(`${t.root}`).replace(/^\/+|\/+$/g,"");e=/\.(ts|js)$/.test(e)?e.replace(/\.(ts|js)$/,""):h(e,"game");const n={rawRoot:`${t.root}`,entry:e,dirname:m(e)};return{...t,...n}})),((t,n,o)=>{var s,r,i;const a=t=>(y(o||"game.json",t),!1);if(t.subPackages.length)for(let o=0;o<t.subPackages.length;o++){const{name:c,root:p,entry:l,dirname:u}=t.subPackages[o];if("string"!=typeof c)return a(`subPackages[${o}].name 字段类型需为 string`);if("string"!=typeof p)return a(`subPackages[${o}].root 字段类型需为 string`);if(!(null===(s=null==c?void 0:c.trim)||void 0===s?void 0:s.call(c)))return a(`subPackages[${o}].name 不能为空`);if(!(null===(r=null==p?void 0:p.trim)||void 0===r?void 0:r.call(p)))return a(`subPackages[${o}].root 不能为空`);if(!(null===(i=null==l?void 0:l.trim)||void 0===i?void 0:i.call(l)))return a(`subPackages[${o}].root 不能为 ${p}`);if("/"===u)return a(`subPackages[${o}].root 不能在根目录下 ${p}`);const g=h(n,l);if(!e.existsSync(`${g}.js`)&&!e.existsSync(`${g}.ts`))return a(`未找到 subPackages[${o}].root 对应的 ${l}.(js|ts)`);for(let e=o+1;e<t.subPackages.length;e++){const{name:n,entry:s,dirname:r}=t.subPackages[e];if(c===n)return a(`subPackages[${o}].name 和 subPackages[${e}].name 相同`);if(l===s)return a(`subPackages[${o}].root 和 subPackages[${e}].root 指向相同文件`);if(u===r)return a(`subPackages[${o}].root 和 subPackages[${e}].root 在同一目录下`);if(u.startsWith(`${r}/`)||r.startsWith(`${u}/`))return a(`subPackages[${o}].root 和 subPackages[${e}].root 目录层级存在包含关系`)}}return t.networkTimeout&&"object"!=typeof t.networkTimeout?a("networkTimeout 字段需为 object"):!t.workers||"workers"===t.workers||"object"==typeof t.workers||a("workers 字段需为 string | object")})(o,t,n)?{appJSON:o}:{appJSON:{pages:[],subPackages:[]}}};class U{constructor(t){this.project=t}}const X=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},Q=(t,e)=>{let n=[];((null==t?void 0:t.subPackages)||[]).forEach((t=>{n=n.concat(t.pages)}));const o=n.find((t=>t.path===e))||{};return(null==o?void 0:o.usingComponents)||{}},Y=t=>(null==t?void 0:t.usingComponents)||{};const Z=(t,e)=>{var n;const o=t.componentsMap||{};return(null===(n=null==o?void 0:o[e])||void 0===n?void 0:n.usingComponents)||{}},tt=require("chokidar"),et={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class nt extends r.EventEmitter{constructor(t){super(),this._projectJson="project.config.json",this._appJson="app.json",this._extJson="ext.json",this._projectPath="",this._extJsonDir="",this._canUploadExtJson=!1,this._appMode=b,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=l((()=>{var t,e,n,o;const{projectConfig:r}=this.snapshot,i=D(this.projectJsonPath);this._projectJsonContent=i,this.snapshot.projectConfig=s.cloneDeep(i),this.emit("project-config-json-changed",{oldData:r,newData:i}),(null==r?void 0:r.libVersion)!==(null==i?void 0:i.libVersion)&&this.emit("lib-version-changed",{oldData:null==r?void 0:r.libVersion,newData:null==i?void 0:i.libVersion}),(null===(t=null==r?void 0:r.setting)||void 0===t?void 0:t.enableSimulatorV2)!==(null===(e=null==i?void 0:i.setting)||void 0===e?void 0:e.enableSimulatorV2)&&this.emit("simulator-mode-changed",{oldData:null===(n=null==r?void 0:r.setting)||void 0===n?void 0:n.enableSimulatorV2,newData:null===(o=null==i?void 0:i.setting)||void 0===o?void 0:o.enableSimulatorV2})}),100),this.onProjectPrivateJsonChange=l((()=>{const{customEntry:t}=this.snapshot;this.updateCustomEntry();const e=this._customEntry;this.snapshot.customEntry=e,this.emit("project-private-json-changed"),this.emit("custom-entry-changed",{oldData:t,newData:e})}),100),this.onAppJsonChange=l((()=>{const{appJson:t}=this.snapshot,e=D(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=s.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=l((()=>{const{extJson:t}=this.snapshot,e=D(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=s.cloneDeep(e),this.emit(P,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,o,s;const r={pathName:"",query:"",entry:"",item:{}};try{if(!p.existsSync(t))return r;const i=D(t),a=(null===(n=null===(e=null==i?void 0:i.condition)||void 0===e?void 0:e.miniprogram)||void 0===n?void 0:n.current)||-1,c=null===(s=null===(o=null==i?void 0:i.condition)||void 0===o?void 0:o.miniprogram)||void 0===s?void 0:s.list;if(-1===a)return r;const l=c.filter((t=>t.id===a))[0];return l?(r.pathName=l.pathName,r.query=l.query,"halfPageNativeFunctionalized"===l.launchMode&&(r.query=l.query+="&runtime_mode=2"),r.entry=r.query?`${r.pathName}?${r.query}`:`${r.pathName}`,r):r}catch(t){return console.log("getCustomEntry error: ",t),r}},this.getCompileEntryPage=()=>{var t,e,n,o,s;const r=this.projectPrivateConfigJsonPath,i={path:"",query:"",launchMode:"default"};if(!p.existsSync(r))return i;const a=D(r),c=null===(e=null===(t=null==a?void 0:a.condition)||void 0===t?void 0:t.miniprogram)||void 0===e?void 0:e.current,l=null===(s=null===(o=null===(n=null==a?void 0:a.condition)||void 0===n?void 0:n.miniprogram)||void 0===o?void 0:o.list)||void 0===s?void 0:s.filter((t=>t.id===c))[0];return l?{path:l.pathName,query:l.query.trim(),launchMode:l.launchMode}:i};const{watch:e}=t;let n=t.projectPath;if(c.isAbsolute(n)||(n=c.join(i.cwd(),n)),this.checker=new U(this),this._projectPath=u(n),this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",p.existsSync(c.posix.join(this._projectPath,"game.json"))?this._appMode=j:this.projectJsonContent.compileType===v?this._appMode=v:this._appMode=b,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this.appMode===j){const{appJSON:t}=K(this.projectPath);this.appJSON=t}else{const{appJSON:t}=H(this.projectPath,this.miniprogramDir,this.extJsonDir,this.appMode);this.appJSON=t,this.updateAppJson()}e&&this.watchProject()}get projectPath(){return this._projectPath}get projectJsonPath(){return c.resolve(this._projectPath,this._projectJson)}get miniprogramRoot(){return this.projectJsonContent.miniprogramRoot||""}get projectMiniprogramPath(){const t=u(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get miniprogramDir(){const t=u(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get appMode(){return this._appMode}get lazyload(){return this._isLazyload}get projectJsonContent(){return p.pathExistsSync(this.projectJsonPath)?(this._projectJsonContent||(this._projectJsonContent=D(this.projectJsonPath)),this._projectJsonContent):{}}set projectJsonContent(t){this._projectJsonContent=t,p.writeJSONSync(this.projectJsonPath,t,{spaces:2})}get projectPrivateConfigJsonPath(){return c.resolve(this._projectPath,"project.private.config.json")}get entryJson(){return this.appMode===j?"game.json":"app.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=D(this.projectJsonPath);let e=".";return t.miniprogramRoot&&(e=t.miniprogramRoot),c.resolve(this._projectPath,e,this.entryJson)}get appJsonContent(){if(!this.entryJsonPath||!p.existsSync(this.entryJsonPath))throw new Error(`打开项目失败: ${this.entryJson}不存在`);return this._appJsonContent||(this._appJsonContent=D(this.entryJsonPath)),this._appJsonContent}get extJsonPath(){return c.resolve(this._projectPath,this._extJson)}get extJsonContent(){return this.extJsonPath&&p.existsSync(this.extJsonPath)?(this._extJsonContent||(this._extJsonContent=D(this.extJsonPath)||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(f))}get canUploadExtJson(){return this._canUploadExtJson}get customEntry(){return this._customEntry}get projectAppId(){let t="";try{t=this.projectJsonContent.appid}catch(t){console.error("get projectAppId error:",t)}if(!t)throw new Error("appid can not be empty!!!");return t}watchProject(){this.snapshot=s.cloneDeep({projectConfig:this.projectJsonContent,appJson:this.appJsonContent,extJson:this.extJsonContent,customEntry:this._customEntry});const t=tt.watch(this.projectPath,{persistent:!0,ignoreInitial:!0,ignored:/node_modules/});return t.on("change",(t=>{this.onFileChange("change",t)})).on("add",(t=>{this.onFileChange("add",t)})).on("unlink",(t=>{this.onFileChange("unlink",t)})),t}onFileChange(t,e){const n=c.relative(this.projectPath,e);"change"===t&&("project.config.json"===n?this.onProjectConfigJsonChange():"project.private.config.json"===n?this.onProjectPrivateJsonChange():"ext.json"===n?this.onExtJsonChange():"app.json"===n&&this.onAppJsonChange()),this.emit("onFileChange",{eventName:t,filePath:e,relativePath:n})}checkFileExist(){if(!this._projectPath)return;const t=this.projectJsonPath;p.existsSync(t)||p.writeJSONSync(this.projectJsonPath,et,{spaces:2})}updateCustomEntry(){const t=this.getCustomEntry(this.projectPrivateConfigJsonPath),e=(null==t?void 0:t.entry)||"";this._customEntry=e}get settings(){var t;return{minified:!0,urlCheck:!0,useNewCompiler:!1,enableV2:!1,...(null===(t=this.projectJsonContent)||void 0===t?void 0:t.setting)||{}}}changeCompileSetting(t){const e=this.projectJsonContent;e.setting?e.setting={...e.setting,...t}:e.setting=t,this.projectJsonContent=e}createDefaultProjectConfig(t){const e={description:"项目配置文件",setting:{urlCheck:!0,minified:!0},compileType:"miniprogram",libVersion:"latest",appid:"wx93e095ba089dbfb5",projectname:c.basename(t)},n=c.resolve(t,this._projectJson);p.writeJSONSync(n,e,{spaces:2})}getSubPackages(){return this.appJSON.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e.root}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e.root}/`)))}getComponentsMap(){return this.appJSON.componentsMap||{}}getSketchMap(){return this.appJSON.sketchMap||{}}getPages(){return this.appJSON.pages||[]}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const o=c.extname(e);return n.some((t=>`${t}${o}`===e))}getPageDependComponentPaths(t,e){const n=function(t){var e,n;if(!(null==t?void 0:t.subPackages)||!(null==t?void 0:t.subPackages.length))return!1;let o=!1;try{o=!("string"==typeof(null===(n=null===(e=null==t?void 0:t.subPackages)||void 0===e?void 0:e[0].pages)||void 0===n?void 0:n[0]))}catch(t){}return o}(this.appJSON),o=((t,e,n=!1,o=!0)=>{try{const s=new Set;let r={};r=o?{...n?{}:Y(e),...X(e,t),...Q(e,t)}:{...Y(e),...X(e,t)};const i=t=>{for(const n in t)if(t[n]){const o=t[n];if(s.has(o))continue;s.add(o);const r=Z(e,o);r&&Object.keys(r).length&&i(r)}};return i(r),Array.from(s)}catch(t){return console.error("parse page depend components has error happen"),[]}})(t,this.appJSON,e,n);return o}formatGameAppJSON(t){return t.map((t=>{let e=u(`${t.root}`).replace(/^\/+|\/+$/g,"");if(/\.(ts|js)$/.test(e)){const t=e.split("/").filter(Boolean);e=t.slice(0,t.length-1).join("/")}return{root:e}}))}static deSerialize(t){return new nt(t)}updateAppJson(){const{appJSON:t}=this;for(const e of t.pages)this.jsonDependencieMap.add(c.normalize(`${this.projectPath}/${e.path}.json`));const e=this.getSubPackages();if(null==e?void 0:e.length)for(const t of e)for(const e of t.pages)this.jsonDependencieMap.add(c.normalize(`${this.projectPath}/${e.path}.json`));for(const e of Object.keys(t.componentsMap))this.jsonDependencieMap.add(c.normalize(`${this.projectPath}/${e}.json`));this.jsonDependencies=Array.from(this.jsonDependencieMap)}}const ot={transformAppJson:H,transformGameJson:K,formatGameAppJSON:t=>{var e;const n=s.cloneDeep(t);return n.subPackages=(null===(e=n.subPackages)||void 0===e?void 0:e.map((t=>{const{entry:e,dirname:n,name:o,independent:s,rawRoot:r}=t;return{independent:s,root:n,path:e,rawRoot:r,name:o,pages:[{path:e}]}})))||[],n.pages=[{path:"/"}],Reflect.deleteProperty(n,"subpackages"),n.compileType=j,n}};exports.Project=nt,exports.utils=ot;
1
+ "use strict";var t=require("path"),e=require("fs-extra"),n=require("xhs-mp-shared"),o=require("json-to-ast"),s=require("lodash"),r=require("events"),i=require("process");function a(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,o.get?o:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var c=a(t),p=a(e);require("crypto"),require("net");const l=(t,e=100)=>{let n=null;return function(...o){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,o))}},u=(t="")=>t.split(c.sep).join(c.posix.sep),h=(...t)=>u(c.join(...t)),g=(...t)=>u(c.resolve(...t)),d=c.posix.sep,f=t=>{if(!u(t).includes(d))return"/";const e=u(t).split(d).filter(Boolean);return e.slice(0,e.length-1).join(d)},m="baba:changeExtJsonPath",P="baba:changeExtJson",b="miniprogram",j="minigame",v="miniwidget";require("util").inspect.custom;const y=(t,e)=>{const n=new Error(["\n<xhsml!",`[error:文件内容错误]:[file:${null==t?void 0:t.trim()}]`,e,"!xhsml>\n"].join("\n"));throw n.stack=void 0,n};function k(t,e){if(!Array.isArray(t))return e("pages 字段需为 array");for(let n=0;n<t.length;n++){const o=t[n];if(o.startsWith("/"))return e(`pages[${n}] 不应该以 '/' 开头`);if(o.startsWith("."))return e(`pages[${n}] 不应该以 '.' 开头`)}}const $=(t,e)=>t===e||(!(!t.startsWith(e)||!t.replace(e,"").startsWith("/"))||!(!e.startsWith(t)||!e.replace(t,"").startsWith("/")));function J(t,e,n){var o,s;if(!Array.isArray(t))return n("subPackages 字段需为 array");const r=[],i=[];for(let e=0;e<t.length;e++){if("string"!=typeof(null===(s=null===(o=t[e].root)||void 0===o?void 0:o.trim)||void 0===s?void 0:s.call(o)))return n(`subPackages[${e}].root 字段类型需为 string`);const a=t[e].root;if(a.startsWith("/"))return n(`subPackages[${e}].root 不应该以 '/' 开头`);if(a.startsWith("."))return n(`subPackages[${e}].root 不应该以 '.' 开头`);if(!Array.isArray(t[e].pages))return n(`subPackages[${e}].pages 类型需为array`);t[e].pages.length;for(let o=0;o<t[e].pages.length;o++){const s=t[e].pages[o];if(s.startsWith("/"))return n(`subPackages[${e}].pages[${o}] 不应该以 '/' 开头`);if(s.startsWith("."))return n(`subPackages[${e}].pages[${o}] 不应该以 '.' 开头`)}if(t[e].name){const o=i.indexOf(t[e].name);if(o>-1)return n(`subPackages[${e}] 与 subPackages[${o}] 的 name 相同`);i.push(t[e].name)}r.push(t[e].root)}for(let t=0;t<r.length;t++){const o=e.findIndex((e=>$(e,r[t])));if(o>-1)return n(`pages[${o}] 与 subPackages[${t}] 存在包含关系`);for(let e=t+1;e<r.length;e++)if($(r[t],r[e]))return n(`subPackages[${t}] 与 subPackages[${e}] 存在包含关系`)}}function C(t,e){return _(t)?null==t.navigationBarTextStyle||["black","white"].includes(t.navigationBarTextStyle)?null==t.backgroundTextStyle||["dark","light"].includes(t.backgroundTextStyle)?null==t.navigationStyle||["default","custom"].includes(t.navigationStyle)?void 0:e("window.navigationStyle 错误: 仅支持 default / custom"):e("window.backgroundTextStyle 错误: 仅支持 dark / light"):e("window.navigationBarTextStyle 错误: 仅支持 black / white"):e("window 字段需为 object")}function x(t,e){var n;if(!_(t))return e("tabBar 字段需为 object");if(!Array.isArray(null==t?void 0:t.list))return e("tabBar.list 字段需为 array");if(!(null===(n=null==t?void 0:t.list)||void 0===n?void 0:n.length))return e("tabBar.list 不能为空");if(t.list.length<2)return e("tabBar.list 错误: tabBar.list 需至少包含 2 项");if(t.list.length>5)return e("tabBar.list 错误: tabBar.list 不能超过 5 项");for(let n=0;n<t.list.length;n++){const{pagePath:o}=t.list[n];if(!o)return e(`tabBar.list[${n}] 错误: "pagePath" 不能为空`);if(o.startsWith("/"))return e(`tabBar.list[${n}].pagePath 不应该以 '/' 开头`);if(o.startsWith("."))return e(`tabBar.list[${n}].pagePath 不应该以 '.' 开头`)}}function S(t,e){if(!_(t))return e("usingComponents 字段需为 object")}function w(t,e){if(!_(t))return e("networkTimeout 字段需为 object");const n=["request","connectSocket","uploadFile","downloadFile"];for(const o of n)if(null!=t[o]&&"number"!=typeof t[o])return e(`networkTimeout.${o} 字段需为 number`)}function _(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const O=["window","networkTimeout","tabBar"],E=(t,e,n)=>{if(!((t,e,n)=>{var o,s,r,i,a,c;const p=t=>y(n,t),l=t.pages,u=t.subpackages||t.subPackages||[],g=t.usingComponents||{};if("pages"in e){k(e.pages,p);for(let t=0;t<e.pages.length;t++)if(!l.includes(e.pages[t]))return p(`pages[${t}]: "${e.pages[t]}" 不在 app.json pages 内`)}if("subpackages"in e||"subPackages"in e){J(e.subpackages||e.subPackages,e.pages?e.pages:l,p);const t=e.subpackages||e.subPackages||[];for(let e=0;e<t.length;e++){const n=t[e],s=u.find((t=>t.root===n.root));if(!s)return p(`subPackages[${e}].root: "${n.root}" 不在 app.json subPackages 内`);for(let t=0;t<n.pages.length;t++){const r=n.pages[t];if(!(null===(o=s.pages)||void 0===o?void 0:o.includes(r)))return p(`subPackages[${e}].pages[${t}]: "${r}" 不在 app.json subPackages 内`)}}}if("extPages"in e){if(!_(e.extPages))return p("extPages 字段需为 object");const t=u.flatMap((t=>t.pages.map((e=>h(t.root,e)))));for(const n of Object.keys(e.extPages))if(!l.includes(n)&&!t.includes(n))return p(`extPages["${n}"] 不在 app.json pages | subPackages 内`)}if("tabBar"in e){if(!_(e.tabBar))return p("tabBar 字段需为 object");if("list"in e.tabBar){x(e.tabBar,p);for(const{pagePath:t}of e.tabBar.list)if(null===(s=e.pages)||void 0===s?void 0:s.length){if(!e.pages.includes(t))return p(`tabBar.list 错误: "${t}" 不在 ext.json pages 内`)}else if(!l.includes(t))return p(`tabBar.list 错误: "${t}" 不在 app.json pages 内`)}}if((null===(r=e.pages)||void 0===r?void 0:r.length)&&!(null===(a=null===(i=e.tabBar)||void 0===i?void 0:i.list)||void 0===a?void 0:a.length)&&t.tabBar)for(let n=0;n<(null===(c=t.tabBar.list)||void 0===c?void 0:c.length);n++){const o=t.tabBar.list[n];if(!e.pages.includes(o.pagePath))return p(`ext.json pages 未包含 "${o.pagePath}",此页面在 app.json tabBar.list[${n}] 中被声明`)}if("window"in e&&C(e.window,p),"networkTimeout"in e&&w(e.networkTimeout,p),"usingComponents"in e){S(e.usingComponents,p);for(const t in e.usingComponents)if(!g[t])return p(`usingComponents["${t}"] 不在 app.json usingComponents 内`)}return!0})(t,e,n))return t;for(const[n,o]of Object.entries(e))if("extPages"!==n)if("object"==typeof o&&O.includes(n)){const e=Array.isArray(o);t[n]=t[n]||(e?[]:{}),t[n]=e?[...new Set([...t[n],...o])]:{...t[n],...o}}else t[n]=o;return t},B=t=>{try{const n=e.readFileSync(`${t}`,"utf-8");return o(n,{loc:!0,source:t}),JSON.parse(n)}catch(e){return y(t||e.source,e.message),{pages:[]}}};let D=1;const M={};function N(t,n){if("win32"!==process.platform)return!0;try{if(!t.startsWith(n+"/"))return!1;const o=t.substring(n.length+1).split("/").filter(Boolean);let s=n;for(const t of o){if(M[s]||(M[s]=e.readdirSync(s)),!M[s].includes(t))return!1;s=s+"/"+t}return!0}catch{return!1}}function T(t,n,o="Component"){const s=e.existsSync(`${t}.js`),r=e.existsSync(`${t}.ts`),i=e.existsSync(`${t}.json`);return!(!s&&!r)&&(!(s&&!N(`${t}.js`,n))&&(!(r&&!N(`${t}.ts`,n))&&!!("Component"!==o||i&&N(`${t}.json`,n))))}const W=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件,且文件名大小写与配置内容一致`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且文件名大小写与配置内容一致,且json中需设置component字段为true`,A=(t,e)=>t.replace(e.endsWith("/")?e:`${e}/`,""),q=(n,o,s)=>{if(o.startsWith("plugin://"))return y(s,`小红书小程序暂不支持插件:${o}`);let r="";if(o.startsWith("/")&&T(r=h(n,o),n))return A(r,n);if(o.startsWith(".")&&T(r=g(t.dirname(s),o),n))return A(r,n);if(T(r=g(n,o),n))return A(r,n);if(T(r=g(t.dirname(s),o),n))return A(r,n);const i=((t,n)=>{try{const o=`${t}/package.json`;if(e.existsSync(o)){const s=B(o),r=Object.keys(s.dependencies||[]).find((t=>n.includes(t)))||"",i=`${t}/node_modules/${r}/package.json`;if(e.existsSync(i)){const e=n.replace(r,"").replace(/^\//,""),o=B(i);let s=o.miniprogram||o.main||"";s=/\.(ts|js)$/.test(s)?s.replace(/\.(js|ts)$/,""):s;let a=(e||s||"").trim();const c=h(t,"node_modules",r,a).replace(`${t}/`,"");if(T(`${t}/${c}`,t))return c;if(T(`${t}/${c}/index`,t))return`${c}/index`}}}catch(t){}})(n,o);return i||y(s,`${W("Component")}:${o}`)},R=/\.|\/|\.\/|\.\.\//g;var F;!function(t){t.SKETCH="骨架屏",t.COMPONENT="组件"}(F||(F={}));const z=t=>t.endsWith("/")?t:`${t}/`,V=(t,e,n,o,s,r)=>{if((null==r?void 0:r.independent)&&(null==r?void 0:r.root)&&!t.startsWith(z(r.root)))return y(o,`独立分包${r.root}只可引用${r.root}目录下的${s}:${e}`);if((null==r?void 0:r.root)&&!t.startsWith(z(r.root))){for(const i of n.subPackages)if((null==r?void 0:r.root)!==i.root&&t.startsWith(z(i.root)))return y(o,`分包${null==r?void 0:r.root}不可引用分包${i.root}目录下的${s}:${e}`)}else if(!r)for(const r of n.subPackages)if(t.startsWith(z(r.root)))return y(o,`主包不可引用分包${r.root}目录下的${s}:${e}`)},L=(t,e,n,o,s)=>{const r={};for(const[i,a]of Object.entries(t)){let t;R.test(a)?(t=q(n,a,o),H(e,n,t,s)):t=a,r[i]=t}return r},I=(t,e,n,o,s)=>{const r={};for(const[i,a]of Object.entries(t)){const t=q(n,a,o);r[i]=t,V(t,a,e,o,F.COMPONENT,s),H(e,n,t,s)}return r},G=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),H=(t,e,n,o)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=g(e,`${n}.json`),r=B(s);t.componentsMap[n]=r,t.componentsMap[n].index=D++,G(r.componentPlaceholder)?r.componentPlaceholder=L(r.componentPlaceholder,t,e,s,o):Reflect.deleteProperty(r,"componentPlaceholder"),G(r.usingComponents)?r.usingComponents=I(r.usingComponents,t,e,s,o):Reflect.deleteProperty(r,"usingComponents")},K=(o,s,r,i,a)=>o.map((o=>{var c,p;let l={path:o};const u=g(s,`${o}.json`);if(e.existsSync(u)){let d,f=B(u);l={...l,...f},(d=null==i?void 0:i[o])&&(l={...l,...d}),l.path=o,G(l.componentPlaceholder)?l.componentPlaceholder=L(l.componentPlaceholder,r,s,u,a):Reflect.deleteProperty(l,"componentPlaceholder"),G(l.usingComponents)?l.usingComponents=I(l.usingComponents,r,s,u,a):Reflect.deleteProperty(l,"usingComponents");const m=null===(c=null==f?void 0:f.skeleton)||void 0===c?void 0:c.path;if(G(f.skeleton)&&m&&"string"==typeof m){let o="";if(o=m.startsWith("/")?h(s,m):g(t.dirname(u),m),!n.SKETCH_ML_REG.test(o))return y(u,`骨架屏后缀需为 ${n.SKETCH_ML_REG} 格式:${m}`);if(!e.existsSync(o))return y(u,`骨架屏不存在:${m}`);const i=A(o,s);V(i,m,r,u,F.SKETCH,a),l.skeleton={path:i.replace(n.ML_COMMON_EXT,""),options:(null===(p=f.skeleton)||void 0===p?void 0:p.options)||{}}}else Reflect.deleteProperty(l,"skeleton")}return l})),U=(t,e,n,o)=>{D=1;const s=g(e,"app.json");let r=B(s);r.subPackages=r.subPackages||r.subpackages||[],r=((t,e,n)=>{var o,s;const r=t=>y(e,t);if(!("pages"in t))return r("缺少 pages 字段");if(!Array.isArray(t.pages))return r("pages 字段需为 array");if(t.pages.length<=0)return r("pages 不能为空");if(k(t.pages,r),n===v){if(1!==(null===(o=t.pages)||void 0===o?void 0:o.length))return r("小组件有且仅有一个页面");if((null===(s=(null==t?void 0:t.subPackages)||(null==t?void 0:t.subpackages))||void 0===s?void 0:s.length)>0)return r("小组件不允许存在分包,请删除 subpackages 或 subPackages 字段");if((null==t?void 0:t.tabBar)&&Object.keys(t.tabBar).length>0)return r("小组件不允许存在 tabBar,请删除 tabBar 字段")}if("tabBar"in t){x(t.tabBar,r);for(const{pagePath:e}of t.tabBar.list)if(!t.pages.includes(e))return r(`tabBar.list 错误: "${e}" 不在 "pages" 内`)}return("subpackages"in t||"subPackages"in t)&&J(t.subPackages||t.subpackages,t.pages,r),"window"in t&&C(t.window,r),"networkTimeout"in t&&w(t.networkTimeout,r),"usingComponents"in t&&S(t.usingComponents,r),t})(r,s,o),r.compileType=o;const i=`${n}/ext.json`,a=n?B(i):{};let c={};return((null==a?void 0:a.extEnable)&&(null==a?void 0:a.directCommit)||(null==a?void 0:a.extEnable))&&(r=E(r,a,i),c=a.extPages||{}),r.componentsMap={},G(r.usingComponents)?r.usingComponents=I(r.usingComponents,r,e,s):Reflect.deleteProperty(r,"usingComponents"),r.pages.map(((t,n)=>{if(!T(g(e,t),e,"Page"))return y(s,`${W("Page")}:${t} in pages[${n}]`)})),r.pages=K(r.pages,e,r,c),r.subPackages=r.subPackages.map(((t,n)=>{const o=t.pages.map(((o,r)=>{const i=h(t.root,o);return T(g(e,i),e,"Page")?i:y(s,`${W("Page")}:${o} in subPackages[${n}].pages[${r}] = ${i}`)}));return{...t,pages:K(o,e,r,c,t)}})),r.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0},{appJSON:r}},X=t=>{t=u(t);const n=g(t,"game.json");let o=B(n);return o.subPackages=o.subPackages||o.subpackages||[],o.pages=[],o.subPackages=o.subPackages.map((t=>{let e=u(`${t.root}`).replace(/^\/+|\/+$/g,"");e=/\.(ts|js)$/.test(e)?e.replace(/\.(ts|js)$/,""):h(e,"game");const n={rawRoot:`${t.root}`,entry:e,dirname:f(e)};return{...t,...n}})),((t,n,o)=>{var s,r,i;const a=t=>(y(o||"game.json",t),!1);if(t.subPackages.length)for(let o=0;o<t.subPackages.length;o++){const{name:c,root:p,entry:l,dirname:u}=t.subPackages[o];if("string"!=typeof c)return a(`subPackages[${o}].name 字段类型需为 string`);if("string"!=typeof p)return a(`subPackages[${o}].root 字段类型需为 string`);if(!(null===(s=null==c?void 0:c.trim)||void 0===s?void 0:s.call(c)))return a(`subPackages[${o}].name 不能为空`);if(!(null===(r=null==p?void 0:p.trim)||void 0===r?void 0:r.call(p)))return a(`subPackages[${o}].root 不能为空`);if(!(null===(i=null==l?void 0:l.trim)||void 0===i?void 0:i.call(l)))return a(`subPackages[${o}].root 不能为 ${p}`);if("/"===u)return a(`subPackages[${o}].root 不能在根目录下 ${p}`);const g=h(n,l);if(!e.existsSync(`${g}.js`)&&!e.existsSync(`${g}.ts`))return a(`未找到 subPackages[${o}].root 对应的 ${l}.(js|ts)`);for(let e=o+1;e<t.subPackages.length;e++){const{name:n,entry:s,dirname:r}=t.subPackages[e];if(c===n)return a(`subPackages[${o}].name 和 subPackages[${e}].name 相同`);if(l===s)return a(`subPackages[${o}].root 和 subPackages[${e}].root 指向相同文件`);if(u===r)return a(`subPackages[${o}].root 和 subPackages[${e}].root 在同一目录下`);if(u.startsWith(`${r}/`)||r.startsWith(`${u}/`))return a(`subPackages[${o}].root 和 subPackages[${e}].root 目录层级存在包含关系`)}}return t.networkTimeout&&"object"!=typeof t.networkTimeout?a("networkTimeout 字段需为 object"):!t.workers||"workers"===t.workers||"object"==typeof t.workers||a("workers 字段需为 string | object")})(o,t,n)?{appJSON:o}:{appJSON:{pages:[],subPackages:[]}}};class Q{constructor(t){this.project=t}}const Y=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},Z=(t,e)=>{let n=[];((null==t?void 0:t.subPackages)||[]).forEach((t=>{n=n.concat(t.pages)}));const o=n.find((t=>t.path===e))||{};return(null==o?void 0:o.usingComponents)||{}},tt=t=>(null==t?void 0:t.usingComponents)||{};const et=(t,e)=>{var n;const o=t.componentsMap||{};return(null===(n=null==o?void 0:o[e])||void 0===n?void 0:n.usingComponents)||{}},nt=require("chokidar"),ot={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class st extends r.EventEmitter{constructor(t){super(),this._projectJson="project.config.json",this._appJson="app.json",this._extJson="ext.json",this._projectPath="",this._extJsonDir="",this._canUploadExtJson=!1,this._appMode=b,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=l((()=>{var t,e,n,o;const{projectConfig:r}=this.snapshot,i=B(this.projectJsonPath);this._projectJsonContent=i,this.snapshot.projectConfig=s.cloneDeep(i),this.emit("project-config-json-changed",{oldData:r,newData:i}),(null==r?void 0:r.libVersion)!==(null==i?void 0:i.libVersion)&&this.emit("lib-version-changed",{oldData:null==r?void 0:r.libVersion,newData:null==i?void 0:i.libVersion}),(null===(t=null==r?void 0:r.setting)||void 0===t?void 0:t.enableSimulatorV2)!==(null===(e=null==i?void 0:i.setting)||void 0===e?void 0:e.enableSimulatorV2)&&this.emit("simulator-mode-changed",{oldData:null===(n=null==r?void 0:r.setting)||void 0===n?void 0:n.enableSimulatorV2,newData:null===(o=null==i?void 0:i.setting)||void 0===o?void 0:o.enableSimulatorV2})}),100),this.onProjectPrivateJsonChange=l((()=>{const{customEntry:t}=this.snapshot;this.updateCustomEntry();const e=this._customEntry;this.snapshot.customEntry=e,this.emit("project-private-json-changed"),this.emit("custom-entry-changed",{oldData:t,newData:e})}),100),this.onAppJsonChange=l((()=>{const{appJson:t}=this.snapshot,e=B(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=s.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=l((()=>{const{extJson:t}=this.snapshot,e=B(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=s.cloneDeep(e),this.emit(P,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,o,s;const r={pathName:"",query:"",entry:"",item:{}};try{if(!p.existsSync(t))return r;const i=B(t),a=(null===(n=null===(e=null==i?void 0:i.condition)||void 0===e?void 0:e.miniprogram)||void 0===n?void 0:n.current)||-1,c=null===(s=null===(o=null==i?void 0:i.condition)||void 0===o?void 0:o.miniprogram)||void 0===s?void 0:s.list;if(-1===a)return r;const l=c.filter((t=>t.id===a))[0];return l?(r.pathName=l.pathName,r.query=l.query,"halfPageNativeFunctionalized"===l.launchMode&&(r.query=l.query+="&runtime_mode=2"),r.entry=r.query?`${r.pathName}?${r.query}`:`${r.pathName}`,r):r}catch(t){return console.log("getCustomEntry error: ",t),r}},this.getCompileEntryPage=()=>{var t,e,n,o,s;const r=this.projectPrivateConfigJsonPath,i={path:"",query:"",launchMode:"default"};if(!p.existsSync(r))return i;const a=B(r),c=null===(e=null===(t=null==a?void 0:a.condition)||void 0===t?void 0:t.miniprogram)||void 0===e?void 0:e.current,l=null===(s=null===(o=null===(n=null==a?void 0:a.condition)||void 0===n?void 0:n.miniprogram)||void 0===o?void 0:o.list)||void 0===s?void 0:s.filter((t=>t.id===c))[0];return l?{path:l.pathName,query:l.query.trim(),launchMode:l.launchMode}:i};const{watch:e}=t;let n=t.projectPath;if(c.isAbsolute(n)||(n=c.join(i.cwd(),n)),this.checker=new Q(this),this._projectPath=u(n),this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",p.existsSync(c.posix.join(this._projectPath,"game.json"))?this._appMode=j:this.projectJsonContent.compileType===v?this._appMode=v:this._appMode=b,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this.appMode===j){const{appJSON:t}=X(this.projectPath);this.appJSON=t}else{const{appJSON:t}=U(this.projectPath,this.miniprogramDir,this.extJsonDir,this.appMode);this.appJSON=t,this.updateAppJson()}e&&this.watchProject()}get projectPath(){return this._projectPath}get projectJsonPath(){return c.resolve(this._projectPath,this._projectJson)}get miniprogramRoot(){return this.projectJsonContent.miniprogramRoot||""}get projectMiniprogramPath(){const t=u(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get miniprogramDir(){const t=u(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get appMode(){return this._appMode}get lazyload(){return this._isLazyload}get projectJsonContent(){return p.pathExistsSync(this.projectJsonPath)?(this._projectJsonContent||(this._projectJsonContent=B(this.projectJsonPath)),this._projectJsonContent):{}}set projectJsonContent(t){this._projectJsonContent=t,p.writeJSONSync(this.projectJsonPath,t,{spaces:2})}get projectPrivateConfigJsonPath(){return c.resolve(this._projectPath,"project.private.config.json")}get entryJson(){return this.appMode===j?"game.json":"app.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=B(this.projectJsonPath);let e=".";return t.miniprogramRoot&&(e=t.miniprogramRoot),c.resolve(this._projectPath,e,this.entryJson)}get appJsonContent(){if(!this.entryJsonPath||!p.existsSync(this.entryJsonPath))throw new Error(`打开项目失败: ${this.entryJson}不存在`);return this._appJsonContent||(this._appJsonContent=B(this.entryJsonPath)),this._appJsonContent}get extJsonPath(){return c.resolve(this._projectPath,this._extJson)}get extJsonContent(){return this.extJsonPath&&p.existsSync(this.extJsonPath)?(this._extJsonContent||(this._extJsonContent=B(this.extJsonPath)||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(m))}get canUploadExtJson(){return this._canUploadExtJson}get customEntry(){return this._customEntry}get projectAppId(){let t="";try{t=this.projectJsonContent.appid}catch(t){console.error("get projectAppId error:",t)}if(!t)throw new Error("appid can not be empty!!!");return t}watchProject(){this.snapshot=s.cloneDeep({projectConfig:this.projectJsonContent,appJson:this.appJsonContent,extJson:this.extJsonContent,customEntry:this._customEntry});const t=nt.watch(this.projectPath,{persistent:!0,ignoreInitial:!0,ignored:/node_modules/});return t.on("change",(t=>{this.onFileChange("change",t)})).on("add",(t=>{this.onFileChange("add",t)})).on("unlink",(t=>{this.onFileChange("unlink",t)})),t}onFileChange(t,e){const n=c.relative(this.projectPath,e);"change"===t&&("project.config.json"===n?this.onProjectConfigJsonChange():"project.private.config.json"===n?this.onProjectPrivateJsonChange():"ext.json"===n?this.onExtJsonChange():"app.json"===n&&this.onAppJsonChange()),this.emit("onFileChange",{eventName:t,filePath:e,relativePath:n})}checkFileExist(){if(!this._projectPath)return;const t=this.projectJsonPath;p.existsSync(t)||p.writeJSONSync(this.projectJsonPath,ot,{spaces:2})}updateCustomEntry(){const t=this.getCustomEntry(this.projectPrivateConfigJsonPath),e=(null==t?void 0:t.entry)||"";this._customEntry=e}get settings(){var t;return{minified:!0,urlCheck:!0,useNewCompiler:!1,enableV2:!1,...(null===(t=this.projectJsonContent)||void 0===t?void 0:t.setting)||{}}}changeCompileSetting(t){const e=this.projectJsonContent;e.setting?e.setting={...e.setting,...t}:e.setting=t,this.projectJsonContent=e}createDefaultProjectConfig(t){const e={description:"项目配置文件",setting:{urlCheck:!0,minified:!0},compileType:"miniprogram",libVersion:"latest",appid:"wx93e095ba089dbfb5",projectname:c.basename(t)},n=c.resolve(t,this._projectJson);p.writeJSONSync(n,e,{spaces:2})}getSubPackages(){return this.appJSON.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e.root}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e.root}/`)))}getComponentsMap(){return this.appJSON.componentsMap||{}}getSketchMap(){return this.appJSON.sketchMap||{}}getPages(){return this.appJSON.pages||[]}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const o=c.extname(e);return n.some((t=>`${t}${o}`===e))}getPageDependComponentPaths(t,e){const n=function(t){var e,n;if(!(null==t?void 0:t.subPackages)||!(null==t?void 0:t.subPackages.length))return!1;let o=!1;try{o=!("string"==typeof(null===(n=null===(e=null==t?void 0:t.subPackages)||void 0===e?void 0:e[0].pages)||void 0===n?void 0:n[0]))}catch(t){}return o}(this.appJSON),o=((t,e,n=!1,o=!0)=>{try{const s=new Set;let r={};r=o?{...n?{}:tt(e),...Y(e,t),...Z(e,t)}:{...tt(e),...Y(e,t)};const i=t=>{for(const n in t)if(t[n]){const o=t[n];if(s.has(o))continue;s.add(o);const r=et(e,o);r&&Object.keys(r).length&&i(r)}};return i(r),Array.from(s)}catch(t){return console.error("parse page depend components has error happen"),[]}})(t,this.appJSON,e,n);return o}formatGameAppJSON(t){return t.map((t=>{let e=u(`${t.root}`).replace(/^\/+|\/+$/g,"");if(/\.(ts|js)$/.test(e)){const t=e.split("/").filter(Boolean);e=t.slice(0,t.length-1).join("/")}return{root:e}}))}static deSerialize(t){return new st(t)}updateAppJson(){const{appJSON:t}=this;for(const e of t.pages)this.jsonDependencieMap.add(c.normalize(`${this.projectPath}/${e.path}.json`));const e=this.getSubPackages();if(null==e?void 0:e.length)for(const t of e)for(const e of t.pages)this.jsonDependencieMap.add(c.normalize(`${this.projectPath}/${e.path}.json`));for(const e of Object.keys(t.componentsMap))this.jsonDependencieMap.add(c.normalize(`${this.projectPath}/${e}.json`));this.jsonDependencies=Array.from(this.jsonDependencieMap)}}const rt={transformAppJson:U,transformGameJson:X,formatGameAppJSON:t=>{var e;const n=s.cloneDeep(t);return n.subPackages=(null===(e=n.subPackages)||void 0===e?void 0:e.map((t=>{const{entry:e,dirname:n,name:o,independent:s,rawRoot:r}=t;return{independent:s,root:n,path:e,rawRoot:r,name:o,pages:[{path:e}]}})))||[],n.pages=[{path:"/"}],Reflect.deleteProperty(n,"subpackages"),n.compileType=j,n}};exports.Project=st,exports.utils=rt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-project",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "author": "",
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
- "xhs-mp-shared": "2.1.1",
17
+ "xhs-mp-shared": "2.1.2",
18
18
  "chokidar": "^3.6.0",
19
19
  "fs-extra": "^11.2.0",
20
20
  "lodash": "^4.17.21",