@tomjs/vite-plugin-vscode 2.3.0 → 2.3.1

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/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/index.ts
2
2
  var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
3
+ var _os = require('os'); var _os2 = _interopRequireDefault(_os);
3
4
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
4
5
  var _process = require('process');
5
6
  var _lodashclonedeep = require('lodash.clonedeep'); var _lodashclonedeep2 = _interopRequireDefault(_lodashclonedeep);
@@ -56,11 +57,11 @@ var createLogger = (tag) => {
56
57
  };
57
58
 
58
59
  // src/utils.ts
59
- var _child_process = require('child_process');
60
60
 
61
61
  var _module = require('module');
62
62
 
63
63
 
64
+ var _execa = require('execa'); var _execa2 = _interopRequireDefault(_execa);
64
65
  function readJson(path3) {
65
66
  if (_fs2.default.existsSync(path3)) {
66
67
  return JSON.parse(_fs2.default.readFileSync(path3, "utf8"));
@@ -96,40 +97,32 @@ function resolveServerUrl(server) {
96
97
  return url;
97
98
  }
98
99
  }
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
- }
100
+ function getWebviewPnpmPath() {
101
+ try {
102
+ const res = _execa2.default.sync("pnpm", ["list", "--dev", "--depth=1", "--json"], {});
103
+ if (res.stdout) {
104
+ const list = JSON.parse(res.stdout.trim());
105
+ if (list.length === 0) {
106
+ return;
107
+ }
108
+ const self = (list[0].devDependencies || {})[PACKAGE_NAME];
109
+ if (!self) {
110
+ return;
111
+ }
112
+ const dep = self.dependencies[WEBVIEW_PACKAGE_NAME];
113
+ if (dep) {
114
+ return dep.path;
125
115
  }
126
- } catch (e) {
127
- npmPath = "";
128
- }
129
- if (npmPath) {
130
- return _path2.default.join(npmPath, "dist");
131
116
  }
117
+ } catch (e) {
118
+ }
119
+ }
120
+ function getWebviewNpmPath() {
121
+ const npmPath = _path2.default.join(_process.cwd.call(void 0, ), "node_modules", WEBVIEW_PACKAGE_NAME);
122
+ if (_fs2.default.existsSync(npmPath)) {
123
+ return npmPath;
132
124
  }
125
+ return getWebviewPnpmPath();
133
126
  }
134
127
 
135
128
  // src/index.ts
@@ -263,7 +256,13 @@ export default function getWebviewHtml(webview: Webview, context: ExtensionConte
263
256
  `
264
257
  );
265
258
  _fs2.default.writeFileSync(destFile, code, { encoding: "utf8" });
266
- return destFile.replaceAll("\\", "/");
259
+ return fixWindowsPath(destFile);
260
+ }
261
+ function fixWindowsPath(webviewPath) {
262
+ if (_os2.default.platform() === "win32") {
263
+ webviewPath = webviewPath.replaceAll("\\", "/");
264
+ }
265
+ return webviewPath;
267
266
  }
268
267
  function useVSCodePlugin(options) {
269
268
  const opts = preMergeOptions(options);
@@ -297,19 +296,22 @@ function useVSCodePlugin(options) {
297
296
  }
298
297
  };
299
298
  };
300
- let webviewClient;
301
- let webviewNpmPath;
299
+ let devWebviewClient;
300
+ let devWebviewPath;
302
301
  if (opts.webview) {
303
- webviewNpmPath = getWebviewNpmPath();
304
- if (!webviewNpmPath || !_fs2.default.existsSync(webviewNpmPath)) {
302
+ devWebviewPath = getWebviewNpmPath();
303
+ if (devWebviewPath && _os2.default.platform() === "win32") {
304
+ devWebviewPath = devWebviewPath.replaceAll("\\", "/");
305
+ }
306
+ if (!devWebviewPath || !_fs2.default.existsSync(devWebviewPath)) {
305
307
  logger.warn(`[${WEBVIEW_PACKAGE_NAME}] is not installed, please install it first!`);
306
308
  } else {
307
309
  const fileName = "client.global.js";
308
- const clientFile = _path2.default.join(webviewNpmPath, fileName);
310
+ const clientFile = _path2.default.join(devWebviewPath, "dist", fileName);
309
311
  if (!_fs2.default.existsSync(clientFile)) {
310
312
  logger.warn(`[${fileName}] is does not exist, please update the package!`);
311
313
  } else {
312
- webviewClient = _fs2.default.readFileSync(clientFile, "utf-8");
314
+ devWebviewClient = _fs2.default.readFileSync(clientFile, "utf-8");
313
315
  }
314
316
  }
315
317
  }
@@ -349,7 +351,7 @@ function useVSCodePlugin(options) {
349
351
  const file = _fs2.default.readFileSync(args.path, "utf-8");
350
352
  if (file.includes(`${webview.name}(`)) {
351
353
  return {
352
- contents: `import ${webview.name} from '@tomjs/vscode-extension-webview';
354
+ contents: `import ${webview.name} from '${devWebviewPath}';
353
355
  ` + file,
354
356
  loader: "ts"
355
357
  };
@@ -374,10 +376,10 @@ function useVSCodePlugin(options) {
374
376
  });
375
377
  },
376
378
  transformIndexHtml(html) {
377
- if (!opts.webview || !webviewClient) {
379
+ if (!opts.webview || !devWebviewClient) {
378
380
  return html;
379
381
  }
380
- return html.replace(/<\/title>/i, `</title><script>${webviewClient}</script>`);
382
+ return html.replace(/<\/title>/i, `</title><script>${devWebviewClient}</script>`);
381
383
  }
382
384
  },
