v-vite-plugin
Version:
A Vite.js plugin for integrating the V programming language with modern frontend tooling.
3 lines (2 loc) • 6.08 kB
JavaScript
import{execSync as E}from"node:child_process";import l from"node:fs";import h from"node:path";import{fileURLToPath as R}from"node:url";import p from"picocolors";import{loadEnv as y}from"vite";import b from"vite-plugin-full-reload";var g=!1,_=["src/resources/**","src/templates/**"].filter(r=>l.existsSync(r.replace(/\*\*$/,"")));function S(r){let e=I(r);return[D(e),...O(e)]}function D(r){let e,n,t,a={"@":"/src/resources"};return{name:"v",enforce:"post",config:(i,{command:f,mode:d})=>{t=i;let s=!!t.build?.ssr,o=y(d,t.envDir||process.cwd(),""),c=o.ASSET_URL??"",u=f==="serve"?F(o):void 0;return P(f,o),{base:t.base??(f==="build"?$(r,c):""),publicDir:t.publicDir??!1,build:{manifest:t.build?.manifest??(s?!1:"manifest.json"),ssrManifest:t.build?.ssrManifest??(s?"ssr-manifest.json":!1),outDir:t.build?.outDir??U(r,s),rollupOptions:{input:t.build?.rollupOptions?.input??A(r,s)},assetsInlineLimit:t.build?.assetsInlineLimit??0},server:{origin:t.server?.origin??"http://__v_vite_placeholder__.test",cors:t.server?.cors??{origin:t.server?.origin??[/^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/,...o.APP_URL?[o.APP_URL]:[],/^https?:\/\/.*\.(?:test|local)(:\d+)?$/]},...u?{host:t.server?.host??u.host,hmr:t.server?.hmr===!1?!1:{...u.hmr,...t.server?.hmr===!0?{}:t.server?.hmr},https:t.server?.https??u.https}:void 0},resolve:{alias:Array.isArray(t.resolve?.alias)?[...t.resolve?.alias??[],...Object.keys(a).map(v=>({find:v,replacement:a[v]}))]:{...a,...t.resolve?.alias}},ssr:{noExternal:w(t)}}},configResolved(i){n=i},transform(i){if(n.command==="serve")return i=i.replace(/http:\/\/__v_vite_placeholder__\.test/g,e),r.transformOnServe(i,e)},configureServer(i){let f=n.envDir||process.cwd(),d=y(n.mode,f,"APP_URL").APP_URL??"undefined";if(i.httpServer?.once("listening",()=>{let s=i.httpServer?.address();(c=>typeof c=="object")(s)&&(e=t.server?.origin?t.server.origin:x(s,i.config),l.writeFileSync(r.hotFile,`${e}${i.config.base.replace(/\/$/,"")}`),setTimeout(()=>{i.config.logger.info(`
${p.red(`${p.bold("V")} ${V()}`)} ${p.dim("plugin")} ${p.bold(`v${C()}`)}`),i.config.logger.info(""),i.config.logger.info(` ${p.green("\u279C")} ${p.bold("APP_URL")}: ${p.cyan(d.replace(/:(\d+)/,(c,u)=>`:${p.bold(u)}`))}`)},100))}),!g){let s=()=>{l.existsSync(r.hotFile)&&l.rmSync(r.hotFile)};process.on("exit",s),process.on("SIGINT",()=>process.exit()),process.on("SIGTERM",()=>process.exit()),process.on("SIGHUP",()=>process.exit()),g=!0}return()=>i.middlewares.use((s,o,c)=>{s.url==="/index.html"&&(o.statusCode=404,o.end(l.readFileSync(h.join(m(),"index.html")).toString().replace(/{{ APP_URL }}/g,d))),c()})}}}function P(r,e){if(!(r==="build"||e.V_BYPASS_ENV_CHECK==="1")&&typeof e.CI<"u")throw Error("You should not run the Vite HMR server in CI environments. You should build your assets for production instead. To disable this ENV check you may set V_BYPASS_ENV_CHECK=1")}function V(){try{let r=E("v --version").toString().trim();return r=r.replace(/(?:v|V\s+)?(\d+(?:\.\d+(?:\.\d+)?)?).*/,"$1"),r}catch{return""}}function C(){try{return JSON.parse(l.readFileSync(h.join(m(),"../package.json")).toString())?.version}catch{return""}}function I(r){if(typeof r>"u")throw new Error("v-vite-plugin: missing configuration.");if((typeof r=="string"||Array.isArray(r))&&(r={input:r,ssr:r}),typeof r.input>"u")throw new Error('v-vite-plugin: missing configuration for "input".');if(typeof r.publicDirectory=="string"&&(r.publicDirectory=r.publicDirectory.trim().replace(/^\/+/,""),r.publicDirectory===""))throw new Error("v-vite-plugin: publicDirectory must be a subdirectory. E.g. 'public'.");if(typeof r.buildDirectory=="string"&&(r.buildDirectory=r.buildDirectory.trim().replace(/^\/+/,"").replace(/\/+$/,""),r.buildDirectory===""))throw new Error("v-vite-plugin: buildDirectory must be a subdirectory. E.g. 'build'.");return typeof r.ssrOutputDirectory=="string"&&(r.ssrOutputDirectory=r.ssrOutputDirectory.trim().replace(/^\/+/,"").replace(/\/+$/,"")),r.refresh===!0&&(r.refresh=[{paths:_}]),{input:r.input,publicDirectory:r.publicDirectory??"public",buildDirectory:r.buildDirectory??"build",ssr:r.ssr??r.input,ssrOutputDirectory:r.ssrOutputDirectory??"ssr",refresh:r.refresh??!1,hotFile:r.hotFile??h.join(r.publicDirectory??"public","hot"),transformOnServe:r.transformOnServe??(e=>e)}}function $(r,e){return`${e}${e.endsWith("/")?"":"/"}${r.buildDirectory}/`}function A(r,e){return e?r.ssr:r.input}function U(r,e){return e?r.ssrOutputDirectory:h.join(r.publicDirectory,r.buildDirectory)}function O({refresh:r}){return typeof r=="boolean"?[]:(typeof r=="string"&&(r=[{paths:[r]}]),Array.isArray(r)||(r=[r]),r.some(e=>typeof e=="string")&&(r=[{paths:r}]),r.flatMap(e=>{let n=b(e.paths,e.config);return n.__v_plugin_config=e,n}))}function x(r,e){let n=typeof e.server.hmr=="object"?e.server.hmr.protocol:null,t=n?n==="wss"?"https":"http":null,a=e.server.https?"https":"http",i=t??a,f=typeof e.server.hmr=="object"?e.server.hmr.host:null,d=typeof e.server.host=="string"?e.server.host:null,s=T(r)?`[${r.address}]`:r.address,o=f??d??s,u=(typeof e.server.hmr=="object"?e.server.hmr.clientPort:null)??r.port;return`${i}://${o}:${u}`}function T(r){return r.family==="IPv6"||r.family===6}function w(r){let e=r.ssr?.noExternal,n=["v-vite-plugin"];return e===!0?!0:typeof e>"u"?n:[...Array.isArray(e)?e:[e],...n]}function F(r){if(!(r.VITE_DEV_SERVER_KEY||r.VITE_DEV_SERVER_CERT))return;if(!(l.existsSync(r.VITE_DEV_SERVER_KEY)&&l.existsSync(r.VITE_DEV_SERVER_CERT)))throw Error(`Unable to find the certificate files specified in your environment. Ensure you have correctly configured VITE_DEV_SERVER_KEY: [${r.VITE_DEV_SERVER_KEY}] and VITE_DEV_SERVER_CERT: [${r.VITE_DEV_SERVER_CERT}].`);let e=L(r);if(!e)throw Error(`Unable to determine the host from the environment's APP_URL: [${r.APP_URL}].`);return{hmr:{host:e},host:e,https:{key:l.readFileSync(r.VITE_DEV_SERVER_KEY),cert:l.readFileSync(r.VITE_DEV_SERVER_CERT)}}}function L(r){try{return new URL(r.APP_URL).host}catch{return}}function m(){return R(new URL(".",import.meta.url))}export{S as default,_ as refreshPaths};