@tomjs/vite-plugin-vscode 4.2.1 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/webview.d.ts CHANGED
@@ -1,13 +1,14 @@
1
+ //#region src/webview/webview.d.ts
1
2
  interface WebviewHtmlOptions {
2
- /**
3
- * local server url
4
- */
5
- serverUrl: string;
3
+ /**
4
+ * local server url
5
+ */
6
+ serverUrl: string;
6
7
  }
7
8
  /**
8
9
  *
9
10
  * @param options serverUrl string or object options
10
11
  */
11
12
  declare function getWebviewHtml(options: WebviewHtmlOptions): string;
12
-
13
- export { type WebviewHtmlOptions, getWebviewHtml as default, getWebviewHtml };
13
+ //#endregion
14
+ export { WebviewHtmlOptions, getWebviewHtml as default, getWebviewHtml };
package/dist/webview.js CHANGED
@@ -1,203 +1,18 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/webview/template.html
2
- var template_default = `<!doctype html>
3
- <html lang="en">
4
- <head>
5
- <meta charset="UTF-8" />
6
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
7
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
8
- <style>
9
- html,
10
- body {
11
- width: 100%;
12
- height: 100%;
13
- margin: 0;
14
- padding: 0;
15
- overflow: hidden;
16
- }
17
-
18
- #webview-patch-iframe {
19
- width: 100%;
20
- height: 100%;
21
- border: none;
22
- }
23
-
24
- .outer {
25
- width: 100%;
26
- height: 100%;
27
- overflow: hidden;
28
- }
29
- </style>
30
-
31
- <script type="module" id="webview-patch">
32
- const TAG = '[@tomjs:vscode:extension] ';
33
-
34
- function onDomReady(callback, doc) {
35
- const _doc = doc || document;
36
- if (_doc.readyState === 'interactive' || _doc.readyState === 'complete') {
37
- callback();
38
- } else {
39
- _doc.addEventListener('DOMContentLoaded', callback);
40
- }
41
- }
42
-
43
- let vsCodeApi;
44
-
45
- function getApi() {
46
- if (vsCodeApi) return vsCodeApi;
47
- return (vsCodeApi = acquireVsCodeApi());
48
- }
49
-
50
- function sendInitData(iframe) {
51
- console.log(TAG + 'init data');
52
- const dataset = {};
53
- Object.keys(document.body.dataset).forEach((key) => {
54
- dataset[key] = document.body.dataset[key];
55
- });
56
-
57
- iframe.contentWindow.postMessage(
58
- {
59
- type: '[vscode:extension]:init',
60
- data: {
61
- state: getApi().getState(),
62
- style: document.getElementById('_defaultStyles').innerHTML,
63
- root: {
64
- cssText: document.documentElement.style.cssText,
65
- },
66
- body: {
67
- dataset: dataset,
68
- className: document.body.className,
69
- role: document.body.getAttribute('role'),
70
- },
71
- },
72
- },
73
- '*',
74
- );
75
- }
76
-
77
- function observeAttributeChanges(element, attributeName, callback) {
78
- const observer = new MutationObserver(function (mutationsList) {
79
- for (let mutation of mutationsList) {
80
- if (mutation.type === 'attributes' && mutation.attributeName === attributeName) {
81
- callback(mutation.target.getAttribute(attributeName));
82
- }
83
- }
84
- });
85
- observer.observe(element, { attributes: true });
86
- return observer;
87
- }
88
-
89
- // message handler
90
- let iframeLoaded = false;
91
- const cacheMessages = [];
92
-
93
- function handleMessage(e) {
94
- const iframe = document.getElementById('webview-patch-iframe');
95
- if (!iframeLoaded || !iframe) {
96
- return;
97
- }
98
- if (e.origin.startsWith('vscode-webview://')) {
99
- iframe.contentWindow.postMessage(e.data, '*');
100
- } else if ('{{serverUrl}}'.startsWith(e.origin)) {
101
- const { type, data } = e.data;
102
- console.log(TAG + ' received:', e.data);
103
- if (type === '[vscode:client]:postMessage') {
104
- getApi().postMessage(data);
105
- } else if (type === '[vscode:client]:commands') {
106
- if (data === 'F1') {
107
- window.dispatchEvent(
108
- new KeyboardEvent('keydown', {
109
- key: 'F1',
110
- keyCode: 112,
111
- code: 'F1',
112
- }),
113
- );
114
- }
115
- }
116
- }
117
- }
118
-
119
- window.addEventListener('message', function (event) {
120
- if (event.origin.startsWith('vscode-webview://')) {
121
- cacheMessages.push(event);
122
- return;
123
- }
124
- handleMessage(event);
125
- });
126
-
127
- let isCacheWorking = false;
128
- setInterval(() => {
129
- if (isCacheWorking) {
130
- return;
131
- }
132
-
133
- isCacheWorking = true;
134
- if (iframeLoaded) {
135
- let event = cacheMessages.shift();
136
- while (event) {
137
- handleMessage(event);
138
- event = cacheMessages.shift();
139
- }
140
- }
141
- isCacheWorking = false;
142
- }, 50);
143
-
144
- onDomReady(function () {
145
- /** @type {HTMLIFrameElement} */
146
- const iframe = document.getElementById('webview-patch-iframe');
147
- observeAttributeChanges(document.body, 'class', function (className) {
148
- sendInitData(iframe);
149
- });
150
-
151
- onDomReady(function () {
152
- iframeLoaded = true;
153
- sendInitData(iframe);
154
- }, iframe.contentDocument);
155
-
156
- iframe.addEventListener('load', function (e) {
157
- iframeLoaded = true;
158
-
159
- let interval = setInterval(() => {
160
- try {
161
- if (document.getElementById('_defaultStyles')) {
162
- sendInitData(iframe);
163
- // addListeners(iframe);
164
- clearInterval(interval);
165
- return;
166
- }
167
- } catch (e) {
168
- clearInterval(interval);
169
- console.error(e);
170
- }
171
- }, 10);
172
- });
173
- });
174
- </script>
175
- </head>
176
-
177
- <body>
178
- <div class="outer">
179
- <iframe
180
- id="webview-patch-iframe"
181
- frameborder="0"
182
- sandbox="allow-scripts allow-same-origin allow-forms allow-pointer-lock allow-downloads"
183
- allow="cross-origin-isolated; autoplay; clipboard-read; clipboard-write"
184
- src="{{serverUrl}}"
185
- ></iframe>
186
- </div>
187
- </body>
188
- </html>
189
- `;
190
-
191
- // src/webview/webview.ts
1
+ //#region src/webview/template.html
2
+ var template_default = "<!doctype html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\" />\n <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n <style>\n html,\n body {\n width: 100%;\n height: 100%;\n margin: 0;\n padding: 0;\n overflow: hidden;\n }\n\n #webview-patch-iframe {\n width: 100%;\n height: 100%;\n border: none;\n }\n\n .outer {\n width: 100%;\n height: 100%;\n overflow: hidden;\n }\n </style>\n\n <script type=\"module\" id=\"webview-patch\">\n const TAG = '[@tomjs:vscode:extension] ';\n\n function onDomReady(callback, doc) {\n const _doc = doc || document;\n if (_doc.readyState === 'interactive' || _doc.readyState === 'complete') {\n callback();\n } else {\n _doc.addEventListener('DOMContentLoaded', callback);\n }\n }\n\n let vsCodeApi;\n\n function getApi() {\n if (vsCodeApi) return vsCodeApi;\n return (vsCodeApi = acquireVsCodeApi());\n }\n\n function sendInitData(iframe) {\n console.log(TAG + 'init data');\n const dataset = {};\n Object.keys(document.body.dataset).forEach((key) => {\n dataset[key] = document.body.dataset[key];\n });\n\n iframe.contentWindow.postMessage(\n {\n type: '[vscode:extension]:init',\n data: {\n state: getApi().getState(),\n style: document.getElementById('_defaultStyles').innerHTML,\n root: {\n cssText: document.documentElement.style.cssText,\n },\n body: {\n dataset: dataset,\n className: document.body.className,\n role: document.body.getAttribute('role'),\n },\n },\n },\n '*',\n );\n }\n\n function observeAttributeChanges(element, attributeName, callback) {\n const observer = new MutationObserver(function (mutationsList) {\n for (let mutation of mutationsList) {\n if (mutation.type === 'attributes' && mutation.attributeName === attributeName) {\n callback(mutation.target.getAttribute(attributeName));\n }\n }\n });\n observer.observe(element, { attributes: true });\n return observer;\n }\n\n // message handler\n let iframeLoaded = false;\n const cacheMessages = [];\n\n function handleMessage(e) {\n const iframe = document.getElementById('webview-patch-iframe');\n if (!iframeLoaded || !iframe) {\n return;\n }\n if (e.origin.startsWith('vscode-webview://')) {\n iframe.contentWindow.postMessage(e.data, '*');\n } else if ('{{serverUrl}}'.startsWith(e.origin)) {\n const { type, data } = e.data;\n console.log(TAG + ' received:', e.data);\n if (type === '[vscode:client]:postMessage') {\n getApi().postMessage(data);\n } else if (type === '[vscode:client]:commands') {\n if (data === 'F1') {\n window.dispatchEvent(\n new KeyboardEvent('keydown', {\n key: 'F1',\n keyCode: 112,\n code: 'F1',\n }),\n );\n }\n }\n }\n }\n\n window.addEventListener('message', function (event) {\n if (event.origin.startsWith('vscode-webview://')) {\n cacheMessages.push(event);\n return;\n }\n handleMessage(event);\n });\n\n let isCacheWorking = false;\n setInterval(() => {\n if (isCacheWorking) {\n return;\n }\n\n isCacheWorking = true;\n if (iframeLoaded) {\n let event = cacheMessages.shift();\n while (event) {\n handleMessage(event);\n event = cacheMessages.shift();\n }\n }\n isCacheWorking = false;\n }, 50);\n\n onDomReady(function () {\n /** @type {HTMLIFrameElement} */\n const iframe = document.getElementById('webview-patch-iframe');\n observeAttributeChanges(document.body, 'class', function (className) {\n sendInitData(iframe);\n });\n\n onDomReady(function () {\n iframeLoaded = true;\n sendInitData(iframe);\n }, iframe.contentDocument);\n\n iframe.addEventListener('load', function (e) {\n iframeLoaded = true;\n\n let interval = setInterval(() => {\n try {\n if (document.getElementById('_defaultStyles')) {\n sendInitData(iframe);\n // addListeners(iframe);\n clearInterval(interval);\n return;\n }\n } catch (e) {\n clearInterval(interval);\n console.error(e);\n }\n }, 10);\n });\n });\n <\/script>\n </head>\n\n <body>\n <div class=\"outer\">\n <iframe\n id=\"webview-patch-iframe\"\n frameborder=\"0\"\n sandbox=\"allow-scripts allow-same-origin allow-forms allow-pointer-lock allow-downloads\"\n allow=\"cross-origin-isolated; autoplay; clipboard-read; clipboard-write\"\n src=\"{{serverUrl}}\"\n ></iframe>\n </div>\n </body>\n</html>\n";
3
+
4
+ //#endregion
5
+ //#region src/webview/webview.ts
6
+ /**
7
+ *
8
+ * @param options serverUrl string or object options
9
+ */
192
10
  function getWebviewHtml(options) {
193
- const opts = {
194
- serverUrl: ""
195
- };
196
- Object.assign(opts, options);
197
- return template_default.replace(/\{\{serverUrl\}\}/g, opts.serverUrl);
11
+ const opts = { serverUrl: "" };
12
+ Object.assign(opts, options);
13
+ return template_default.replace(/\{\{serverUrl\}\}/g, opts.serverUrl);
198
14
  }
