@tomjs/vite-plugin-vscode 2.0.1 → 2.2.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
@@ -30,19 +30,15 @@ yarn add @tomjs/vite-plugin-vscode -D
30
30
  npm i @tomjs/vite-plugin-vscode --save-dev
31
31
  ```
32
32
 
33
- If you use `pnpm` and enable `webview` debugging, you need to additionally install the dependency [@tomjs/vscode-extension-webview](https://github.com/tomjs/vscode-extension-webview)
34
-
35
- ```bash
36
- pnpm add @tomjs/vscode-extension-webview -D
37
- ```
38
-
39
33
  ## Usage
40
34
 
41
- ### Recommended Agreement
35
+ ### Recommended
36
+
37
+ Setting `recommended` will modify some preset configurations. See [PluginOptions](#pluginoptions) and `recommended` parameter descriptions in detail.
42
38
 
43
39
  #### Directory Structure
44
40
 
45
- - Recommend `extension` and page `src` code directory structure
41
+ - By default, `recommended:true` will be based on the following directory structure as a convention.
46
42
 
47
43
  ```
48
44
  |--extension // extension code
@@ -50,6 +46,7 @@ pnpm add @tomjs/vscode-extension-webview -D
50
46
  |--src // front-end code
51
47
  | |--App.vue
52
48
  | |--main.ts
49
+ |--index.html
53
50
  ```
54
51
 
55
52
  - Zero configuration, default dist output directory
@@ -63,9 +60,16 @@ pnpm add @tomjs/vscode-extension-webview -D
63
60
  | | |--index.html
64
61
  ```
65
62
 
66
- #### Default configuration and behavior
63
+ - If you want to modify the `extension` source code directory to `src`, you can set `{ extension: { entry: 'src/index.ts' } }`
67
64
 
68
- See [PluginOptions](#pluginoptions) and `recommended` parameter descriptions in detail
65
+ ```
66
+ |--src // extension code
67
+ | |--index.ts
68
+ |--webview // front-end code
69
+ | |--App.vue
70
+ | |--main.ts
71
+ |--index.html
72
+ ```
69
73
 
70
74
  ### extension
71
75
 
@@ -111,6 +115,8 @@ export default defineConfig({
111
115
  },
112
116
  }),
113
117
  vscode(),
118
+ // Modify the extension source code entry path, and also modify the `index.html` entry file path
119
+ // vscode({ extension: { entry: 'src/index.ts' } }),
114
120
  ],
115
121
  });
116
122
  ```
package/README.zh_CN.md CHANGED
@@ -30,19 +30,15 @@ yarn add @tomjs/vite-plugin-vscode -D
30
30
  npm i @tomjs/vite-plugin-vscode --save-dev
31
31
  ```
32
32
 
33
- 如果使用 `pnpm` 且开启 `webview` 调试,需要额外安装[@tomjs/vscode-extension-webview](https://github.com/tomjs/vscode-extension-webview) 这个依赖
34
-
35
- ```bash
36
- pnpm add @tomjs/vscode-extension-webview -D
37
- ```
38
-
39
33
  ## 使用说明
40
34
 
41
35
  ### 推荐约定
42
36
 
37
+ 设置 `recommended` 参数会修改一些预置配置,详细查看 [PluginOptions](#pluginoptions) 和 `recommended` 参数说明。
38
+
43
39
  #### 目录结构
44
40
 
45
- - 推荐 `extension` 和 页面 `src` 代码目录结构
41
+ - 默认情况下,`recommended:true` 会根据如下目录结构作为约定
46
42
 
47
43
  ```
48
44
  |--extension // extension code
@@ -63,9 +59,15 @@ pnpm add @tomjs/vscode-extension-webview -D
63
59
  | | |--index.html
64
60
  ```
65
61
 
66
- #### 默认配置和行为
62
+ - 如果你想修改 `extension` 源码目录为 `src`,可以设置 `{ extension: { entry: 'src/index.ts' } }`
67
63
 
68
- 详细查看 [PluginOptions](#pluginoptions) 和 `recommended` 参数说明
64
+ ```
65
+ |--src // extension code
66
+ | |--index.ts
67
+ |--webview // front-end code
68
+ | |--App.vue
69
+ | |--main.ts
70
+ ```
69
71
 
70
72
  ### extension
71
73
 
@@ -115,6 +117,8 @@ export default defineConfig({
115
117
  },
116
118
  }),
117
119
  vscode(),
120
+ // 修改扩展源码入口路径,同时修改`index.html`入口文件路径
121
+ // vscode({ extension: { entry: 'src/index.ts' } }),
118
122
  ],
119
123
  });
