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: Keyboard shortcut to reopen the browser without restarting the dev command #1211

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9098b4a
fix: Add / to PublicPath and browser.runtime.getURL
nishu-murmu Nov 14, 2024
055761c
fix(test): adding missing snapshot for getURL test.
nishu-murmu Nov 14, 2024
5c93301
Merge branch 'wxt-dev:main' into main
nishu-murmu Nov 14, 2024
3be59af
fix: Unused '@ts-expect-error' directive.
nishu-murmu Nov 14, 2024
d7be35d
simplify concat
aklinker1 Nov 14, 2024
7638770
Add back type check in demo
aklinker1 Nov 14, 2024
1b8af16
Merge branch 'wxt-dev:main' into main
nishu-murmu Nov 14, 2024
1086285
Merge branch 'wxt-dev:main' into main
nishu-murmu Nov 15, 2024
9e00536
Merge branch 'wxt-dev:main' into main
nishu-murmu Nov 20, 2024
884bc08
Merge branch 'main' of github.com:nishu-murmu/wxt
nishu-murmu Nov 21, 2024
fa46a0e
Merge branch 'wxt-dev:main' into main
nishu-murmu Nov 22, 2024
6e4437f
fix(#1082): ESLint config being generated when ESLint is not installed,
nishu-murmu Nov 22, 2024
f0bf972
Add E2E tests
aklinker1 Nov 22, 2024
9ed01d2
Merge branch 'main' of github.com:nishu-murmu/wxt
nishu-murmu Nov 22, 2024
bd4104a
Merge branch 'main' of github.com:nishu-murmu/wxt
nishu-murmu Nov 25, 2024
c9b9b32
Merge branch 'main' of github.com:nishu-murmu/wxt
nishu-murmu Nov 25, 2024
884b730
feat: keypress to reopen the browser like vite.
nishu-murmu Nov 25, 2024
42efc80
Merge branch 'main' of github.com:nishu-murmu/wxt
nishu-murmu Nov 26, 2024
f8f65da
feat: handling closing and opening of the listener on server start, s…
nishu-murmu Nov 26, 2024
88d15a2
Merge branch 'main' of github.com:nishu-murmu/wxt
nishu-murmu Nov 26, 2024
634c5c1
refact: separating code for keyboard-shortcuts feature.
nishu-murmu Nov 27, 2024
7f1beb7
feat(wip): excluding entrypoints when exclude option is set to partic…
nishu-murmu Nov 27, 2024
3d736a8
Revert "feat(wip): excluding entrypoints when exclude option is set t…
nishu-murmu Nov 28, 2024
d730572
fix: handle killing the server when in raw input mode.
nishu-murmu Nov 28, 2024
cda147b
Merge branch 'wxt-dev:main' into main
nishu-murmu Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/wxt/src/core/create-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getContentScriptJs,
mapWxtOptionsToRegisteredContentScript,
} from './utils/content-scripts';
import { createKeyBoardShortCuts } from './keyboard-shortcuts';

/**
* Creates a dev server and pre-builds all the files that need to exist before loading the extension.
Expand Down Expand Up @@ -108,9 +109,12 @@ async function createServerInternal(): Promise<WxtDevServer> {
// Listen for file changes and reload different parts of the extension accordingly
const reloadOnChange = createFileReloader(server);
server.watcher.on('all', reloadOnChange);
keyboardsShortCuts.start();
},

async stop() {
wasStopped = true;
keyboardsShortCuts.stop();
await runner.closeBrowser();
await builderServer.close();
await wxt.hooks.callHook('server:closed', wxt, server);
Expand All @@ -136,11 +140,14 @@ async function createServerInternal(): Promise<WxtDevServer> {
},
async restartBrowser() {
await runner.closeBrowser();
keyboardsShortCuts.stop();
await wxt.reloadConfig();
runner = await createExtensionRunner();
await runner.openBrowser();
keyboardsShortCuts.start();
},
};
const keyboardsShortCuts = createKeyBoardShortCuts(server);

const buildAndOpenBrowser = async () => {
// Build after starting the dev server so it can be used to transform HTML files
Expand Down Expand Up @@ -230,6 +237,7 @@ function createFileReloader(server: WxtDevServer) {
break;
case 'content-script-reload':
reloadContentScripts(changes.changedSteps, server);

const rebuiltNames = changes.rebuildGroups
.flat()
.map((entry) => entry.name);
Expand Down
44 changes: 44 additions & 0 deletions packages/wxt/src/core/keyboard-shortcuts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { WxtDevServer } from '../types';

export interface KeyboardShortcutWatcher {
start(): void;
stop(): void;
}

/**
* Function that creates a key board shortcut the extension.
*/
export function createKeyBoardShortCuts(
server: WxtDevServer,
): KeyboardShortcutWatcher {
let originalRawMode: boolean | undefined;
let isWatching = false;
const handleInput = (data: Buffer) => {
const char = data.toString();
if (char === 'o') {
server.restartBrowser();
}
};

return {
start() {
if (isWatching) return;
originalRawMode = process.stdin.isRaw;
process.stdin.setRawMode(true);
process.stdin.resume();

process.stdin.on('data', handleInput);
isWatching = true;
},

stop() {
if (!isWatching) return;
process.stdin.removeListener('data', handleInput);
if (originalRawMode !== undefined) {
process.stdin.setRawMode(originalRawMode);
}
process.stdin.pause();
isWatching = false;
},
};
}