UNPKG

@metricinsights/pp-dev

Version:
3 lines (2 loc) 9.59 kB
"use strict";const e=require("./plugin-xN0M6WT7.js"),t=require("fs"),n=require("path"),i=require("url"),s=require("vite"),a=require("ejs");var r="undefined"!=typeof document?document.currentScript:null;function o(e){const t=Object.create(null);if(e)for(const n in e)if("default"!==n){const i=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,i.get?i:{enumerable:!0,get:()=>e[n]})}return t.default=e,Object.freeze(t)}const c=o(t),p=o(n),l=n.resolve("undefined"!=typeof __filename&&__filename||i.fileURLToPath("undefined"==typeof document?require("url").pathToFileURL(__filename).href:r&&"SCRIPT"===r.tagName.toUpperCase()&&r.src||new URL("index-DJdZe-Ks.js",document.baseURI).href),"../../.."),u=n.resolve("undefined"!=typeof __filename&&__filename||i.fileURLToPath("undefined"==typeof document?require("url").pathToFileURL(__filename).href:r&&"SCRIPT"===r.tagName.toUpperCase()&&r.src||new URL("index-DJdZe-Ks.js",document.baseURI).href),"../.."),f=t.existsSync(n.resolve(l,"package.json"))?l:u,m=n.resolve(f,"dist/client/client.js"),{version:d,name:g}=JSON.parse(t.readFileSync(n.resolve(f,"package.json")).toString()),h=d,v=g,j=[".pp-watch.config.js",".pp-watch.config.ts",".pp-watch.config.json"],y=[".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 w=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),x=new Map,b=p.dirname("undefined"!=typeof __filename&&__filename||i.fileURLToPath(new URL(".","undefined"==typeof document?require("url").pathToFileURL(__filename).href:r&&"SCRIPT"===r.tagName.toUpperCase()&&r.src||new URL("index-DJdZe-Ks.js",document.baseURI).href))),P=`/${v}/client`,S=`/${P}`,_=new RegExp(`^\\/?${v}\\/client\\/(.*)$`);const R={templateCompilations:0,cacheHits:0,totalRequests:0};function D(e){let t="",n=!1,i=null;return{name:"pp-dev:client",apply:"serve",config:e=>(e.optimizeDeps?.exclude?.push(`${v}/client`),e),resolveId(e){if(_.test(e))return{id:s.normalizePath(p.join(f,"dist/client",e.replace(_,"$1")))}},transformIndexHtml:async(e,s)=>{R.totalRequests++;const r=s.server?.config.base||"";r!==t&&(t=r,n=!0,i=null),i&&!n||(w.has(t)?(i=w.get(t),R.cacheHits++):(i=function(e,t=!0){const n=e;if(t&&w.has(n))return w.get(n);const i=p.resolve(b,"client","index.html");let s;x.has(i)?s=x.get(i):(s=c.readFileSync(i,{encoding:"utf8"}),x.set(i,s));const r=S.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),o=a.compile(s.replace(new RegExp(r,"g"),p.posix.join(e,P)),{openDelimiter:"{",closeDelimiter:"}",async:!0,cache:!0,filename:i,rmWhitespace:!0,compileDebug:!1});return t&&w.set(n,o),o}(t,true),R.templateCompilations++),n=!1);const o=function(e){return{css:p.posix.join(e,v,"client/client.css"),js:p.posix.join(e,v,"client/client.js")}}(t),l={html:e,tags:[{tag:"link",injectTo:"head",attrs:{rel:"stylesheet",href:o.css}}]},{backendBaseURL:u,templateLess:f,portalPageId:m,canSync:d=!0}=s.server?.config.clientInjectionPlugin||{},g={PACKAGE_NAME:v,VERSION:h,backendBaseURL:u,templateLess:f,portalPageId:m,canSync:d};return l.tags.push({tag:"div",injectTo:"body-prepend",children:await i(g)}),l.tags.push({tag:"script",injectTo:"body-prepend",attrs:{src:o.js,type:"module"}}),l},configureServer(e){const t=s.normalizePath(p.resolve(e.config.root,p.dirname(m)));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(R,null,2))}))},closeBundle(){w.clear(),x.clear()}}}const $=(e,t)=>` * ${t}`;function N(e){return`/*!\n${"***** DO NOT EDIT THIS CODE! *****\n***** ------- *****".replace(/^(.*)$/gm,$)}\n */`}async function C(){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}`)}}const I=new Map,T=3e4;let L=null;function O(){const e=Date.now();if(L&&e-L.timestamp<6e4)return L.data;const i=process.cwd();try{const s=JSON.parse(t.readFileSync(n.resolve(i,"package.json"),{encoding:"utf-8",flag:"r"}));return L={data:s,timestamp:e},s}catch{const t={};return L={data:t,timestamp:e},t}}let U=null;async function F(e){const s=process.cwd(),a=`ts:${e}`,r=I.get(a);if(r&&Date.now()-r.timestamp<T)return r.data;let o=!1;if(/\.m[jt]s$/.test(e))o=!0;else if(/\.c[jt]s$/.test(e))o=!1;else{const e=O();o=!!e&&"module"===e.type}const c=await async function(){return U||(U=await import("esbuild")),U}(),p=await c.build({absWorkingDir:s,entryPoints:[e],outfile:"out.js",write:!1,target:["node14.18","node16"],platform:"node",bundle:!0,format:o?"esm":"cjs",mainFields:["main"],sourcemap:"inline",metafile:!0}),{text:l}=p.outputFiles[0],u=`${`pp-config.timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`}.js`,f=i.pathToFileURL(n.resolve(s,u)).toString();t.writeFileSync(u,l);let m={};try{const t=(await import(f)).default;m=t?.default||t,I.set(a,{data:m,timestamp:Date.now(),filePath:e})}finally{t.existsSync(u)&&t.unlink(u,(()=>{}))}return m}async function E(e){const t=`js:${e}`,n=I.get(t);if(n&&Date.now()-n.timestamp<T)return n.data;const s=(await import(i.pathToFileURL(e).toString())).default;return I.set(t,{data:s,timestamp:Date.now(),filePath:e}),s}async function k(e){const n=`json:${e}`,i=I.get(n);if(i&&Date.now()-i.timestamp<T)return i.data;const s=JSON.parse(t.readFileSync(e,{encoding:"utf-8"}));return I.set(n,{data:s,timestamp:Date.now(),filePath:e}),s}let z=null;async function q(e,t){for(const i of t)if(e.includes(i)){if(/\.[cm]?ts$/i.test(i))return await F(i);if(/\.[cm]?js$/i.test(i))return await E(n.resolve(".",i));if(i.endsWith(".json"))return await k(n.resolve(".",i))}return null}function M(){return O()}async function V(){const e=function(){const e=Date.now();if(z&&e-z.timestamp<1e4)return z.files;const n=/\.config\.(([cm]?ts)|([cm]?js)|(json))$/,i=process.cwd(),s=t.readdirSync(i,{withFileTypes:!0}).filter((e=>e.isFile()&&n.test(e.name))).map((e=>e.name));return z={files:s,timestamp:e},s}();let n={},i=!1;const s=await q(e,y);if(s&&(n=s,i=!0),e.length&&!i){const t=await q(e,j);t&&(n={backendBaseURL:t.baseURL,portalPageId:t.portalPageId},i=!0)}const a=O();return i||"object"!=typeof a["pp-dev"]||(n=a["pp-dev"]),n}const A=Object.freeze({__proto__:null,clearConfigCache:function(){I.clear(),L=null,z=null},getConfig:V,getPkg:M});function H(e){return e?.experimental?.ppDev||e?.ppDev||{}}function B(e,t,n){return Object.assign({},e,t,n)}exports.PP_DEV_CONFIG_NAMES=y,exports.PP_WATCH_CONFIG_NAMES=j,exports.VERSION=h,exports.config=A,exports.getNextVersion=async function(){try{return(await import("next/package.json")).version}catch{return null}},exports.getPPDevConfigFromNextConfig=H,exports.getViteConfig=async function(){const t=M().name,n=await V(),i=e.normalizeVitePPDevConfig(Object.assign(n,{templateName:t})),s=[e.vitePPDev(i),D()],{outDir:a,distZip:r,imageOptimizer:o,templateLess:c,integrateMiTopBar:p}=i;if(p&&s.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"}}]}),o){const{ViteImageOptimizer:e}=await import("vite-plugin-image-optimizer");s.push(e("object"==typeof o?o:void 0))}if(r){const{default:e}=await import("vite-plugin-zip-pack");s.push({...e("object"==typeof r?r:{outFileName:`${t}.zip`}),enforce:"post"})}return{base:c?`/p/${t}`:`/pt/${t}`,server:{port:3e3},build:{minify:!1,assetsInlineLimit:4096,rollupOptions:{output:{banner:N}},outDir:a},css:{modules:{localsConvention:"dashes"},scss:{api:"modern"}},ppDevConfig:i,plugins:s}},exports.isNextAvailable=async function(){try{return await import("next"),!0}catch(e){return!1}},exports.safeNextImport=C,exports.withPPDev=function(e,t){return async(n,i={})=>{try{const{constants:s}=await C(),{PHASE_DEVELOPMENT_SERVER:a}=s,r=await V(),o=M().name,c="function"==typeof e?await e(n,i):e,p=H(c),l=n===a,u=function(e,t,n,i){return n?t?`/p/${e}`:`/pl/${e}`:i?`/pt/${e}`:`/pl/${e}`}(o,r.templateLess??!1,l,r.v7Features??!1),f={basePath:u,trailingSlash:!!l||void 0};if(l){const e=function(e,t){const{appId:n,portalPageId:i,backendBaseURL:s,templateLess:a,v7Features:r,...o}=t,c=n||i,p={backendBaseURL:s,portalPageId:c,appId:c,templateLess:a,v7Features:r,...o};return{serverRuntimeConfig:{templateName:e,ppDevConfig:p},publicRuntimeConfig:{templateName:e,ppDevConfig:p},experimental:{ppDev:p}}}(o,Object.assign({},r,p,t));return B(f,c,e)}return B(f,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,i):e}catch(e){return console.error("Error in fallback configuration:",e),{}}}}}; //# sourceMappingURL=index-DJdZe-Ks.js.map