xhs-mp-project 1.9.3 → 1.9.4

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 CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("path"),e=require("clone"),n=require("fs-extra"),s=require("json-to-ast"),o=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 s=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,s.get?s:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var c=a(t),p=a(n);require("crypto"),require("net");const u=(t,e=100)=>{let n=null;return function(...s){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,s))}},l=(t="")=>t.split(c.sep).join(c.posix.sep),h=(t="")=>t.split(c.sep).join(c.posix.sep),g=(...t)=>h(c.join(...t)),d=(...t)=>h(c.resolve(...t)),m=c.posix.sep,f=t=>{if(!h(t).includes(m))return"/";const e=h(t).split(m).filter(Boolean);return e.slice(0,e.length-1).join(m)};require("util").inspect.custom;const P=(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 j(t,e){if(!Array.isArray(t))return e("pages 字段需为 array");for(let n=0;n<t.length;n++){const s=t[n];if(s.startsWith("/"))return e(`pages[${n}] 不应该以 '/' 开头`);if(s.startsWith("."))return e(`pages[${n}] 不应该以 '.' 开头`)}}const b=(t,e)=>t===e||(!(!t.startsWith(e)||!t.replace(e,"").startsWith("/"))||!(!e.startsWith(t)||!e.replace(t,"").startsWith("/")));function y(t,e,n){var s,o;if(!Array.isArray(t))return n("subPackages 字段需为 array");const r=[],i=[];for(let e=0;e<t.length;e++){if("string"!=typeof(null===(o=null===(s=t[e].root)||void 0===s?void 0:s.trim)||void 0===o?void 0:o.call(s)))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`);for(let s=0;s<t[e].pages.length;s++){const o=t[e].pages[s];if(o.startsWith("/"))return n(`subPackages[${e}].pages[${s}] 不应该以 '/' 开头`);if(o.startsWith("."))return n(`subPackages[${e}].pages[${s}] 不应该以 '.' 开头`)}if(t[e].name){const s=i.indexOf(t[e].name);if(s>-1)return n(`subPackages[${e}] 与 subPackages[${s}] 的 name 相同`);i.push(t[e].name)}r.push(t[e].root)}for(let t=0;t<r.length;t++){const s=e.findIndex((e=>b(e,r[t])));if(s>-1)return n(`pages[${s}] 与 subPackages[${t}] 存在包含关系`);for(let e=t+1;e<r.length;e++)if(b(r[t],r[e]))return n(`subPackages[${t}] 与 subPackages[${e}] 存在包含关系`)}}function v(t,e){return x(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 J(t,e){var n;if(!x(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:s}=t.list[n];if(!s)return e(`tabBar.list[${n}] 错误: "pagePath" 不能为空`);if(s.startsWith("/"))return e(`tabBar.list[${n}].pagePath 不应该以 '/' 开头`);if(s.startsWith("."))return e(`tabBar.list[${n}].pagePath 不应该以 '.' 开头`)}}function k(t,e){if(!x(t))return e("usingComponents 字段需为 object")}function $(t,e){if(!x(t))return e("networkTimeout 字段需为 object");const n=["request","connectSocket","uploadFile","downloadFile"];for(const s of n)if(null!=t[s]&&"number"!=typeof t[s])return e(`networkTimeout.${s} 字段需为 number`)}function x(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const C=["window","networkTimeout","tabBar"],S=(t,e,n)=>{if(!((t,e,n)=>{const s=t=>P(n,t),o=t.pages;if("pages"in e){j(e.pages,s);for(const t of e.pages)if(!o.includes(t))return s(`page 错误: "${t}" 不在 app.json 内`)}if("extPages"in e){if(!x(e.extPages))return s("extPages 字段需为 object");for(const t of Object.keys(e.extPages))if(!o.includes(t))return s(`extPages 错误: "${t}" 不在 app.json 内`)}if(("subpackages"in e||"subPackages"in e)&&y(e.subpackages||e.subPackages,e.pages?e.pages:o,s),"tabBar"in e){if(!x(e.tabBar))return s("tabBar 字段需为 object");if("list"in e.tabBar){J(e.tabBar,s);for(const{pagePath:t}of e.tabBar.list)if(!o.includes(t))return s(`tabBar.list 错误: "${t}" 不在 app.json 内`)}}return"window"in e&&v(e.window,s),"networkTimeout"in e&&$(e.networkTimeout,s),"usingComponents"in e&&k(e.usingComponents,s),!0})(t,e,n))return t;for(const[n,s]of Object.entries(e))if("extPages"!==n)if("object"==typeof s&&C.includes(n)){const e=Array.isArray(s);t[n]=t[n]||(e?[]:{}),t[n]=e?[...new Set([...t[n],...s])]:{...t[n],...s}}else t[n]=s;return t},w=t=>{try{const e=n.readFileSync(`${t}`,"utf-8");return s(e,{loc:!0,source:t}),JSON.parse(e)}catch(e){return P(t||e.source,e.message),{pages:[]}}};let _=1;function O(t,e="Component"){return!(!n.existsSync(`${t}.js`)&&!n.existsSync(`${t}.ts`))&&("Component"!==e||n.existsSync(`${t}.json`))}const D=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,E=(e,s,o)=>{if(s.startsWith("plugin://"))return P(o,`小红书小程序暂不支持插件:${s}`);function r(t){return t.replace(`${e}/`,"")}let i="";if(s.startsWith("/")&&O(i=g(e,s)))return r(i);if(s.startsWith(".")&&O(i=d(t.dirname(o),s)))return r(i);if(O(i=d(e,s)))return r(i);if(O(i=d(t.dirname(o),s)))return r(i);const a=((t,e)=>{try{const s=`${t}/package.json`;if(n.existsSync(s)){const o=w(s),r=Object.keys(o.dependencies||[]).find((t=>e.includes(t)))||"",i=`${t}/node_modules/${r}/package.json`;if(n.existsSync(i)){const n=e.replace(r,"").replace(/^\//,""),s=w(i);let o=s.miniprogram||s.main||"";o=/\.(ts|js)$/.test(o)?o.replace(/\.(js|ts)$/,""):o;let a=(n||o||"").trim();const c=g(t,"node_modules",r,a).replace(`${t}/`,"");if(O(`${t}/${c}`))return c;if(O(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(e,s);return a||P(o,`${D("Component")}:${s}`)},N=/\.|\/|\.\/|\.\.\//g,A=(t,e,n,s)=>{const o={};for(const[r,i]of Object.entries(t)){let t;N.test(i)?(t=E(n,i,s),q(e,n,t)):t=i,o[r]=t}return o},B=(t,e,n,s)=>{const o={};for(const[r,i]of Object.entries(t)){const t=E(n,i,s);o[r]=t,q(e,n,t)}return o},M=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),q=(t,e,n)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=d(e,`${n}.json`),o=w(s);t.componentsMap[n]=o,t.componentsMap[n].index=_++,M(o.componentPlaceholder)?o.componentPlaceholder=A(o.componentPlaceholder,t,e,s):Reflect.deleteProperty(o,"componentPlaceholder"),M(o.usingComponents)?o.usingComponents=B(o.usingComponents,t,e,s):Reflect.deleteProperty(o,"usingComponents")},T=(t,e,s,o)=>t.map((t=>{var r,i;let a={path:t};const c=d(e,`${a.path}.json`);if(n.existsSync(c)){let t=w(c);(null===(r=s.extPages)||void 0===r?void 0:r[`${a.path}`])&&(t={...t,...null===(i=s.extPages)||void 0===i?void 0:i[`${a.path}`]}),a={...a,...t},M(a.componentPlaceholder)?a.componentPlaceholder=A(a.componentPlaceholder,o,e,c):Reflect.deleteProperty(a,"componentPlaceholder"),M(a.usingComponents)?a.usingComponents=B(a.usingComponents,o,e,c):Reflect.deleteProperty(a,"usingComponents")}return a})),W=(t,n,s)=>{_=1;const o=d(n,"app.json");let r=w(o);r.subPackages=r.subPackages||r.subpackages||[],r=((t,e)=>{const n=t=>P(e,t);if(!("pages"in t))return n("缺少 pages 字段");if(!Array.isArray(t.pages))return n("pages 字段需为 array");if(t.pages.length<=0)return n("pages 不能为空");if(j(t.pages,n),"tabBar"in t){J(t.tabBar,n);for(const{pagePath:e}of t.tabBar.list)if(!t.pages.includes(e))return n(`tabBar.list 错误: "${e}" 不在 "pages" 内`)}return("subpackages"in t||"subPackages"in t)&&y(t.subPackages||t.subpackages,t.pages,n),"window"in t&&v(t.window,n),"networkTimeout"in t&&$(t.networkTimeout,n),"usingComponents"in t&&k(t.usingComponents,n),t})(r,o);const i=s?w(`${s}/ext.json`):{};(null==i?void 0:i.extEnable)&&(r=S(r,i,`${s}/ext.json`)),r.componentsMap={},M(r.usingComponents)?r.usingComponents=B(r.usingComponents,r,n,o):Reflect.deleteProperty(r,"usingComponents"),r.pages.map(((t,e)=>{if(!O(d(n,t),"Page"))return P(o,`${D("Page")}:${t} in pages[${e}]`)})),r.pages=T(r.pages,n,i,r),r.subPackages=r.subPackages.map(((t,e)=>{const s=t.pages.map(((s,r)=>{const i=g(t.root,s);return O(d(n,i),"Page")?i:P(o,`${D("Page")}:${s} in subPackages[${e}].pages[${r}]`)}));return{...t,pages:T(s,n,i,r)}})),r.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0};const a=e(r);Reflect.deleteProperty(a,"subPackages");const c=r.subPackages.reduce(((t,e)=>[...t,...e.pages]),[]);return a.pages=c?[...a.pages,...c]:a.pages,{legacyAppJSON:a,appJSON:r}},R="baba:changeExtJsonPath",F="baba:changeExtJson",z="miniprogram",V="minigame",I=t=>{t=h(t);const e=d(t,"game.json");let s=w(e);return s.subPackages=s.subPackages||s.subpackages||[],s.pages=[],s.subPackages=s.subPackages.map((t=>{let e=h(`${t.root}`).replace(/^\/+|\/+$/g,"");e=/\.(ts|js)$/.test(e)?e.replace(/\.(ts|js)$/,""):g(e,"game");const n={rawRoot:`${t.root}`,entry:e,dirname:f(e)};return{...t,...n}})),((t,e,s)=>{var o,r,i;const a=t=>(P(s||"game.json",t),!1);if(t.subPackages.length)for(let s=0;s<t.subPackages.length;s++){const{name:c,root:p,entry:u,dirname:l}=t.subPackages[s];if("string"!=typeof c)return a(`subPackages[${s}].name 字段类型需为 string`);if("string"!=typeof p)return a(`subPackages[${s}].root 字段类型需为 string`);if(!(null===(o=null==c?void 0:c.trim)||void 0===o?void 0:o.call(c)))return a(`subPackages[${s}].name 不能为空`);if(!(null===(r=null==p?void 0:p.trim)||void 0===r?void 0:r.call(p)))return a(`subPackages[${s}].root 不能为空`);if(!(null===(i=null==u?void 0:u.trim)||void 0===i?void 0:i.call(u)))return a(`subPackages[${s}].root 不能为 ${p}`);if("/"===l)return a(`subPackages[${s}].root 不能在根目录下 ${p}`);const h=g(e,u);if(!n.existsSync(`${h}.js`)&&!n.existsSync(`${h}.ts`))return a(`未找到 subPackages[${s}].root 对应的 ${u}.(js|ts)`);for(let e=s+1;e<t.subPackages.length;e++){const{name:n,entry:o,dirname:r}=t.subPackages[e];if(c===n)return a(`subPackages[${s}].name 和 subPackages[${e}].name 相同`);if(u===o)return a(`subPackages[${s}].root 和 subPackages[${e}].root 指向相同文件`);if(l===r)return a(`subPackages[${s}].root 和 subPackages[${e}].root 在同一目录下`);if(l.startsWith(`${r}/`)||r.startsWith(`${l}/`))return a(`subPackages[${s}].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")})(s,t,e)?{appJSON:s}:{appJSON:{pages:[],subPackages:[]}}};class L{constructor(t){this.project=t}}const U=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},G=(t,e)=>{let n=[];((null==t?void 0:t.subPackages)||[]).forEach((t=>{n=n.concat(t.pages)}));const s=n.find((t=>t.path===e))||{};return(null==s?void 0:s.usingComponents)||{}},H=t=>(null==t?void 0:t.usingComponents)||{};const K=(t,e)=>{var n;const s=t.componentsMap||{};return(null===(n=null==s?void 0:s[e])||void 0===n?void 0:n.usingComponents)||{}},Q=require("chokidar"),X={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class Y 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=z,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=u((()=>{var t,e,n,s;const{projectConfig:r}=this.snapshot,i=w(this.projectJsonPath);this._projectJsonContent=i,this.snapshot.projectConfig=o.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===(s=null==i?void 0:i.setting)||void 0===s?void 0:s.enableSimulatorV2})}),100),this.onProjectPrivateJsonChange=u((()=>{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=u((()=>{const{appJson:t}=this.snapshot,e=w(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=o.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=u((()=>{const{extJson:t}=this.snapshot,e=w(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=o.cloneDeep(e),this.emit(F,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,s,o;const r={pathName:"",query:"",entry:"",item:{}};try{if(!p.existsSync(t))return r;const i=w(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===(o=null===(s=null==i?void 0:i.condition)||void 0===s?void 0:s.miniprogram)||void 0===o?void 0:o.list;if(-1===a)return r;const u=c.filter((t=>t.id===a))[0];return u?(r.pathName=u.pathName,r.query=u.query,"halfPageNativeFunctionalized"===u.launchMode&&(r.query=u.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,s,o;const r=this.projectPrivateConfigJsonPath,i={path:"",query:"",launchMode:"default"};if(!p.existsSync(r))return i;const a=w(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,u=null===(o=null===(s=null===(n=null==a?void 0:a.condition)||void 0===n?void 0:n.miniprogram)||void 0===s?void 0:s.list)||void 0===o?void 0:o.filter((t=>t.id===c))[0];return u?{path:u.pathName,query:u.query.trim(),launchMode:u.launchMode}:i};const{watch:e}=t;let n=t.projectPath;if(c.isAbsolute(n)||(n=c.join(i.cwd(),n)),this.checker=new L(this),this._projectPath=l(n),this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",this._appMode=p.existsSync(c.posix.join(this._projectPath,"game.json"))?V:z,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this._appMode===z){const{legacyAppJSON:t,appJSON:e}=W(this.projectPath,this.miniprogramDir,this.extJsonDir);this.appJSON=e,this.legacyAppJSON=t,this.updateAppJson()}else{const{appJSON:t}=I(this.projectPath);this.appJSON=t,this.legacyAppJSON={}}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=l(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get miniprogramDir(){const t=l(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=w(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===z?"app.json":"game.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=w(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=w(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=w(this.extJsonPath).ext||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(R))}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=o.cloneDeep({projectConfig:this.projectJsonContent,appJson:this.appJsonContent,extJson:this.extJsonContent,customEntry:this._customEntry});const t=Q.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,X,{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})}changeExtJsonPermission(t){this._canUploadExtJson=t,t&&p.existsSync(this.extJsonPath)?this.extJsonDir=this.projectPath:this.extJsonDir=""}getSubPackages(){return this.appJSON.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e}/`)))}getComponentsMap(){return this.appJSON.componentsMap||{}}getPages(){return this.appJSON.pages||[]}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const s=c.extname(e);return n.some((t=>`${t}${s}`===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 s=!1;try{s=!("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 s}(this.appJSON),s=((t,e,n=!1,s=!0)=>{try{const o=new Set;let r={};r=s?{...n?{}:H(e),...U(e,t),...G(e,t)}:{...H(e),...U(e,t)};const i=t=>{for(const n in t)if(t[n]){const s=t[n];if(o.has(s))continue;o.add(s);const r=K(e,s);r&&Object.keys(r).length&&i(r)}};return i(r),Array.from(o)}catch(t){return console.error("parse page depend components has error happen"),[]}})(t,this.appJSON,e,n);return s}formatGameAppJSON(t){return t.map((t=>{let e=h(`${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 Y(t)}updateAppJson(){const{legacyAppJSON:t}=this;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 Z={transformAppJson:W,transformGameJson:I,pureAppJSON:(t,e="v1")=>{var n,s;const r=o.cloneDeep(t);if("v1"===e&&(r.subPackages=(null===(n=r.subPackages)||void 0===n?void 0:n.map((t=>{const{entry:e,dirname:n,name:s,independent:o,rawRoot:r}=t;return{independent:o,root:n,path:e,rawRoot:r,name:s,pages:[{path:e}]}})))||[],r.pages=[{path:"/"}]),"v0"===e){const t=null===(s=r.subPackages)||void 0===s?void 0:s.map((t=>({path:t.entry})));Reflect.deleteProperty(r,"subPackages"),r.pages=[{path:"/"},...t]}return Reflect.deleteProperty(r,"subpackages"),r.compileType="minigame",r}};exports.Project=Y,exports.utils=Z;
1
+ "use strict";var t=require("path"),e=require("clone"),n=require("fs-extra"),s=require("json-to-ast"),o=require("lodash"),r=require("events"),a=require("process");function i(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var s=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,s.get?s:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,Object.freeze(e)}var c=i(t),p=i(n);require("crypto"),require("net");const u=(t,e=100)=>{let n=null;return function(...s){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,s))}},l=(t="")=>t.split(c.sep).join(c.posix.sep),g=(t="")=>t.split(c.sep).join(c.posix.sep),h=(...t)=>g(c.join(...t)),d=(...t)=>g(c.resolve(...t)),m=c.posix.sep,f=t=>{if(!g(t).includes(m))return"/";const e=g(t).split(m).filter(Boolean);return e.slice(0,e.length-1).join(m)};require("util").inspect.custom;const P=(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 j(t,e){if(!Array.isArray(t))return e("pages 字段需为 array");for(let n=0;n<t.length;n++){const s=t[n];if(s.startsWith("/"))return e(`pages[${n}] 不应该以 '/' 开头`);if(s.startsWith("."))return e(`pages[${n}] 不应该以 '.' 开头`)}}const b=(t,e)=>t===e||(!(!t.startsWith(e)||!t.replace(e,"").startsWith("/"))||!(!e.startsWith(t)||!e.replace(t,"").startsWith("/")));function y(t,e,n){var s,o;if(!Array.isArray(t))return n("subPackages 字段需为 array");const r=[],a=[];for(let e=0;e<t.length;e++){if("string"!=typeof(null===(o=null===(s=t[e].root)||void 0===s?void 0:s.trim)||void 0===o?void 0:o.call(s)))return n(`subPackages[${e}].root 字段类型需为 string`);const i=t[e].root;if(i.startsWith("/"))return n(`subPackages[${e}].root 不应该以 '/' 开头`);if(i.startsWith("."))return n(`subPackages[${e}].root 不应该以 '.' 开头`);if(!Array.isArray(t[e].pages))return n(`subPackages[${e}].pages 类型需为array`);for(let s=0;s<t[e].pages.length;s++){const o=t[e].pages[s];if(o.startsWith("/"))return n(`subPackages[${e}].pages[${s}] 不应该以 '/' 开头`);if(o.startsWith("."))return n(`subPackages[${e}].pages[${s}] 不应该以 '.' 开头`)}if(t[e].name){const s=a.indexOf(t[e].name);if(s>-1)return n(`subPackages[${e}] 与 subPackages[${s}] 的 name 相同`);a.push(t[e].name)}r.push(t[e].root)}for(let t=0;t<r.length;t++){const s=e.findIndex((e=>b(e,r[t])));if(s>-1)return n(`pages[${s}] 与 subPackages[${t}] 存在包含关系`);for(let e=t+1;e<r.length;e++)if(b(r[t],r[e]))return n(`subPackages[${t}] 与 subPackages[${e}] 存在包含关系`)}}function v(t,e){return x(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 k(t,e){var n;if(!x(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:s}=t.list[n];if(!s)return e(`tabBar.list[${n}] 错误: "pagePath" 不能为空`);if(s.startsWith("/"))return e(`tabBar.list[${n}].pagePath 不应该以 '/' 开头`);if(s.startsWith("."))return e(`tabBar.list[${n}].pagePath 不应该以 '.' 开头`)}}function J(t,e){if(!x(t))return e("usingComponents 字段需为 object")}function $(t,e){if(!x(t))return e("networkTimeout 字段需为 object");const n=["request","connectSocket","uploadFile","downloadFile"];for(const s of n)if(null!=t[s]&&"number"!=typeof t[s])return e(`networkTimeout.${s} 字段需为 number`)}function x(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const C=["window","networkTimeout","tabBar"],S=(t,e,n)=>{if(!((t,e,n)=>{var s,o;const r=t=>P(n,t),a=t.pages,i=t.subpackages||t.subPackages||[],c=t.usingComponents||{};if("pages"in e){j(e.pages,r);for(let t=0;t<e.pages.length;t++)if(!a.includes(e.pages[t]))return r(`pages[${t}]: "${e.pages[t]}" 不在 app.json pages 内`)}if("subpackages"in e||"subPackages"in e){y(e.subpackages||e.subPackages,e.pages?e.pages:a,r);const t=e.subpackages||e.subPackages||[];for(let e=0;e<t.length;e++){const n=t[e],o=i.find((t=>t.root===n.root));if(!o)return r(`subPackages[${e}].root: "${n.root}" 不在 app.json subPackages 内`);for(let t=0;t<n.pages.length;t++){const a=n.pages[t];if(!(null===(s=o.pages)||void 0===s?void 0:s.includes(a)))return r(`subPackages[${e}].pages[${t}]: "${a}" 不在 app.json subPackages 内`)}}}if("extPages"in e){if(!x(e.extPages))return r("extPages 字段需为 object");const t=i.flatMap((t=>t.pages.map((e=>h(t.root,e)))));for(const n of Object.keys(e.extPages))if(!a.includes(n)&&!t.includes(n))return r(`extPages["${n}"] 不在 app.json pages | subPackages 内`)}if("tabBar"in e){if(!x(e.tabBar))return r("tabBar 字段需为 object");if("list"in e.tabBar){k(e.tabBar,r);for(const{pagePath:t}of e.tabBar.list)if(null===(o=e.pages)||void 0===o?void 0:o.length){if(!e.pages.includes(t))return r(`tabBar.list 错误: "${t}" 不在 ext.json pages 内`)}else if(!a.includes(t))return r(`tabBar.list 错误: "${t}" 不在 app.json pages 内`)}}if("window"in e&&v(e.window,r),"networkTimeout"in e&&$(e.networkTimeout,r),"usingComponents"in e){J(e.usingComponents,r);for(const t in e.usingComponents)if(!c[t])return r(`usingComponents["${t}"] 不在 app.json usingComponents 内`)}return!0})(t,e,n))return t;for(const[n,s]of Object.entries(e))if("extPages"!==n)if("object"==typeof s&&C.includes(n)){const e=Array.isArray(s);t[n]=t[n]||(e?[]:{}),t[n]=e?[...new Set([...t[n],...s])]:{...t[n],...s}}else t[n]=s;return t},w=t=>{try{const e=n.readFileSync(`${t}`,"utf-8");return s(e,{loc:!0,source:t}),JSON.parse(e)}catch(e){return P(t||e.source,e.message),{pages:[]}}};let _=1;function O(t,e="Component"){return!(!n.existsSync(`${t}.js`)&&!n.existsSync(`${t}.ts`))&&("Component"!==e||n.existsSync(`${t}.json`))}const D=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,E=(e,s,o)=>{if(s.startsWith("plugin://"))return P(o,`小红书小程序暂不支持插件:${s}`);function r(t){return t.replace(`${e}/`,"")}let a="";if(s.startsWith("/")&&O(a=h(e,s)))return r(a);if(s.startsWith(".")&&O(a=d(t.dirname(o),s)))return r(a);if(O(a=d(e,s)))return r(a);if(O(a=d(t.dirname(o),s)))return r(a);const i=((t,e)=>{try{const s=`${t}/package.json`;if(n.existsSync(s)){const o=w(s),r=Object.keys(o.dependencies||[]).find((t=>e.includes(t)))||"",a=`${t}/node_modules/${r}/package.json`;if(n.existsSync(a)){const n=e.replace(r,"").replace(/^\//,""),s=w(a);let o=s.miniprogram||s.main||"";o=/\.(ts|js)$/.test(o)?o.replace(/\.(js|ts)$/,""):o;let i=(n||o||"").trim();const c=h(t,"node_modules",r,i).replace(`${t}/`,"");if(O(`${t}/${c}`))return c;if(O(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(e,s);return i||P(o,`${D("Component")}:${s}`)},N=/\.|\/|\.\/|\.\.\//g,A=(t,e,n,s)=>{const o={};for(const[r,a]of Object.entries(t)){let t;N.test(a)?(t=E(n,a,s),q(e,n,t)):t=a,o[r]=t}return o},B=(t,e,n,s)=>{const o={};for(const[r,a]of Object.entries(t)){const t=E(n,a,s);o[r]=t,q(e,n,t)}return o},M=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),q=(t,e,n)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=d(e,`${n}.json`),o=w(s);t.componentsMap[n]=o,t.componentsMap[n].index=_++,M(o.componentPlaceholder)?o.componentPlaceholder=A(o.componentPlaceholder,t,e,s):Reflect.deleteProperty(o,"componentPlaceholder"),M(o.usingComponents)?o.usingComponents=B(o.usingComponents,t,e,s):Reflect.deleteProperty(o,"usingComponents")},T=(t,e,s,o)=>t.map((t=>{let r={path:t};const a=d(e,`${r.path}.json`);if(n.existsSync(a)){let t,n=w(a);(t=null==o?void 0:o[`${r.path}`])&&(n={...n,...t}),r={...r,...n},M(r.componentPlaceholder)?r.componentPlaceholder=A(r.componentPlaceholder,s,e,a):Reflect.deleteProperty(r,"componentPlaceholder"),M(r.usingComponents)?r.usingComponents=B(r.usingComponents,s,e,a):Reflect.deleteProperty(r,"usingComponents")}return r})),W=(t,n,s)=>{_=1;const o=d(n,"app.json");let r=w(o);r.subPackages=r.subPackages||r.subpackages||[],r=((t,e)=>{const n=t=>P(e,t);if(!("pages"in t))return n("缺少 pages 字段");if(!Array.isArray(t.pages))return n("pages 字段需为 array");if(t.pages.length<=0)return n("pages 不能为空");if(j(t.pages,n),"tabBar"in t){k(t.tabBar,n);for(const{pagePath:e}of t.tabBar.list)if(!t.pages.includes(e))return n(`tabBar.list 错误: "${e}" 不在 "pages" 内`)}return("subpackages"in t||"subPackages"in t)&&y(t.subPackages||t.subpackages,t.pages,n),"window"in t&&v(t.window,n),"networkTimeout"in t&&$(t.networkTimeout,n),"usingComponents"in t&&J(t.usingComponents,n),t})(r,o);const a=`${s}/ext.json`,i=s?w(a):{};let c={};((null==i?void 0:i.extEnable)&&(null==i?void 0:i.directCommit)||(null==i?void 0:i.extEnable))&&(r=S(r,i,a),c=i.extPages||{}),r.componentsMap={},M(r.usingComponents)?r.usingComponents=B(r.usingComponents,r,n,o):Reflect.deleteProperty(r,"usingComponents"),r.pages.map(((t,e)=>{if(!O(d(n,t),"Page"))return P(o,`${D("Page")}:${t} in pages[${e}]`)})),r.pages=T(r.pages,n,r,c),r.subPackages=r.subPackages.map(((t,e)=>{const s=t.pages.map(((s,r)=>{const a=h(t.root,s);return O(d(n,a),"Page")?a:P(o,`${D("Page")}:${s} in subPackages[${e}].pages[${r}]`)}));return{...t,pages:T(s,n,r,c)}})),r.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0};const p=e(r);Reflect.deleteProperty(p,"subPackages");const u=r.subPackages.reduce(((t,e)=>[...t,...e.pages]),[]);return p.pages=u?[...p.pages,...u]:p.pages,{legacyAppJSON:p,appJSON:r}},R="baba:changeExtJsonPath",F="baba:changeExtJson",z="miniprogram",V="minigame",I=t=>{t=g(t);const e=d(t,"game.json");let s=w(e);return s.subPackages=s.subPackages||s.subpackages||[],s.pages=[],s.subPackages=s.subPackages.map((t=>{let e=g(`${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,e,s)=>{var o,r,a;const i=t=>(P(s||"game.json",t),!1);if(t.subPackages.length)for(let s=0;s<t.subPackages.length;s++){const{name:c,root:p,entry:u,dirname:l}=t.subPackages[s];if("string"!=typeof c)return i(`subPackages[${s}].name 字段类型需为 string`);if("string"!=typeof p)return i(`subPackages[${s}].root 字段类型需为 string`);if(!(null===(o=null==c?void 0:c.trim)||void 0===o?void 0:o.call(c)))return i(`subPackages[${s}].name 不能为空`);if(!(null===(r=null==p?void 0:p.trim)||void 0===r?void 0:r.call(p)))return i(`subPackages[${s}].root 不能为空`);if(!(null===(a=null==u?void 0:u.trim)||void 0===a?void 0:a.call(u)))return i(`subPackages[${s}].root 不能为 ${p}`);if("/"===l)return i(`subPackages[${s}].root 不能在根目录下 ${p}`);const g=h(e,u);if(!n.existsSync(`${g}.js`)&&!n.existsSync(`${g}.ts`))return i(`未找到 subPackages[${s}].root 对应的 ${u}.(js|ts)`);for(let e=s+1;e<t.subPackages.length;e++){const{name:n,entry:o,dirname:r}=t.subPackages[e];if(c===n)return i(`subPackages[${s}].name 和 subPackages[${e}].name 相同`);if(u===o)return i(`subPackages[${s}].root 和 subPackages[${e}].root 指向相同文件`);if(l===r)return i(`subPackages[${s}].root 和 subPackages[${e}].root 在同一目录下`);if(l.startsWith(`${r}/`)||r.startsWith(`${l}/`))return i(`subPackages[${s}].root 和 subPackages[${e}].root 目录层级存在包含关系`)}}return t.networkTimeout&&"object"!=typeof t.networkTimeout?i("networkTimeout 字段需为 object"):!t.workers||"workers"===t.workers||"object"==typeof t.workers||i("workers 字段需为 string | object")})(s,t,e)?{appJSON:s}:{appJSON:{pages:[],subPackages:[]}}};class L{constructor(t){this.project=t}}const U=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},G=(t,e)=>{let n=[];((null==t?void 0:t.subPackages)||[]).forEach((t=>{n=n.concat(t.pages)}));const s=n.find((t=>t.path===e))||{};return(null==s?void 0:s.usingComponents)||{}},H=t=>(null==t?void 0:t.usingComponents)||{};const K=(t,e)=>{var n;const s=t.componentsMap||{};return(null===(n=null==s?void 0:s[e])||void 0===n?void 0:n.usingComponents)||{}},Q=require("chokidar"),X={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class Y 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=z,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=u((()=>{var t,e,n,s;const{projectConfig:r}=this.snapshot,a=w(this.projectJsonPath);this._projectJsonContent=a,this.snapshot.projectConfig=o.cloneDeep(a),this.emit("project-config-json-changed",{oldData:r,newData:a}),(null==r?void 0:r.libVersion)!==(null==a?void 0:a.libVersion)&&this.emit("lib-version-changed",{oldData:null==r?void 0:r.libVersion,newData:null==a?void 0:a.libVersion}),(null===(t=null==r?void 0:r.setting)||void 0===t?void 0:t.enableSimulatorV2)!==(null===(e=null==a?void 0:a.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===(s=null==a?void 0:a.setting)||void 0===s?void 0:s.enableSimulatorV2})}),100),this.onProjectPrivateJsonChange=u((()=>{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=u((()=>{const{appJson:t}=this.snapshot,e=w(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=o.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=u((()=>{const{extJson:t}=this.snapshot,e=w(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=o.cloneDeep(e),this.emit(F,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,s,o;const r={pathName:"",query:"",entry:"",item:{}};try{if(!p.existsSync(t))return r;const a=w(t),i=(null===(n=null===(e=null==a?void 0:a.condition)||void 0===e?void 0:e.miniprogram)||void 0===n?void 0:n.current)||-1,c=null===(o=null===(s=null==a?void 0:a.condition)||void 0===s?void 0:s.miniprogram)||void 0===o?void 0:o.list;if(-1===i)return r;const u=c.filter((t=>t.id===i))[0];return u?(r.pathName=u.pathName,r.query=u.query,"halfPageNativeFunctionalized"===u.launchMode&&(r.query=u.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,s,o;const r=this.projectPrivateConfigJsonPath,a={path:"",query:"",launchMode:"default"};if(!p.existsSync(r))return a;const i=w(r),c=null===(e=null===(t=null==i?void 0:i.condition)||void 0===t?void 0:t.miniprogram)||void 0===e?void 0:e.current,u=null===(o=null===(s=null===(n=null==i?void 0:i.condition)||void 0===n?void 0:n.miniprogram)||void 0===s?void 0:s.list)||void 0===o?void 0:o.filter((t=>t.id===c))[0];return u?{path:u.pathName,query:u.query.trim(),launchMode:u.launchMode}:a};const{watch:e}=t;let n=t.projectPath;if(c.isAbsolute(n)||(n=c.join(a.cwd(),n)),this.checker=new L(this),this._projectPath=l(n),this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",this._appMode=p.existsSync(c.posix.join(this._projectPath,"game.json"))?V:z,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this._appMode===z){const{legacyAppJSON:t,appJSON:e}=W(this.projectPath,this.miniprogramDir,this.extJsonDir);this.appJSON=e,this.legacyAppJSON=t,this.updateAppJson()}else{const{appJSON:t}=I(this.projectPath);this.appJSON=t,this.legacyAppJSON={}}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=l(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get miniprogramDir(){const t=l(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=w(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===z?"app.json":"game.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=w(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=w(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=w(this.extJsonPath)||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(R))}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=o.cloneDeep({projectConfig:this.projectJsonContent,appJson:this.appJsonContent,extJson:this.extJsonContent,customEntry:this._customEntry});const t=Q.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,X,{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})}changeExtJsonPermission(t){this._canUploadExtJson=t,t&&p.existsSync(this.extJsonPath)?this.extJsonDir=this.projectPath:this.extJsonDir=""}getSubPackages(){return this.appJSON.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e}/`)))}getComponentsMap(){return this.appJSON.componentsMap||{}}getPages(){return this.appJSON.pages||[]}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const s=c.extname(e);return n.some((t=>`${t}${s}`===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 s=!1;try{s=!("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 s}(this.appJSON),s=((t,e,n=!1,s=!0)=>{try{const o=new Set;let r={};r=s?{...n?{}:H(e),...U(e,t),...G(e,t)}:{...H(e),...U(e,t)};const a=t=>{for(const n in t)if(t[n]){const s=t[n];if(o.has(s))continue;o.add(s);const r=K(e,s);r&&Object.keys(r).length&&a(r)}};return a(r),Array.from(o)}catch(t){return console.error("parse page depend components has error happen"),[]}})(t,this.appJSON,e,n);return s}formatGameAppJSON(t){return t.map((t=>{let e=g(`${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 Y(t)}updateAppJson(){const{legacyAppJSON:t}=this;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 Z={transformAppJson:W,transformGameJson:I,pureAppJSON:(t,e="v1")=>{var n,s;const r=o.cloneDeep(t);if("v1"===e&&(r.subPackages=(null===(n=r.subPackages)||void 0===n?void 0:n.map((t=>{const{entry:e,dirname:n,name:s,independent:o,rawRoot:r}=t;return{independent:o,root:n,path:e,rawRoot:r,name:s,pages:[{path:e}]}})))||[],r.pages=[{path:"/"}]),"v0"===e){const t=null===(s=r.subPackages)||void 0===s?void 0:s.map((t=>({path:t.entry})));Reflect.deleteProperty(r,"subPackages"),r.pages=[{path:"/"},...t]}return Reflect.deleteProperty(r,"subpackages"),r.compileType="minigame",r}};exports.Project=Y,exports.utils=Z;
@@ -1,6 +1,6 @@
1
1
  import { IAppJSON } from '../../types';
2
2
  export declare const PATH_REG: RegExp;
3
- export declare const transformAppJson: (_: string, miniprogramDir: string, extJsonPath?: string) => {
3
+ export declare const transformAppJson: (_: string, miniprogramDir: string, extJsonDir?: string) => {
4
4
  legacyAppJSON: any;
5
5
  appJSON: IAppJSON;
6
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-project",
3
- "version": "1.9.3",
3
+ "version": "1.9.4",
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-utils": "1.9.3",
17
+ "xhs-mp-utils": "1.9.4",
18
18
  "chokidar": "^3.6.0",
19
19
  "fs-extra": "^11.2.0",
20
20
  "lodash": "^4.17.21",