UNPKG

@plugin-web-update-notice/webpack

Version:
8 lines 2.42 kB
import{accessSync as P,constants as T,readFileSync as d,writeFileSync as v}from"fs";import{resolve as y}from"path";import{DIRECTORY_NAME as f,INJECT_SCRIPT_FILE_NAME as m,INJECT_STYLE_FILE_NAME as F,JSON_FILE_NAME as I,NOTIFICATION_ANCHOR_CLASS_NAME as _,generateJSONFileContent as L,generateJsFileContent as b,getFileHash as N,getVersion as C,get__Dirname as S}from"@plugin-web-update-notification/core";var g="WebUpdateNotificationPlugin";function A($,t,p,{cssFileHash:a,jsFileHash:e}){let{customNotificationHTML:s,hiddenDefaultNotification:c,injectFileBase:h="/"}=p,n=`<script>window.pluginWebUpdateNotice_version = '${t}';</script>`,o=s||c?"":`<link rel="stylesheet" href="${h}${f}/${F}.${a}.css">`,r=$;return r=r.replace("<head>",`<head> ${o} <script src="${h}${f}/${m}.${e}.js"></script> ${n}`),c||(r=r.replace("</body>",`<div class="${_}"></div></body>`)),r}function M($,t,p,{injectStyleContent:a,injectScriptContent:e}){let{customNotificationHTML:s,hiddenDefaultNotification:c}=p,h=`<script>window.pluginWebUpdateNotice_version = '${t}';</script>`,n;s||c?n="":n=`<style>${a}</style>`;let o=$,r=`<script>${e}</script>`;return o=o.replace("<head>",`<head> ${n} ${r} ${h}`),c||(o=o.replace("</body>",`<div class="${_}"></div></body>`)),o}var j=class{constructor(t){this.options=t||{}}apply(t){let p="",a="",e="",s="",{publicPath:c}=t.options.output;this.options.injectFileBase===void 0&&(this.options.injectFileBase=typeof c=="string"?c:"/");let{hiddenDefaultNotification:h,versionType:n,indexHtmlFilePath:o,customVersion:r,silence:H,microApp:O}=this.options,u="";u=n==="custom"?C(n,r):C(n),t.hooks.emit.tap(g,l=>{let i=L(u,H);l.assets[`${f}/${I}.json`]={source:()=>i,size:()=>i.length},h||(s=d(`${S()}/${F}.css`,"utf8"),a=N(s),l.assets[`${f}/${F}.${a}.css`]={source:()=>s,size:()=>s.length});let E=y(`${S()}/${m}.js`);e=b(d(E,"utf8").toString(),u,this.options),p=N(e),l.assets[`${f}/${m}.${p}.js`]={source:()=>e,size:()=>e.length}}),t.hooks.afterEmit.tap(g,()=>{let l=y(t.outputPath,o||"./index.html");try{P(l,T.F_OK);let i=d(l,"utf8");O?i=M(i,u,this.options,{injectStyleContent:s,injectScriptContent:e}):i=A(i,u,this.options,{jsFileHash:p,cssFileHash:a}),v(l,i)}catch(i){console.error(i),console.error(`${g} failed to inject the plugin into the HTML file. index.html\uFF08${l}\uFF09 not found.`)}})}};export{j as WebUpdateNotificationPlugin}; //# sourceMappingURL=index.js.map