120
124
  ```
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ var _nodehtmlparser = require('node-html-parser');
8
8
  var _tsup = require('tsup');
9
9
 
10
10
  // src/constants.ts
11
- var PLUGIN_NAME = "@tomjs:vscode";
11
+ var PLUGIN_NAME = "tomjs:vscode";
12
12
  var PACKAGE_NAME = "@tomjs/vite-plugin-vscode";
13
13
  var WEBVIEW_PACKAGE_NAME = "@tomjs/vscode-extension-webview";
14
14
  var WEBVIEW_METHOD_NAME = "__getWebviewHtml__";
@@ -56,11 +56,14 @@ var createLogger = (tag) => {
56
56
  };
57
57
 
58
58
  // src/utils.ts
59
+ var _child_process = require('child_process');
59
60
 
60
61
  var _module = require('module');
61
- function readJson(path2) {
62
- if (_fs2.default.existsSync(path2)) {
63
- return JSON.parse(_fs2.default.readFileSync(path2, "utf8"));
62
+
63
+
64
+ function readJson(path3) {
65
+ if (_fs2.default.existsSync(path3)) {
66
+ return JSON.parse(_fs2.default.readFileSync(path3, "utf8"));
64
67
  }
65
68
  }
66
69
  function emptyPath(dest) {
@@ -88,11 +91,46 @@ function resolveServerUrl(server) {
88
91
  const options = server.config.server;
89
92
  const protocol = options.https ? "https" : "http";
90
93
  const devBase = server.config.base;
91
- const path2 = typeof options.open === "string" ? options.open : devBase;
92
- const url = path2.startsWith("http") ? path2 : `${protocol}://${hostname}:${port}${path2}`;
94
+ const path3 = typeof options.open === "string" ? options.open : devBase;
95
+ const url = path3.startsWith("http") ? path3 : `${protocol}://${hostname}:${port}${path3}`;
93
96
  return url;
94
97
  }
95
98
  }
99
+ function getWebviewNpmPath() {
100
+ let npmPath = _path2.default.join(_process.cwd.call(void 0, ), "node_modules", WEBVIEW_PACKAGE_NAME);
101
+ if (!_fs2.default.existsSync(npmPath)) {
102
+ try {
103
+ const res = _child_process.spawnSync.call(void 0,
104
+ process.platform === "win32" ? "pnpm.cmd" : "pnpm",
105
+ ["list", "--dev", "--depth=1", "--json"],
106
+ {
107
+ // stdio: ['inherit', 'ignore'],
108
+ cwd: process.cwd(),
109
+ encoding: "utf-8"
110
+ }
111
+ );
112
+ if (res.status === 0 && res.stdout) {
113
+ const list = JSON.parse(res.stdout.trim());
114
+ if (list.length === 0) {
115
+ return;
116
+ }
117
+ const self = (list[0].devDependencies || {})[PACKAGE_NAME];
118
+ if (!self) {
119
+ return;
120
+ }
121
+ const dep = self.dependencies[WEBVIEW_PACKAGE_NAME];
122
+ if (dep) {
123
+ npmPath = dep.path;
124
+ }
125
+ }
126
+ } catch (e) {
127
+ npmPath = "";
128
+ }
129
+ if (npmPath) {
130
+ return _path2.default.join(npmPath, "dist");
131
+ }
132
+ }
133
+ }
96
134
 
97
135
  // src/index.ts
98
136
  var isDev = process.env.NODE_ENV === "development";
