@qwik.dev/core
Version:
An open source framework for building instant loading web apps at any scale, without the extra effort.
2 lines (1 loc) • 2.02 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("node:fs"),y=require("node:fs/promises"),m=require("node:path"),b=(r,...o)=>{console.warn("\x1B[33m%s\x1B[0m",`qwikInsight()[WARN]: ${r}`,...o)},k=r=>{console.log("\x1B[35m%s\x1B[0m",`qwikInsight(): ${r}`)};async function _(r){const{publicApiKey:o,baseUrl:I="https://insights.qwik.dev",outDir:w=""}=r;if(!o){console.warn("qwikInsights: publicApiKey is required, skipping...");return}let u=!1,g,n,e=null,l=null;const f=`${I}/api/v1/${o}`,p=`${f}/post/`;async function q(){return e||(h.existsSync(n)?(k("Reading Qwik Insight data from: "+n),e=JSON.parse(await y.readFile(n,"utf-8"))):null)}return{name:"vite-plugin-qwik-insights",enforce:"pre",apply:"build",async config(i){return g=m.resolve(i.root||".",w),n=m.join(g,"q-insights.json"),u=i.mode!=="ssr",{define:{"globalThis.__QI_KEY__":JSON.stringify(o),"globalThis.__QI_URL__":JSON.stringify(p)}}},configResolved:{order:"post",async handler(i){if(l=i.plugins.find(t=>t.name==="vite-plugin-qwik"),!l)throw new Error("Missing vite-plugin-qwik");const c=l.api.getOptions();if(u)try{const t={manual:{},prefetch:[]},a=await(await fetch(`${f}/bundles/strategy/`)).json();Object.assign(t,a),e=t,h.mkdirSync(g,{recursive:!0}),k("Fetched latest Qwik Insight data into: "+n),await y.writeFile(n,JSON.stringify(t))}catch(t){b(`Failed to fetch manifest from Insights DB at ${f}/bundles/strategy/`,t),await q()}else await q();e&&(c.entryStrategy.manual={...e.manual,...c.entryStrategy.manual},l.api.registerBundleGraphAdder(t=>{const d={};for(const a of e?.prefetch||[])if(a.symbols){let s=a.route;s.startsWith("/")&&(s=s.slice(1)),s.endsWith("/")||(s+="/"),d[s]={...t.bundles[s],imports:a.symbols}}return d}))}},closeBundle:async()=>{const i=m.resolve(w,"q-manifest.json");if(u&&h.existsSync(i)){const c=await y.readFile(i,"utf-8");try{await fetch(`${p}manifest`,{method:"post",body:c})}catch(t){b(`Failed to post manifest to Insights DB at ${p}manifest`,t)}}}}}exports.qwikInsights=_;