Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log qrcode in preview server #112

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-laws-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-qrcode': minor
---

Log qrcode in preview server for Vite 4.3.0 and above
2 changes: 1 addition & 1 deletion packages/playground/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
"preview": "vite preview"
},
"devDependencies": {
"vite": "^4.3.9",
Expand Down
13 changes: 11 additions & 2 deletions packages/vite-plugin-qrcode/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin, ViteDevServer } from 'vite';
import type { Plugin, PreviewServerForHook, ViteDevServer } from 'vite';
import qr from 'qrcode-terminal';

export function qrcode(options: PluginOptions = {}): Plugin {
Expand All @@ -19,11 +19,20 @@ export function qrcode(options: PluginOptions = {}): Plugin {
// eslint-disable-next-line prefer-rest-params
return _listen.apply(this, arguments);
};
},
configurePreviewServer(server) {
// Preview server has no restarts, so we can hook directly
// The `resolvedUrls` only exist in Vite >=4.3.0, so add a guard to prevent unnecessary hook
if ('resolvedUrls' in server) {
server.httpServer?.on('listening', () => {
setTimeout(() => logQrcode(server, options), 0);
});
}
}
};
}

function logQrcode(server: ViteDevServer, options: PluginOptions) {
function logQrcode(server: ViteDevServer | PreviewServerForHook, options: PluginOptions) {
let networkUrls = server.resolvedUrls?.network;

if (!networkUrls) return;
Expand Down