383
385
  {
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/index.ts
2
2
  import fs2 from "fs";
3
+ import os from "os";
3
4
  import path2 from "path";
4
5
  import { cwd as cwd2 } from "process";
5
6
  import cloneDeep from "lodash.clonedeep";
@@ -56,10 +57,10 @@ var createLogger = (tag) => {
56
57
  };
57
58
 
58
59
  // src/utils.ts
59
- import { spawnSync } from "child_process";
60
60
  import fs from "fs";
61
61
  import path from "path";
62
62
  import { cwd } from "process";
63
+ import execa from "execa";
63
64
  function readJson(path3) {
64
65
  if (fs.existsSync(path3)) {
65
66
  return JSON.parse(fs.readFileSync(path3, "utf8"));
@@ -95,40 +96,32 @@ function resolveServerUrl(server) {
95
96
  return url;
96
97
  }
97
98
  }
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
- }
99
+ function getWebviewPnpmPath() {
100
+ try {
101
+ const res = execa.sync("pnpm", ["list", "--dev", "--depth=1", "--json"], {});
102
+ if (res.stdout) {
103
+ const list = JSON.parse(res.stdout.trim());
104
+ if (list.length === 0) {
105
+ return;
106
+ }
107
+ const self = (list[0].devDependencies || {})[PACKAGE_NAME];
108
+ if (!self) {
109
+ return;
110
+ }
111
+ const dep = self.dependencies[WEBVIEW_PACKAGE_NAME];
112
+ if (dep) {
113
+ return dep.path;
124
114
  }
125
- } catch {
126
- npmPath = "";
127
- }
128
- if (npmPath) {
129
- return path.join(npmPath, "dist");
130
115
  }
116
+ } catch {
117
+ }
118
+ }
119
+ function getWebviewNpmPath() {
120
+ const npmPath = path.join(cwd(), "node_modules", WEBVIEW_PACKAGE_NAME);
121
+ if (fs.existsSync(npmPath)) {
122
+ return npmPath;
131
123
  }
124
+ return getWebviewPnpmPath();
132
125
  }
133
126
 
134
127
  // src/index.ts
@@ -262,7 +255,13 @@ export default function getWebviewHtml(webview: Webview, context: ExtensionConte
262
255
  `
263
256
  );
264
257
  fs2.writeFileSync(destFile, code, { encoding: "utf8" });
265
- return destFile.replaceAll("\\", "/");
258
+ return fixWindowsPath(destFile);
259
+ }
260
+ function fixWindowsPath(webviewPath) {
261
+ if (os.platform() === "win32") {
262
+ webviewPath = webviewPath.replaceAll("\\", "/");
263
+ }
264
+ return webviewPath;
266
265
  }
267
266
  function useVSCodePlugin(options) {
268
267
  const opts = preMergeOptions(options);
@@ -296,19 +295,22 @@ function useVSCodePlugin(options) {
296
295
  }
297
296
  };
298
297
  };
299
- let webviewClient;
300
- let webviewNpmPath;
298
+ let devWebviewClient;
299
+ let devWebviewPath;
301
300
  if (opts.webview) {
302
- webviewNpmPath = getWebviewNpmPath();
303
- if (!webviewNpmPath || !fs2.existsSync(webviewNpmPath)) {
301
+ devWebviewPath = getWebviewNpmPath();
302
+ if (devWebviewPath && os.platform() === "win32") {
303
+ devWebviewPath = devWebviewPath.replaceAll("\\", "/");
304
+ }
305
+ if (!devWebviewPath || !fs2.existsSync(devWebviewPath)) {
304
306
  logger.warn(`[${WEBVIEW_PACKAGE_NAME}] is not installed, please install it first!`);
305
307
  } else {
306
308
  const fileName = "client.global.js";
307
- const clientFile = path2.join(webviewNpmPath, fileName);
309
+ const clientFile = path2.join(devWebviewPath, "dist", fileName);
308
310
  if (!fs2.existsSync(clientFile)) {
309
311
  logger.warn(`[${fileName}] is does not exist, please update the package!`);
310
312
  } else {
311
- webviewClient = fs2.readFileSync(clientFile, "utf-8");
313
+ devWebviewClient = fs2.readFileSync(clientFile, "utf-8");
312
314
  }
313
315
  }
314
316
  }
@@ -348,7 +350,7 @@ function useVSCodePlugin(options) {
348
350
  const file = fs2.readFileSync(args.path, "utf-8");
349
351
  if (file.includes(`${webview.name}(`)) {
350
352
  return {
351
- contents: `import ${webview.name} from '@tomjs/vscode-extension-webview';
353
+ contents: `import ${webview.name} from '${devWebviewPath}';
352
354
  ` + file,
353
355
  loader: "ts"
354
356
  };
@@ -373,10 +375,10 @@ function useVSCodePlugin(options) {
373
375
  });
374
376
  },
375
377
  transformIndexHtml(html) {
376
- if (!opts.webview || !webviewClient) {
378
+ if (!opts.webview || !devWebviewClient) {
377
379
  return html;
378
380
  }
379
- return html.replace(/<\/title>/i, `</title><script>${webviewClient}</script>`);
381
+ return html.replace(/<\/title>/i, `</title><script>${devWebviewClient}</script>`);
380
382
  }
381
383
  },
382
384
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-vscode",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "Use vue/react to develop 'vscode extension webview', supporting esm/cjs",
5
5
  "keywords": [
6
6
  "vite",
@@ -46,6 +46,7 @@
46
46
  "dependencies": {
47
47
  "@tomjs/vscode-extension-webview": "^1.2.0",
48
48
  "dayjs": "^1.11.10",
49
+ "execa": "^5.1.1",
49
50
  "kolorist": "^1.8.0",
50
51
  "lodash.clonedeep": "^4.5.0",
51
52
  "lodash.merge": "^4.6.2",