🧑💻 hot module reloading for extension webview
This commit is contained in:
parent
d53d8416f4
commit
2139d85661
7 changed files with 61 additions and 123 deletions
5
.vscode/launch.json
vendored
5
.vscode/launch.json
vendored
|
|
@ -41,10 +41,7 @@
|
|||
"--extensionDevelopmentPath=${workspaceFolder}/extensions/vscode"
|
||||
],
|
||||
"outFiles": ["${workspaceFolder}/extensions/vscode/out/**/*.js"],
|
||||
"preLaunchTask": "vscode-extension:build",
|
||||
"env": {
|
||||
"CONTINUE_SERVER_URL": "http://localhost:8001"
|
||||
}
|
||||
"preLaunchTask": "vscode-extension:build"
|
||||
},
|
||||
// Has to be run after starting the server (separately or using the compound configuration)
|
||||
{
|
||||
|
|
|
|||
22
.vscode/tasks.json
vendored
22
.vscode/tasks.json
vendored
|
|
@ -10,7 +10,9 @@
|
|||
// To build the React app that is used in the extension
|
||||
"vscode-extension:continue-ui:build",
|
||||
// To bundle the code the same way we do for publishing
|
||||
"vscode-extension:esbuild"
|
||||
"vscode-extension:esbuild",
|
||||
// Start the React app that is used in the extension
|
||||
"gui:dev"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
|
|
@ -117,7 +119,23 @@
|
|||
},
|
||||
"args": ["run", "dev"],
|
||||
"isBackground": true,
|
||||
"problemMatcher": ["$tsc"]
|
||||
"problemMatcher": [
|
||||
{
|
||||
"pattern": [
|
||||
{
|
||||
"regexp": ".",
|
||||
"file": 1,
|
||||
"location": 2,
|
||||
"message": 3
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"activeOnStart": true,
|
||||
"beginsPattern": ".",
|
||||
"endsPattern": "."
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
3
extensions/vscode/.gitignore
vendored
3
extensions/vscode/.gitignore
vendored
|
|
@ -8,4 +8,5 @@ src/client
|
|||
|
||||
gui
|
||||
exe
|
||||
bin
|
||||
bin
|
||||
assets
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
import * as vscode from "vscode";
|
||||
|
||||
function _getRawServerUrl() {
|
||||
// Passed in from launch.json
|
||||
if (process.env.CONTINUE_SERVER_URL) {
|
||||
return process.env.CONTINUE_SERVER_URL;
|
||||
}
|
||||
|
||||
return (
|
||||
vscode.workspace.getConfiguration("continue").get<string>("serverUrl") ||
|
||||
"http://localhost:65432"
|
||||
);
|
||||
}
|
||||
|
||||
export function getContinueServerUrl() {
|
||||
let url = _getRawServerUrl();
|
||||
if (url.endsWith("/")) {
|
||||
url = url.slice(0, -1);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ import * as os from "os";
|
|||
import * as path from "path";
|
||||
import * as vscode from "vscode";
|
||||
|
||||
import { ideProtocolClient } from "./activation/activate";
|
||||
import { extensionContext, ideProtocolClient } from "./activation/activate";
|
||||
import { debugPanelWebview, getSidebarContent } from "./debugPanel";
|
||||
import { acceptDiffCommand, rejectDiffCommand } from "./diff/horizontal";
|
||||
import {
|
||||
|
|
@ -300,7 +300,13 @@ const commandsMap: { [command: string]: (...args: any) => any } = {
|
|||
"Continue",
|
||||
vscode.ViewColumn.One
|
||||
);
|
||||
panel.webview.html = getSidebarContent(panel, undefined, undefined, true);
|
||||
panel.webview.html = getSidebarContent(
|
||||
extensionContext,
|
||||
panel,
|
||||
undefined,
|
||||
undefined,
|
||||
true
|
||||
);
|
||||
},
|
||||
"continue.selectFilesAsContext": (
|
||||
firstUri: vscode.Uri,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { getDevDataFilePath } from "core/util/paths";
|
|||
import { writeFileSync } from "fs";
|
||||
import * as path from "path";
|
||||
import * as vscode from "vscode";
|
||||
import { extensionContext } from "./activation/activate";
|
||||
import { debugPanelWebview, getSidebarContent } from "./debugPanel";
|
||||
import { diffManager } from "./diff/horizontal";
|
||||
import { TabAutocompleteModel, configHandler } from "./loadConfig";
|
||||
|
|
@ -217,7 +218,12 @@ class IdeProtocolClient {
|
|||
"Continue",
|
||||
vscode.ViewColumn.One
|
||||
);
|
||||
panel.webview.html = getSidebarContent(panel, "/monaco", edits);
|
||||
panel.webview.html = getSidebarContent(
|
||||
extensionContext,
|
||||
panel,
|
||||
"/monaco",
|
||||
edits
|
||||
);
|
||||
}
|
||||
|
||||
openFile(filepath: string, range?: vscode.Range) {
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ import * as io from "socket.io-client";
|
|||
import { v4 as uuidv4 } from "uuid";
|
||||
import * as vscode from "vscode";
|
||||
import {
|
||||
extensionContext,
|
||||
ideProtocolClient,
|
||||
showTutorial,
|
||||
windowId,
|
||||
} from "./activation/activate";
|
||||
import { getContinueServerUrl } from "./bridge";
|
||||
import { streamEdit } from "./diff/verticalPerLine/manager";
|
||||
import historyManager from "./history";
|
||||
import { VsCodeIde } from "./ideProtocol";
|
||||
|
|
@ -52,6 +52,7 @@ export async function webviewRequest(
|
|||
const abortedMessageIds: Set<string> = new Set();
|
||||
|
||||
export function getSidebarContent(
|
||||
context: vscode.ExtensionContext | undefined,
|
||||
panel: vscode.WebviewPanel | vscode.WebviewView,
|
||||
page: string | undefined = undefined,
|
||||
edits: FileEdit[] | undefined = undefined,
|
||||
|
|
@ -71,8 +72,9 @@ export function getSidebarContent(
|
|||
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui"))
|
||||
.toString();
|
||||
|
||||
const isProduction = true; // context?.extensionMode === vscode.ExtensionMode.Development;
|
||||
if (isProduction) {
|
||||
const inDevelopmentMode =
|
||||
context?.extensionMode === vscode.ExtensionMode.Development;
|
||||
if (!inDevelopmentMode) {
|
||||
scriptUri = panel.webview
|
||||
.asWebviewUri(vscode.Uri.joinPath(extensionUri, "gui/assets/index.js"))
|
||||
.toString();
|
||||
|
|
@ -81,12 +83,15 @@ export function getSidebarContent(
|
|||
.toString();
|
||||
} else {
|
||||
scriptUri = "http://localhost:5173/src/main.tsx";
|
||||
styleMainUri = "http://localhost:5173/src/main.css";
|
||||
styleMainUri = "http://localhost:5173/src/index.css";
|
||||
}
|
||||
|
||||
panel.webview.options = {
|
||||
enableScripts: true,
|
||||
localResourceRoots: [vscode.Uri.joinPath(extensionUri, "gui")],
|
||||
localResourceRoots: [
|
||||
vscode.Uri.joinPath(extensionUri, "gui"),
|
||||
vscode.Uri.joinPath(extensionUri, "assets"),
|
||||
],
|
||||
enableCommandUris: true,
|
||||
portMapping: [
|
||||
{
|
||||
|
|
@ -98,53 +103,6 @@ export function getSidebarContent(
|
|||
|
||||
const nonce = getNonce();
|
||||
|
||||
async function connectWebsocket(url: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const onMessage = (message: any) => {
|
||||
panel.webview.postMessage({
|
||||
type: "websocketForwardingMessage",
|
||||
url,
|
||||
data: message,
|
||||
});
|
||||
};
|
||||
const onOpen = () => {
|
||||
panel.webview.postMessage({
|
||||
type: "websocketForwardingOpen",
|
||||
url,
|
||||
});
|
||||
resolve(null);
|
||||
};
|
||||
const onClose = () => {
|
||||
sockets[url] = undefined;
|
||||
panel.webview.postMessage({
|
||||
type: "websocketForwardingClose",
|
||||
url,
|
||||
});
|
||||
};
|
||||
const onError = (e: any) => {
|
||||
panel.webview.postMessage({
|
||||
type: "websocketForwardingError",
|
||||
url,
|
||||
error: e,
|
||||
});
|
||||
};
|
||||
try {
|
||||
const socket = io.io(
|
||||
`${getContinueServerUrl()}?window_id=${windowId}`,
|
||||
{
|
||||
path: "/gui/socket.io",
|
||||
transports: ["websocket", "polling", "flashsocket"],
|
||||
}
|
||||
);
|
||||
sockets[url] = socket;
|
||||
resolve(null);
|
||||
} catch (e) {
|
||||
console.log("Failed to connect to GUI websocket for forwarding", e);
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
panel.webview.onDidReceiveMessage(async (data) => {
|
||||
const ide = new VsCodeIde();
|
||||
const respond = (message: any) => {
|
||||
|
|
@ -160,44 +118,6 @@ export function getSidebarContent(
|
|||
abortedMessageIds.add(data.messageId);
|
||||
break;
|
||||
}
|
||||
case "websocketForwardingOpen": {
|
||||
let url = data.url;
|
||||
if (typeof sockets[url] === "undefined") {
|
||||
await connectWebsocket(url);
|
||||
} else {
|
||||
console.log(
|
||||
"Websocket connection requested by GUI already open at",
|
||||
url
|
||||
);
|
||||
panel.webview.postMessage({
|
||||
type: "websocketForwardingOpen",
|
||||
url,
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "websocketForwardingClose": {
|
||||
let url = data.url;
|
||||
let socket = sockets[url];
|
||||
if (typeof socket !== "undefined") {
|
||||
socket.close();
|
||||
sockets[url] = undefined;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "websocketForwardingMessage": {
|
||||
let url = data.url;
|
||||
let socket = sockets[url];
|
||||
if (typeof socket === "undefined") {
|
||||
await connectWebsocket(url);
|
||||
}
|
||||
socket = sockets[url];
|
||||
if (typeof socket === "undefined") {
|
||||
throw new Error("Failed to connect socket for forwarding");
|
||||
}
|
||||
socket.send(data.message);
|
||||
break;
|
||||
}
|
||||
case "showFile": {
|
||||
ideProtocolClient.openFile(data.filepath);
|
||||
break;
|
||||
|
|
@ -815,11 +735,22 @@ export function getSidebarContent(
|
|||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
${
|
||||
inDevelopmentMode &&
|
||||
`<script type="module">
|
||||
import RefreshRuntime from "http://localhost:5173/@react-refresh"
|
||||
RefreshRuntime.injectIntoGlobalHook(window)
|
||||
window.$RefreshReg$ = () => {}
|
||||
window.$RefreshSig$ = () => (type) => type
|
||||
window.__vite_plugin_react_preamble_installed__ = true
|
||||
</script>`
|
||||
}
|
||||
|
||||
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
|
||||
|
||||
<script>localStorage.setItem("ide", "vscode")</script>
|
||||
<script>window.windowId = "${windowId}"</script>
|
||||
<script>window.serverUrl = "${getContinueServerUrl()}"</script>
|
||||
<script>window.vscMachineId = "${getUniqueId()}"</script>
|
||||
<script>window.vscMediaUrl = "${vscMediaUrl}"</script>
|
||||
<script>window.ide = "vscode"</script>
|
||||
|
|
@ -855,6 +786,6 @@ export class ContinueGUIWebviewViewProvider
|
|||
_context: vscode.WebviewViewResolveContext,
|
||||
_token: vscode.CancellationToken
|
||||
): void | Thenable<void> {
|
||||
webviewView.webview.html = getSidebarContent(webviewView);
|
||||
webviewView.webview.html = getSidebarContent(extensionContext, webviewView);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue