├── .env
├── src
├── react-app-env.d.ts
├── index.css
├── index.tsx
├── App.test.tsx
├── App.css
├── App.tsx
└── logo.svg
├── public
├── favicon.ico
├── manifest.json
└── index.html
├── tslint.json
├── .gitignore
├── tsconfig.extension.json
├── scripts
└── build-non-split.js
├── tsconfig.json
├── .vscode
└── launch.json
├── LICENSE
├── package.json
├── README.md
└── ext-src
└── extension.ts
/.env:
--------------------------------------------------------------------------------
1 | PUBLIC_URL=./
--------------------------------------------------------------------------------
/src/react-app-env.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | font-family: sans-serif;
5 | }
6 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rebornix/vscode-webview-react/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/src/index.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import * as ReactDOM from 'react-dom';
3 | import App from './App';
4 | import './index.css';
5 |
6 | ReactDOM.render(
7 | ,
8 | document.getElementById('root') as HTMLElement
9 | );
10 |
--------------------------------------------------------------------------------
/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
3 | "linterOptions": {
4 | "exclude": [
5 | "config/**/*.js",
6 | "node_modules/**/*.ts",
7 | "ext-src/**/*.ts"
8 | ]
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/App.test.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import * as ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
7 | ReactDOM.render(, div);
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/ignore-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 |
6 | # testing
7 | /coverage
8 |
9 | # production
10 | /build
11 |
12 | # misc
13 | .DS_Store
14 | .env.local
15 | .env.development.local
16 | .env.test.local
17 | .env.production.local
18 |
19 | npm-debug.log*
20 | yarn-debug.log*
21 | yarn-error.log*
22 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | }
10 | ],
11 | "start_url": "./index.html",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/tsconfig.extension.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "target": "es6",
5 | "outDir": "build",
6 | "lib": [
7 | "es6",
8 | "dom"
9 | ],
10 | "sourceMap": true,
11 | "rootDir": ".",
12 | "strict": true
13 | },
14 | "include": [
15 | "ext-src"
16 | ],
17 | "exclude": [
18 | "node_modules",
19 | ".vscode-test"
20 | ]
21 | }
--------------------------------------------------------------------------------
/scripts/build-non-split.js:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | // Disables code splitting into chunks
4 | // See https://github.com/facebook/create-react-app/issues/5306#issuecomment-433425838
5 |
6 | const rewire = require("rewire");
7 | const defaults = rewire("react-scripts/scripts/build.js");
8 | let config = defaults.__get__("config");
9 |
10 | config.optimization.splitChunks = {
11 | cacheGroups: {
12 | default: false
13 | }
14 | };
15 |
16 | config.optimization.runtimeChunk = false;
17 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | animation: App-logo-spin infinite 20s linear;
7 | height: 80px;
8 | }
9 |
10 | .App-header {
11 | background-color: #222;
12 | height: 150px;
13 | padding: 20px;
14 | color: white;
15 | }
16 |
17 | .App-title {
18 | font-size: 1.5em;
19 | }
20 |
21 | .App-intro {
22 | font-size: large;
23 | }
24 |
25 | @keyframes App-logo-spin {
26 | from { transform: rotate(0deg); }
27 | to { transform: rotate(360deg); }
28 | }
29 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "es5",
4 | "lib": [
5 | "dom",
6 | "dom.iterable",
7 | "esnext"
8 | ],
9 | "allowJs": true,
10 | "skipLibCheck": true,
11 | "esModuleInterop": true,
12 | "allowSyntheticDefaultImports": true,
13 | "strict": true,
14 | "forceConsistentCasingInFileNames": true,
15 | "module": "esnext",
16 | "moduleResolution": "node",
17 | "resolveJsonModule": true,
18 | "isolatedModules": true,
19 | "noEmit": true,
20 | "jsx": "preserve"
21 | },
22 | "include": [
23 | "src"
24 | ]
25 | }
26 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | // A launch configuration that compiles the extension and then opens it inside a new window
2 | {
3 | "version": "0.1.0",
4 | "configurations": [
5 | {
6 | "name": "Launch Extension",
7 | "type": "extensionHost",
8 | "request": "launch",
9 | "runtimeExecutable": "${execPath}",
10 | "args": [
11 | "--extensionDevelopmentPath=${workspaceRoot}"
12 | ],
13 | "stopOnEntry": false,
14 | "sourceMaps": true,
15 | "outDir": "${workspaceRoot}/ext-src"
16 | }
17 | ]
18 | }
--------------------------------------------------------------------------------
/src/App.tsx:
--------------------------------------------------------------------------------
1 | import * as React from 'react';
2 | import './App.css';
3 |
4 | import logo from './logo.svg';
5 |
6 | class App extends React.Component {
7 | public render() {
8 | return (
9 |
10 |
11 |
12 | Welcome to React
13 |
14 |
15 | To get started, edit src/App.tsx and save to reload.
16 |
17 |
18 | );
19 | }
20 | }
21 |
22 | export default App;
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Peng Lyu
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vscode-webview-react",
3 | "version": "0.1.0",
4 | "engines": {
5 | "vscode": "^1.23.0"
6 | },
7 | "publisher": "rebornix",
8 | "activationEvents": [
9 | "onCommand:react-webview.start"
10 | ],
11 | "main": "./build/ext-src/extension.js",
12 | "contributes": {
13 | "commands": [
14 | {
15 | "command": "react-webview.start",
16 | "title": "Start React Webview",
17 | "category": "React"
18 | }
19 | ]
20 | },
21 | "dependencies": {
22 | "react": "^16.3.2",
23 | "react-dom": "^16.3.2",
24 | "terser": "^5.15.0",
25 | "yarn": "^1.22.19",
26 | "vscode": "^1.1.17"
27 | },
28 | "resolutions": {
29 | },
30 | "scripts": {
31 | "vscode:prepublish": "node ./scripts/build-non-split.js && tsc -p tsconfig.extension.json",
32 | "postinstall": "node ./node_modules/vscode/bin/install",
33 | "start": "react-scripts start",
34 | "build": "node ./scripts/build-non-split.js && tsc -p tsconfig.extension.json",
35 | "test": "react-scripts test --env=jsdom",
36 | "eject": "react-scripts eject"
37 | },
38 | "devDependencies": {
39 | "@types/jest": "^23.3.13",
40 | "@types/node": "^10.1.2",
41 | "@types/react": "^16.3.14",
42 | "@types/react-dom": "^16.0.5",
43 | "react-scripts": "^5.0.1",
44 | "rewire": "^6.0.0",
45 | "typescript": "^3.3.1"
46 | },
47 | "browserslist": [
48 | ">0.2%",
49 | "not dead",
50 | "not ie <= 11",
51 | "not op_mini all"
52 | ]
53 | }
54 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
11 |
12 |
13 |
22 | React App
23 |
24 |
25 |
28 |
29 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # VSCode Webview React
2 |
3 | This project was bootstrapped with
4 | * [Create React App](https://github.com/facebookincubator/create-react-app)
5 | * [Create React App TypeScript](https://github.com/wmonk/create-react-app-typescript)
6 | * [VSCode Extension Webview Sample](https://github.com/Microsoft/vscode-extension-samples/tree/master/webview-sample)
7 |
8 | [The webview API](https://code.visualstudio.com/docs/extensions/webview) allows extensions to create customizable views within VSCode. Single Page Application frameworks are perfect fit for this use case. However, to make modern JavaScript frameworks/toolchains appeal to VSCode webview API's [security best practices](https://code.visualstudio.com/docs/extensions/webview#_security) requires some knowledge of both the bundling framework you are using and how VSCode secures webview. This project aims to provide an out-of-box starter kit for Create React App and TypeScript in VSCode's webview.
9 |
10 | ## Development
11 |
12 | Run following commands in the terminal
13 |
14 | ```shell
15 | yarn install --ignore-engines
16 | yarn run build
17 | ```
18 | And then press F5, in Extension Development Host session, run `Start React Webview` command from command palette.
19 |
20 | ## Under the hood
21 |
22 | Things we did on top of Create React App TypeScript template
23 |
24 | * We inline `index.html` content in `ext-src/extension.ts` when creating the webview
25 | * We set strict security policy for accessing resources in the webview.
26 | * Only resources in `/build` can be accessed
27 | * Onlu resources whose scheme is `vscode-resource` can be accessed.
28 | * For all resources we are going to use in the webview, we change their schemes to `vscode-resource`
29 | * Since we only allow local resources, absolute path for styles/images (e.g., `/static/media/logo.svg`) will not work. We add a `.env` file which sets `PUBLIC_URL` to `./` and after bundling, resource urls will be relative.
30 | * We add baseUrl `` and then all relative paths work.
31 |
32 | ## Limitations
33 |
34 | Right now you can only run production bits (`yarn run build`) in the webview, how to make dev bits work (webpack dev server) is still unknown yet. Suggestions and PRs welcome !
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/ext-src/extension.ts:
--------------------------------------------------------------------------------
1 | import * as path from 'path';
2 | import * as vscode from 'vscode';
3 |
4 | export function activate(context: vscode.ExtensionContext) {
5 |
6 | context.subscriptions.push(vscode.commands.registerCommand('react-webview.start', () => {
7 | ReactPanel.createOrShow(context.extensionPath);
8 | }));
9 | }
10 |
11 | /**
12 | * Manages react webview panels
13 | */
14 | class ReactPanel {
15 | /**
16 | * Track the currently panel. Only allow a single panel to exist at a time.
17 | */
18 | public static currentPanel: ReactPanel | undefined;
19 |
20 | private static readonly viewType = 'react';
21 |
22 | private readonly _panel: vscode.WebviewPanel;
23 | private readonly _extensionPath: string;
24 | private _disposables: vscode.Disposable[] = [];
25 |
26 | public static createOrShow(extensionPath: string) {
27 | const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined;
28 |
29 | // If we already have a panel, show it.
30 | // Otherwise, create a new panel.
31 | if (ReactPanel.currentPanel) {
32 | ReactPanel.currentPanel._panel.reveal(column);
33 | } else {
34 | ReactPanel.currentPanel = new ReactPanel(extensionPath, column || vscode.ViewColumn.One);
35 | }
36 | }
37 |
38 | private constructor(extensionPath: string, column: vscode.ViewColumn) {
39 | this._extensionPath = extensionPath;
40 |
41 | // Create and show a new webview panel
42 | this._panel = vscode.window.createWebviewPanel(ReactPanel.viewType, "React", column, {
43 | // Enable javascript in the webview
44 | enableScripts: true,
45 |
46 | // And restric the webview to only loading content from our extension's `media` directory.
47 | localResourceRoots: [
48 | vscode.Uri.file(path.join(this._extensionPath, 'build'))
49 | ]
50 | });
51 |
52 | // Set the webview's initial html content
53 | this._panel.webview.html = this._getHtmlForWebview();
54 |
55 | // Listen for when the panel is disposed
56 | // This happens when the user closes the panel or when the panel is closed programatically
57 | this._panel.onDidDispose(() => this.dispose(), null, this._disposables);
58 |
59 | // Handle messages from the webview
60 | this._panel.webview.onDidReceiveMessage(message => {
61 | switch (message.command) {
62 | case 'alert':
63 | vscode.window.showErrorMessage(message.text);
64 | return;
65 | }
66 | }, null, this._disposables);
67 | }
68 |
69 | public doRefactor() {
70 | // Send a message to the webview webview.
71 | // You can send any JSON serializable data.
72 | this._panel.webview.postMessage({ command: 'refactor' });
73 | }
74 |
75 | public dispose() {
76 | ReactPanel.currentPanel = undefined;
77 |
78 | // Clean up our resources
79 | this._panel.dispose();
80 |
81 | while (this._disposables.length) {
82 | const x = this._disposables.pop();
83 | if (x) {
84 | x.dispose();
85 | }
86 | }
87 | }
88 |
89 | private _getHtmlForWebview() {
90 | const manifest = require(path.join(this._extensionPath, 'build', 'asset-manifest.json'));
91 | const mainScript = manifest['files']['main.js'];
92 | const mainStyle = manifest['files']['main.css'];
93 |
94 | const scriptPathOnDisk = vscode.Uri.file(path.join(this._extensionPath, 'build', mainScript));
95 | const scriptUri = scriptPathOnDisk.with({ scheme: 'vscode-resource' });
96 | const stylePathOnDisk = vscode.Uri.file(path.join(this._extensionPath, 'build', mainStyle));
97 | const styleUri = stylePathOnDisk.with({ scheme: 'vscode-resource' });
98 |
99 | // Use a nonce to whitelist which scripts can be run
100 | const nonce = getNonce();
101 |
102 | return `
103 |
104 |
105 |
106 |
107 |
108 | React App
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 | `;
121 | }
122 | }
123 |
124 | function getNonce() {
125 | let text = "";
126 | const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
127 | for (let i = 0; i < 32; i++) {
128 | text += possible.charAt(Math.floor(Math.random() * possible.length));
129 | }
130 | return text;
131 | }
--------------------------------------------------------------------------------