Skip to content

Commit 7cc1aaf

Browse files
committed
Share types between the extension and the webviews
1 parent ed2d30b commit 7cc1aaf

File tree

7 files changed

+28
-38
lines changed

7 files changed

+28
-38
lines changed

.vscodeignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ pnpm-workspace.yaml
2929

3030
# Webview packages (exclude everything except built output in dist/webviews)
3131
packages/**
32-
tsconfig.webview.json
33-
vite.config.base.ts
3432
!dist/webviews/**
3533

3634
# Nix/flake files

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ code, use "Developer: Reload Webviews" or close/reopen the panel to see updates.
201201

202202
### Shared Package (`@coder/shared`)
203203

204+
The extension can import types from `@coder/shared` via path mapping in `tsconfig.json`.
205+
The mapping points to `extension.d.ts`, which re-exports only the types meant for
206+
extension use (excluding `@coder/shared/react` which is webview-only).
207+
204208
Type-safe message passing between extension and webview:
205209

206210
```typescript

packages/shared/extension.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Types exposed to the extension (react/ subpath is excluded).
2+
export type { WebviewMessage } from "./src/index";

src/webviews/tasks/TasksPanel.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as vscode from "vscode";
22

3-
import { getWebviewHtml, type WebviewMessage } from "../util";
3+
import { getWebviewHtml } from "../util";
4+
5+
import type { WebviewMessage } from "@coder/shared";
46

57
export class TasksPanel implements vscode.WebviewViewProvider {
68
public static readonly viewType = "coder.tasksPanel";

src/webviews/util.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,25 @@ import { randomBytes } from "node:crypto";
22
import * as vscode from "vscode";
33

44
/**
5-
* Message type for webview communication.
6-
* Matches @coder/shared WebviewMessage for consistency.
7-
*/
8-
export interface WebviewMessage<T = unknown> {
9-
type: string;
10-
data?: T;
11-
}
12-
13-
/**
14-
* Generate a cryptographically secure nonce for CSP
15-
*/
16-
export function getNonce(): string {
17-
return randomBytes(16).toString("base64");
18-
}
19-
20-
/**
21-
* Get the HTML content for a webview
5+
* Get the HTML content for a webview.
226
*/
237
export function getWebviewHtml(
248
webview: vscode.Webview,
259
extensionUri: vscode.Uri,
2610
webviewName: string,
2711
): string {
2812
const nonce = getNonce();
29-
13+
const baseUri = vscode.Uri.joinPath(
14+
extensionUri,
15+
"dist",
16+
"webviews",
17+
webviewName,
18+
);
3019
const scriptUri = webview.asWebviewUri(
31-
vscode.Uri.joinPath(
32-
extensionUri,
33-
"dist",
34-
"webviews",
35-
webviewName,
36-
"index.js",
37-
),
20+
vscode.Uri.joinPath(baseUri, "index.js"),
3821
);
39-
4022
const styleUri = webview.asWebviewUri(
41-
vscode.Uri.joinPath(
42-
extensionUri,
43-
"dist",
44-
"webviews",
45-
webviewName,
46-
"index.css",
47-
),
23+
vscode.Uri.joinPath(baseUri, "index.css"),
4824
);
4925

5026
return `<!DOCTYPE html>
@@ -61,3 +37,7 @@ export function getWebviewHtml(
6137
</body>
6238
</html>`;
6339
}
40+
41+
function getNonce(): string {
42+
return randomBytes(16).toString("base64");
43+
}

test/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"baseUrl": "..",
55
"paths": {
6-
"@/*": ["src/*"]
6+
"@/*": ["src/*"],
7+
"@coder/shared": ["packages/shared/extension.d.ts"]
78
}
89
},
910
"include": [".", "../src"]

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"sourceMap": true,
1010
"noFallthroughCasesInSwitch": true,
1111
"noImplicitOverride": true,
12-
"noImplicitReturns": true
12+
"noImplicitReturns": true,
13+
"paths": {
14+
"@coder/shared": ["./packages/shared/extension.d.ts"]
15+
}
1316
},
1417
"include": ["src"]
1518
}

0 commit comments

Comments
 (0)