vite-plugin-url-copy
Advanced tools
Comparing version 1.0.2 to 1.1.0
import { Plugin } from 'vite'; | ||
interface Options { | ||
/** | ||
* Disabled copy. | ||
* @default false | ||
*/ | ||
type DeepRequired<T> = { | ||
[P in keyof T]-?: DeepRequired<T[P]>; | ||
}; | ||
interface CopyOptions { | ||
disabled?: boolean; | ||
@@ -14,13 +13,23 @@ /** | ||
/** | ||
* Custom any string to copy. | ||
* Custom any string. | ||
*/ | ||
custom?: string; | ||
custom?: string | undefined; | ||
} | ||
interface QRCodeOptions { | ||
disabled?: boolean; | ||
/** | ||
* Log config info | ||
* Custom any string. | ||
*/ | ||
custom?: string; | ||
} | ||
interface Options { | ||
copy?: CopyOptions; | ||
qrcode?: QRCodeOptions; | ||
disabled?: boolean; | ||
debug?: boolean; | ||
} | ||
type ResolveOptions = DeepRequired<Options>; | ||
declare function VitePluginServerUrl(options?: Options): Plugin; | ||
declare function VitePluginServerUrl(rawOptions?: Options): Plugin; | ||
export { type Options, VitePluginServerUrl as default }; | ||
export { type Options, type ResolveOptions, VitePluginServerUrl as default }; |
@@ -1,6 +0,8 @@ | ||
import{consola as f}from"consola";import p from"clipboardy";import{colorize as n}from"consola/utils";const l=console.log,w=(r=0)=>new Promise(e=>{setTimeout(()=>{e()},r)}),v=async r=>{try{return await r()}catch(e){return Promise.reject(e)}};function $(r={}){return{name:"server-url-copy",configureServer(e){r.disabled||e.httpServer?.once("listening",()=>{g(e,r)})},configurePreviewServer(e){r.disabled||e.httpServer.once("listening",()=>{g(e,r)})}}}function g(r,e){const{custom:i="",debug:m=!1,mode:c="local"}=e;let t="";c!=="local"&&c!=="network"?t="local":t=c,v(async()=>{let o="",u=!1;typeof i=="string"&&i&&(u=!0,o=i),o||(await w(),o=(await s())[t][0]);const d=u?"cutsom":t;if(m&&(l(n("bgYellow",` | ||
Url-copy_debug: `),n("yellow",` ${d} - ${o} | ||
`)),l(n("yellow",` ${JSON.stringify(e)} | ||
`))),!o){f.warn(`Url-copy: ${t} mode URL is undefined, Please check your vite configuration.`);return}await p.write(o),l(n("green",` | ||
\u2714 `),n("bgGreen",` \xB7${d}\xB7 already copied to clipboard: ${o} `),` | ||
`)}).catch(o=>{f.warn(`Url-copy: ${o}`)});const y=10;let a=0;async function s(){const o=r.resolvedUrls;if(a>=y)throw new Error("timeout");return o||(a++,await w(),s())}}export{$ as default}; | ||
import w,{consola as $}from"consola";import S from"clipboardy";import{colorize as d}from"consola/utils";import{renderUnicodeCompact as x}from"uqr";const l=console.log;function P(t){return Object.prototype.toString.call(t)==="[object Object]"}function h(t=0){return new Promise(e=>{setTimeout(()=>{e()},t)})}async function f(t){try{return await t()}catch(e){return Promise.reject(e)}}let v={};function k(t){let e=!1;function o(n,i){for(const r of Object.keys(n))if(P(n[r])){const c=o(n[r],i[r]);if(c)return c}else if(n[r]!==i[r])return e=!0,e;return e}return o(t,v),e&&(v=t),e}const U=10;let y=0;async function b(t){const e=t.resolvedUrls;if(y>=U)throw new Error("timeout");return e?(y=0,e):(y++,await h(),b(t))}const p=[];function C(...t){p.push(...t)}const B=d("blue"," vite-plugin-url-copy_debug ");function O(t){if(!t)return;const e=p.length;let o="";for(const[n,i]of p.entries())o+=d("blue",i),n!==e-1&&(o+=` | ||
`);l(` | ||
`),w.box({title:B,message:o,style:{padding:1,borderColor:"gray",borderStyle:"rounded"}}),p.length=0}const j=(t,e)=>{const{custom:o,mode:n,disabled:i}=e.copy,r=e.debug;if(!i)return f(async()=>{let c="",u=!1;typeof o=="string"&&o&&(u=!0,c=o),c||(c=t[n][0]);const s=u?"cutsom":n;if(r&&C(`copy: ${s} - ${c}`,`copy: ${JSON.stringify(e.copy)}`),!c){await h(),$.warn(`url-copy: ${n} mode URL is undefined, Please check your vite configuration.`);return}return await S.write(c),l(d("green",` | ||
\u2714 `),d("bgGreen",` \xB7${s}\xB7 already copied to clipboard - ${c} `)),c}).catch(c=>{$.warn(`url-copy: ${c}`)})},m="network",q=(t,e)=>{const{custom:o,disabled:n}=e.qrcode,i=e.debug;if(n)return;let r="",c=!1;if(typeof o=="string"&&o&&(c=!0,r=o),r||(r=t[m][0]),i&&C(`qrcode: ${c?"cutsom":m} - ${r}`,`qrcode: ${JSON.stringify(e.copy)}`),!r){w.warn("url-copy: QR-Code uses a network URL, Please check your vite configuration.");return}const u=Q(r);let s="";try{s=[...u].map((a,g)=>(g||(a=` ${a}`),a===` | ||
`&&(a=`${a} `),a)).join("")}catch{s=u}l(d("green",` | ||
\u2714 `),d("bgGreen",` \xB7QRCode\xB7 of the ${m} - ${r} `),` | ||
`),l(d("green",s))};function Q(t){return x(t,{ecc:"L",border:1})}const L={copy:{disabled:!1,mode:"local",custom:""},qrcode:{disabled:!0,custom:""},disabled:!1,debug:!1};function N(t){const{disabled:e,copy:o={},qrcode:n={},debug:i}=t,{disabled:r,copy:c,qrcode:u,debug:s}=L;return{copy:{...c,...o},qrcode:{...u,...n},disabled:e??r,debug:i??s}}function E(t={}){const e=N(t);return{name:"server-url-copy",configureServer(o){if(e.disabled)return;const n=o.listen,i=o;i.listen=function(...r){const[c,u]=r;return f(async()=>{const s=await n.apply(this,r),a=await b(s);return f(async()=>{const g=s.config.server.port,R=k({port:g,...e});u&&!R||(await j(a,e),q(a,e),O(e.debug))}),s})}},configurePreviewServer(o){e.disabled||o.httpServer.once("listening",()=>{f(async()=>{const n=await b(o);await j(n,e),q(n,e),O(e.debug)})})}}}export{E as default}; |
{ | ||
"name": "vite-plugin-url-copy", | ||
"type": "module", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"packageManager": "pnpm@8.12.1", | ||
@@ -19,2 +19,5 @@ "description": "Auto copy the vite server url when dev or preview", | ||
"vite-plugin", | ||
"vite plugin", | ||
"QR Code", | ||
"QRCode", | ||
"copy server url" | ||
@@ -54,11 +57,11 @@ ], | ||
"devDependencies": { | ||
"@antfu/eslint-config": "^2.4.5", | ||
"@types/node": "^20.10.4", | ||
"@antfu/eslint-config": "^2.4.6", | ||
"@types/node": "^20.10.5", | ||
"bumpp": "^9.2.1", | ||
"eslint": "^8.55.0", | ||
"eslint": "^8.56.0", | ||
"tsup": "^8.0.1", | ||
"typescript": "^5.3.3", | ||
"unbuild": "^2.0.0", | ||
"vite": "^5.0.9" | ||
"vite": "^5.0.10" | ||
} | ||
} |
@@ -6,7 +6,9 @@ <h1 align="center"> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/vite-plugin-url-copy"><img src="https://img.shields.io/npm/v/vite-plugin-url-copy.svg?style=flat&colorA=18181B&colorB=28CF8D" alt="Version"></a> | ||
<a href="https://www.npmjs.com/package/vite-plugin-url-copy" target="_blank"><img src="https://img.shields.io/npm/v/vite-plugin-url-copy.svg?style=flat&colorA=18181B&colorB=28CF8D" alt="Version"></a> | ||
</p> | ||
<p align="center">⚡️ Auto copy the vite server url when dev or preview</p> | ||
<p align="center">⚡️ Auto copy the vite server URL & generate a QR code when dev or preview.</p> | ||
<p align="center"><img src="example/public/preview.png" width="100%"></p> | ||
> [!IMPORTANT] | ||
@@ -39,2 +41,13 @@ > Recommended minimum vite version is v4. | ||
#### QR code is disabled by default and needs to be explicitly enabled | ||
```ts | ||
ServerUrlCopy({ | ||
// QR code using network URL | ||
qrcode: { | ||
disabled: false | ||
} | ||
}) | ||
``` | ||
## Configuration | ||
@@ -44,10 +57,20 @@ | ||
ServerUrlCopy({ | ||
// 'local' | 'network', default 'local' | ||
mode: 'local', | ||
// Custom any string to copy when the server start, It will overwrite the server URL. | ||
custom: '', | ||
// Disabled copy, default false | ||
copy: { | ||
// 'local' | 'network', default 'local' | ||
mode: 'local', | ||
// Custom any string to copy when the server start, It will overwrite the server URL | ||
custom: '', | ||
// Disable Copy, default false | ||
disabled: false, | ||
}, | ||
qrcode: { | ||
// Custom any string for the generate network QR code, It will overwrite the server URL | ||
custom: '', | ||
// Disable QRCode, default true | ||
disabled: true, | ||
}, | ||
// Disable plugin, default false | ||
disabled: false, | ||
// Log config info, default false | ||
debug: false, | ||
// Log config info, default false | ||
disabled: false, | ||
}) | ||
@@ -58,3 +81,3 @@ ``` | ||
```ts | ||
```js | ||
// package.json | ||
@@ -61,0 +84,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12092
63
98