199
15
  var webview_default = getWebviewHtml;
200
16
 
201
-
202
-
203
- exports.default = webview_default; exports.getWebviewHtml = getWebviewHtml;
17
+ //#endregion
18
+ export { webview_default as default, getWebviewHtml };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-vscode",
3
- "version": "4.2.1",
3
+ "type": "module",
4
+ "version": "5.0.0",
4
5
  "description": "Use vue/react to develop 'vscode extension webview', supporting esm/cjs",
5
6
  "author": {
6
7
  "name": "Tom Gao",
@@ -23,26 +24,19 @@
23
24
  "cjs"
24
25
  ],
25
26
  "exports": {
26
- ".": {
27
- "import": "./dist/index.mjs",
28
- "require": "./dist/index.js"
29
- },
30
- "./webview": {
31
- "import": "./dist/webview.mjs",
32
- "require": "./dist/webview.js"
33
- },
34
- "./client": "./dist/client.global.js",
27
+ ".": "./dist/index.js",
28
+ "./webview": "./dist/webview.js",
29
+ "./client": "./dist/client.iife.js",
35
30
  "./env": "./env.d.ts"
36
31
  },
37
- "main": "./dist/index.js",
38
- "module": "./dist/index.mjs",
32
+ "module": "./dist/index.js",
39
33
  "types": "./dist/index.d.ts",
