UNPKG

@metricinsights/pp-dev

Version:
3 lines (2 loc) 8.59 kB
import{n as e,v as t}from"./plugin-_rCgP0h-.js";import*as n from"fs";import{readFileSync as a,existsSync as s,readdirSync as i,writeFileSync as o,unlink as r}from"fs";import*as c from"path";import p,{resolve as l}from"path";import{fileURLToPath as m,pathToFileURL as u}from"url";import{normalizePath as f}from"vite";import{compile as d}from"ejs";const g=l("undefined"!=typeof __filename&&__filename||m(import.meta.url),"../../.."),h=l("undefined"!=typeof __filename&&__filename||m(import.meta.url),"../.."),j=s(l(g,"package.json"))?g:h,v=l(j,"dist/client/client.js"),{version:w,name:y}=JSON.parse(a(l(j,"package.json")).toString()),b=w,x=y,$=[".pp-watch.config.js",".pp-watch.config.ts",".pp-watch.config.json"],D=[".pp-dev.config.js",".pp-dev.config.cjs",".pp-dev.config.mjs",".pp-dev.config.ts",".pp-dev.config.cts",".pp-dev.config.mts",".pp-dev.config.json","pp-dev.config.js","pp-dev.config.cjs","pp-dev.config.mjs","pp-dev.config.ts","pp-dev.config.cts","pp-dev.config.mts","pp-dev.config.json"];const _=new class{cache=new Map;_maxSize;constructor(e=10){this._maxSize=e}get maxSize(){return this._maxSize}set maxSize(e){this._maxSize=e}get(e){if(this.cache.has(e)){const t=this.cache.get(e);return this.cache.delete(e),this.cache.set(e,t),t}}set(e,t){if(this.cache.has(e))this.cache.delete(e);else if(this.cache.size>=this._maxSize){const e=this.cache.keys().next().value;void 0!==e&&this.cache.delete(e)}this.cache.set(e,t)}clear(){this.cache.clear()}has(e){return this.cache.has(e)}}(5),S=new Map,P=c.dirname("undefined"!=typeof __filename&&__filename||m(new URL(".",import.meta.url))),k=`/${x}/client`,E=`/${k}`,I=new RegExp(`^\\/?${x}\\/client\\/(.*)$`);const N={templateCompilations:0,cacheHits:0,totalRequests:0};function O(e){let t="",a=!1,s=null;return{name:"pp-dev:client",apply:"serve",config:e=>(e.optimizeDeps?.exclude?.push(`${x}/client`),e),resolveId(e){if(I.test(e))return{id:f(c.join(j,"dist/client",e.replace(I,"$1")))}},transformIndexHtml:async(e,i)=>{N.totalRequests++;const o=i.server?.config.base||"";o!==t&&(t=o,a=!0,s=null),s&&!a||(_.has(t)?(s=_.get(t),N.cacheHits++):(s=function(e,t=!0){const a=e;if(t&&_.has(a))return _.get(a);const s=c.resolve(P,"client","index.html");let i;S.has(s)?i=S.get(s):(i=n.readFileSync(s,{encoding:"utf8"}),S.set(s,i));const o=E.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),r=d(i.replace(new RegExp(o,"g"),c.posix.join(e,k)),{openDelimiter:"{",closeDelimiter:"}",async:!0,cache:!0,filename:s,rmWhitespace:!0,compileDebug:!1});return t&&_.set(a,r),r}(t,true),N.templateCompilations++),a=!1);const r=function(e){return{css:c.posix.join(e,x,"client/client.css"),js:c.posix.join(e,x,"client/client.js")}}(t),p={html:e,tags:[{tag:"link",injectTo:"head",attrs:{rel:"stylesheet",href:r.css}}]},{backendBaseURL:l,templateLess:m,portalPageId:u,canSync:f=!0}=i.server?.config.clientInjectionPlugin||{},g={PACKAGE_NAME:x,VERSION:b,backendBaseURL:l,templateLess:m,portalPageId:u,canSync:f};return p.tags.push({tag:"div",injectTo:"body-prepend",children:await s(g)}),p.tags.push({tag:"script",injectTo:"body-prepend",attrs:{src:r.js,type:"module"}}),p},configureServer(e){const t=f(c.resolve(e.config.root,c.dirname(v)));e.config.server?.fs?.allow&&e.config.server.fs.allow.push(t),e.middlewares.use("/@api/__pp-dev-metrics",((e,t)=>{t.setHeader("Content-Type","application/json"),t.end(JSON.stringify(N,null,2))}))},closeBundle(){_.clear(),S.clear()}}}const R=(e,t)=>` * ${t}`;function z(e){return`/*!\n${"***** DO NOT EDIT THIS CODE! *****\n***** ------- *****".replace(/^(.*)$/gm,R)}\n */`}async function L(){try{const[e,t]=await Promise.all([import("next"),import("next/constants.js")]);return{next:e.default,constants:t}}catch(e){throw new Error(`Next.js is required but not available. Please install Next.js as a dependency:\nnpm install next@^15\n\nThis package requires Next.js >=15 <17 as a peer dependency.\n\nError: ${e}`)}}async function T(){try{return await import("next"),!0}catch(e){return!1}}async function C(){try{return(await import("next/package.json")).version}catch{return null}}const F=new Map,B=3e4;let H=null;function M(){const e=Date.now();if(H&&e-H.timestamp<6e4)return H.data;const t=process.cwd();try{const n=JSON.parse(a(p.resolve(t,"package.json"),{encoding:"utf-8",flag:"r"}));return H={data:n,timestamp:e},n}catch{const t={};return H={data:t,timestamp:e},t}}let U=null;async function V(e){const t=process.cwd(),n=`ts:${e}`,a=F.get(n);if(a&&Date.now()-a.timestamp<B)return a.data;let i=!1;if(/\.m[jt]s$/.test(e))i=!0;else if(/\.c[jt]s$/.test(e))i=!1;else{const e=M();i=!!e&&"module"===e.type}const c=await async function(){return U||(U=await import("esbuild")),U}(),l=await c.build({absWorkingDir:t,entryPoints:[e],outfile:"out.js",write:!1,target:["node14.18","node16"],platform:"node",bundle:!0,format:i?"esm":"cjs",mainFields:["main"],sourcemap:"inline",metafile:!0}),{text:m}=l.outputFiles[0],f=`${`pp-config.timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`}.js`,d=u(p.resolve(t,f)).toString();o(f,m);let g={};try{const t=(await import(d)).default;g=t?.default||t,F.set(n,{data:g,timestamp:Date.now(),filePath:e})}finally{s(f)&&r(f,(()=>{}))}return g}async function q(e){const t=`js:${e}`,n=F.get(t);if(n&&Date.now()-n.timestamp<B)return n.data;const a=(await import(u(e).toString())).default;return F.set(t,{data:a,timestamp:Date.now(),filePath:e}),a}async function A(e){const t=`json:${e}`,n=F.get(t);if(n&&Date.now()-n.timestamp<B)return n.data;const s=JSON.parse(a(e,{encoding:"utf-8"}));return F.set(t,{data:s,timestamp:Date.now(),filePath:e}),s}let J=null;async function W(e,t){for(const n of t)if(e.includes(n)){if(/\.[cm]?ts$/i.test(n))return await V(n);if(/\.[cm]?js$/i.test(n))return await q(p.resolve(".",n));if(n.endsWith(".json"))return await A(p.resolve(".",n))}return null}function G(){return M()}async function K(){const e=function(){const e=Date.now();if(J&&e-J.timestamp<1e4)return J.files;const t=/\.config\.(([cm]?ts)|([cm]?js)|(json))$/,n=process.cwd(),a=i(n,{withFileTypes:!0}).filter((e=>e.isFile()&&t.test(e.name))).map((e=>e.name));return J={files:a,timestamp:e},a}();let t={},n=!1;const a=await W(e,D);if(a&&(t=a,n=!0),e.length&&!n){const a=await W(e,$);a&&(t={backendBaseURL:a.baseURL,portalPageId:a.portalPageId},n=!0)}const s=M();return n||"object"!=typeof s["pp-dev"]||(t=s["pp-dev"]),t}const Z=Object.freeze({__proto__:null,clearConfigCache:function(){F.clear(),H=null,J=null},getConfig:K,getPkg:G});async function Q(){const n=G().name,a=await K(),s=e(Object.assign(a,{templateName:n})),i=[t(s),O()],{outDir:o,distZip:r,imageOptimizer:c,templateLess:p,integrateMiTopBar:l}=s;if(l&&i.push({name:"mi-topbar-plugin",transformIndexHtml:()=>[{tag:"script",injectTo:"body",attrs:{src:"/auth/info.js"}},{tag:"script",injectTo:"body",attrs:{src:"/js/main.js",defer:"defer"}},{tag:"link",injectTo:"head",attrs:{href:"/css/main.css",rel:"stylesheet"}},{tag:"div",injectTo:"body-prepend",attrs:{id:"mi-react-root"}}]}),c){const{ViteImageOptimizer:e}=await import("vite-plugin-image-optimizer");i.push(e("object"==typeof c?c:void 0))}if(r){const{default:e}=await import("vite-plugin-zip-pack");i.push({...e("object"==typeof r?r:{outFileName:`${n}.zip`}),enforce:"post"})}return{base:p?`/p/${n}`:`/pt/${n}`,server:{port:3e3},build:{minify:!1,assetsInlineLimit:4096,rollupOptions:{output:{banner:z}},outDir:o},css:{modules:{localsConvention:"dashes"},scss:{api:"modern"}},ppDevConfig:s,plugins:i}}function X(e){return e?.experimental?.ppDev||e?.ppDev||{}}function Y(e,t,n){return Object.assign({},e,t,n)}function ee(e,t){return async(n,a={})=>{try{const{constants:s}=await L(),{PHASE_DEVELOPMENT_SERVER:i}=s,o=await K(),r=G().name,c="function"==typeof e?await e(n,a):e,p=X(c),l=n===i,m=function(e,t,n,a){return n?t?`/p/${e}`:`/pl/${e}`:a?`/pt/${e}`:`/pl/${e}`}(r,o.templateLess??!1,l,o.v7Features??!1),u={basePath:m,trailingSlash:!!l||void 0};if(l){const e=function(e,t){const{appId:n,portalPageId:a,backendBaseURL:s,templateLess:i,v7Features:o,...r}=t,c=n||a,p={backendBaseURL:s,portalPageId:c,appId:c,templateLess:i,v7Features:o,...r};return{serverRuntimeConfig:{templateName:e,ppDevConfig:p},publicRuntimeConfig:{templateName:e,ppDevConfig:p},experimental:{ppDev:p}}}(r,Object.assign({},o,p,t));return Y(u,c,e)}return Y(u,c)}catch(t){console.error("Error in withPPDev:",t),console.warn("Falling back to original Next.js configuration");try{return"function"==typeof e?await e(n,a):e}catch(e){return console.error("Error in fallback configuration:",e),{}}}}}export{D as P,b as V,$ as a,X as b,C as c,Z as d,Q as g,T as i,L as s,ee as w}; //# sourceMappingURL=index-CmcaJY7U.js.map