xhs-mp-project 2.0.13 → 2.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +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)};require("util").inspect.custom;const f=(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 P(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 b=(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=>b(e,r[t])));if(o>-1)return n(`pages[${o}] 与 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 J(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 y(t,e){var n;if(!J(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 k(t,e){if(!J(t))return e("usingComponents 字段需为 object")}function $(t,e){if(!J(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 J(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const C=["window","networkTimeout","tabBar"],x=(t,e,n)=>{if(!((t,e,n)=>{var o,s,r,i,a,c;const p=t=>f(n,t),l=t.pages,u=t.subpackages||t.subPackages||[],g=t.usingComponents||{};if("pages"in e){P(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(!J(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(!J(e.tabBar))return p("tabBar 字段需为 object");if("list"in e.tabBar){y(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&&v(e.window,p),"networkTimeout"in e&&$(e.networkTimeout,p),"usingComponents"in e){k(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&&C.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},S=t=>{try{const n=e.readFileSync(`${t}`,"utf-8");return o(n,{loc:!0,source:t}),JSON.parse(n)}catch(e){return f(t||e.source,e.message),{pages:[]}}};let w=1;function _(t,n="Component"){return!(!e.existsSync(`${t}.js`)&&!e.existsSync(`${t}.ts`))&&("Component"!==n||e.existsSync(`${t}.json`))}const O=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,E=(t,e)=>t.replace(e.endsWith("/")?e:`${e}/`,""),D=(n,o,s)=>{if(o.startsWith("plugin://"))return f(s,`小红书小程序暂不支持插件:${o}`);let r="";if(o.startsWith("/")&&_(r=h(n,o)))return E(r,n);if(o.startsWith(".")&&_(r=g(t.dirname(s),o)))return E(r,n);if(_(r=g(n,o)))return E(r,n);if(_(r=g(t.dirname(s),o)))return E(r,n);const i=((t,n)=>{try{const o=`${t}/package.json`;if(e.existsSync(o)){const s=S(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=S(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}/${c}`))return c;if(_(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(n,o);return i||f(s,`${O("Component")}:${o}`)},B=/\.|\/|\.\/|\.\.\//g,M=(t,e,n,o)=>{const s={};for(const[r,i]of Object.entries(t)){let t;B.test(i)?(t=D(n,i,o),A(e,n,t)):t=i,s[r]=t}return s},N=(t,e,n,o)=>{const s={};for(const[r,i]of Object.entries(t)){const t=D(n,i,o);s[r]=t,A(e,n,t)}return s},W=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),A=(t,e,n)=>{if(Object.keys(t.componentsMap).includes(n))return;const o=g(e,`${n}.json`),s=S(o);t.componentsMap[n]=s,t.componentsMap[n].index=w++,W(s.componentPlaceholder)?s.componentPlaceholder=M(s.componentPlaceholder,t,e,o):Reflect.deleteProperty(s,"componentPlaceholder"),W(s.usingComponents)?s.usingComponents=N(s.usingComponents,t,e,o):Reflect.deleteProperty(s,"usingComponents")},T=(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=S(u);l={...l,...m},(d=null==i?void 0:i[o])&&(l={...l,...d}),l.path=o,W(l.componentPlaceholder)?l.componentPlaceholder=M(l.componentPlaceholder,r,s,u):Reflect.deleteProperty(l,"componentPlaceholder"),W(l.usingComponents)?l.usingComponents=N(l.usingComponents,r,s,u):Reflect.deleteProperty(l,"usingComponents");const P=null===(c=null==m?void 0:m.skeleton)||void 0===c?void 0:c.path;if(W(m.skeleton)&&P&&"string"==typeof P){let o="";if(o=P.startsWith("/")?h(s,P):g(t.dirname(u),P),!n.SKETCH_ML_REG.test(o))return f(u,`骨架屏后缀需为 ${n.SKETCH_ML_REG} 格式:${P}`);if(!e.existsSync(o))return f(u,`骨架屏不存在:${P}`);const i=E(o,s);if((null==a?void 0:a.independent)&&(null==a?void 0:a.root)&&!i.startsWith(`${a.root}/`))return f(u,`独立分包${a.root}只可引用${a.root}下的骨架屏:${P}`);if((null==a?void 0:a.root)&&!i.startsWith(`${a.root}/`)){for(const t of r.subPackages)if((null==a?void 0:a.root)!==t.root&&i.startsWith(`${t.root}/`))return f(u,`分包${null==a?void 0:a.root}不可引用分包${t.root}下的骨架屏:${P}`)}else if(!a)for(const t of r.subPackages)if(i.startsWith(`${t.root}/`))return f(u,`主包不可引用分包${t.root}下的骨架屏:${P}`);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})),q=(t,e,n)=>{w=1;const o=g(e,"app.json");let s=S(o);s.subPackages=s.subPackages||s.subpackages||[],s=((t,e)=>{const n=t=>f(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(P(t.pages,n),"tabBar"in t){y(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)&&j(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})(s,o);const r=`${n}/ext.json`,i=n?S(r):{};let a={};return((null==i?void 0:i.extEnable)&&(null==i?void 0:i.directCommit)||(null==i?void 0:i.extEnable))&&(s=x(s,i,r),a=i.extPages||{}),s.componentsMap={},W(s.usingComponents)?s.usingComponents=N(s.usingComponents,s,e,o):Reflect.deleteProperty(s,"usingComponents"),s.pages.map(((t,n)=>{if(!_(g(e,t),"Page"))return f(o,`${O("Page")}:${t} in pages[${n}]`)})),s.pages=T(s.pages,e,s,a),s.subPackages=s.subPackages.map(((t,n)=>{const r=t.pages.map(((s,r)=>{const i=h(t.root,s);return _(g(e,i),"Page")?i:f(o,`${O("Page")}:${s} in subPackages[${n}].pages[${r}]`)}));return{...t,pages:T(r,e,s,a,t)}})),s.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0},{appJSON:s}},R="baba:changeExtJsonPath",F="baba:changeExtJson",z="miniprogram",V="minigame",L=t=>{t=u(t);const n=g(t,"game.json");let o=S(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=>(f(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 I{constructor(t){this.project=t}}const G=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},U=(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)||{}},H=t=>(null==t?void 0:t.usingComponents)||{};const K=(t,e)=>{var n;const o=t.componentsMap||{};return(null===(n=null==o?void 0:o[e])||void 0===n?void 0:n.usingComponents)||{}},X=require("chokidar"),Q={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=l((()=>{var t,e,n,o;const{projectConfig:r}=this.snapshot,i=S(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=S(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=S(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=s.cloneDeep(e),this.emit(F,{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=S(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=S(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 I(this),this._projectPath=u(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{appJSON:t}=q(this.projectPath,this.miniprogramDir,this.extJsonDir);this.appJSON=t,this.updateAppJson()}else{const{appJSON:t}=L(this.projectPath);this.appJSON=t}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=S(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=S(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=S(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=S(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=s.cloneDeep({projectConfig:this.projectJsonContent,appJson:this.appJsonContent,extJson:this.extJsonContent,customEntry:this._customEntry});const t=X.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,Q,{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?{}:H(e),...G(e,t),...U(e,t)}:{...H(e),...G(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=K(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 Y(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 Z={transformAppJson:q,transformGameJson:L,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="minigame",n}};exports.Project=Y,exports.utils=Z;
|
|
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)};require("util").inspect.custom;const f=(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 P(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 b=(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=>b(e,r[t])));if(o>-1)return n(`pages[${o}] 与 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 J(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 y(t,e){var n;if(!J(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 k(t,e){if(!J(t))return e("usingComponents 字段需为 object")}function $(t,e){if(!J(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 J(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const C=["window","networkTimeout","tabBar"],x=(t,e,n)=>{if(!((t,e,n)=>{var o,s,r,i,a,c;const p=t=>f(n,t),l=t.pages,u=t.subpackages||t.subPackages||[],g=t.usingComponents||{};if("pages"in e){P(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(!J(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(!J(e.tabBar))return p("tabBar 字段需为 object");if("list"in e.tabBar){y(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&&v(e.window,p),"networkTimeout"in e&&$(e.networkTimeout,p),"usingComponents"in e){k(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&&C.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},S=t=>{try{const n=e.readFileSync(`${t}`,"utf-8");return o(n,{loc:!0,source:t}),JSON.parse(n)}catch(e){return f(t||e.source,e.message),{pages:[]}}};let w=1;function _(t,n="Component"){return!(!e.existsSync(`${t}.js`)&&!e.existsSync(`${t}.ts`))&&("Component"!==n||e.existsSync(`${t}.json`))}const O=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,E=(t,e)=>t.replace(e.endsWith("/")?e:`${e}/`,""),D=(n,o,s)=>{if(o.startsWith("plugin://"))return f(s,`小红书小程序暂不支持插件:${o}`);let r="";if(o.startsWith("/")&&_(r=h(n,o)))return E(r,n);if(o.startsWith(".")&&_(r=g(t.dirname(s),o)))return E(r,n);if(_(r=g(n,o)))return E(r,n);if(_(r=g(t.dirname(s),o)))return E(r,n);const i=((t,n)=>{try{const o=`${t}/package.json`;if(e.existsSync(o)){const s=S(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=S(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}/${c}`))return c;if(_(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(n,o);return i||f(s,`${O("Component")}:${o}`)},M=/\.|\/|\.\/|\.\.\//g;var B;!function(t){t.SKETCH="骨架屏",t.COMPONENT="组件"}(B||(B={}));const N=t=>t.endsWith("/")?t:`${t}/`,T=(t,e,n,o,s,r)=>{if((null==r?void 0:r.independent)&&(null==r?void 0:r.root)&&!t.startsWith(N(r.root)))return f(o,`独立分包${r.root}只可引用${r.root}目录下的${s}:${e}`);if((null==r?void 0:r.root)&&!t.startsWith(N(r.root))){for(const i of n.subPackages)if((null==r?void 0:r.root)!==i.root&&t.startsWith(N(i.root)))return f(o,`分包${null==r?void 0:r.root}不可引用分包${i.root}目录下的${s}:${e}`)}else if(!r)for(const r of n.subPackages)if(t.startsWith(N(r.root)))return f(o,`主包不可引用分包${r.root}目录下的${s}:${e}`)},W=(t,e,n,o,s)=>{const r={};for(const[i,a]of Object.entries(t)){let t;M.test(a)?(t=D(n,a,o),R(e,n,t,s)):t=a,r[i]=t}return r},A=(t,e,n,o,s)=>{const r={};for(const[i,a]of Object.entries(t)){const t=D(n,a,o);r[i]=t,T(t,a,e,o,B.COMPONENT,s),R(e,n,t,s)}return r},q=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),R=(t,e,n,o)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=g(e,`${n}.json`),r=S(s);t.componentsMap[n]=r,t.componentsMap[n].index=w++,q(r.componentPlaceholder)?r.componentPlaceholder=W(r.componentPlaceholder,t,e,s,o):Reflect.deleteProperty(r,"componentPlaceholder"),q(r.usingComponents)?r.usingComponents=A(r.usingComponents,t,e,s,o):Reflect.deleteProperty(r,"usingComponents")},F=(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=S(u);l={...l,...m},(d=null==i?void 0:i[o])&&(l={...l,...d}),l.path=o,q(l.componentPlaceholder)?l.componentPlaceholder=W(l.componentPlaceholder,r,s,u,a):Reflect.deleteProperty(l,"componentPlaceholder"),q(l.usingComponents)?l.usingComponents=A(l.usingComponents,r,s,u,a):Reflect.deleteProperty(l,"usingComponents");const P=null===(c=null==m?void 0:m.skeleton)||void 0===c?void 0:c.path;if(q(m.skeleton)&&P&&"string"==typeof P){let o="";if(o=P.startsWith("/")?h(s,P):g(t.dirname(u),P),!n.SKETCH_ML_REG.test(o))return f(u,`骨架屏后缀需为 ${n.SKETCH_ML_REG} 格式:${P}`);if(!e.existsSync(o))return f(u,`骨架屏不存在:${P}`);const i=E(o,s);T(i,P,r,u,B.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})),z=(t,e,n)=>{w=1;const o=g(e,"app.json");let s=S(o);s.subPackages=s.subPackages||s.subpackages||[],s=((t,e)=>{const n=t=>f(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(P(t.pages,n),"tabBar"in t){y(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)&&j(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})(s,o);const r=`${n}/ext.json`,i=n?S(r):{};let a={};return((null==i?void 0:i.extEnable)&&(null==i?void 0:i.directCommit)||(null==i?void 0:i.extEnable))&&(s=x(s,i,r),a=i.extPages||{}),s.componentsMap={},q(s.usingComponents)?s.usingComponents=A(s.usingComponents,s,e,o):Reflect.deleteProperty(s,"usingComponents"),s.pages.map(((t,n)=>{if(!_(g(e,t),"Page"))return f(o,`${O("Page")}:${t} in pages[${n}]`)})),s.pages=F(s.pages,e,s,a),s.subPackages=s.subPackages.map(((t,n)=>{const r=t.pages.map(((s,r)=>{const i=h(t.root,s);return _(g(e,i),"Page")?i:f(o,`${O("Page")}:${s} in subPackages[${n}].pages[${r}]`)}));return{...t,pages:F(r,e,s,a,t)}})),s.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0},{appJSON:s}},V="baba:changeExtJsonPath",L="baba:changeExtJson",I="miniprogram",G="minigame",H=t=>{t=u(t);const n=g(t,"game.json");let o=S(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=>(f(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 K{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:{}},X=(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)||{}},Q=t=>(null==t?void 0:t.usingComponents)||{};const Y=(t,e)=>{var n;const o=t.componentsMap||{};return(null===(n=null==o?void 0:o[e])||void 0===n?void 0:n.usingComponents)||{}},Z=require("chokidar"),tt={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class et 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=I,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=l((()=>{var t,e,n,o;const{projectConfig:r}=this.snapshot,i=S(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=S(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=S(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=s.cloneDeep(e),this.emit(L,{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=S(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=S(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 K(this),this._projectPath=u(n),this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",this._appMode=p.existsSync(c.posix.join(this._projectPath,"game.json"))?G:I,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this._appMode===I){const{appJSON:t}=z(this.projectPath,this.miniprogramDir,this.extJsonDir);this.appJSON=t,this.updateAppJson()}else{const{appJSON:t}=H(this.projectPath);this.appJSON=t}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=S(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===I?"app.json":"game.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=S(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=S(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=S(this.extJsonPath)||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(V))}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=Z.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,tt,{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?{}:Q(e),...U(e,t),...X(e,t)}:{...Q(e),...U(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=Y(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 et(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 nt={transformAppJson:z,transformGameJson:H,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="minigame",n}};exports.Project=et,exports.utils=nt;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xhs-mp-project",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.14",
|
|
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.0.
|
|
17
|
+
"xhs-mp-shared": "2.0.14",
|
|
18
18
|
"chokidar": "^3.6.0",
|
|
19
19
|
"fs-extra": "^11.2.0",
|
|
20
20
|
"lodash": "^4.17.21",
|