40
34
  "files": [
41
35
  "dist",
42
36
  "env.d.ts"
43
37
  ],
44
38
  "engines": {
45
- "node": ">=18"
39
+ "node": ">=18.19"
46
40
  },
47
41
  "publishConfig": {
48
42
  "access": "public",
@@ -55,39 +49,40 @@
55
49
  "@tomjs/logger": "^1.4.0",
56
50
  "@tomjs/node": "^2.2.3",
57
51
  "dayjs": "^1.11.13",
58
- "execa": "^5.1.1",
52
+ "execa": "^8.0.1",
59
53
  "kolorist": "^1.8.0",
60
54
  "lodash.clonedeep": "^4.5.0",
61
55
  "lodash.merge": "^4.6.2",
62
- "node-html-parser": "^6.1.13",
63
- "tsup": "^8.4.0"
56
+ "node-html-parser": "^7.0.1",
57
+ "tsdown": "~0.12.9"
64
58
  },
65
59
  "devDependencies": {
66
60
  "@commitlint/cli": "^19.8.1",
67
61
  "@tomjs/commitlint": "^4.0.0",
68
- "@tomjs/eslint": "^5.0.0",
62
+ "@tomjs/eslint": "^5.2.0",
69
63
  "@tomjs/stylelint": "^6.0.0",
70
- "@tomjs/tsconfig": "^1.7.2",
64
+ "@tomjs/tsconfig": "^2.0.0",
71
65
  "@types/lodash.clonedeep": "^4.5.9",
72
66
  "@types/lodash.merge": "^4.6.9",
73
- "@types/node": "18.19.100",
67
+ "@types/node": "18.19.120",
74
68
  "@vitejs/plugin-vue": "^5.2.4",
75
69
  "cross-env": "^7.0.3",
76
- "eslint": "^9.26.0",
77
- "globals": "^15.15.0",
78
- "lint-staged": "^15.5.2",
70
+ "eslint": "^9.31.0",
71
+ "globals": "^16.3.0",
72
+ "lint-staged": "^16.1.2",
79
73
  "npm-run-all": "^4.1.5",
74
+ "publint": "^0.3.12",
80
75
  "rimraf": "^6.0.1",
81
76
  "simple-git-hooks": "^2.13.0",
82
- "stylelint": "^16.19.1",
83
- "tsx": "^4.19.4",
77
+ "stylelint": "^16.22.0",
78
+ "tsx": "^4.20.3",
84
79
  "typescript": "~5.7.3",
85
80
  "vite": "^6.3.5",
86
- "vue-tsc": "^2.2.10"
81
+ "vue-tsc": "^2.2.12"
87
82
  },