@@ -259,8 +297,8 @@ function useVSCodePlugin(options) {
259
297
  let webviewClient;
260
298
  let webviewNpmPath;
261
299
  if (opts.webview) {
262
- webviewNpmPath = _path2.default.join(_process.cwd.call(void 0, ), "node_modules", WEBVIEW_PACKAGE_NAME, "/dist");
263
- if (!_fs2.default.existsSync(webviewNpmPath)) {
300
+ webviewNpmPath = getWebviewNpmPath();
301
+ if (!webviewNpmPath || !_fs2.default.existsSync(webviewNpmPath)) {
264
302
  logger.warn(`[${WEBVIEW_PACKAGE_NAME}] is not installed, please install it first!`);
265
303
  } else {
266
304
  const fileName = "client.global.js";
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  // src/index.ts
2
2
  import fs2 from "fs";
3
- import path from "path";
4
- import { cwd } from "process";
3
+ import path2 from "path";
4
+ import { cwd as cwd2 } from "process";
5
5
  import cloneDeep from "lodash.clonedeep";
6
6
  import merge from "lodash.merge";
7
7
  import { parse as htmlParser } from "node-html-parser";
8
8
  import { build as tsupBuild } from "tsup";
9
9
 
10
10
  // src/constants.ts
11
- var PLUGIN_NAME = "@tomjs:vscode";
11
+ var PLUGIN_NAME = "tomjs:vscode";
12
12
  var PACKAGE_NAME = "@tomjs/vite-plugin-vscode";
13
13
  var WEBVIEW_PACKAGE_NAME = "@tomjs/vscode-extension-webview";
14
14
  var WEBVIEW_METHOD_NAME = "__getWebviewHtml__";
@@ -56,10 +56,13 @@ var createLogger = (tag) => {
56
56
  };
57
57
 
58
58
  // src/utils.ts
59
+ import { spawnSync } from "child_process";
59
60
  import fs from "fs";
60
- function readJson(path2) {
61
- if (fs.existsSync(path2)) {
62
- return JSON.parse(fs.readFileSync(path2, "utf8"));
61
+ import path from "path";
62
+ import { cwd } from "process";
63
+ function readJson(path3) {
64
+ if (fs.existsSync(path3)) {
65
+ return JSON.parse(fs.readFileSync(path3, "utf8"));
63
66
  }
64
67
  }
65
68
  function emptyPath(dest) {
@@ -87,17 +90,52 @@ function resolveServerUrl(server) {
87
90
  const options = server.config.server;
88
91
  const protocol = options.https ? "https" : "http";
89
92
  const devBase = server.config.base;
90
- const path2 = typeof options.open === "string" ? options.open : devBase;
91
- const url = path2.startsWith("http") ? path2 : `${protocol}://${hostname}:${port}${path2}`;
93
+ const path3 = typeof options.open === "string" ? options.open : devBase;
94
+ const url = path3.startsWith("http") ? path3 : `${protocol}://${hostname}:${port}${path3}`;
92
95
  return url;
93
96
  }
94
97
  }
98
+ function getWebviewNpmPath() {
99
+ let npmPath = path.join(cwd(), "node_modules", WEBVIEW_PACKAGE_NAME);
100
+ if (!fs.existsSync(npmPath)) {
101
+ try {
102
+ const res = spawnSync(
103
+ process.platform === "win32" ? "pnpm.cmd" : "pnpm",
104
+ ["list", "--dev", "--depth=1", "--json"],
105
+ {
106
+ // stdio: ['inherit', 'ignore'],
107
+ cwd: process.cwd(),
108
+ encoding: "utf-8"
109
+ }
110
+ );
111
+ if (res.status === 0 && res.stdout) {
112
+ const list = JSON.parse(res.stdout.trim());
113
+ if (list.length === 0) {
114
+ return;
115
+ }
116
+ const self = (list[0].devDependencies || {})[PACKAGE_NAME];
117
+ if (!self) {
118
+ return;
119
+ }
120
+ const dep = self.dependencies[WEBVIEW_PACKAGE_NAME];
121
+ if (dep) {
122
+ npmPath = dep.path;
123
+ }
124
+ }
125
+ } catch {
126
+ npmPath = "";
127
+ }
128
+ if (npmPath) {
129
+ return path.join(npmPath, "dist");
130
+ }
131
+ }
132
+ }
95
133
 
96
134
  // src/index.ts
97
135
  var isDev = process.env.NODE_ENV === "development";
98
136
  var logger = createLogger();
99
137
  function getPkg() {
100
- const pkgFile = path.resolve(process.cwd(), "package.json");
138
+ const pkgFile = path2.resolve(process.cwd(), "package.json");
101
139
  if (!fs2.existsSync(pkgFile)) {
102
140
  throw new Error("Main file is not specified, and no package.json found");
103
141
  }
@@ -161,9 +199,9 @@ function preMergeOptions(options) {
161
199
  }
162
200
  var prodCachePkgName = `${PACKAGE_NAME}-inject`;
163
201
  function genProdWebviewCode(cache) {
164
- const prodCacheFolder = path.join(cwd(), "node_modules", prodCachePkgName);
202
+ const prodCacheFolder = path2.join(cwd2(), "node_modules", prodCachePkgName);
165
203
  emptyPath(prodCacheFolder);
166
- const destFile = path.join(prodCacheFolder, "index.ts");
204
+ const destFile = path2.join(prodCacheFolder, "index.ts");
167
205
  function handleHtmlCode(html) {
168
206
  const root = htmlParser(html);
169
207
  const head = root.querySelector("head");
@@ -230,8 +268,8 @@ function useVSCodePlugin(options) {
230
268
  let outDir = ((_a = config == null ? void 0 : config.build) == null ? void 0 : _a.outDir) || "dist";
231
269
  opts.extension ??= {};
232
270
  if (opts.recommended) {
233
- opts.extension.outDir = path.resolve(outDir, "extension");
234
- outDir = path.resolve(outDir, "webview");
271
+ opts.extension.outDir = path2.resolve(outDir, "extension");
272
+ outDir = path2.resolve(outDir, "webview");
235
273
  }
236
274
  const assetsDir = ((_b = config == null ? void 0 : config.build) == null ? void 0 : _b.assetsDir) || "assets";
237
275
  const output = {
@@ -258,12 +296,12 @@ function useVSCodePlugin(options) {
258
296
  let webviewClient;
259
297
  let webviewNpmPath;
260
298
  if (opts.webview) {
261
- webviewNpmPath = path.join(cwd(), "node_modules", WEBVIEW_PACKAGE_NAME, "/dist");
262
- if (!fs2.existsSync(webviewNpmPath)) {
299
+ webviewNpmPath = getWebviewNpmPath();
300
+ if (!webviewNpmPath || !fs2.existsSync(webviewNpmPath)) {
263
301
  logger.warn(`[${WEBVIEW_PACKAGE_NAME}] is not installed, please install it first!`);
264
302
  } else {
265
303
  const fileName = "client.global.js";
266
- const clientFile = path.join(webviewNpmPath, fileName);
304
+ const clientFile = path2.join(webviewNpmPath, fileName);
267
305
  if (!fs2.existsSync(clientFile)) {
268
306
  logger.warn(`[${fileName}] is does not exist, please update the package!`);
269
307
  } else {
@@ -360,7 +398,7 @@ function useVSCodePlugin(options) {
360
398
  if (opts.webview) {
361
399
  webviewPath = genProdWebviewCode(prodHtmlCache);
362
400
  }
363
- let outDir = buildConfig.build.outDir.replace(cwd(), "").replaceAll("\\", "/");
401
+ let outDir = buildConfig.build.outDir.replace(cwd2(), "").replaceAll("\\", "/");
364
402
  if (outDir.startsWith("/")) {
365
403
  outDir = outDir.substring(1);
366
404
  }
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-vscode",
3
- "version": "2.0.1",
3
+ "version": "2.2.0",
4
4
  "description": "Use vue/react to develop 'vscode extension webview', supporting esm/cjs",
5
5
  "keywords": [
6
6
  "vite",
7
7
  "plugin",
8
8
  "vscode",
9
+ "html",
10
+ "hmr",
9
11
  "extension",
10
12
  "webview",
11
13
  "esm",