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

Refactor of webview code #2489

Merged
merged 9 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"private": true,
"workspaces": [
"packages/*",
"packages/zowe-explorer/webviews/*"
"packages/zowe-explorer/src/webviews"
],
"engines": {
"vscode": "^1.53.2"
Expand Down
15 changes: 10 additions & 5 deletions packages/zowe-explorer-api/src/vscode/ui/WebView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ export class WebView {

/**
* Constructs a webview for use with bundled assets.
* The webview entrypoint must be located at <webview folder>/dist/assets/index.js.
* The webview entrypoint must be located at src/<webview folder>/dist/<webview-name>/index.js.
*
* @param title The title for the new webview
* @param dirName The directory name (relative to the "webviews" folder in the extension root) with a valid entrypoint (see above).
* @param webviewName The webview name, the same name given to the directory of your webview in the webviews/src directory.
* @param context The VSCode extension context
* @param onDidReceiveMessage Event callback: called when messages are received from the webview
*/
public constructor(title: string, dirName: string, context: ExtensionContext, onDidReceiveMessage?: (message: object) => void | Promise<void>) {
public constructor(
title: string,
webviewName: string,
context: ExtensionContext,
onDidReceiveMessage?: (message: object) => void | Promise<void>
) {
this.disposables = [];

// Generate random nonce for loading the bundled script
Expand All @@ -52,8 +57,8 @@ export class WebView {

// Build URIs for the webview directory and get the paths as VScode resources
this.uris.disk = {
build: Uri.file(joinPath(context.extensionPath, "webviews", dirName)),
script: Uri.file(joinPath(context.extensionPath, "webviews", dirName, "dist", "assets", "index.js")),
build: Uri.file(joinPath(context.extensionPath, "src", "webviews")),
script: Uri.file(joinPath(context.extensionPath, "src", "webviews", "dist", webviewName, `${webviewName}.js`)),
};

this.panel = window.createWebviewPanel("ZEAPIWebview", this.title, ViewColumn.Beside, {
Expand Down
2 changes: 1 addition & 1 deletion packages/zowe-explorer/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
out
results
scripts
webviews/**/dist
src/webviews/dist
4 changes: 3 additions & 1 deletion packages/zowe-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,9 @@
"updateStrings": "node ../../scripts/stringUpdateScript.js && prettier --write --loglevel warn ./i18n package.nls*",
"package": "vsce package --allow-star-activation --yarn && node ../../scripts/mv-pack.js vscode-extension-for-zowe vsix",
"license": "node ../../scripts/updateLicenses.js",
"watch": "gulp build && webpack --mode development --watch --info-verbosity verbose",
"watch": "concurrently -n \"yarn run watch:webviews\" \"yarn run watch:zowe-explorer\"",
"watch:zowe-explorer": "gulp build && webpack --mode development --watch --info-verbosity verbose",
"watch:webviews": "yarn workspace webviews dev",
"compile": "tsc -b",
"compile-web": "webpack --target webworker --entry ./src/web/extension.ts --output-path ./out/src/web",
"markdown": "markdownlint CHANGELOG.md README.md",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"name": "edit-attributes",
"name": "webviews",
"private": true,
"type": "module",
"version": "2.12.0-SNAPSHOT",
"main": "index.js",
"license": "EPL-2.0",
"scripts": {
"dev": "vite",
"dev": "vite build --watch --config ./vite.config.js",
"build": "tsc && vite build",
"preview": "vite preview",
"fresh-clone": "yarn clean && rimraf node_modules",
"clean": "gulp clean",
"package": "echo \"edit-attributes: nothing to package.\"",
"test": "echo \"edit-attributes: nothing to test\"",
"lint": "echo \"edit-attributes: nothing to lint.\"",
"lint:html": "echo \"edit-attributes: nothing to lint.\"",
"pretty": "echo \"edit-attributes: nothing to pretty.\""
"package": "echo \"webviews: nothing to package.\"",
"test": "echo \"webviews: nothing to test\"",
"lint": "echo \"webviews: nothing to lint.\"",
"lint:html": "echo \"webviews: nothing to lint.\"",
"pretty": "echo \"edit-attributes: nothing to pretty.\"",
"madge": "echo \"webviews: nothing to madge.\""
},
"dependencies": {
"@types/vscode-webview": "^1.57.1",
Expand All @@ -28,6 +29,7 @@
"@types/lodash.isequal": "^4.5.6",
"@vscode/codicons": "^0.0.33",
"typescript": "^4.5.3",
"vite": "^4.4.8"
"vite": "^4.4.9",
"vite-plugin-checker": "^0.6.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</head>
<body>
<div id="webviewRoot"></div>
<script type="module" src="/src/index.tsx"></script>
<script type="module" src="./index.tsx"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

export type PermissionSet = {
read: boolean;
write: boolean;
Expand Down
58 changes: 58 additions & 0 deletions packages/zowe-explorer/src/webviews/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/

import { defineConfig } from "vite";
import preact from "@preact/preset-vite";
import * as path from "path";
import { readdirSync } from "fs";
import checker from "vite-plugin-checker";

// https://vitejs.dev/config/

interface Webviews {
webview?: string;
webviewLocation?: string;
}

/**
* Get all available webviews under the source specified
* @param source
* @returns Object the object where the key is the webview and the value is the location of the webview
*/
const getAvailableWebviews = (source: string): Webviews => {
const webviews = readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

return webviews.reduce((o, key) => Object.assign(o, { [key]: path.resolve("src", key, "index.html") }), {});
};

export default defineConfig({
plugins: [
preact(),
checker({
typescript: true,
}),
],
root: path.resolve(__dirname, "src"),
build: {
emptyOutDir: true,
outDir: path.resolve(__dirname, "dist"),
rollupOptions: {
input: getAvailableWebviews(path.resolve(__dirname, "src")) as any,
output: {
entryFileNames: `[name]/[name].js`,
chunkFileNames: `[name]/[name].js`,
assetFileNames: `[name]/[name].[ext]`,
},
},
},
});
Loading
Loading