88
83
  "scripts": {
89
- "dev": "pnpm clean && tsup --watch",
90
- "build": "pnpm clean && tsup",
84
+ "dev": "pnpm clean && tsdown --watch",
85
+ "build": "pnpm clean && tsdown",
91
86
  "clean": "rimraf ./dist",
92
87
  "lint": "run-s lint:stylelint lint:eslint",
93
88
  "lint:eslint": "eslint --fix",
@@ -1,93 +0,0 @@
1
- "use strict";
2
- (() => {
3
- // src/webview/client.ts
4
- if (window.top === window.self) {
5
- throw new Error("[vscode:client]: must run in vscode webview");
6
- }
7
- var TAG = "[@tomjs:vscode:client] ";
8
- patchAcquireVsCodeApi();
9
- function onDomReady(callback) {
10
- if (document.readyState === "interactive" || document.readyState === "complete") {
11
- callback();
12
- } else {
13
- document.addEventListener("DOMContentLoaded", callback);
14
- }
15
- }
16
- function patchInitData(data) {
17
- onDomReady(() => {
18
- console.log(TAG, "patch client style");
19
- const { style, body, root } = data;
20
- document.documentElement.style.cssText = root.cssText;
21
- document.body.className = body.className;
22
- Object.keys(body.dataset).forEach((key) => {
23
- document.body.dataset[key] = body.dataset[key];
24
- });
25
- const defaultStyles = document.createElement("style");
26
- defaultStyles.id = "_defaultStyles";
27
- defaultStyles.textContent = style;
28
- document.head.appendChild(defaultStyles);
29
- });
30
- }
31
- var POST_MESSAGE_TYPE = "[vscode:client]:postMessage";
32
- function patchAcquireVsCodeApi() {
33
- class AcquireVsCodeApi {
34
- postMessage(message) {
35
- console.log(TAG, "mock acquireVsCodeApi.postMessage:", message);
36
- window.parent.postMessage({ type: POST_MESSAGE_TYPE, data: message }, "*");
37
- }
38
- getState() {
39
- console.log(TAG, "mock acquireVsCodeApi.getState");
40
- const state = sessionStorage.getItem("vscodeState");
41
- return state ? JSON.parse(state) : void 0;
42
- }
43
- setState(newState) {
44
- console.log(TAG, "mock acquireVsCodeApi.setState:", newState);
45
- sessionStorage.setItem("vscodeState", JSON.stringify(newState));
46
- return newState;
47
- }
48
- }
49
- console.log(TAG, "patch acquireVsCodeApi");
50
- let api;
51
- window.acquireVsCodeApi = () => {
52
- if (!api) {
53
- api = new AcquireVsCodeApi();
54
- return api;
55
- } else {
56
- return api;
57
- }
58
- };
59
- }
60
- var INIT_TYPE = "[vscode:extension]:init";
61
- window.addEventListener("message", (e) => {
62
- const { type, data } = e.data || {};
63
- if (!e.origin.startsWith("vscode-webview://") || type !== INIT_TYPE) {
64
- return;
65
- }
66
- patchInitData(data);
67
- });
68
- var KEYBOARD_EVENT_TYPE = "[vscode:client]:commands";
69
- var isMac = navigator.userAgent.includes("Macintosh");
70
- document.addEventListener("keydown", (e) => {
71
- console.log(e);
72
- const { metaKey, shiftKey, ctrlKey, altKey, key } = e;
73
- if (key === "F1") {
74
- window.parent.postMessage({ type: KEYBOARD_EVENT_TYPE, data: "F1" }, "*");
75
- } else if (isMac && metaKey && !altKey && !ctrlKey) {
76
- if (shiftKey) {
77
- if (key === "z") {
78
- document.execCommand("redo");
79
- }
80
- } else if (key === "a") {
81
- document.execCommand("selectAll");
82
- } else if (key === "c") {
83
- document.execCommand("copy");
84
- } else if (key === "v") {
85
- document.execCommand("paste");
86
- } else if (key === "x") {
87
- document.execCommand("cut");
88
- } else if (key === "z") {
89
- document.execCommand("undo");
90
- }
91
- }
92
- });
93
- })();
package/dist/index.d.mts DELETED
@@ -1,93 +0,0 @@
1
- import { PluginOption } from 'vite';
2
- import { Options } from 'tsup';
3
-
4
- /**
5
- * vscode extension options. See [tsup](https://tsup.egoist.dev/) and [API Doc](https://paka.dev/npm/tsup) for more information.
6
- */
7
- interface ExtensionOptions extends Omit<Options, 'entry' | 'format' | 'outDir' | 'watch' | 'onSuccess' | 'skipNodeModulesBundle'> {
8
- /**
9
- * The extension entry file.
10
- * @default "extension/index.ts"
11
- */
12
- entry?: string;
13
- /**
14
- * The output directory for the extension files. Default is `dist-extension`.
15
- * @default "dist-extension"
16
- */
17
- outDir?: string;
18
- /**
19
- * The bundle format. Currently only supports cjs.
20
- */
21
- format?: 'cjs';
22
- /**
23
- * Skip dependencies and peerDependencies bundle. Default is false.
24
- */
25
- skipNodeModulesBundle?: boolean;
26
- /**
27
- * A function that will be executed after the build succeeds.
28
- */
29
- onSuccess?: () => Promise<void | undefined | (() => void | Promise<void>)>;
30
- }
31
- /**
32
- * vscode webview options.
33
- */
34
- interface WebviewOption {
35
- /**
36
- * The method name to inject. Default is '__getWebviewHtml__'
37
- */
38
- name?: string;
39
- /**
40
- * The CSP meta for the webview. Default is `<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src {{cspSource}} 'unsafe-inline'; script-src 'nonce-{{nonce}}' 'unsafe-eval';">`
41
- */
42
- csp?: string;
43
- }
44
- /**
45
- * vite plugin options.
46
- */
47
- interface PluginOptions {
48
- /**
49
- * Recommended switch. Default is true.
50
- * if true, will have the following default behavior:
51
- * will change the extension/webview outDir to be parallel outDir;
52
- * eg. if vite build.outDir is 'dist', will change extension/webview to 'dist/extension' and 'dist/webview'
53
- * @default true
54
- */
55
- recommended?: boolean;
56
- /**
57
- * Inject code into vscode extension code and web client code, so that webview can support HMR during the development stage.
58
- *
59
- * - vite serve
60
- * - extension: Inject `import __getWebviewHtml__ from '@tomjs/vite-plugin-vscode/webview';` at the top of the file that calls the `__getWebviewHtml__` method
61
- * - web: Add `<script>` tag to index.html and inject `@tomjs/vite-plugin-vscode/client` code
62
- * - vite build
63
- * - extension: Inject `import __getWebviewHtml__ from '@tomjs/vite-plugin-vscode-inject';` at the top of the file that calls the `__getWebviewHtml__` method
64
- *
65
- * If is string, will set inject method name. Default is '__getWebviewHtml__'.
66
- *
67
- *
68
- * @example
69
- * extension file
70
- * ```ts
71
- *function setupHtml(webview: Webview, context: ExtensionContext) {
72
- * return __getWebviewHtml__({serverUrl:WebviewHtmlOptions, webview, context});
73
- *}
74
- * ```
75
- */
76
- webview?: boolean | string | WebviewOption;
77
- /**
78
- * extension vite config.
79
- */
80
- extension?: ExtensionOptions;
81
- /**
82
- * Whether to enable devtools. Inject `<script src="http://localhost:<devtools-port>"></script>` into webview client . Default is true.
83
- * - true:
84
- * - react: inject `<script src="http://localhost:8097"></script>`
85
- * - vue: inject `<script src="http://localhost:8098"></script>`
86
- * @default true
87
- */
88
- devtools?: boolean;
89
- }
90
-
91
- declare function useVSCodePlugin(options?: PluginOptions): PluginOption;
92
-
93
- export { type ExtensionOptions, type PluginOptions, type WebviewOption, useVSCodePlugin as default, useVSCodePlugin };