xhs-mp-project 2.0.5 → 2.0.6
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/dist/project.d.ts +0 -1
- package/dist/types/project.d.ts +0 -1
- package/package.json +2 -2
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"),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 l=(t,e=100)=>{let n=null;return function(...s){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,s))}},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 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 j(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 $(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(!$(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(!$(t))return e("usingComponents 字段需为 object")}function J(t,e){if(!$(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 $(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const x=["window","networkTimeout","tabBar"],C=(t,e,n)=>{if(!((t,e,n)=>{var s,o,r,a,i,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],o=u.find((t=>t.root===n.root));if(!o)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===(s=o.pages)||void 0===s?void 0:s.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){y(e.tabBar,p);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 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===(i=null===(a=e.tabBar)||void 0===a?void 0:a.list)||void 0===i?void 0:i.length)&&t.tabBar)for(let n=0;n<(null===(c=t.tabBar.list)||void 0===c?void 0:c.length);n++){const s=t.tabBar.list[n];if(!e.pages.includes(s.pagePath))return p(`ext.json pages 未包含 "${s.pagePath}",此页面在 app.json tabBar.list[${n}] 中被声明`)}if("window"in e&&v(e.window,p),"networkTimeout"in e&&J(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,s]of Object.entries(e))if("extPages"!==n)if("object"==typeof s&&x.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},S=t=>{try{const e=n.readFileSync(`${t}`,"utf-8");return s(e,{loc:!0,source:t}),JSON.parse(e)}catch(e){return f(t||e.source,e.message),{pages:[]}}};let w=1;function _(t,e="Component"){return!(!n.existsSync(`${t}.js`)&&!n.existsSync(`${t}.ts`))&&("Component"!==e||n.existsSync(`${t}.json`))}const O=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,D=(t,e)=>t.replace(e.endsWith("/")?e:`${e}/`,""),E=(e,s,o)=>{if(s.startsWith("plugin://"))return f(o,`小红书小程序暂不支持插件:${s}`);let r="";if(s.startsWith("/")&&_(r=h(e,s)))return D(r,e);if(s.startsWith(".")&&_(r=g(t.dirname(o),s)))return D(r,e);if(_(r=g(e,s)))return D(r,e);if(_(r=g(t.dirname(o),s)))return D(r,e);const a=((t,e)=>{try{const s=`${t}/package.json`;if(n.existsSync(s)){const o=S(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=S(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(_(`${t}/${c}`))return c;if(_(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(e,s);return a||f(o,`${O("Component")}:${s}`)},B=/\.|\/|\.\/|\.\.\//g,N=(t,e,n,s)=>{const o={};for(const[r,a]of Object.entries(t)){let t;B.test(a)?(t=E(n,a,s),W(e,n,t)):t=a,o[r]=t}return o},A=(t,e,n,s)=>{const o={};for(const[r,a]of Object.entries(t)){const t=E(n,a,s);o[r]=t,W(e,n,t)}return o},M=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),W=(t,e,n)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=g(e,`${n}.json`),o=S(s);t.componentsMap[n]=o,t.componentsMap[n].index=w++,M(o.componentPlaceholder)?o.componentPlaceholder=N(o.componentPlaceholder,t,e,s):Reflect.deleteProperty(o,"componentPlaceholder"),M(o.usingComponents)?o.usingComponents=A(o.usingComponents,t,e,s):Reflect.deleteProperty(o,"usingComponents")},q=(e,s,o,r,a)=>e.map((e=>{var i,c,p;let l={path:e};const u=g(s,`${e}.json`);if(n.existsSync(u)){let d,m=S(u);if(l={...l,...m},(d=null==r?void 0:r[e])&&(l={...l,...d}),l.path=e,M(l.componentPlaceholder)?l.componentPlaceholder=N(l.componentPlaceholder,o,s,u):Reflect.deleteProperty(l,"componentPlaceholder"),M(l.usingComponents)?l.usingComponents=A(l.usingComponents,o,s,u):Reflect.deleteProperty(l,"usingComponents"),M(m.skeleton)&&(null===(i=m.skeleton)||void 0===i?void 0:i.path)){if("string"!=typeof(null===(c=m.skeleton)||void 0===c?void 0:c.path))return f(u,"skeleton:需配置path,类型为string");let e="";if(e=m.skeleton.path.startsWith("/")?h(s,m.skeleton.path):g(t.dirname(u),m.skeleton.path),!e.match(/\.skeleton\.(wx|xhs)ml$/))return f(u,`骨架屏后缀需为 .skeleton.xhsml :${m.skeleton.path}`);if(!n.existsSync(e))return f(u,`骨架屏不存在:${m.skeleton.path}`);const r=D(e,s);if(!0===(null==a?void 0:a.independent)&&!r.startsWith(`${a.root}/`))return f(u,`独立分包只可引用分包root下的骨架屏:${m.skeleton.path}`);if(!a)for(const t of o.subPackages)if(r.startsWith(`${t.root}/`))return f(u,`主包不可引用分包下骨架屏:${m.skeleton.path}`);l.skeleton={path:r.replace(/\.(wx|xhs)ml$/,""),options:(null===(p=m.skeleton)||void 0===p?void 0:p.options)||{}}}else Reflect.deleteProperty(l,"skeleton")}return l})),T=(t,n,s)=>{w=1;const o=g(n,"app.json");let r=S(o);r.subPackages=r.subPackages||r.subpackages||[],r=((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&&J(t.networkTimeout,n),"usingComponents"in t&&k(t.usingComponents,n),t})(r,o);const a=`${s}/ext.json`,i=s?S(a):{};let c={};((null==i?void 0:i.extEnable)&&(null==i?void 0:i.directCommit)||(null==i?void 0:i.extEnable))&&(r=C(r,i,a),c=i.extPages||{}),r.componentsMap={},M(r.usingComponents)?r.usingComponents=A(r.usingComponents,r,n,o):Reflect.deleteProperty(r,"usingComponents"),r.pages.map(((t,e)=>{if(!_(g(n,t),"Page"))return f(o,`${O("Page")}:${t} in pages[${e}]`)})),r.pages=q(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 _(g(n,a),"Page")?a:f(o,`${O("Page")}:${s} in subPackages[${e}].pages[${r}]`)}));return{...t,pages:q(s,n,r,c,t)}})),r.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0};const p=e(r);Reflect.deleteProperty(p,"subPackages");const l=r.subPackages.reduce(((t,e)=>[...t,...e.pages]),[]);return p.pages=l?[...p.pages,...l]:p.pages,{legacyAppJSON:p,appJSON:r}},R="baba:changeExtJsonPath",F="baba:changeExtJson",z="miniprogram",V="minigame",I=t=>{t=u(t);const e=g(t,"game.json");let s=S(e);return s.subPackages=s.subPackages||s.subpackages||[],s.pages=[],s.subPackages=s.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,e,s)=>{var o,r,a;const i=t=>(f(s||"game.json",t),!1);if(t.subPackages.length)for(let s=0;s<t.subPackages.length;s++){const{name:c,root:p,entry:l,dirname:u}=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==l?void 0:l.trim)||void 0===a?void 0:a.call(l)))return i(`subPackages[${s}].root 不能为 ${p}`);if("/"===u)return i(`subPackages[${s}].root 不能在根目录下 ${p}`);const g=h(e,l);if(!n.existsSync(`${g}.js`)&&!n.existsSync(`${g}.ts`))return i(`未找到 subPackages[${s}].root 对应的 ${l}.(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(l===o)return i(`subPackages[${s}].root 和 subPackages[${e}].root 指向相同文件`);if(u===r)return i(`subPackages[${s}].root 和 subPackages[${e}].root 在同一目录下`);if(u.startsWith(`${r}/`)||r.startsWith(`${u}/`))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=l((()=>{var t,e,n,s;const{projectConfig:r}=this.snapshot,a=S(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=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=o.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=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=S(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 l=c.filter((t=>t.id===i))[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,s,o;const r=this.projectPrivateConfigJsonPath,a={path:"",query:"",launchMode:"default"};if(!p.existsSync(r))return a;const i=S(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,l=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 l?{path:l.pathName,query:l.query.trim(),launchMode:l.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=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{legacyAppJSON:t,appJSON:e}=T(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=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=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.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 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=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{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:T,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 l=(t,e=100)=>{let n=null;return function(...s){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,s))}},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 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 j(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 $(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(!$(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(!$(t))return e("usingComponents 字段需为 object")}function J(t,e){if(!$(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 $(t){return Boolean(t&&"[object Object]"===Object.prototype.toString.call(t))}const x=["window","networkTimeout","tabBar"],C=(t,e,n)=>{if(!((t,e,n)=>{var s,o,r,a,i,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],o=u.find((t=>t.root===n.root));if(!o)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===(s=o.pages)||void 0===s?void 0:s.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){y(e.tabBar,p);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 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===(i=null===(a=e.tabBar)||void 0===a?void 0:a.list)||void 0===i?void 0:i.length)&&t.tabBar)for(let n=0;n<(null===(c=t.tabBar.list)||void 0===c?void 0:c.length);n++){const s=t.tabBar.list[n];if(!e.pages.includes(s.pagePath))return p(`ext.json pages 未包含 "${s.pagePath}",此页面在 app.json tabBar.list[${n}] 中被声明`)}if("window"in e&&v(e.window,p),"networkTimeout"in e&&J(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,s]of Object.entries(e))if("extPages"!==n)if("object"==typeof s&&x.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},S=t=>{try{const e=n.readFileSync(`${t}`,"utf-8");return s(e,{loc:!0,source:t}),JSON.parse(e)}catch(e){return f(t||e.source,e.message),{pages:[]}}};let w=1;function _(t,e="Component"){return!(!n.existsSync(`${t}.js`)&&!n.existsSync(`${t}.ts`))&&("Component"!==e||n.existsSync(`${t}.json`))}const O=t=>"Page"===t?`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml文件`:`${t}路径不正确,请检查目标路径是否存在(js|ts)、xhsml、json文件,且json中需设置component字段为true`,D=(t,e)=>t.replace(e.endsWith("/")?e:`${e}/`,""),E=(e,s,o)=>{if(s.startsWith("plugin://"))return f(o,`小红书小程序暂不支持插件:${s}`);let r="";if(s.startsWith("/")&&_(r=h(e,s)))return D(r,e);if(s.startsWith(".")&&_(r=g(t.dirname(o),s)))return D(r,e);if(_(r=g(e,s)))return D(r,e);if(_(r=g(t.dirname(o),s)))return D(r,e);const a=((t,e)=>{try{const s=`${t}/package.json`;if(n.existsSync(s)){const o=S(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=S(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(_(`${t}/${c}`))return c;if(_(`${t}/${c}/index`))return`${c}/index`}}}catch(t){}})(e,s);return a||f(o,`${O("Component")}:${s}`)},B=/\.|\/|\.\/|\.\.\//g,N=(t,e,n,s)=>{const o={};for(const[r,a]of Object.entries(t)){let t;B.test(a)?(t=E(n,a,s),W(e,n,t)):t=a,o[r]=t}return o},A=(t,e,n,s)=>{const o={};for(const[r,a]of Object.entries(t)){const t=E(n,a,s);o[r]=t,W(e,n,t)}return o},M=t=>!(!t||"[object Object]"!==Object.prototype.toString.call(t))&&Boolean(Object.keys(t).length),W=(t,e,n)=>{if(Object.keys(t.componentsMap).includes(n))return;const s=g(e,`${n}.json`),o=S(s);t.componentsMap[n]=o,t.componentsMap[n].index=w++,M(o.componentPlaceholder)?o.componentPlaceholder=N(o.componentPlaceholder,t,e,s):Reflect.deleteProperty(o,"componentPlaceholder"),M(o.usingComponents)?o.usingComponents=A(o.usingComponents,t,e,s):Reflect.deleteProperty(o,"usingComponents")},q=(e,s,o,r,a)=>e.map((e=>{var i,c,p;let l={path:e};const u=g(s,`${e}.json`);if(n.existsSync(u)){let d,m=S(u);if(l={...l,...m},(d=null==r?void 0:r[e])&&(l={...l,...d}),l.path=e,M(l.componentPlaceholder)?l.componentPlaceholder=N(l.componentPlaceholder,o,s,u):Reflect.deleteProperty(l,"componentPlaceholder"),M(l.usingComponents)?l.usingComponents=A(l.usingComponents,o,s,u):Reflect.deleteProperty(l,"usingComponents"),M(m.skeleton)&&(null===(i=m.skeleton)||void 0===i?void 0:i.path)){if("string"!=typeof(null===(c=m.skeleton)||void 0===c?void 0:c.path))return f(u,"skeleton:需配置path,类型为string");let e="";if(e=m.skeleton.path.startsWith("/")?h(s,m.skeleton.path):g(t.dirname(u),m.skeleton.path),!e.match(/\.skeleton\.(wx|xhs)ml$/))return f(u,`骨架屏后缀需为 .skeleton.xhsml :${m.skeleton.path}`);if(!n.existsSync(e))return f(u,`骨架屏不存在:${m.skeleton.path}`);const r=D(e,s);if(!0===(null==a?void 0:a.independent)&&!r.startsWith(`${a.root}/`))return f(u,`独立分包只可引用分包root下的骨架屏:${m.skeleton.path}`);if(!a)for(const t of o.subPackages)if(r.startsWith(`${t.root}/`))return f(u,`主包不可引用分包下骨架屏:${m.skeleton.path}`);l.skeleton={path:r.replace(/\.(wx|xhs)ml$/,""),options:(null===(p=m.skeleton)||void 0===p?void 0:p.options)||{}}}else Reflect.deleteProperty(l,"skeleton")}return l})),T=(t,n,s)=>{w=1;const o=g(n,"app.json");let r=S(o);r.subPackages=r.subPackages||r.subpackages||[],r=((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&&J(t.networkTimeout,n),"usingComponents"in t&&k(t.usingComponents,n),t})(r,o);const a=`${s}/ext.json`,i=s?S(a):{};let c={};((null==i?void 0:i.extEnable)&&(null==i?void 0:i.directCommit)||(null==i?void 0:i.extEnable))&&(r=C(r,i,a),c=i.extPages||{}),r.componentsMap={},M(r.usingComponents)?r.usingComponents=A(r.usingComponents,r,n,o):Reflect.deleteProperty(r,"usingComponents"),r.pages.map(((t,e)=>{if(!_(g(n,t),"Page"))return f(o,`${O("Page")}:${t} in pages[${e}]`)})),r.pages=q(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 _(g(n,a),"Page")?a:f(o,`${O("Page")}:${s} in subPackages[${e}].pages[${r}]`)}));return{...t,pages:q(s,n,r,c,t)}})),r.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1",allowLazyAppJs:!0};const p=e(r);Reflect.deleteProperty(p,"subPackages");const l=r.subPackages.reduce(((t,e)=>[...t,...e.pages]),[]);return p.pages=l?[...p.pages,...l]:p.pages,{legacyAppJSON:p,appJSON:r}},R="baba:changeExtJsonPath",F="baba:changeExtJson",z="miniprogram",V="minigame",I=t=>{t=u(t);const e=g(t,"game.json");let s=S(e);return s.subPackages=s.subPackages||s.subpackages||[],s.pages=[],s.subPackages=s.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,e,s)=>{var o,r,a;const i=t=>(f(s||"game.json",t),!1);if(t.subPackages.length)for(let s=0;s<t.subPackages.length;s++){const{name:c,root:p,entry:l,dirname:u}=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==l?void 0:l.trim)||void 0===a?void 0:a.call(l)))return i(`subPackages[${s}].root 不能为 ${p}`);if("/"===u)return i(`subPackages[${s}].root 不能在根目录下 ${p}`);const g=h(e,l);if(!n.existsSync(`${g}.js`)&&!n.existsSync(`${g}.ts`))return i(`未找到 subPackages[${s}].root 对应的 ${l}.(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(l===o)return i(`subPackages[${s}].root 和 subPackages[${e}].root 指向相同文件`);if(u===r)return i(`subPackages[${s}].root 和 subPackages[${e}].root 在同一目录下`);if(u.startsWith(`${r}/`)||r.startsWith(`${u}/`))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=l((()=>{var t,e,n,s;const{projectConfig:r}=this.snapshot,a=S(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=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=o.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=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=S(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 l=c.filter((t=>t.id===i))[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,s,o;const r=this.projectPrivateConfigJsonPath,a={path:"",query:"",launchMode:"default"};if(!p.existsSync(r))return a;const i=S(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,l=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 l?{path:l.pathName,query:l.query.trim(),launchMode:l.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=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{legacyAppJSON:t,appJSON:e}=T(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=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=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})}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 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=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{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:T,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;
|
package/dist/project.d.ts
CHANGED
|
@@ -66,7 +66,6 @@ export declare class Project extends EventEmitter {
|
|
|
66
66
|
get settings(): any;
|
|
67
67
|
changeCompileSetting(setting: any): void;
|
|
68
68
|
createDefaultProjectConfig(projectPath: string): void;
|
|
69
|
-
changeExtJsonPermission(permission: boolean): void;
|
|
70
69
|
getSubPackages(): any;
|
|
71
70
|
isFileInMainPkg(filePath: any): any;
|
|
72
71
|
isFileInSubpackage(filePath: any): any;
|
package/dist/types/project.d.ts
CHANGED
|
@@ -20,7 +20,6 @@ export interface IProject extends EventEmitter {
|
|
|
20
20
|
getTransformedAppJson(): any;
|
|
21
21
|
getTransformedGameJson(): any;
|
|
22
22
|
updateCustomEntry(): any;
|
|
23
|
-
changeExtJsonPermission(can_upload_ext_json: boolean): any;
|
|
24
23
|
getSubPackages(): ISubPackage[];
|
|
25
24
|
isFileInMainPkg(filePath: any): any;
|
|
26
25
|
isFileInSubpackage(filePath: any): any;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xhs-mp-project",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.6",
|
|
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.6",
|
|
18
18
|
"chokidar": "^3.6.0",
|
|
19
19
|
"fs-extra": "^11.2.0",
|
|
20
20
|
"lodash": "^4.17.21",
|