@hap-toolkit/packager
Version:
@hap-toolkit/packager
3 lines (2 loc) • 4.83 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.CardPlugin=void 0;var _Compilation=_interopRequireDefault(require("webpack/lib/Compilation")),_path=_interopRequireDefault(require("path")),_utils=require("../common/utils"),_sharedUtils=require("@hap-toolkit/shared-utils"),_constant=require("../common/constant"),_postHandler=require("../post-handler");function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}const SUFFIX_UX=".ux",CARD_ENTRY="#entry",TYPE_IMPORT="import",STYLE_OBJECT_ID="styleObjectId";class CardPlugin{constructor(t={}){this.options=t||{}}apply(t){let e=t.webpack.sources.ConcatSource;t.hooks.compilation.tap("CardPlugin",(t=>{t.hooks.processAssets.tap({name:"CardPlugin",stage:_Compilation.default.PROCESS_ASSETS_STAGE_ADDITIONAL},(()=>{let{pathSrc:n}=this.options;n=n.replace(/\\/g,"/");const s=t.moduleGraph;for(const i of t.chunks){const r=this.getEntryModule(t,i);if(!r)continue;const{rawRequest:o,request:a}=r;if(this.needExtractTemplateAndStyle(o)){const{templateFileName:i,cssFileName:l,bundleFilePath:u}=this.getCardBuildPath(a,n);let c={"#entry":{}};const d={};this.findOutgoingModules(s,r,c,c,d,n,u),c=this.isLiteCard(o)?(0,_postHandler.postHandleLiteCardRes)(c):(0,_postHandler.postHandleJSCardRes)(c);let p=[];recordKeys(c,p),p=[...new Set(p.sort())].filter((t=>"children"!==t&&"type"!==t&&"template"!==t&&"data"!==t)).concat("children"),p.unshift("type","template","data");const f=JSON.stringify(c,p);t.assets[i]=new e(f),t.assets[l]=new e(JSON.stringify(d))}}}))}))}findOutgoingModules(t,e,n,s,i,r,o){const a=t.getOutgoingConnectionsByModule(e);if(void 0!==a)for(const e of a.keys()){const{rawRequest:a,_source:l,request:u}=e;if(!this.isValidJsonModule(a))continue;const{_valueAsString:c,_valueAsBuffer:d}=l||{},p=c||(null==d?void 0:d.toString())||"";let f={},h=p;const g=p.trim();if(0===g.indexOf("module.exports")){h=g.substring(g.indexOf("{"));try{f=JSON.parse(h)}catch(t){console.error("invalid json:",t,"\n",h)}}const _=u.replace(/\\/g,"/"),m=(0,_utils.getLastLoaderPath)(_);let O="";if(m){const t=_constant.LOADER_INFO_LIST.find((t=>t.path===m));O=t&&t.type||""}const y=_constant.LOADER_INFO_LIST.map((t=>t.type)),x=!!n["#entry"];if(O===_constant.LOADER_PATH_UX.type){const{compName:o,relativeSrcPath:a}=this.getComponentName(_,r);if(!o)throw new Error(`Build failed, invalid component name, path: ${_}`);if(x?(n["#entry"].import||(n["#entry"].import={}),n["#entry"].import[o]=a):(n.import||(n.import={}),n.import[o]=a),s[a])continue;const l={};s[a]=l,this.findOutgoingModules(t,e,l,s,i,r,a)}else if(y.includes(O))if(O===_constant.LOADER_PATH_STYLE.type){i[(0,_sharedUtils.getStyleObjectId)(o)]=f}else if(O===_constant.LOADER_PATH_TEMPLATE.type){const t=(0,_sharedUtils.getStyleObjectId)(o);x?(n["#entry"][O]=f,n["#entry"][O].styleObjectId=t):(n[O]=f,n[O].styleObjectId=t)}else x?n["#entry"][O]=f:n[O]=f;else console.warn("Invalid module:",O,a)}}getComponentName(t,e){if(!t)return{};if((0,_utils.getLastLoaderPath)(t)===_constant.LOADER_PATH_UX.path){const n=t.split("!"),s=n[n.length-1].split("?"),i=s[0],r=this.getRelativeCompPath(e,i),o=s[1],a="uxType=comp",l="name=";let u=r;if(o&&o.includes(a)&&o.includes(l)){const t=o.split("&").find((t=>0===t.indexOf(l)));t&&(u=t.substring(l.length))}return{compName:u,relativeSrcPath:r}}return{}}getRelativeCompPath(t,e){let n=_path.default.relative(t,e);return n=n.replace(/\\/g,"/"),n&&n.endsWith(".ux")?n.substring(0,n.length-3):n}needExtractTemplateAndStyle(t){if(t&&t.lastIndexOf("?")>0){const e=t.lastIndexOf("?"),n=t.substring(e+1).split("&");return n&&n.indexOf("uxType=card")>=0&&(n.indexOf("lite=1")>=0||n.indexOf("newJSCard=1")>=0)}return!1}isLiteCard(t){if(t&&t.lastIndexOf("?")>0){const e=t.lastIndexOf("?"),n=t.substring(e+1).split("&");return n&&n.indexOf("lite=1")>=0&&n.indexOf("uxType=card")>=0}return!1}isValidJsonModule(t){const e=t.indexOf("?"),n=t.substring(e+1).split("&");return n&&!n.some((t=>t.indexOf("type=script")>=0))}getCardBuildPath(t,e){if(!t||!e)throw new Error(`Invalid request path or src path:\n${t}\n${e}`);const n=(t=t.replace(/\\/g,"/")).split("!");let s=n[n.length-1];s.indexOf("?")>0&&(s=s.substring(0,s.indexOf("?")));const i=_path.default.relative(e,s);if(!i.endsWith(".ux"))throw new Error(`Invalid relative path for ux:\n${i}`);const r=`${i.substring(0,i.length-3)}`;return{bundleFilePath:r.replace(/\\/g,"/"),templateFileName:`${r}.template.json`,cssFileName:`${r}.css.json`}}getEntryModule(t,e){const n=t.chunkGraph.getChunkModules(e);for(let s=0;s<n.length;s++){const i=n[s];if(t.chunkGraph.isEntryModuleInChunk(i,e))return i}return null}}function recordKeys(t,e){const n=function(t){if(!t||"object"!=typeof t)return;const s=Object.keys(t);e.push(...s),s.forEach((s=>n(t[s],e)))};n(t,e)}exports.CardPlugin=CardPlugin;
//# sourceMappingURL=card-plugin.js.map