xhs-mp-project 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("path"),e=require("json-to-ast"),n=require("clone"),s=require("fs-extra"),o=require("lodash"),i=require("events"),a=require("process");function r(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=r(t),p=r(s);require("crypto"),require("net");const h=(t,e=100)=>{let n=null;return function(...s){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,s))}},l=(t="")=>t.split(c.sep).join(c.posix.sep),u=(...t)=>l(c.join(...t)),g=(...t)=>l(c.resolve(...t)),d=c.posix.sep,m=t=>{if(!l(t).includes(d))return"/";const e=l(t).split(d).filter(Boolean);return e.slice(0,e.length-1).join(d)};const f=require("util").inspect.custom;class j extends Error{constructor(t){super(t),this.details=void 0,this.module=void 0,this.loc=void 0,this.hideStack=void 0,this.chunk=void 0,this.file=void 0}[f](){return this.stack+(this.details?`\n${this.details}`:"")}serialize({write:t}){t(this.name),t(this.message),t(this.stack),t(this.details),t(this.loc),t(this.hideStack)}deserialize({read:t}){this.name=t(),this.message=t(),this.stack=t(),this.details=t(),this.loc=t(),this.hideStack=t()}}const P=(t,e)=>t===e||(!(!t.startsWith(e)||!t.replace(e,"").startsWith("/"))||!(!e.startsWith(t)||!e.replace(t,"").startsWith("/"))),J=(t,e)=>{const n=t=>{const n=new j(`[ app.json 内容错误 ]: ${t}`);return n.file="app.json",n.stack=void 0,null==e||e.errors.push(n),{pages:[],subPackages:[]}},s=t.pages;if(!("pages"in t))return n("缺少 pages 字段");if(t.pages.length<=0)return n("pages 不能为空");if(t.window){if(t.window.navigationBarTextStyle&&!["black","white"].includes(t.window.navigationBarTextStyle))return n("window.navigationBarTextStyle 错误: 仅支持 black / white");if(t.window.backgroundTextStyle&&!["dark","light"].includes(t.window.backgroundTextStyle))return n("window.backgroundTextStyle 错误: 仅支持 dark / light");if(t.window.navigationStyle&&!["default","custom"].includes(t.window.navigationStyle))return n("window.navigationStyle 错误: 仅支持 default / custom")}if(t.tabBar){if(!t.tabBar.list)return n("tabBar.list 不能为空");if(t.tabBar.list.length<2)return n("tabBar.list 错误: tabBar.list 需至少包含 2 项");if(t.tabBar.list.length>5)return n("tabBar.list 错误: tabBar.list 不能超过 5 项");for(const{pagePath:e}of t.tabBar.list){if(!e)return n('tabBar.list 错误: "pagePath" 不能为空');if(!s.includes(e))return n(`tabBar.list 错误: "${e}" 不在 "pages" 内`)}}if(t.subPackages.length){const e=[],o=[];for(let s=0;s<t.subPackages.length;s++){if("string"!=typeof t.subPackages[s].root)return n(`subPackages[${s}].root 字段类型需为 string`);if(t.subPackages[s].name){const e=o.indexOf(t.subPackages[s].name);if(e>-1)return n(`subPackages[${s}] 与 subPackages[${e}] 的 name 相同`);o.push(t.subPackages[s].name)}e.push(t.subPackages[s].root)}for(let o=0;o<e.length;o++){if(!e[o])return n(`subPackages[${o}].root 不能为空`);if(!t.subPackages[o].pages)return n(`subPackages[${o}].pages 不能为空`);const i=s.findIndex((t=>P(t,e[o])));if(i>-1)return n(`pages[${i}] 与 subPackages[${o}] 存在包含关系`);for(let t=o+1;t<e.length;t++)if(P(e[o],e[t]))return n(`subPackages[${o}] 与 subPackages[${t}] 存在包含关系`)}}return t.networkTimeout&&"object"!=typeof t.networkTimeout?n("networkTimeout 字段需为 object"):t},v=["window","networkTimeout","tabBar"],y=(t,e,n)=>{if(!((t,e,n)=>{var s;const o=t=>{const e=new j(`[ ext.json 内容错误 ]: ${t}`);e.file="ext.json",e.stack=void 0,null==n||n.errors.push(e)},i=t.pages;if(e.pages)for(const t of e.pages)if(!i.includes(t))return o(`page 错误: "${t}" 不在 app.json 内`),!1;if(e.extPages)for(const t of Object.keys(e.extPages))if(!i.includes(t))return o(`extPages 错误: "${t}" 不在 app.json 内`),!1;if(null===(s=null==e?void 0:e.tabBar)||void 0===s?void 0:s.list){if(e.tabBar.list.length<2)return o("tabBar.list 错误: tabBar.list 需至少包含 2 项"),!1;for(const{pagePath:t}of e.tabBar.list){if(!t)return o('tabBar.list 错误: "pagePath" 不能为空'),!1;if(!i.includes(t))return o(`tabBar.list 错误: "${t}" 不在 app.json 内`),!1}}return!0})(t,e,n))return t;for(const[n,s]of Object.entries(e))if("extPages"!==n)if("object"==typeof s&&v.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};let b=1;const x=(t,n)=>{try{const n=s.readFileSync(`${t}`,"utf-8");return e(n,{loc:!0,source:t}),JSON.parse(n)}catch(e){console.error(e);const s=new j(`[ ${t} 内容错误 ]: ${e.message}`);return s.file=e.source,s.stack=void 0,null==n||n.errors.push(s),{pages:[]}}},k=(e,n,o,i)=>{if(i.startsWith("/"))return u(n,i).replace(`${n}/`,"");if(i.startsWith("."))return u(n,t.dirname(o),i).replace(`${n}/`,"");if(s.existsSync(`${u(n,i)}.js`))return u(n,i).replace(`${n}/`,"");if(i.startsWith("plugin://"))return console.log("小红书小程序暂不支持插件:",i),"";const a=x(`${e}/package.json`),r=Object.keys(a.dependencies||[]).find((t=>i.includes(t)))||"",c=i.replace(r,""),p=x(`${e}/node_modules/${r}/package.json`);let h=p.miniprogram||p.main;try{h=h.endsWith(".js")?h.replace(/.js$/,""):h;const n=t.join(e,"node_modules",r,h,c).replace(`${e}/`,"");return s.existsSync(`${e}/${n}.${`${process.env.__platform}ml`.trim()}`)?n:`${n}/index`}catch(t){throw new Error(`组件路径不正确:${i}`)}},S=/\.|\/|\.\/|\.\.\//g,C=(t,e,n,o)=>{if(!Object.keys(t.componentsMap).includes(o)&&s.existsSync(`${n}/${o}.json`)){const s=x(`${n}/${o}.json`);if(t.componentsMap[o]=s,t.componentsMap[o].index=b++,s.componentPlaceholder){const i=s.componentPlaceholder;for(const[s,a]of Object.entries(i)){let r;r=S.test(a)?k(e,n,o,a):a,i[s]=r,S.test(a)&&C(t,e,n,r)}t.componentsMap[o].componentPlaceholder=i}if(s.usingComponents)for(const[i,a]of Object.entries(s.usingComponents)){const r=k(e,n,o,a);s.usingComponents[i]=r,C(t,e,n,r)}}},$=(t,e,n,o,i)=>t.map((t=>{var a,r;let c={path:t};const p=g(n,`${c.path}.json`);if(s.existsSync(p)){let t=x(p);if((null===(a=o.extPages)||void 0===a?void 0:a[`${c.path}`])&&(t={...t,...null===(r=o.extPages)||void 0===r?void 0:r[`${c.path}`]}),c={...c,...t},c.componentPlaceholder)for(const[t,s]of Object.entries(c.componentPlaceholder))if(S.test(s)){const o=k(e,n,c.path,s);c.componentPlaceholder[t]=o,C(i,e,n,o)}if(c.usingComponents)for(const[t,s]of Object.entries(c.usingComponents)){const o=k(e,n,c.path,s);c.usingComponents[t]=o,C(i,e,n,o)}}return c})),w=(t,e,s,o)=>{b=1;const i=g(e,"app.json");let a=x(i,o);a.subPackages=a.subPackages||a.subpackages||[],a=J(a,o);const r=s?x(`${s}/ext.json`,o):{};if((null==r?void 0:r.extEnable)&&(a=y(a,r,o)),a.componentsMap={},a.usingComponents)for(const[n,s]of Object.entries(a.usingComponents)){const o=k(t,e,"",s);a.usingComponents[n]=o,C(a,t,e,o)}a.pages=$(a.pages,t,e,r,a),a.subPackages=a.subPackages.map((n=>{const s=n.pages.map((t=>u(n.root,t)));return{...n,pages:$(s,t,e,r,a)}})),a.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1"};const c=n(a);Reflect.deleteProperty(c,"subPackages");const p=a.subPackages.reduce(((t,e)=>[...t,...e.pages]),[]);return c.pages=p?[...c.pages,...p]:c.pages,{legacyAppJSON:c,appJSON:a}},_="baba:changeExtJsonPath",O="baba:changeExtJson",D="miniprogram",N="minigame",E=(t,e)=>{t=l(t);const n=g(t,"game.json");let s=x(n,e);return s.subPackages=s.subPackages||s.subpackages||[],s.pages=[],s.subPackages=s.subPackages.map((t=>{let e=l(`${t.root}`).replace(/^\/+|\/+$/g,"");e=/\.(ts|js)$/.test(e)?e.replace(/\.(ts|js)$/,""):u(e,"game");const n={rawRoot:`${t.root}`,entry:e,dirname:m(e)};return{...t,...n}})),J(s,e)?{appJSON:s}:{appJSON:{pages:[],subPackages:[]}}};class M{constructor(t){this.project=t}}const B=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},q=(t,e)=>{let n=[];((null==t?void 0:t.subPackages)||[]).forEach((t=>{n=n.concat(t.pages)}));const s=n.find((t=>t.path===e))||{};return(null==s?void 0:s.usingComponents)||{}},A=t=>(null==t?void 0:t.usingComponents)||{};const T=(t,e)=>{var n;const s=t.componentsMap||{};return(null===(n=null==s?void 0:s[e])||void 0===n?void 0:n.usingComponents)||{}},z=require("chokidar"),R={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class F extends i.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=D,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=h((()=>{var t,e,n,s;const{projectConfig:i}=this.snapshot,a=p.readJSONSync(this.projectJsonPath);this._projectJsonContent=a,this.snapshot.projectConfig=o.cloneDeep(a),this.emit("project-config-json-changed",{oldData:i,newData:a}),(null==i?void 0:i.libVersion)!==(null==a?void 0:a.libVersion)&&this.emit("lib-version-changed",{oldData:null==i?void 0:i.libVersion,newData:null==a?void 0:a.libVersion}),(null===(t=null==i?void 0:i.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==i?void 0:i.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=h((()=>{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=h((()=>{const{appJson:t}=this.snapshot,e=p.readJSONSync(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=o.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=h((()=>{const{extJson:t}=this.snapshot,e=p.readJSONSync(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=o.cloneDeep(e),this.emit(O,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,s,o;const i={pathName:"",query:"",entry:"",item:{}};try{if(!p.existsSync(t))return i;const a=p.readJSONSync(t),r=(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===r)return i;const h=c.filter((t=>t.id===r))[0];return h?(i.pathName=h.pathName,i.query=h.query,"halfPageNativeFunctionalized"===h.launchMode&&(i.query=h.query+="&runtime_mode=2"),i.entry=i.query?`${i.pathName}?${i.query}`:`${i.pathName}`,i):i}catch(t){return console.log("getCustomEntry error: ",t),i}},this.getCompileEntryPage=()=>{var t,e,n,s,o;const i=this.projectPrivateConfigJsonPath,a={path:"",query:"",launchMode:"default"};if(!p.existsSync(i))return a;const r=p.readJsonSync(i),c=null===(e=null===(t=null==r?void 0:r.condition)||void 0===t?void 0:t.miniprogram)||void 0===e?void 0:e.current,h=null===(o=null===(s=null===(n=null==r?void 0:r.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 h?{path:h.pathName,query:h.query.trim(),launchMode:h.launchMode}:a};const{watch:e}=t;let n=t.projectPath;if(c.isAbsolute(n)||(n=c.join(a.cwd(),n)),this.checker=new M(this),this._projectPath=n,this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",this._appMode=p.existsSync(c.join(this._projectPath,"game.json"))?N:D,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this._appMode===D){const{legacyAppJSON:t,appJSON:e}=w(this.projectPath,this.miniprogramDir,this.extJsonDir);this.appJSON=e,this.legacyAppJSON=t,this.updateAppJson()}else{const{appJSON:t}=E(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=((t="")=>t.split(c.sep).join(c.posix.sep))(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get miniprogramDir(){const t=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=p.readJSONSync(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===D?"app.json":"game.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=p.readJsonSync(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=p.readJSONSync(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=p.readJSONSync(this.extJsonPath).ext||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(_))}get canUploadExtJson(){return this._canUploadExtJson}get customEntry(){return this._customEntry}get projectAppId(){let t="";try{t=this.projectJsonContent.appid}catch(t){console.log("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=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,R,{spaces:2})}updateCustomEntry(){const t=this.getCustomEntry(this.projectPrivateConfigJsonPath),e=(null==t?void 0:t.entry)||"";this._customEntry=e}get settings(){var t;return{minified:!0,urlCheck:!0,useNewCompiler:!1,enableV2:!1,...(null===(t=this.projectJsonContent)||void 0===t?void 0:t.setting)||{}}}changeCompileSetting(t){const e=this.projectJsonContent;e.setting?e.setting={...e.setting,...t}:e.setting=t,this.projectJsonContent=e}createDefaultProjectConfig(t){const e={description:"项目配置文件",setting:{urlCheck:!0,minified:!0},compileType:"miniprogram",libVersion:"latest",appid:"wx93e095ba089dbfb5",projectname:c.basename(t)},n=c.resolve(t,this._projectJson);p.writeJSONSync(n,e,{spaces:2})}changeExtJsonPermission(t){this._canUploadExtJson=t,t&&p.existsSync(this.extJsonPath)?this.extJsonDir=this.projectPath:this.extJsonDir=""}getSubPackages(){return this.appJSON.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e}/`)))}getComponentsMap(){return this.appJSON.componentsMap||{}}getPages(){return this.appJSON.pages||[]}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const s=c.extname(e);return n.some((t=>`${t}${s}`===e))}getPageDependComponentPaths(t,e){const n=function(t){var e,n;if(!(null==t?void 0:t.subPackages)||!(null==t?void 0:t.subPackages.length))return!1;let s=!1;try{s=!("string"==typeof(null===(n=null===(e=null==t?void 0:t.subPackages)||void 0===e?void 0:e[0].pages)||void 0===n?void 0:n[0]))}catch(t){}return s}(this.appJSON),s=((t,e,n=!1,s=!0)=>{try{const o=new Set;let i={};i=s?{...n?{}:A(e),...B(e,t),...q(e,t)}:{...A(e),...B(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 i=T(e,s);i&&Object.keys(i).length&&a(i)}};return a(i),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=l(`${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 F(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 W={transformAppJson:w,transformGameJson:E,pureAppJSON:(t,e="v1")=>{var n,s;const i=o.cloneDeep(t);if("v1"===e&&(i.subPackages=(null===(n=i.subPackages)||void 0===n?void 0:n.map((t=>{const{entry:e,dirname:n,name:s,independent:o,rawRoot:i}=t;return{independent:o,root:n,path:e,rawRoot:i,name:s,pages:[{path:e}]}})))||[],i.pages=[{path:"/"}]),"v0"===e){const t=null===(s=i.subPackages)||void 0===s?void 0:s.map((t=>({path:t.entry})));Reflect.deleteProperty(i,"subPackages"),i.pages=[{path:"/"},...t]}return Reflect.deleteProperty(i,"subpackages"),i.compileType="minigame",i}};exports.Project=F,exports.utils=W;
|
|
1
|
+
"use strict";var t=require("path"),e=require("json-to-ast"),n=require("clone"),s=require("fs-extra"),o=require("lodash"),i=require("events"),a=require("process");function r(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=r(t),p=r(s);require("crypto"),require("net");const h=(t,e=100)=>{let n=null;return function(...s){(!n||Date.now()-n>e)&&(n=Date.now(),t.apply(this,s))}},l=(t="")=>t.split(c.sep).join(c.posix.sep),u=(t="")=>t.split(c.sep).join(c.posix.sep),g=(...t)=>u(c.join(...t)),d=(...t)=>u(c.resolve(...t)),m=c.posix.sep,f=t=>{if(!u(t).includes(m))return"/";const e=u(t).split(m).filter(Boolean);return e.slice(0,e.length-1).join(m)};const j=require("util").inspect.custom;class P extends Error{constructor(t){super(t),this.details=void 0,this.module=void 0,this.loc=void 0,this.hideStack=void 0,this.chunk=void 0,this.file=void 0}[j](){return this.stack+(this.details?`\n${this.details}`:"")}serialize({write:t}){t(this.name),t(this.message),t(this.stack),t(this.details),t(this.loc),t(this.hideStack)}deserialize({read:t}){this.name=t(),this.message=t(),this.stack=t(),this.details=t(),this.loc=t(),this.hideStack=t()}}const J=(t,e)=>t===e||(!(!t.startsWith(e)||!t.replace(e,"").startsWith("/"))||!(!e.startsWith(t)||!e.replace(t,"").startsWith("/"))),v=(t,e)=>{const n=t=>{const n=new P(`[ app.json 内容错误 ]: ${t}`);return n.file="app.json",n.stack=void 0,null==e||e.errors.push(n),{pages:[],subPackages:[]}},s=t.pages;if(!("pages"in t))return n("缺少 pages 字段");if(t.pages.length<=0)return n("pages 不能为空");if(t.window){if(t.window.navigationBarTextStyle&&!["black","white"].includes(t.window.navigationBarTextStyle))return n("window.navigationBarTextStyle 错误: 仅支持 black / white");if(t.window.backgroundTextStyle&&!["dark","light"].includes(t.window.backgroundTextStyle))return n("window.backgroundTextStyle 错误: 仅支持 dark / light");if(t.window.navigationStyle&&!["default","custom"].includes(t.window.navigationStyle))return n("window.navigationStyle 错误: 仅支持 default / custom")}if(t.tabBar){if(!t.tabBar.list)return n("tabBar.list 不能为空");if(t.tabBar.list.length<2)return n("tabBar.list 错误: tabBar.list 需至少包含 2 项");if(t.tabBar.list.length>5)return n("tabBar.list 错误: tabBar.list 不能超过 5 项");for(const{pagePath:e}of t.tabBar.list){if(!e)return n('tabBar.list 错误: "pagePath" 不能为空');if(!s.includes(e))return n(`tabBar.list 错误: "${e}" 不在 "pages" 内`)}}if(t.subPackages.length){const e=[],o=[];for(let s=0;s<t.subPackages.length;s++){if("string"!=typeof t.subPackages[s].root)return n(`subPackages[${s}].root 字段类型需为 string`);if(t.subPackages[s].name){const e=o.indexOf(t.subPackages[s].name);if(e>-1)return n(`subPackages[${s}] 与 subPackages[${e}] 的 name 相同`);o.push(t.subPackages[s].name)}e.push(t.subPackages[s].root)}for(let o=0;o<e.length;o++){if(!e[o])return n(`subPackages[${o}].root 不能为空`);if(!t.subPackages[o].pages)return n(`subPackages[${o}].pages 不能为空`);const i=s.findIndex((t=>J(t,e[o])));if(i>-1)return n(`pages[${i}] 与 subPackages[${o}] 存在包含关系`);for(let t=o+1;t<e.length;t++)if(J(e[o],e[t]))return n(`subPackages[${o}] 与 subPackages[${t}] 存在包含关系`)}}return t.networkTimeout&&"object"!=typeof t.networkTimeout?n("networkTimeout 字段需为 object"):t},y=["window","networkTimeout","tabBar"],b=(t,e,n)=>{if(!((t,e,n)=>{var s;const o=t=>{const e=new P(`[ ext.json 内容错误 ]: ${t}`);e.file="ext.json",e.stack=void 0,null==n||n.errors.push(e)},i=t.pages;if(e.pages)for(const t of e.pages)if(!i.includes(t))return o(`page 错误: "${t}" 不在 app.json 内`),!1;if(e.extPages)for(const t of Object.keys(e.extPages))if(!i.includes(t))return o(`extPages 错误: "${t}" 不在 app.json 内`),!1;if(null===(s=null==e?void 0:e.tabBar)||void 0===s?void 0:s.list){if(e.tabBar.list.length<2)return o("tabBar.list 错误: tabBar.list 需至少包含 2 项"),!1;for(const{pagePath:t}of e.tabBar.list){if(!t)return o('tabBar.list 错误: "pagePath" 不能为空'),!1;if(!i.includes(t))return o(`tabBar.list 错误: "${t}" 不在 app.json 内`),!1}}return!0})(t,e,n))return t;for(const[n,s]of Object.entries(e))if("extPages"!==n)if("object"==typeof s&&y.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};let x=1;const k=(t,n)=>{try{const n=s.readFileSync(`${t}`,"utf-8");return e(n,{loc:!0,source:t}),JSON.parse(n)}catch(e){console.error(e);const s=new P(`[ ${t} 内容错误 ]: ${e.message}`);return s.file=e.source,s.stack=void 0,null==n||n.errors.push(s),{pages:[]}}},S=(e,n,o,i)=>{if(i.startsWith("/"))return g(n,i).replace(`${n}/`,"");if(i.startsWith("."))return g(n,t.dirname(o),i).replace(`${n}/`,"");if(s.existsSync(`${g(n,i)}.js`))return g(n,i).replace(`${n}/`,"");if(i.startsWith("plugin://"))return console.log("小红书小程序暂不支持插件:",i),"";const a=k(`${e}/package.json`),r=Object.keys(a.dependencies||[]).find((t=>i.includes(t)))||"",c=i.replace(r,""),p=k(`${e}/node_modules/${r}/package.json`);let h=p.miniprogram||p.main;try{h=h.endsWith(".js")?h.replace(/.js$/,""):h;const n=t.join(e,"node_modules",r,h,c).replace(`${e}/`,"");return s.existsSync(`${e}/${n}.${`${process.env.__platform}ml`.trim()}`)?n:`${n}/index`}catch(t){throw new Error(`组件路径不正确:${i}`)}},C=/\.|\/|\.\/|\.\.\//g,$=(t,e,n,o)=>{if(!Object.keys(t.componentsMap).includes(o))if(s.existsSync(`${n}/${o}.json`)){const s=k(`${n}/${o}.json`);if(t.componentsMap[o]=s,t.componentsMap[o].index=x++,s.componentPlaceholder){const i=s.componentPlaceholder;for(const[s,a]of Object.entries(i)){let r;r=C.test(a)?S(e,n,o,a):a,i[s]=r,C.test(a)&&$(t,e,n,r)}t.componentsMap[o].componentPlaceholder=i}if(s.usingComponents)for(const[i,a]of Object.entries(s.usingComponents)){const r=S(e,n,o,a);s.usingComponents[i]=r,$(t,e,n,r)}}else console.error(`cann't no found ${n}/${o}.json`)},w=(t,e,n,o,i)=>t.map((t=>{var a,r;let c={path:t};const p=d(n,`${c.path}.json`);if(s.existsSync(p)){let t=k(p);if((null===(a=o.extPages)||void 0===a?void 0:a[`${c.path}`])&&(t={...t,...null===(r=o.extPages)||void 0===r?void 0:r[`${c.path}`]}),c={...c,...t},c.componentPlaceholder)for(const[t,s]of Object.entries(c.componentPlaceholder))if(C.test(s)){const o=S(e,n,c.path,s);c.componentPlaceholder[t]=o,$(i,e,n,o)}if(c.usingComponents)for(const[t,s]of Object.entries(c.usingComponents)){const o=S(e,n,c.path,s);c.usingComponents[t]=o,$(i,e,n,o)}}return c})),_=(t,e,s,o)=>{x=1;const i=d(e,"app.json");let a=k(i,o);a.subPackages=a.subPackages||a.subpackages||[],a=v(a,o);const r=s?k(`${s}/ext.json`,o):{};if((null==r?void 0:r.extEnable)&&(a=b(a,r,o)),a.componentsMap={},a.usingComponents)for(const[n,s]of Object.entries(a.usingComponents)){const o=S(t,e,"",s);a.usingComponents[n]=o,$(a,t,e,o)}a.pages=w(a.pages,t,e,r,a),a.subPackages=a.subPackages.map((n=>{const s=n.pages.map((t=>g(n.root,t)));return{...n,pages:w(s,t,e,r,a)}})),a.xhsVersionStrategy={classCalc:"v1",pageNotFoundMigration:!0,animationEventTriggerPatchPerfix:"xhs_animate_",renderEventDelegation:!0,canvasToTempFilePath:"v1"};const c=n(a);Reflect.deleteProperty(c,"subPackages");const p=a.subPackages.reduce(((t,e)=>[...t,...e.pages]),[]);return c.pages=p?[...c.pages,...p]:c.pages,{legacyAppJSON:c,appJSON:a}},O="baba:changeExtJsonPath",D="baba:changeExtJson",N="miniprogram",E="minigame",M=(t,e)=>{t=u(t);const n=d(t,"game.json");let s=k(n,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)$/,""):g(e,"game");const n={rawRoot:`${t.root}`,entry:e,dirname:f(e)};return{...t,...n}})),v(s,e)?{appJSON:s}:{appJSON:{pages:[],subPackages:[]}}};class B{constructor(t){this.project=t}}const q=(t,e)=>{const n=t.pages.find((t=>t.path===e))||{};return(null==n?void 0:n.usingComponents)?n.usingComponents:{}},A=(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)||{}},T=t=>(null==t?void 0:t.usingComponents)||{};const z=(t,e)=>{var n;const s=t.componentsMap||{};return(null===(n=null==s?void 0:s[e])||void 0===n?void 0:n.usingComponents)||{}},R=require("chokidar"),F={description:"项目配置文件",packOptions:{ignore:[]},appid:"testappid",miniprogramRoot:"."};class W extends i.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=N,this._isLazyload=!1,this._customEntry="",this.jsonDependencies=[],this.onProjectConfigJsonChange=h((()=>{var t,e,n,s;const{projectConfig:i}=this.snapshot,a=p.readJSONSync(this.projectJsonPath);this._projectJsonContent=a,this.snapshot.projectConfig=o.cloneDeep(a),this.emit("project-config-json-changed",{oldData:i,newData:a}),(null==i?void 0:i.libVersion)!==(null==a?void 0:a.libVersion)&&this.emit("lib-version-changed",{oldData:null==i?void 0:i.libVersion,newData:null==a?void 0:a.libVersion}),(null===(t=null==i?void 0:i.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==i?void 0:i.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=h((()=>{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=h((()=>{const{appJson:t}=this.snapshot,e=p.readJSONSync(this.entryJsonPath);this._appJsonContent=e,this.snapshot.appJson=o.cloneDeep(e),this.emit("app-json-changed",{oldData:t,newData:e})}),100),this.onExtJsonChange=h((()=>{const{extJson:t}=this.snapshot,e=p.readJSONSync(this.extJsonPath).ext||{};this._extJsonContent=e,this.snapshot.extJson=o.cloneDeep(e),this.emit(D,{oldData:t,newData:e})}),100),this.getCustomEntry=t=>{var e,n,s,o;const i={pathName:"",query:"",entry:"",item:{}};try{if(!p.existsSync(t))return i;const a=p.readJSONSync(t),r=(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===r)return i;const h=c.filter((t=>t.id===r))[0];return h?(i.pathName=h.pathName,i.query=h.query,"halfPageNativeFunctionalized"===h.launchMode&&(i.query=h.query+="&runtime_mode=2"),i.entry=i.query?`${i.pathName}?${i.query}`:`${i.pathName}`,i):i}catch(t){return console.log("getCustomEntry error: ",t),i}},this.getCompileEntryPage=()=>{var t,e,n,s,o;const i=this.projectPrivateConfigJsonPath,a={path:"",query:"",launchMode:"default"};if(!p.existsSync(i))return a;const r=p.readJsonSync(i),c=null===(e=null===(t=null==r?void 0:r.condition)||void 0===t?void 0:t.miniprogram)||void 0===e?void 0:e.current,h=null===(o=null===(s=null===(n=null==r?void 0:r.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 h?{path:h.pathName,query:h.query.trim(),launchMode:h.launchMode}:a};const{watch:e}=t;let n=t.projectPath;if(c.isAbsolute(n)||(n=c.join(a.cwd(),n)),this.checker=new B(this),this._projectPath=l(n),this._extJsonDir=p.existsSync(this.extJsonPath)?this.projectPath:"",this._appMode=p.existsSync(c.posix.join(this._projectPath,"game.json"))?E:N,this.jsonDependencieMap=new Set([c.normalize(this.entryJsonPath)]),this._isLazyload="requiredComponents"===this.appJsonContent.lazyCodeLoading,this.updateCustomEntry(),this._appMode===N){const{legacyAppJSON:t,appJSON:e}=_(this.projectPath,this.miniprogramDir,this.extJsonDir);this.appJSON=e,this.legacyAppJSON=t,this.updateAppJson()}else{const{appJSON:t}=M(this.projectPath);this.appJSON=t,this.legacyAppJSON={}}e&&this.watchProject()}get projectPath(){return this._projectPath}get projectJsonPath(){return c.resolve(this._projectPath,this._projectJson)}get miniprogramRoot(){return this.projectJsonContent.miniprogramRoot||""}get projectMiniprogramPath(){const t=l(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get miniprogramDir(){const t=l(c.join(this.projectPath,this.miniprogramRoot));return t.endsWith("/")?t.slice(0,-1):t}get appMode(){return this._appMode}get lazyload(){return this._isLazyload}get projectJsonContent(){return p.pathExistsSync(this.projectJsonPath)?(this._projectJsonContent||(this._projectJsonContent=p.readJSONSync(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===N?"app.json":"game.json"}get entryJsonPath(){if(!p.existsSync(this.projectJsonPath))return"";const t=p.readJsonSync(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=p.readJSONSync(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=p.readJSONSync(this.extJsonPath).ext||{}),this._extJsonContent):{}}get extJsonDir(){return this._extJsonDir}set extJsonDir(t){this.extJsonDir!==t&&(this._extJsonDir=t,this.emit(O))}get canUploadExtJson(){return this._canUploadExtJson}get customEntry(){return this._customEntry}get projectAppId(){let t="";try{t=this.projectJsonContent.appid}catch(t){console.log("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=R.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,F,{spaces:2})}updateCustomEntry(){const t=this.getCustomEntry(this.projectPrivateConfigJsonPath),e=(null==t?void 0:t.entry)||"";this._customEntry=e}get settings(){var t;return{minified:!0,urlCheck:!0,useNewCompiler:!1,enableV2:!1,...(null===(t=this.projectJsonContent)||void 0===t?void 0:t.setting)||{}}}changeCompileSetting(t){const e=this.projectJsonContent;e.setting?e.setting={...e.setting,...t}:e.setting=t,this.projectJsonContent=e}createDefaultProjectConfig(t){const e={description:"项目配置文件",setting:{urlCheck:!0,minified:!0},compileType:"miniprogram",libVersion:"latest",appid:"wx93e095ba089dbfb5",projectname:c.basename(t)},n=c.resolve(t,this._projectJson);p.writeJSONSync(n,e,{spaces:2})}changeExtJsonPermission(t){this._canUploadExtJson=t,t&&p.existsSync(this.extJsonPath)?this.extJsonDir=this.projectPath:this.extJsonDir=""}getSubPackages(){return this.appJSON.subPackages||[]}isFileInMainPkg(t){return this.getSubPackages().every((e=>!t.startsWith(`${e}/`)))}isFileInSubpackage(t){return this.getSubPackages().some((e=>t.startsWith(`${e}/`)))}getComponentsMap(){return this.appJSON.componentsMap||{}}getPages(){return this.appJSON.pages||[]}isPkgPage(t,e){let n=[];n="xhs-main"===t?this.appJsonContent.pages||[]:t.pages||[];const s=c.extname(e);return n.some((t=>`${t}${s}`===e))}getPageDependComponentPaths(t,e){const n=function(t){var e,n;if(!(null==t?void 0:t.subPackages)||!(null==t?void 0:t.subPackages.length))return!1;let s=!1;try{s=!("string"==typeof(null===(n=null===(e=null==t?void 0:t.subPackages)||void 0===e?void 0:e[0].pages)||void 0===n?void 0:n[0]))}catch(t){}return s}(this.appJSON),s=((t,e,n=!1,s=!0)=>{try{const o=new Set;let i={};i=s?{...n?{}:T(e),...q(e,t),...A(e,t)}:{...T(e),...q(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 i=z(e,s);i&&Object.keys(i).length&&a(i)}};return a(i),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 W(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 V={transformAppJson:_,transformGameJson:M,pureAppJSON:(t,e="v1")=>{var n,s;const i=o.cloneDeep(t);if("v1"===e&&(i.subPackages=(null===(n=i.subPackages)||void 0===n?void 0:n.map((t=>{const{entry:e,dirname:n,name:s,independent:o,rawRoot:i}=t;return{independent:o,root:n,path:e,rawRoot:i,name:s,pages:[{path:e}]}})))||[],i.pages=[{path:"/"}]),"v0"===e){const t=null===(s=i.subPackages)||void 0===s?void 0:s.map((t=>({path:t.entry})));Reflect.deleteProperty(i,"subPackages"),i.pages=[{path:"/"},...t]}return Reflect.deleteProperty(i,"subpackages"),i.compileType="minigame",i}};exports.Project=W,exports.utils=V;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xhs-mp-project",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"author": "",
|
|
15
15
|
"license": "ISC",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"xhs-mp-utils": "^1.3.
|
|
17
|
+
"xhs-mp-utils": "^1.3.4",
|
|
18
18
|
"chokidar": "^3.6.0",
|
|
19
19
|
"fs-extra": "^11.2.0",
|
|
20
20
|
"lodash": "^4.17.21",
|