@tomjs/vite-plugin-vscode 2.5.3 → 2.5.4
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 +1 -1
- package/README.zh_CN.md +1 -1
- package/package.json +1 -1
- package/dist/client.global.js +0 -98
- package/dist/index.d.mts +0 -95
- package/dist/index.d.ts +0 -95
- package/dist/index.js +0 -397
- package/dist/index.mjs +0 -404
- package/dist/webview.d.mts +0 -13
- package/dist/webview.d.ts +0 -13
- package/dist/webview.js +0 -206
- package/dist/webview.mjs +0 -206
package/dist/index.js
DELETED
|
@@ -1,397 +0,0 @@
|
|
|
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
|
-
var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
3
|
-
var _os = require('os'); var _os2 = _interopRequireDefault(_os);
|
|
4
|
-
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
5
|
-
var _process = require('process');
|
|
6
|
-
var _lodashclonedeep = require('lodash.clonedeep'); var _lodashclonedeep2 = _interopRequireDefault(_lodashclonedeep);
|
|
7
|
-
var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
|
|
8
|
-
var _nodehtmlparser = require('node-html-parser');
|
|
9
|
-
var _tsup = require('tsup');
|
|
10
|
-
var _node = require('@tomjs/node');
|
|
11
|
-
|
|
12
|
-
// src/constants.ts
|
|
13
|
-
var PLUGIN_NAME = "tomjs:vscode";
|
|
14
|
-
var PACKAGE_NAME = "@tomjs/vite-plugin-vscode";
|
|
15
|
-
var WEBVIEW_METHOD_NAME = "__getWebviewHtml__";
|
|
16
|
-
|
|
17
|
-
// src/logger.ts
|
|
18
|
-
var _dayjs = require('dayjs'); var _dayjs2 = _interopRequireDefault(_dayjs);
|
|
19
|
-
var _kolorist = require('kolorist');
|
|
20
|
-
var Logger = class {
|
|
21
|
-
constructor(tag, withTime) {
|
|
22
|
-
this.tag = PLUGIN_NAME;
|
|
23
|
-
this.withTime = true;
|
|
24
|
-
this.tag = `[${tag}]`;
|
|
25
|
-
this.withTime = _nullishCoalesce(withTime, () => ( true));
|
|
26
|
-
}
|
|
27
|
-
getTime() {
|
|
28
|
-
return `${this.withTime ? _dayjs2.default.call(void 0, ).format("HH:mm:ss") : ""} `;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 调试
|
|
32
|
-
*/
|
|
33
|
-
debug(msg, ...rest) {
|
|
34
|
-
console.log(`${this.getTime()}${_kolorist.gray.call(void 0, this.tag)}`, msg, ...rest);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 调试日志 等同 debug
|
|
38
|
-
*/
|
|
39
|
-
log(msg, ...rest) {
|
|
40
|
-
this.debug(msg, ...rest);
|
|
41
|
-
}
|
|
42
|
-
info(msg, ...rest) {
|
|
43
|
-
console.log(`${this.getTime()}${_kolorist.blue.call(void 0, this.tag)}`, msg, ...rest);
|
|
44
|
-
}
|
|
45
|
-
warn(msg, ...rest) {
|
|
46
|
-
console.log(`${this.getTime()}${_kolorist.yellow.call(void 0, this.tag)}`, msg, ...rest);
|
|
47
|
-
}
|
|
48
|
-
error(msg, ...rest) {
|
|
49
|
-
console.log(`${this.getTime()}${_kolorist.red.call(void 0, this.tag)}`, msg, ...rest);
|
|
50
|
-
}
|
|
51
|
-
success(msg, ...rest) {
|
|
52
|
-
console.log(`${this.getTime()}${_kolorist.green.call(void 0, this.tag)}`, msg, ...rest);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
var createLogger = (tag) => {
|
|
56
|
-
return new Logger(tag || PLUGIN_NAME, true);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
// src/utils.ts
|
|
60
|
-
function resolveHostname(hostname) {
|
|
61
|
-
const loopbackHosts = /* @__PURE__ */ new Set([
|
|
62
|
-
"localhost",
|
|
63
|
-
"127.0.0.1",
|
|
64
|
-
"::1",
|
|
65
|
-
"0000:0000:0000:0000:0000:0000:0000:0001"
|
|
66
|
-
]);
|
|
67
|
-
const wildcardHosts = /* @__PURE__ */ new Set(["0.0.0.0", "::", "0000:0000:0000:0000:0000:0000:0000:0000"]);
|
|
68
|
-
return loopbackHosts.has(hostname) || wildcardHosts.has(hostname) ? "localhost" : hostname;
|
|
69
|
-
}
|
|
70
|
-
function resolveServerUrl(server) {
|
|
71
|
-
const addressInfo = server.httpServer.address();
|
|
72
|
-
const isAddressInfo = (x) => x == null ? void 0 : x.address;
|
|
73
|
-
if (isAddressInfo(addressInfo)) {
|
|
74
|
-
const { address, port } = addressInfo;
|
|
75
|
-
const hostname = resolveHostname(address);
|
|
76
|
-
const options = server.config.server;
|
|
77
|
-
const protocol = options.https ? "https" : "http";
|
|
78
|
-
const devBase = server.config.base;
|
|
79
|
-
const path2 = typeof options.open === "string" ? options.open : devBase;
|
|
80
|
-
const url = path2.startsWith("http") ? path2 : `${protocol}://${hostname}:${port}${path2}`;
|
|
81
|
-
return url;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// src/index.ts
|
|
86
|
-
var isDev = process.env.NODE_ENV === "development";
|
|
87
|
-
var logger = createLogger();
|
|
88
|
-
function getPkg() {
|
|
89
|
-
const pkgFile = _path2.default.resolve(process.cwd(), "package.json");
|
|
90
|
-
if (!_fs2.default.existsSync(pkgFile)) {
|
|
91
|
-
throw new Error("Main file is not specified, and no package.json found");
|
|
92
|
-
}
|
|
93
|
-
const pkg = _node.readJsonSync.call(void 0, pkgFile);
|
|
94
|
-
if (!pkg.main) {
|
|
95
|
-
throw new Error("Main file is not specified, please check package.json");
|
|
96
|
-
}
|
|
97
|
-
return pkg;
|
|
98
|
-
}
|
|
99
|
-
function preMergeOptions(options) {
|
|
100
|
-
const pkg = getPkg();
|
|
101
|
-
const opts = _lodashmerge2.default.call(void 0,
|
|
102
|
-
{
|
|
103
|
-
webview: true,
|
|
104
|
-
recommended: true,
|
|
105
|
-
debug: false,
|
|
106
|
-
extension: {
|
|
107
|
-
entry: "extension/index.ts",
|
|
108
|
-
outDir: "dist-extension",
|
|
109
|
-
target: ["es2019", "node14"],
|
|
110
|
-
format: "cjs",
|
|
111
|
-
shims: true,
|
|
112
|
-
clean: true,
|
|
113
|
-
dts: false,
|
|
114
|
-
treeshake: isDev ? false : "smallest",
|
|
115
|
-
outExtension() {
|
|
116
|
-
return { js: ".js" };
|
|
117
|
-
},
|
|
118
|
-
external: ["vscode"],
|
|
119
|
-
skipNodeModulesBundle: isDev
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
_lodashclonedeep2.default.call(void 0, options)
|
|
123
|
-
);
|
|
124
|
-
const opt = opts.extension || {};
|
|
125
|
-
["entry", "format"].forEach((prop) => {
|
|
126
|
-
const value = opt[prop];
|
|
127
|
-
if (!Array.isArray(value) && value) {
|
|
128
|
-
opt[prop] = [value];
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
if (isDev) {
|
|
132
|
-
opt.sourcemap = _nullishCoalesce(opt.sourcemap, () => ( true));
|
|
133
|
-
} else {
|
|
134
|
-
opt.minify ??= true;
|
|
135
|
-
}
|
|
136
|
-
opt.external = ["vscode"].concat(_nullishCoalesce(opt.external, () => ( [])));
|
|
137
|
-
if (!opt.skipNodeModulesBundle) {
|
|
138
|
-
opt.noExternal = Object.keys(pkg.dependencies || {}).concat(
|
|
139
|
-
Object.keys(pkg.peerDependencies || {})
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
opts.extension = opt;
|
|
143
|
-
if (opts.webview !== false) {
|
|
144
|
-
let name = WEBVIEW_METHOD_NAME;
|
|
145
|
-
if (typeof opts.webview === "string") {
|
|
146
|
-
name = _nullishCoalesce(opts.webview, () => ( WEBVIEW_METHOD_NAME));
|
|
147
|
-
}
|
|
148
|
-
opts.webview = Object.assign({ name }, opts.webview);
|
|
149
|
-
}
|
|
150
|
-
return opts;
|
|
151
|
-
}
|
|
152
|
-
var prodCachePkgName = `${PACKAGE_NAME}-inject`;
|
|
153
|
-
function genProdWebviewCode(cache, webview) {
|
|
154
|
-
webview = Object.assign({}, webview);
|
|
155
|
-
const prodCacheFolder = _path2.default.join(_process.cwd.call(void 0, ), "node_modules", prodCachePkgName);
|
|
156
|
-
_node.emptyDirSync.call(void 0, prodCacheFolder);
|
|
157
|
-
const destFile = _path2.default.join(prodCacheFolder, "index.ts");
|
|
158
|
-
function handleHtmlCode(html) {
|
|
159
|
-
const root = _nodehtmlparser.parse.call(void 0, html);
|
|
160
|
-
const head = root.querySelector("head");
|
|
161
|
-
if (!head) {
|
|
162
|
-
root == null ? void 0 : root.insertAdjacentHTML("beforeend", "<head></head>");
|
|
163
|
-
}
|
|
164
|
-
const csp = (webview == null ? void 0 : webview.csp) || `<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src {{cspSource}} 'unsafe-inline'; script-src 'nonce-{{nonce}}' 'unsafe-eval';">`;
|
|
165
|
-
head.insertAdjacentHTML("afterbegin", csp);
|
|
166
|
-
if (csp && csp.includes("{{nonce}}")) {
|
|
167
|
-
const tags = {
|
|
168
|
-
script: "src",
|
|
169
|
-
link: "href"
|
|
170
|
-
};
|
|
171
|
-
Object.keys(tags).forEach((tag) => {
|
|
172
|
-
const elements = root.querySelectorAll(tag);
|
|
173
|
-
elements.forEach((element) => {
|
|
174
|
-
const attr = element.getAttribute(tags[tag]);
|
|
175
|
-
if (attr) {
|
|
176
|
-
element.setAttribute(tags[tag], `{{baseUri}}${attr}`);
|
|
177
|
-
}
|
|
178
|
-
element.setAttribute("nonce", "{{nonce}}");
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return root.removeWhitespace().toString();
|
|
183
|
-
}
|
|
184
|
-
const cacheCode = (
|
|
185
|
-
/* js */
|
|
186
|
-
`const htmlCode = {
|
|
187
|
-
${Object.keys(cache).map((s) => `${s}: \`${handleHtmlCode(cache[s])}\`,`).join("\n")}
|
|
188
|
-
};`
|
|
189
|
-
);
|
|
190
|
-
const code = (
|
|
191
|
-
/* js */
|
|
192
|
-
`import { ExtensionContext, Uri, Webview } from 'vscode';
|
|
193
|
-
|
|
194
|
-
${cacheCode}
|
|
195
|
-
|
|
196
|
-
function uuid() {
|
|
197
|
-
let text = '';
|
|
198
|
-
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
199
|
-
for (let i = 0; i < 32; i++) {
|
|
200
|
-
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
201
|
-
}
|
|
202
|
-
return text;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export default function getWebviewHtml(webview: Webview, context: ExtensionContext, inputName?:string){
|
|
206
|
-
const nonce = uuid();
|
|
207
|
-
const baseUri = webview.asWebviewUri(Uri.joinPath(context.extensionUri, (process.env.VITE_WEBVIEW_DIST || 'dist')));
|
|
208
|
-
const html = htmlCode[inputName || 'index'] || '';
|
|
209
|
-
return html.replaceAll('{{cspSource}}', webview.cspSource).replaceAll('{{nonce}}', nonce).replaceAll('{{baseUri}}', baseUri);
|
|
210
|
-
}
|
|
211
|
-
`
|
|
212
|
-
);
|
|
213
|
-
_fs2.default.writeFileSync(destFile, code, { encoding: "utf8" });
|
|
214
|
-
return fixWindowsPath(destFile);
|
|
215
|
-
}
|
|
216
|
-
function fixWindowsPath(webviewPath) {
|
|
217
|
-
if (_os2.default.platform() === "win32") {
|
|
218
|
-
webviewPath = webviewPath.replaceAll("\\", "/");
|
|
219
|
-
}
|
|
220
|
-
return webviewPath;
|
|
221
|
-
}
|
|
222
|
-
function useVSCodePlugin(options) {
|
|
223
|
-
const opts = preMergeOptions(options);
|
|
224
|
-
const handleConfig = (config) => {
|
|
225
|
-
var _a, _b, _c, _d, _e;
|
|
226
|
-
let outDir = ((_a = config == null ? void 0 : config.build) == null ? void 0 : _a.outDir) || "dist";
|
|
227
|
-
opts.extension ??= {};
|
|
228
|
-
if (opts.recommended) {
|
|
229
|
-
opts.extension.outDir = _path2.default.resolve(outDir, "extension");
|
|
230
|
-
outDir = _path2.default.resolve(outDir, "webview");
|
|
231
|
-
}
|
|
232
|
-
const assetsDir = ((_b = config == null ? void 0 : config.build) == null ? void 0 : _b.assetsDir) || "assets";
|
|
233
|
-
const output = {
|
|
234
|
-
chunkFileNames: `${assetsDir}/[name].js`,
|
|
235
|
-
entryFileNames: `${assetsDir}/[name].js`,
|
|
236
|
-
assetFileNames: `${assetsDir}/[name].[ext]`
|
|
237
|
-
};
|
|
238
|
-
let rollupOutput = _nullishCoalesce(((_d = (_c = config == null ? void 0 : config.build) == null ? void 0 : _c.rollupOptions) == null ? void 0 : _d.output), () => ( {}));
|
|
239
|
-
if (Array.isArray(rollupOutput)) {
|
|
240
|
-
rollupOutput.map((s) => Object.assign(s, output));
|
|
241
|
-
} else {
|
|
242
|
-
rollupOutput = Object.assign({}, rollupOutput, output);
|
|
243
|
-
}
|
|
244
|
-
return {
|
|
245
|
-
build: {
|
|
246
|
-
outDir,
|
|
247
|
-
sourcemap: isDev ? true : (_e = config == null ? void 0 : config.build) == null ? void 0 : _e.sourcemap,
|
|
248
|
-
rollupOptions: {
|
|
249
|
-
output: rollupOutput
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
};
|
|
254
|
-
let devWebviewClient;
|
|
255
|
-
if (opts.webview) {
|
|
256
|
-
devWebviewClient = _node.readFileSync.call(void 0, _path2.default.join(__dirname, "client.global.js"));
|
|
257
|
-
}
|
|
258
|
-
let buildConfig;
|
|
259
|
-
const prodHtmlCache = {};
|
|
260
|
-
return [
|
|
261
|
-
{
|
|
262
|
-
name: "@tomjs:vscode",
|
|
263
|
-
apply: "serve",
|
|
264
|
-
config(config) {
|
|
265
|
-
return handleConfig(config);
|
|
266
|
-
},
|
|
267
|
-
configureServer(server) {
|
|
268
|
-
var _a;
|
|
269
|
-
if (!server || !server.httpServer) {
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
(_a = server.httpServer) == null ? void 0 : _a.once("listening", async () => {
|
|
273
|
-
const env = {
|
|
274
|
-
NODE_ENV: server.config.mode || "development",
|
|
275
|
-
VITE_DEV_SERVER_URL: resolveServerUrl(server)
|
|
276
|
-
};
|
|
277
|
-
logger.info("extension build start");
|
|
278
|
-
let buildCount = 0;
|
|
279
|
-
const webview = opts == null ? void 0 : opts.webview;
|
|
280
|
-
const { onSuccess: _onSuccess, ...tsupOptions } = opts.extension || {};
|
|
281
|
-
await _tsup.build.call(void 0,
|
|
282
|
-
_lodashmerge2.default.call(void 0, tsupOptions, {
|
|
283
|
-
watch: true,
|
|
284
|
-
env,
|
|
285
|
-
silent: true,
|
|
286
|
-
esbuildPlugins: !webview ? [] : [
|
|
287
|
-
{
|
|
288
|
-
name: "@tomjs:vscode:inject",
|
|
289
|
-
setup(build) {
|
|
290
|
-
build.onLoad({ filter: /\.ts$/ }, async (args) => {
|
|
291
|
-
const file = _fs2.default.readFileSync(args.path, "utf-8");
|
|
292
|
-
if (file.includes(`${webview.name}(`)) {
|
|
293
|
-
return {
|
|
294
|
-
contents: `import ${webview.name} from '${PACKAGE_NAME}/webview';
|
|
295
|
-
` + file,
|
|
296
|
-
loader: "ts"
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
return {};
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
],
|
|
304
|
-
async onSuccess() {
|
|
305
|
-
if (typeof _onSuccess === "function") {
|
|
306
|
-
await _onSuccess();
|
|
307
|
-
}
|
|
308
|
-
if (buildCount++ > 1) {
|
|
309
|
-
logger.info("extension rebuild success");
|
|
310
|
-
} else {
|
|
311
|
-
logger.info("extension build success");
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
})
|
|
315
|
-
);
|
|
316
|
-
});
|
|
317
|
-
},
|
|
318
|
-
transformIndexHtml(html) {
|
|
319
|
-
if (!opts.webview) {
|
|
320
|
-
return html;
|
|
321
|
-
}
|
|
322
|
-
return html.replace(/<\/title>/i, `</title><script>${devWebviewClient}</script>`);
|
|
323
|
-
}
|
|
324
|
-
},
|
|
325
|
-
{
|
|
326
|
-
name: "@tomjs:vscode",
|
|
327
|
-
apply: "build",
|
|
328
|
-
enforce: "post",
|
|
329
|
-
config(config) {
|
|
330
|
-
return handleConfig(config);
|
|
331
|
-
},
|
|
332
|
-
configResolved(config) {
|
|
333
|
-
buildConfig = config;
|
|
334
|
-
},
|
|
335
|
-
transformIndexHtml(html, ctx) {
|
|
336
|
-
var _a;
|
|
337
|
-
if (!opts.webview) {
|
|
338
|
-
return html;
|
|
339
|
-
}
|
|
340
|
-
prodHtmlCache[(_a = ctx.chunk) == null ? void 0 : _a.name] = html;
|
|
341
|
-
return html;
|
|
342
|
-
},
|
|
343
|
-
closeBundle() {
|
|
344
|
-
let webviewPath;
|
|
345
|
-
const webview = opts == null ? void 0 : opts.webview;
|
|
346
|
-
if (webview) {
|
|
347
|
-
webviewPath = genProdWebviewCode(prodHtmlCache, webview);
|
|
348
|
-
}
|
|
349
|
-
let outDir = buildConfig.build.outDir.replace(_process.cwd.call(void 0, ), "").replaceAll("\\", "/");
|
|
350
|
-
if (outDir.startsWith("/")) {
|
|
351
|
-
outDir = outDir.substring(1);
|
|
352
|
-
}
|
|
353
|
-
const env = {
|
|
354
|
-
NODE_ENV: buildConfig.mode || "production",
|
|
355
|
-
VITE_WEBVIEW_DIST: outDir
|
|
356
|
-
};
|
|
357
|
-
logger.info("extension build start");
|
|
358
|
-
const { onSuccess: _onSuccess, ...tsupOptions } = opts.extension || {};
|
|
359
|
-
_tsup.build.call(void 0,
|
|
360
|
-
_lodashmerge2.default.call(void 0, tsupOptions, {
|
|
361
|
-
env,
|
|
362
|
-
silent: true,
|
|
363
|
-
esbuildPlugins: !webview ? [] : [
|
|
364
|
-
{
|
|
365
|
-
name: "@tomjs:vscode:inject",
|
|
366
|
-
setup(build) {
|
|
367
|
-
build.onLoad({ filter: /\.ts$/ }, async (args) => {
|
|
368
|
-
const file = _fs2.default.readFileSync(args.path, "utf-8");
|
|
369
|
-
if (file.includes(`${webview.name}(`)) {
|
|
370
|
-
return {
|
|
371
|
-
contents: `import ${webview.name} from \`${webviewPath}\`;
|
|
372
|
-
` + file,
|
|
373
|
-
loader: "ts"
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
return {};
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
],
|
|
381
|
-
async onSuccess() {
|
|
382
|
-
if (typeof _onSuccess === "function") {
|
|
383
|
-
await _onSuccess();
|
|
384
|
-
}
|
|
385
|
-
logger.info("extension build success");
|
|
386
|
-
}
|
|
387
|
-
})
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
];
|
|
392
|
-
}
|
|
393
|
-
var src_default = useVSCodePlugin;
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
exports.default = src_default; exports.useVSCodePlugin = useVSCodePlugin;
|