Skip to content

Commit

Permalink
feat(vscode): add support for GitHub Codespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Dec 2, 2023
1 parent b1ee53e commit 8e2f5d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion integrations/vscode/src/preview-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export function updatePreviewPanel(
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="default-src http://localhost:*; script-src 'unsafe-inline'">
<meta http-equiv="Content-Security-Policy" content="default-src ${previewBaseUrl} ${
previewPanel.webview.cspSource
}; script-src 'unsafe-inline'">
<script>
const vscode = acquireVsCodeApi();
let iframe;
Expand Down
12 changes: 11 additions & 1 deletion integrations/vscode/src/preview-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { exclusivePromiseRunner } from "exclusive-promises";
import vscode from "vscode";
import type { PreviewJsState } from "./state.js";

const locking = exclusivePromiseRunner();
Expand All @@ -23,10 +24,19 @@ export async function ensurePreviewServerStarted(
}
const { url } = await state.client.startPreview({
rootDir,
...(vscode.env.uiKind === vscode.UIKind.Web
? {
// GitHub Codespaces
clientPort: 443,
}
: {}),
});
const remoteCompatibleUrl = await vscode.env
.asExternalUri(vscode.Uri.parse(url))
.then((uri) => uri.toString());
state.currentPreview = {
rootDir,
url,
url: remoteCompatibleUrl,
};
}
return state.currentPreview;
Expand Down
1 change: 1 addition & 0 deletions loader/src/worker-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type WorkerData = {
inMemorySnapshot: InMemoryFilesSnapshot;
frameworkPluginName: string;
port?: number;
clientPort?: number;
onServerStartModuleName?: string;
};

Expand Down
12 changes: 8 additions & 4 deletions loader/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function runWorker({
inMemorySnapshot,
frameworkPluginName,
port,
clientPort,
onServerStartModuleName,
}: WorkerData) {
const logger = createLogger(
Expand Down Expand Up @@ -117,6 +118,7 @@ async function runWorker({

const server = await workspace.startServer({
port,
clientPort,
});

sendMessageFromWorker({
Expand All @@ -142,16 +144,18 @@ const waitForInit = (message: ToWorkerMessage) => {
};
process.on("message", waitForInit);

const parentProcessId = parseInt(process.env["PREVIEWJS_PARENT_PROCESS_PID"] || "0");
const parentProcessId = parseInt(
process.env["PREVIEWJS_PARENT_PROCESS_PID"] || "0"
);
if (!parentProcessId) {
throw new Error("Missing environment variable: PREVIEWJS_PARENT_PROCESS_PID")
throw new Error("Missing environment variable: PREVIEWJS_PARENT_PROCESS_PID");
}
// Kill the worker if the parent process dies.
setInterval(() => {
try {
process.kill(parentProcessId, 0);
// Parent process is still alive, see https://stackoverflow.com/a/21296291.
} catch(e) {
} catch (e) {
process.exit(0);
}
}, 1000)
}, 1000);

0 comments on commit 8e2f5d5

Please sign in to comment.