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