@tomjs/vite-plugin-vscode 3.2.1 → 4.1.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/README.md CHANGED
@@ -13,11 +13,16 @@ In development mode, inject the same code of [@tomjs/vscode-extension-webview](h
13
13
  - Use [tsup](https://github.com/egoist/tsup) to quickly build `extension code`
14
14
  - Simple configuration, focus on business
15
15
  - Support `esm` and `cjs`
16
+ - Support ESM extension (vscode `v1.100.0+`)
16
17
  - Support webview `HMR`
17
18
  - Support `acquireVsCodeApi` of [@types/vscode-webview](https://www.npmjs.com/package/@types/vscode-webview)
18
19
  - Support [Multi-Page App](https://vitejs.dev/guide/build.html#multi-page-app)
19
20
  - Supports `vue` and `react` and other [frameworks](https://cn.vitejs.dev/guide/#trying-vite-online) supported by `vite`
20
21
 
22
+ ### ESM extension
23
+
24
+ The NodeJS extension host now (`v1.100.0+`) supports extensions that use JavaScript-modules (ESM). All it needs is the `"type": "module"` entry in your extension's `package.json` file. With that, the JavaScript code can use `import` and `export` statements, including the special module `import('vscode')`.
25
+
21
26
  ## Install
22
27
 
23
28
  ```bash
@@ -83,9 +88,15 @@ const panel = window.createWebviewPanel('showHelloWorld', 'Hello World', ViewCol
83
88
  });
84
89
 
85
90
  // Vite development mode and production mode inject different webview codes to reduce development work
86
- panel.webview.html = process.env.VITE_DEV_SERVER_URL
87
- ? __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL)
88
- : __getWebviewHtml__(webview, context);
91
+ panel.webview.html = __getWebviewHtml__({
92
+ // vite dev mode
93
+ serverUrl: process.env.VITE_DEV_SERVER_URL,
94
+ // vite prod mode
95
+ webview,
96
+ context,
97
+ inputName: 'index',
98
+ injectCode: `<script>window.__FLAG1__=666;window.__FLAG2__=888;</script>`,
99
+ });
89
100
  ```
90
101
 
91
102
  - `package.json`
@@ -137,19 +148,20 @@ export default defineConfig({
137
148
  });
138
149
  ```
139
150
 
140
- ### Multi-page application
151
+ ### **getWebviewHtml**
141
152
 
142
153
  See [vue-import](./examples/vue-import) example
143
154
 
144
155
  - `vite.config.ts`
145
156
 
146
157
  ```ts
158
+ import { defineConfig } from 'vite';
147
159
  import path from 'node:path';
148
160
  import vscode from '@tomjs/vite-plugin-vscode';
149
161
 
150
162
  export default defineConfig({
163
+ plugins: [vscode()],
151
164
  build: {
152
- plugins: [vscode()]
153
165
  rollupOptions: {
154
166
  // https://cn.vitejs.dev/guide/build.html#multi-page-app
155
167
  input: [path.resolve(__dirname, 'index.html'), path.resolve(__dirname, 'index2.html')],
@@ -166,39 +178,71 @@ export default defineConfig({
166
178
  - page one
167
179
 
168
180
  ```ts
169
- process.env.VITE_DEV_SERVER_URL
170
- ? __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL)
171
- : __getWebviewHtml__(webview, context);
181
+ __getWebviewHtml__({
182
+ // vite dev mode
183
+ serverUrl: process.env.VITE_DEV_SERVER_URL,
184
+ // vite prod mode
185
+ webview,
186
+ context,
187
+ });
172
188
  ```
173
189
 
174
190
  - page two
175
191
 
176
192
  ```ts
177
- process.env.VITE_DEV_SERVER_URL
178
- ? __getWebviewHtml__(`${process.env.VITE_DEV_SERVER_URL}/index2.html`)
179
- : __getWebviewHtml__(webview, context, 'index2');
193
+ __getWebviewHtml__({
194
+ // vite dev mode
195
+ serverUrl: `${process.env.VITE_DEV_SERVER_URL}/index2.html`,
196
+ // vite prod mode
197
+ webview,
198
+ context,
199
+ inputName: 'index2',
200
+ });
201
+ ```
202
+
203
+ - A single page uses different parameters to achieve different functions
204
+
205
+ ```ts
206
+ __getWebviewHtml__({
207
+ // vite dev mode
208
+ serverUrl: `${process.env.VITE_DEV_SERVER_URL}?id=666`,
209
+ // vite prod mode
210
+ webview,
211
+ context,
212
+ injectCode: `<script>window.__id__=666;</script>`,
213
+ });
180
214
  ```
181
215
 
182
216
  **getWebviewHtml** Description
183
217
 
184
218
  ```ts
185
- /**
186
- * `[vite serve]` Gets the html of webview in development mode.
187
- * @param options serverUrl: The url of the vite dev server.
188
- */
189
- function __getWebviewHtml__(options?: string | { serverUrl: string }): string;
219
+ interface WebviewHtmlOptions {
220
+ /**
221
+ * `[vite serve]` The url of the vite dev server. Please use `process.env.VITE_DEV_SERVER_URL`
222
+ */
223
+ serverUrl?: string;
224
+ /**
225
+ * `[vite build]` The Webview instance of the extension.
226
+ */
227
+ webview: Webview;
228
+ /**
229
+ * `[vite build]` The ExtensionContext instance of the extension.
230
+ */
231
+ context: ExtensionContext;
232
+ /**
233
+ * `[vite build]` vite build.rollupOptions.input name. Default is `index`.
234
+ */
235
+ inputName?: string;
236
+ /**
237
+ * `[vite build]` Inject code into the afterbegin of the head element.
238
+ */
239
+ injectCode?: string;
240
+ }
190
241
 
191
242
  /**
192
- * `[vite build]` Gets the html of webview in production mode.
193
- * @param webview The WebviewPanel instance of the extension.
194
- * @param context The ExtensionContext instance of the extension.
195
- * @param inputName vite build.rollupOptions.input name. Default is `index`.
243
+ * Gets the html of webview
196
244
  */
197
- function __getWebviewHtml__(
198
- webview: Webview,
199
- context: ExtensionContext,
200
- inputName?: string,
201
- ): string;
245
+ function __getWebviewHtml__(options?: WebviewHtmlOptions): string;
202
246
  ```
203
247
 
204
248
  ### Warning
@@ -388,6 +432,7 @@ Open the [examples](./examples) directory, there are `vue` and `react` examples.
388
432
 
389
433
  - [react](./examples/react): Simple react example.
390
434
  - [vue](./examples/vue): Simple vue example.
435
+ - [vue-esm](./examples/vue-esm): Simple vue (ESM Extension) example.
391
436
  - [vue-import](./examples/vue-import): Dynamic import() and multi-page examples.
392
437
 
393
438
  ## Related
@@ -398,6 +443,12 @@ Open the [examples](./examples) directory, there are `vue` and `react` examples.
398
443
 
399
444
  ## Important Notes
400
445
 
446
+ ### v4.0.0
447
+
448
+ **Breaking Updates:**
449
+
450
+ - Merge the `__getWebviewHtml__` method for development and production into one, see [getWebviewHtml](#getwebviewhtml)
451
+
401
452
  ### v3.0.0
402
453
 
403
454
  **Breaking Updates:**
package/README.zh_CN.md CHANGED
@@ -13,11 +13,16 @@
13
13
  - 使用 [tsup](https://github.com/egoist/tsup) 快速构建 `扩展代码`
14
14
  - 配置简单,专注业务
15
15
  - 支持 `esm`和 `cjs`
16
+ - 支持 ESM 扩展(vscode `v1.100.0+`)
16
17
  - 支持 webview `HMR`
17
18
  - 支持 [@types/vscode-webview](https://www.npmjs.com/package/@types/vscode-webview) 的 `acquireVsCodeApi`
18
19
  - 支持[多页面应用](https://cn.vitejs.dev/guide/build.html#multi-page-app)
19
20
  - 支持 `vue` 、`react` 等其他 `vite` 支持的[框架](https://cn.vitejs.dev/guide/#trying-vite-online)
20
21
 
22
+ ### ESM 扩展
23
+
24
+ NodeJS 扩展现在(`v1.100.0+`)支持使用 JavaScript 模块 (ESM) 的扩展。它只需要在扩展的 `package.json` 文件中添加 `"type": "module"` 条目即可。这样,JavaScript 代码就可以使用 `import` 和 `export` 语句,包括特殊的模块 `import('vscode')`
25
+
21
26
  ## 安装
22
27
 
23
28
  ```bash
@@ -83,13 +88,15 @@ const panel = window.createWebviewPanel('showHelloWorld', 'Hello World', ViewCol
83
88
  });
84
89
 
85
90
  // vite 开发模式和生产模式注入不同的webview代码,减少开发工作
86
- function getHtml(webview: Webview, context: ExtensionContext) {
87
- process.env.VITE_DEV_SERVER_URL
88
- ? __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL)
89
- : __getWebviewHtml__(webview, context);
90
- }
91
-
92
- panel.webview.html = getHtml(webview, context);
91
+ panel.webview.html = __getWebviewHtml__({
92
+ // vite 开发模式
93
+ serverUrl: process.env.VITE_DEV_SERVER_URL,
94
+ // vite 生产模式
95
+ webview,
96
+ context,
97
+ inputName: 'index',
98
+ injectCode: `<script>window.__FLAG1__=666;window.__FLAG2__=888;</script>`,
99
+ });
93
100
  ```
94
101
 
95
102
  - `package.json`
@@ -152,8 +159,8 @@ import path from 'node:path';
152
159
  import vscode from '@tomjs/vite-plugin-vscode';
153
160
 
154
161
  export default defineConfig({
162
+ plugins: [vscode()],
155
163
  build: {
156
- plugins: [vscode()]
157
164
  rollupOptions: {
158
165
  // https://cn.vitejs.dev/guide/build.html#multi-page-app
159
166
  input: [path.resolve(__dirname, 'index.html'), path.resolve(__dirname, 'index2.html')],
@@ -170,39 +177,71 @@ export default defineConfig({
170
177
  - 页面一
171
178
 
172
179
  ```ts
173
- process.env.VITE_DEV_SERVER_URL
174
- ? __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL)
175
- : __getWebviewHtml__(webview, context);
180
+ __getWebviewHtml__({
181
+ // vite 开发模式
182
+ serverUrl: process.env.VITE_DEV_SERVER_URL,
183
+ // vite 生产模式
184
+ webview,
185
+ context,
186
+ });
176
187
  ```
177
188
 
178
189
  - 页面二
179
190
 
180
191
  ```ts
181
- process.env.VITE_DEV_SERVER_URL
182
- ? __getWebviewHtml__(`${process.env.VITE_DEV_SERVER_URL}/index2.html`)
183
- : __getWebviewHtml__(webview, context, 'index2');
192
+ __getWebviewHtml__({
193
+ // vite 开发模式
194
+ serverUrl: `${process.env.VITE_DEV_SERVER_URL}/index2.html`,
195
+ // vite 生产模式
196
+ webview,
197
+ context,
198
+ inputName: 'index2',
199
+ });
200
+ ```
201
+
202
+ - 单个页面通过不同参数来实现不同功能
203
+
204
+ ```ts
205
+ __getWebviewHtml__({
206
+ // vite 开发模式
207
+ serverUrl: `${process.env.VITE_DEV_SERVER_URL}?id=666`,
208
+ // vite 生产模式
209
+ webview,
210
+ context,
211
+ injectCode: `<script>window.__id__=666;</script>`,
212
+ });
184
213
  ```
185
214
 
186
215
  **getWebviewHtml** 说明
187
216
 
188
217
  ```ts
189
- /**
190
- * `[vite serve]` 在开发模式获取webview的html
191
- * @param options serverUrl: vite开发服务器的url
192
- */
193
- function __getWebviewHtml__(options?: string | { serverUrl: string }): string;
218
+ interface WebviewHtmlOptions {
219
+ /**
220
+ * `[vite serve]` vite开发服务器的url, 请用 `process.env.VITE_DEV_SERVER_URL`
221
+ */
222
+ serverUrl?: string;
223
+ /**
224
+ * `[vite build]` 扩展的 Webview 实例
225
+ */
226
+ webview: Webview;
227
+ /**
228
+ * `[vite build]` 扩展的 ExtensionContext 实例
229
+ */
230
+ context: ExtensionContext;
231
+ /**
232
+ * `[vite build]` vite build.rollupOptions.input 设置的名称. 默认 `index`.
233
+ */
234
+ inputName?: string;
235
+ /**
236
+ * `[vite build]` 向 head 元素的结束前注入代码 <head>--inject--
237
+ */
238
+ injectCode?: string;
239
+ }
194
240
 
195
241
  /**
196
- * `[vite build]` 在生产模式获取webview的html
197
- * @param webview 扩展的 Webview 实例
198
- * @param context 扩展的 ExtensionContext 实例
199
- * @param inputName vite build.rollupOptions.input 设置的名称. 默认 `index`.
242
+ * 获取webview的html
200
243
  */
201
- function __getWebviewHtml__(
202
- webview: Webview,
203
- context: ExtensionContext,
204
- inputName?: string,
205
- ): string;
244
+ function __getWebviewHtml__(options?: WebviewHtmlOptions): string;
206
245
  ```
207
246
 
208
247
  ### 警告
@@ -397,6 +436,7 @@ pnpm build
397
436
 
398
437
  - [react](./examples/react):简单的 react 示例。
399
438
  - [vue](./examples/vue):简单的 vue 示例。
439
+ - [vue-esm](./examples/vue-esm):简单的 vue(ESM 扩展)示例。
400
440
  - [vue-import](./examples/vue-import):动态 import() 和多页面示例。
401
441
 
402
442
  ## 关联
@@ -407,6 +447,12 @@ pnpm build
407
447
 
408
448
  ## 重要说明
409
449
 
450
+ ### v4.0.0
451
+
452
+ **破坏性更新:**
453
+
454
+ - 开发和生产的 `__getWebviewHtml__` 方法合并为同一个,参考 [getWebviewHtml](#getwebviewhtml)
455
+
410
456
  ### v3.0.0
411
457
 
412
458
  **破坏性更新:**
@@ -1,103 +1,93 @@
1
1
  "use strict";
2
2
  (() => {
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __commonJS = (cb, mod) => function __require() {
5
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
6
- };
7
-
8
3
  // src/webview/client.ts
9
- var require_client = __commonJS({
10
- "src/webview/client.ts"() {
11
- if (window.top === window.self) {
12
- return;
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 }, "*");
13
37
  }
14
- var TAG = "[@tomjs:vscode:client] ";
15
- patchAcquireVsCodeApi();
16
- function onDomReady(callback) {
17
- if (document.readyState === "interactive" || document.readyState === "complete") {
18
- callback();
19
- } else {
20
- document.addEventListener("DOMContentLoaded", callback);
21
- }
38
+ getState() {
39
+ console.log(TAG, "mock acquireVsCodeApi.getState");
40
+ const state = sessionStorage.getItem("vscodeState");
41
+ return state ? JSON.parse(state) : void 0;
22
42
  }
23
- function patchInitData(data) {
24
- onDomReady(() => {
25
- console.log(TAG, "patch client style");
26
- const { style, body, root } = data;
27
- document.documentElement.style.cssText = root.cssText;
28
- document.body.className = body.className;
29
- Object.keys(body.dataset).forEach((key) => {
30
- document.body.dataset[key] = body.dataset[key];
31
- });
32
- const defaultStyles = document.createElement("style");
33
- defaultStyles.id = "_defaultStyles";
34
- defaultStyles.textContent = style;
35
- document.head.appendChild(defaultStyles);
36
- });
43
+ setState(newState) {
44
+ console.log(TAG, "mock acquireVsCodeApi.setState:", newState);
45
+ sessionStorage.setItem("vscodeState", JSON.stringify(newState));
46
+ return newState;
37
47
  }
38
- var POST_MESSAGE_TYPE = "[vscode:client]:postMessage";
39
- function patchAcquireVsCodeApi() {
40
- class AcquireVsCodeApi {
41
- postMessage(message) {
42
- console.log(TAG, "mock acquireVsCodeApi.postMessage:", message);
43
- window.parent.postMessage({ type: POST_MESSAGE_TYPE, data: message }, "*");
44
- }
45
- getState() {
46
- console.log(TAG, "mock acquireVsCodeApi.getState");
47
- const state = sessionStorage.getItem("vscodeState");
48
- return state ? JSON.parse(state) : void 0;
49
- }
50
- setState(newState) {
51
- console.log(TAG, "mock acquireVsCodeApi.setState:", newState);
52
- sessionStorage.setItem("vscodeState", JSON.stringify(newState));
53
- return newState;
54
- }
55
- }
56
- console.log(TAG, "patch acquireVsCodeApi");
57
- let api;
58
- window.acquireVsCodeApi = () => {
59
- if (!api) {
60
- api = new AcquireVsCodeApi();
61
- return api;
62
- } else {
63
- return api;
64
- }
65
- };
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;
66
57
  }
67
- var INIT_TYPE = "[vscode:extension]:init";
68
- window.addEventListener("message", (e) => {
69
- const { type, data } = e.data || {};
70
- if (!e.origin.startsWith("vscode-webview://") || type !== INIT_TYPE) {
71
- return;
72
- }
73
- patchInitData(data);
74
- });
75
- var KEYBOARD_EVENT_TYPE = "[vscode:client]:commands";
76
- var isMac = navigator.userAgent.indexOf("Macintosh") >= 0;
77
- document.addEventListener("keydown", (e) => {
78
- console.log(e);
79
- const { metaKey, shiftKey, ctrlKey, altKey, key } = e;
80
- if (key === "F1") {
81
- window.parent.postMessage({ type: KEYBOARD_EVENT_TYPE, data: "F1" }, "*");
82
- } else if (isMac && metaKey && !altKey && !ctrlKey) {
83
- if (shiftKey) {
84
- if (key === "z") {
85
- document.execCommand("redo");
86
- }
87
- } else if (key === "a") {
88
- document.execCommand("selectAll");
89
- } else if (key === "c") {
90
- document.execCommand("copy");
91
- } else if (key === "v") {
92
- document.execCommand("paste");
93
- } else if (key === "x") {
94
- document.execCommand("cut");
95
- } else if (key === "z") {
96
- document.execCommand("undo");
97
- }
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.indexOf("Macintosh") >= 0;
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");
98
79
  }
99
- });
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
+ }
100
91
  }
101
92
  });
102
- require_client();
103
93
  })();
package/dist/index.d.mts CHANGED
@@ -69,20 +69,9 @@ interface PluginOptions {
69
69
  * extension file
70
70
  * ```ts
71
71
  *function setupHtml(webview: Webview, context: ExtensionContext) {
72
- * if (process.env.VITE_DEV_SERVER_URL) {
73
- * return __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL);
74
- * }
75
- * return __getWebviewHtml__(webview, context);
72
+ * return __getWebviewHtml__({serverUrl:WebviewHtmlOptions, webview, context});
76
73
  *}
77
74
  * ```
78
- * webview client
79
- * ```html
80
- * <html>
81
- * <head>
82
- * <script>inject code</script>
83
- * </head>
84
- * </html>
85
- * ```
86
75
  */
87
76
  webview?: boolean | string | WebviewOption;
88
77
  /**
package/dist/index.d.ts CHANGED
@@ -69,20 +69,9 @@ interface PluginOptions {
69
69
  * extension file
70
70
  * ```ts
71
71
  *function setupHtml(webview: Webview, context: ExtensionContext) {
72
- * if (process.env.VITE_DEV_SERVER_URL) {
73
- * return __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL);
74
- * }
75
- * return __getWebviewHtml__(webview, context);
72
+ * return __getWebviewHtml__({serverUrl:WebviewHtmlOptions, webview, context});
76
73
  *}
77
74
  * ```
78
- * webview client
79
- * ```html
80
- * <html>
81
- * <head>
82
- * <script>inject code</script>
83
- * </head>
84
- * </html>
85
- * ```
86
75
  */
87
76
  webview?: boolean | string | WebviewOption;
88
77
  /**
package/dist/index.js CHANGED
@@ -3,10 +3,10 @@ var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
3
3
  var _os = require('os'); var _os2 = _interopRequireDefault(_os);
4
4
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
5
5
  var _process = require('process');
6
+ var _node = require('@tomjs/node');
6
7
  var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
7
8
  var _nodehtmlparser = require('node-html-parser');
8
9
  var _tsup = require('tsup');
9
- var _node = require('@tomjs/node');
10
10
 
11
11
  // src/constants.ts
12
12
  var PLUGIN_NAME = "tomjs:vscode";
@@ -97,6 +97,7 @@ function getPkg() {
97
97
  }
98
98
  function preMergeOptions(options) {
99
99
  const pkg = getPkg();
100
+ const format = pkg.type === "module" ? "esm" : "cjs";
100
101
  const opts = _lodashmerge2.default.call(void 0,
101
102
  {
102
103
  webview: true,
@@ -105,8 +106,8 @@ function preMergeOptions(options) {
105
106
  extension: {
106
107
  entry: "extension/index.ts",
107
108
  outDir: "dist-extension",
108
- target: ["es2019", "node14"],
109
- format: "cjs",
109
+ target: format === "esm" ? ["node18"] : ["es2019", "node14"],
110
+ format,
110
111
  shims: true,
111
112
  clean: true,
112
113
  dts: false,
@@ -153,7 +154,7 @@ function genProdWebviewCode(cache, webview) {
153
154
  webview = Object.assign({}, webview);
154
155
  const prodCacheFolder = _path2.default.join(_process.cwd.call(void 0, ), "node_modules", prodCachePkgName);
155
156
  _node.emptyDirSync.call(void 0, prodCacheFolder);
156
- const destFile = _path2.default.join(prodCacheFolder, "index.ts");
157
+ const destFile = _path2.default.join(prodCacheFolder, "index.js");
157
158
  function handleHtmlCode(html) {
158
159
  const root = _nodehtmlparser.parse.call(void 0, html);
159
160
  const head = root.querySelector("head");
@@ -188,7 +189,7 @@ function genProdWebviewCode(cache, webview) {
188
189
  );
189
190
  const code = (
190
191
  /* js */
191
- `import { ExtensionContext, Uri, Webview } from 'vscode';
192
+ `import { Uri } from 'vscode';
192
193
 
193
194
  ${cacheCode}
194
195
 
@@ -201,10 +202,15 @@ function uuid() {
201
202
  return text;
202
203
  }
203
204
 
204
- export default function getWebviewHtml(webview: Webview, context: ExtensionContext, inputName?:string){
205
+ export default function getWebviewHtml(options){
206
+ const { webview, context, inputName, injectCode } = options || {};
205
207
  const nonce = uuid();
206
208
  const baseUri = webview.asWebviewUri(Uri.joinPath(context.extensionUri, (process.env.VITE_WEBVIEW_DIST || 'dist')));
207
- const html = htmlCode[inputName || 'index'] || '';
209
+ let html = htmlCode[inputName || 'index'] || '';
210
+ if (injectCode) {
211
+ html = html.replace('<head>', '<head>'+ injectCode);
212
+ }
213
+
208
214
  return html.replaceAll('{{cspSource}}', webview.cspSource).replaceAll('{{nonce}}', nonce).replaceAll('{{baseUri}}', baseUri);
209
215
  }
210
216
  `
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // node_modules/.pnpm/tsup@7.2.0_@swc+core@1.7.26_postcss@8.4.32_typescript@5.3.3/node_modules/tsup/assets/esm_shims.js
1
+ // node_modules/.pnpm/tsup@7.2.0_@swc+core@1.11.24_postcss@8.4.49_typescript@5.3.3/node_modules/tsup/assets/esm_shims.js
2
2
  import { fileURLToPath } from "url";
3
3
  import path from "path";
4
4
  var getFilename = () => fileURLToPath(import.meta.url);
@@ -10,10 +10,10 @@ import fs from "fs";
10
10
  import os from "os";
11
11
  import path2 from "path";
12
12
  import { cwd } from "process";
13
+ import { emptyDirSync, readFileSync, readJsonSync } from "@tomjs/node";
13
14
  import merge from "lodash.merge";
14
15
  import { parse as htmlParser } from "node-html-parser";
15
16
  import { build as tsupBuild } from "tsup";
16
- import { emptyDirSync, readFileSync, readJsonSync } from "@tomjs/node";
17
17
 
18
18
  // src/constants.ts
19
19
  var PLUGIN_NAME = "tomjs:vscode";
@@ -104,6 +104,7 @@ function getPkg() {
104
104
  }
105
105
  function preMergeOptions(options) {
106
106
  const pkg = getPkg();
107
+ const format = pkg.type === "module" ? "esm" : "cjs";
107
108
  const opts = merge(
108
109
  {
109
110
  webview: true,
@@ -112,8 +113,8 @@ function preMergeOptions(options) {
112
113
  extension: {
113
114
  entry: "extension/index.ts",
114
115
  outDir: "dist-extension",
115
- target: ["es2019", "node14"],
116
- format: "cjs",
116
+ target: format === "esm" ? ["node18"] : ["es2019", "node14"],
117
+ format,
117
118
  shims: true,
118
119
  clean: true,
119
120
  dts: false,
@@ -160,7 +161,7 @@ function genProdWebviewCode(cache, webview) {
160
161
  webview = Object.assign({}, webview);
161
162
  const prodCacheFolder = path2.join(cwd(), "node_modules", prodCachePkgName);
162
163
  emptyDirSync(prodCacheFolder);
163
- const destFile = path2.join(prodCacheFolder, "index.ts");
164
+ const destFile = path2.join(prodCacheFolder, "index.js");
164
165
  function handleHtmlCode(html) {
165
166
  const root = htmlParser(html);
166
167
  const head = root.querySelector("head");
@@ -195,7 +196,7 @@ function genProdWebviewCode(cache, webview) {
195
196
  );
196
197
  const code = (
197
198
  /* js */
198
- `import { ExtensionContext, Uri, Webview } from 'vscode';
199
+ `import { Uri } from 'vscode';
199
200
 
200
201
  ${cacheCode}
201
202
 
@@ -208,10 +209,15 @@ function uuid() {
208
209
  return text;
209
210
  }
210
211
 
211
- export default function getWebviewHtml(webview: Webview, context: ExtensionContext, inputName?:string){
212
+ export default function getWebviewHtml(options){
213
+ const { webview, context, inputName, injectCode } = options || {};
212
214
  const nonce = uuid();
213
215
  const baseUri = webview.asWebviewUri(Uri.joinPath(context.extensionUri, (process.env.VITE_WEBVIEW_DIST || 'dist')));
214
- const html = htmlCode[inputName || 'index'] || '';
216
+ let html = htmlCode[inputName || 'index'] || '';
217
+ if (injectCode) {
218
+ html = html.replace('<head>', '<head>'+ injectCode);
219
+ }
220
+
215
221
  return html.replaceAll('{{cspSource}}', webview.cspSource).replaceAll('{{nonce}}', nonce).replaceAll('{{baseUri}}', baseUri);
216
222
  }
217
223
  `
@@ -1,4 +1,4 @@
1
- interface WebviewHtmlDevOptions {
1
+ interface WebviewHtmlOptions {
2
2
  /**
3
3
  * local server url
4
4
  */
@@ -8,6 +8,6 @@ interface WebviewHtmlDevOptions {
8
8
  *
9
9
  * @param options serverUrl string or object options
10
10
  */
11
- declare function getWebviewHtml(options: string | WebviewHtmlDevOptions): string;
11
+ declare function getWebviewHtml(options: WebviewHtmlOptions): string;
12
12
 
13
- export { WebviewHtmlDevOptions, getWebviewHtml as default, getWebviewHtml };
13
+ export { WebviewHtmlOptions, getWebviewHtml as default, getWebviewHtml };
package/dist/webview.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- interface WebviewHtmlDevOptions {
1
+ interface WebviewHtmlOptions {
2
2
  /**
3
3
  * local server url
4
4
  */
@@ -8,6 +8,6 @@ interface WebviewHtmlDevOptions {
8
8
  *
9
9
  * @param options serverUrl string or object options
10
10
  */
11
- declare function getWebviewHtml(options: string | WebviewHtmlDevOptions): string;
11
+ declare function getWebviewHtml(options: WebviewHtmlOptions): string;
12
12
 
13
- export { WebviewHtmlDevOptions, getWebviewHtml as default, getWebviewHtml };
13
+ export { WebviewHtmlOptions, getWebviewHtml as default, getWebviewHtml };
package/dist/webview.js CHANGED
@@ -193,11 +193,7 @@ function getWebviewHtml(options) {
193
193
  const opts = {
194
194
  serverUrl: ""
195
195
  };
196
- if (typeof options === "string") {
197
- opts.serverUrl = options;
198
- } else {
199
- Object.assign(opts, options);
200
- }
196
+ Object.assign(opts, options);
201
197
  return template_default.replace(new RegExp("{{serverUrl}}", "g"), opts.serverUrl);
202
198
  }
203
199
  var webview_default = getWebviewHtml;
package/dist/webview.mjs CHANGED
@@ -193,11 +193,7 @@ function getWebviewHtml(options) {
193
193
  const opts = {
194
194
  serverUrl: ""
195
195
  };
196
- if (typeof options === "string") {
197
- opts.serverUrl = options;
198
- } else {
199
- Object.assign(opts, options);
200
- }
196
+ Object.assign(opts, options);
201
197
  return template_default.replace(new RegExp("{{serverUrl}}", "g"), opts.serverUrl);
202
198
  }
203
199
  var webview_default = getWebviewHtml;
package/env.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ExtensionContext, Webview } from 'vscode';
1
+ import type { ExtensionContext, Webview } from 'vscode';
2
2
  // Make this a module
3
3
  export {};
4
4
  declare global {
@@ -24,21 +24,31 @@ declare global {
24
24
  }
25
25
  }
26
26
 
27
- /**
28
- * `[vite serve]` Gets the html of webview in development mode.
29
- * @param options serverUrl: The url of the vite dev server.
30
- */
31
- function __getWebviewHtml__(options?: string | { serverUrl: string }): string;
27
+ interface WebviewHtmlOptions {
28
+ /**
29
+ * `[vite serve]` The url of the vite dev server. Please use `process.env.VITE_DEV_SERVER_URL`
30
+ */
31
+ serverUrl?: string;
32
+ /**
33
+ * `[vite build]` The Webview instance of the extension.
34
+ */
35
+ webview: Webview;
36
+ /**
37
+ * `[vite build]` The ExtensionContext instance of the extension.
38
+ */
39
+ context: ExtensionContext;
40
+ /**
41
+ * `[vite build]` vite build.rollupOptions.input name. Default is `index`.
42
+ */
43
+ inputName?: string;
44
+ /**
45
+ * `[vite build]` Inject code into the afterbegin of the head element.
46
+ */
47
+ injectCode?: string;
48
+ }
32
49
 
33
50
  /**
34
- * `[vite build]` Gets the html of webview in production mode.
35
- * @param webview The Webview instance of the extension.
36
- * @param context The ExtensionContext instance of the extension.
37
- * @param inputName vite build.rollupOptions.input name. Default is `index`.
51
+ * Gets the html of webview
38
52
  */
39
- function __getWebviewHtml__(
40
- webview: Webview,
41
- context: ExtensionContext,
42
- inputName?: string,
43
- ): string;
53
+ function __getWebviewHtml__(options?: WebviewHtmlOptions): string;
44
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-vscode",
3
- "version": "3.2.1",
3
+ "version": "4.1.0",
4
4
  "description": "Use vue/react to develop 'vscode extension webview', supporting esm/cjs",
5
5
  "keywords": [
6
6
  "vite",
@@ -23,24 +23,12 @@
23
23
  "types": "./dist/index.d.ts",
24
24
  "exports": {
25
25
  ".": {
26
- "require": {
27
- "default": "./dist/index.js",
28
- "types": "./dist/index.d.ts"
29
- },
30
- "import": {
31
- "default": "./dist/index.mjs",
32
- "types": "./dist/index.d.mts"
33
- }
26
+ "require": "./dist/index.js",
27
+ "import": "./dist/index.mjs"
34
28
  },
35
29
  "./webview": {
36
- "require": {
37
- "default": "./dist/webview.js",
38
- "types": "./dist/webview.d.ts"
39
- },
40
- "import": {
41
- "default": "./dist/webview.mjs",
42
- "types": "./dist/webview.d.mts"
43
- }
30
+ "require": "./dist/webview.js",
31
+ "import": "./dist/webview.mjs"
44
32
  },
45
33
  "./client": "./dist/client.global.js",
46
34
  "./env": "./env.d.ts"
@@ -61,36 +49,39 @@
61
49
  "url": "git+https://github.com/tomjs/vite-plugin-vscode.git"
62
50
  },
63
51
  "dependencies": {
64
- "@tomjs/node": "^2.2.0",
65
- "dayjs": "^1.11.10",
52
+ "@tomjs/node": "^2.2.3",
53
+ "dayjs": "^1.11.13",
66
54
  "execa": "^5.1.1",
67
55
  "kolorist": "^1.8.0",
68
56
  "lodash.clonedeep": "^4.5.0",
69
57
  "lodash.merge": "^4.6.2",
70
- "node-html-parser": "^6.1.12",
58
+ "node-html-parser": "^6.1.13",
71
59
  "tsup": "7.2.0"
72
60
  },
73
61
  "devDependencies": {
74
- "@commitlint/cli": "^18.4.4",
75
- "@tomjs/commitlint": "^2.1.2",
76
- "@tomjs/eslint": "^1.2.2",
77
- "@tomjs/prettier": "^1.1.1",
78
- "@tomjs/stylelint": "^2.2.0",
79
- "@tomjs/tsconfig": "^1.3.0",
62
+ "@commitlint/cli": "^19.6.0",
63
+ "@tomjs/commitlint": "^3.3.0",
64
+ "@tomjs/eslint": "^4.1.0",
65
+ "@tomjs/prettier": "^1.4.1",
66
+ "@tomjs/stylelint": "^2.6.1",
67
+ "@tomjs/tsconfig": "^1.7.1",
80
68
  "@types/lodash.clonedeep": "^4.5.9",
81
69
  "@types/lodash.merge": "^4.6.9",
82
- "@types/node": "18.19.55",
70
+ "@types/node": "18.19.67",
71
+ "@vitejs/plugin-vue": "^5.2.4",
83
72
  "cross-env": "^7.0.3",
84
- "eslint": "^8.56.0",
85
- "husky": "^8.0.3",
86
- "lint-staged": "^15.2.0",
73
+ "eslint": "^9.16.0",
74
+ "globals": "^15.13.0",
75
+ "husky": "^9.1.7",
76
+ "lint-staged": "^15.2.10",
87
77
  "npm-run-all": "^4.1.5",
88
- "prettier": "^3.2.2",
89
- "rimraf": "^5.0.5",
90
- "stylelint": "^16.1.0",
91
- "tsx": "^4.7.0",
78
+ "prettier": "^3.4.2",
79
+ "rimraf": "^6.0.1",
80
+ "stylelint": "^16.11.0",
81
+ "tsx": "^4.19.2",
92
82
  "typescript": "~5.3.3",
93
- "vite": "5.4.8"
83
+ "vite": "^6.3.5",
84
+ "vue-tsc": "^2.1.10"
94
85
  },
95
86
  "peerDependencies": {
96
87
  "vite": ">=2"
@@ -100,7 +91,7 @@
100
91
  "build": "pnpm clean && tsup",
101
92
  "clean": "rimraf ./dist",
102
93
  "lint": "run-s lint:eslint lint:stylelint lint:prettier",
103
- "lint:eslint": "eslint \"{src,scripts,examples}/**/*.ts\" *.{cjs,ts} --fix --cache",
94
+ "lint:eslint": "eslint \"{src,scripts,examples}/**/*.{tsx,cjs,ts,vue}\" *.{cjs,ts} --fix --cache",
104
95
  "lint:stylelint": "stylelint \"examples/**/*.{css,scss,less,vue,html}\" --fix --cache",
105
96
  "lint:prettier": "prettier --write ."
106
97
  }