vite-plugin-blocklet 0.7.4 → 0.7.6
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.cjs +170 -35
- package/index.js +25 -11
- package/libs/client.js +42 -1
- package/libs/config.js +2 -2
- package/libs/debug.js +81 -0
- package/libs/hmr.js +7 -0
- package/libs/loading.js +12 -18
- package/libs/meta.js +1 -1
- package/package.json +3 -1
package/dist/index.cjs
CHANGED
|
@@ -8,11 +8,13 @@ var semver = require('semver');
|
|
|
8
8
|
var Mcrypto = require('@ocap/mcrypto');
|
|
9
9
|
var util = require('@ocap/util');
|
|
10
10
|
var did = require('@arcblock/did');
|
|
11
|
-
var fs = require('fs');
|
|
12
|
-
var path = require('path');
|
|
11
|
+
var fs = require('node:fs');
|
|
12
|
+
var path = require('node:path');
|
|
13
13
|
var YAML = require('yaml');
|
|
14
|
+
var isMobile = require('ismobilejs');
|
|
14
15
|
var getPort = require('get-port');
|
|
15
16
|
var mri = require('mri');
|
|
17
|
+
var dotenv = require('dotenv');
|
|
16
18
|
|
|
17
19
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
18
20
|
|
|
@@ -21,8 +23,10 @@ var Mcrypto__default = /*#__PURE__*/_interopDefaultLegacy(Mcrypto);
|
|
|
21
23
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
22
24
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
23
25
|
var YAML__default = /*#__PURE__*/_interopDefaultLegacy(YAML);
|
|
26
|
+
var isMobile__default = /*#__PURE__*/_interopDefaultLegacy(isMobile);
|
|
24
27
|
var getPort__default = /*#__PURE__*/_interopDefaultLegacy(getPort);
|
|
25
28
|
var mri__default = /*#__PURE__*/_interopDefaultLegacy(mri);
|
|
29
|
+
var dotenv__default = /*#__PURE__*/_interopDefaultLegacy(dotenv);
|
|
26
30
|
|
|
27
31
|
const { types } = Mcrypto__default["default"];
|
|
28
32
|
|
|
@@ -37,6 +41,13 @@ function toBlockletDid(name) {
|
|
|
37
41
|
|
|
38
42
|
const isInBlocklet = !!process.env.BLOCKLET_PORT;
|
|
39
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Creates a HMR plugin with the given options.
|
|
46
|
+
*
|
|
47
|
+
* @param {Object} options - The options for the HMR plugin.
|
|
48
|
+
* @param {string} options.version - The version of the vite version.
|
|
49
|
+
* @return {Object} The HMR plugin object.
|
|
50
|
+
*/
|
|
40
51
|
function createHmrPlugin(options = {}) {
|
|
41
52
|
const { version = vite.version } = options;
|
|
42
53
|
return {
|
|
@@ -59,7 +70,7 @@ function createHmrPlugin(options = {}) {
|
|
|
59
70
|
};
|
|
60
71
|
}
|
|
61
72
|
|
|
62
|
-
function createConfigPlugin() {
|
|
73
|
+
function createConfigPlugin$1() {
|
|
63
74
|
return {
|
|
64
75
|
name: 'blocklet:config',
|
|
65
76
|
configureServer(server) {
|
|
@@ -171,22 +182,12 @@ function createMetaPlugin() {
|
|
|
171
182
|
}
|
|
172
183
|
|
|
173
184
|
/**
|
|
174
|
-
*
|
|
175
|
-
* color: string;
|
|
176
|
-
* image: string;
|
|
177
|
-
* }} GenerateOptions
|
|
178
|
-
*/
|
|
179
|
-
/**
|
|
180
|
-
* @typedef {{
|
|
181
|
-
* loadingElementId?: string;
|
|
182
|
-
* loadingColor?: string;
|
|
183
|
-
* loadingImage?: string;
|
|
184
|
-
* }} LoadingPluginOptions
|
|
185
|
-
*/
|
|
186
|
-
|
|
187
|
-
/**
|
|
185
|
+
* Generates an HTML string containing a spinner with optional color and image.
|
|
188
186
|
*
|
|
189
|
-
* @param {
|
|
187
|
+
* @param {Object} options - The options for generating the HTML.
|
|
188
|
+
* @param {string} options.color - The color of the spinner. Defaults to "#333".
|
|
189
|
+
* @param {string} options.image - The URL of the image to display alongside the spinner.
|
|
190
|
+
* @return {string} The generated HTML string.
|
|
190
191
|
*/
|
|
191
192
|
function generateHtml({ color, image }) {
|
|
192
193
|
return `<style>
|
|
@@ -288,14 +289,18 @@ function generateHtml({ color, image }) {
|
|
|
288
289
|
}
|
|
289
290
|
|
|
290
291
|
/**
|
|
292
|
+
* Creates a loading plugin for Vite.
|
|
291
293
|
*
|
|
292
|
-
* @param {
|
|
293
|
-
* @
|
|
294
|
+
* @param {Object} options - The options for the loading plugin.
|
|
295
|
+
* @param {string} [options.loadingElementId='app'] - The ID of the loading element.
|
|
296
|
+
* @param {string} [options.loadingColor='#8abaf0'] - The color of the loading animation.
|
|
297
|
+
* @param {string} [options.loadingImage='/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80'] - The URL of the loading image.
|
|
298
|
+
* @return {Object} - The Vite plugin object.
|
|
294
299
|
*/
|
|
295
300
|
function createLoadingPlugin({
|
|
296
301
|
loadingElementId = 'app',
|
|
297
302
|
loadingColor = '#8abaf0',
|
|
298
|
-
loadingImage = '/.well-known/service/blocklet/logo?imageFilter=
|
|
303
|
+
loadingImage = '/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80',
|
|
299
304
|
} = {}) {
|
|
300
305
|
const injectHtml = generateHtml({
|
|
301
306
|
color: loadingColor,
|
|
@@ -311,9 +316,100 @@ function createLoadingPlugin({
|
|
|
311
316
|
};
|
|
312
317
|
}
|
|
313
318
|
|
|
319
|
+
/**
|
|
320
|
+
* Creates a config plugin for Vite development server.
|
|
321
|
+
*
|
|
322
|
+
* @param {object} options - The options for the plugin.
|
|
323
|
+
* @param {'all'|'mobile'|'desktop'} [options.debugPlatform='mobile'] - The platforms to enable debug mode for.
|
|
324
|
+
* @param {string} [options.debugScript] - The initialization code for the debug script.
|
|
325
|
+
* @return {object} The Vite config plugin.
|
|
326
|
+
*/
|
|
327
|
+
function createConfigPlugin(options) {
|
|
328
|
+
return {
|
|
329
|
+
name: 'blocklet:debug', // plugin name
|
|
330
|
+
/**
|
|
331
|
+
* Configure server
|
|
332
|
+
* @param {import('vite').ViteDevServer} server vite server
|
|
333
|
+
*/
|
|
334
|
+
configureServer(server) {
|
|
335
|
+
if (isInBlocklet) {
|
|
336
|
+
server.middlewares.use((req, res, next) => {
|
|
337
|
+
/**
|
|
338
|
+
* Enabled debug mode by platform
|
|
339
|
+
* @type {boolean}
|
|
340
|
+
*/
|
|
341
|
+
let enabled = false;
|
|
342
|
+
const debugPlatform = options.debugPlatform || 'mobile';
|
|
343
|
+
if (debugPlatform.includes('all')) {
|
|
344
|
+
enabled = true;
|
|
345
|
+
}
|
|
346
|
+
const isMobileFn = isMobile__default["default"].default ? isMobile__default["default"].default : isMobile__default["default"];
|
|
347
|
+
if (isMobileFn(req.headers['user-agent']).any) {
|
|
348
|
+
if (debugPlatform.includes('mobile')) {
|
|
349
|
+
enabled = true;
|
|
350
|
+
}
|
|
351
|
+
} else {
|
|
352
|
+
if (debugPlatform.includes('desktop')) {
|
|
353
|
+
enabled = true;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (enabled) {
|
|
357
|
+
try {
|
|
358
|
+
const url = new URL(req.url, 'http://localhost');
|
|
359
|
+
if (url.pathname === '/') {
|
|
360
|
+
url.searchParams.set('debug', '');
|
|
361
|
+
req.originalUrl = url.pathname + url.search;
|
|
362
|
+
}
|
|
363
|
+
} catch {}
|
|
364
|
+
}
|
|
365
|
+
return next();
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
/**
|
|
370
|
+
* Transform index html
|
|
371
|
+
* @param {string} html original html content
|
|
372
|
+
* @param {import('vite').IndexHtmlTransformContext} ctx vite context
|
|
373
|
+
* @returns {import('vite').IndexHtmlTransformResult}
|
|
374
|
+
*/
|
|
375
|
+
transformIndexHtml(html, ctx) {
|
|
376
|
+
const debugScript =
|
|
377
|
+
options.debugScript || "import('https://esm.run/vconsole').then(({ default: vConsole }) => new vConsole())";
|
|
378
|
+
try {
|
|
379
|
+
const url = new URL(ctx.originalUrl, 'http://localhost');
|
|
380
|
+
if (url.searchParams.has('debug')) {
|
|
381
|
+
return {
|
|
382
|
+
html,
|
|
383
|
+
tags: [
|
|
384
|
+
{
|
|
385
|
+
tag: 'script',
|
|
386
|
+
children: debugScript,
|
|
387
|
+
injectTo: 'body',
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
} catch {}
|
|
393
|
+
return html;
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
314
398
|
const argv = process.argv.slice(2);
|
|
315
399
|
const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
|
|
316
400
|
|
|
401
|
+
/**
|
|
402
|
+
* Sets up the client for the application.
|
|
403
|
+
*
|
|
404
|
+
* @param {Object} app - The application object.
|
|
405
|
+
* @param {Object} [options={}] - The options object.
|
|
406
|
+
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
407
|
+
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
408
|
+
* @param {number} [options.port] - The port for the server.
|
|
409
|
+
* @param {string} [options.configFile=''] - The path to the config file.
|
|
410
|
+
* @param {string} [options.appType='spa'] - The type of the application.
|
|
411
|
+
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
412
|
+
*/
|
|
317
413
|
async function setupClient(app, options = {}) {
|
|
318
414
|
if (!isProduction) {
|
|
319
415
|
const params = mri__default["default"](argv, {
|
|
@@ -322,7 +418,33 @@ async function setupClient(app, options = {}) {
|
|
|
322
418
|
},
|
|
323
419
|
});
|
|
324
420
|
const { host = '127.0.0.1', protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
325
|
-
|
|
421
|
+
let skipWritePort = true;
|
|
422
|
+
let envAppendContent = '';
|
|
423
|
+
let envObject = '';
|
|
424
|
+
const envFilePath = path__default["default"].join(process.cwd(), '.env.development.local');
|
|
425
|
+
let port;
|
|
426
|
+
|
|
427
|
+
if (!fs__default["default"].existsSync(envFilePath)) {
|
|
428
|
+
skipWritePort = false;
|
|
429
|
+
port = await getPort__default["default"]({ port: inputPort });
|
|
430
|
+
envAppendContent = `BLOCKLET_VITE_PORT=${port}`;
|
|
431
|
+
} else {
|
|
432
|
+
port = await getPort__default["default"]({ port: inputPort });
|
|
433
|
+
const envContent = await fs__default["default"].promises.readFile(envFilePath, 'utf-8');
|
|
434
|
+
envObject = dotenv__default["default"].parse(envContent);
|
|
435
|
+
|
|
436
|
+
if (!envObject.BLOCKLET_VITE_PORT) {
|
|
437
|
+
skipWritePort = false;
|
|
438
|
+
envAppendContent = `${envContent}\nBLOCKLET_VITE_PORT=${port}`;
|
|
439
|
+
} else {
|
|
440
|
+
port = process.env.BLOCKLET_VITE_PORT;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (!skipWritePort && envAppendContent) {
|
|
444
|
+
// TODO @zhanghan 常见的 env file 处理暂不支持保留 comment,所以不能通过解析后的对象来写入文件
|
|
445
|
+
// @see https://github.com/bevry/envfile/pull/213
|
|
446
|
+
await fs__default["default"].promises.writeFile(envFilePath, envAppendContent);
|
|
447
|
+
}
|
|
326
448
|
// 以中间件模式创建 Vite 服务器
|
|
327
449
|
const vite$1 = await vite.createServer({
|
|
328
450
|
configFile: params.config || configFile || undefined,
|
|
@@ -343,23 +465,28 @@ async function setupClient(app, options = {}) {
|
|
|
343
465
|
}
|
|
344
466
|
|
|
345
467
|
/**
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
468
|
+
* Plugin options.
|
|
469
|
+
*
|
|
470
|
+
* @typedef {Object} PluginOptions
|
|
471
|
+
* @property {boolean} [disableNodePolyfills=false] - Disable node polyfills.
|
|
472
|
+
* @property {boolean} [disableConfig=false] - Disable config plugin.
|
|
473
|
+
* @property {boolean} [disableMeta=false] - Disable meta plugin.
|
|
474
|
+
* @property {boolean} [disableHmr=false] - Disable hmr plugin.
|
|
475
|
+
* @property {boolean} [disableLoading=false] - Disable loading plugin.
|
|
476
|
+
* @property {boolean} [disableDebug=false] - Disable debug plugin.
|
|
352
477
|
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
* }
|
|
478
|
+
* @property {string} [loadingElementId]
|
|
479
|
+
* @property {string} [loadingColor]
|
|
480
|
+
* @property {string} [loadingImage]
|
|
481
|
+
* @property {'all'|'mobile'|'desktop'} [debugPlatform='mobile']
|
|
482
|
+
* @property {string} [debugScript]
|
|
357
483
|
*/
|
|
358
484
|
|
|
359
485
|
/**
|
|
486
|
+
* Create blocklet plugins.
|
|
360
487
|
*
|
|
361
488
|
* @param {PluginOptions} options
|
|
362
|
-
* @returns
|
|
489
|
+
* @returns {import('vite').Plugin[]}
|
|
363
490
|
*/
|
|
364
491
|
function createBlockletPlugin(options = {}) {
|
|
365
492
|
const {
|
|
@@ -370,14 +497,18 @@ function createBlockletPlugin(options = {}) {
|
|
|
370
497
|
disableMeta = false,
|
|
371
498
|
disableHmr = false,
|
|
372
499
|
disableLoading = false,
|
|
500
|
+
disableDebug = false,
|
|
373
501
|
...restOptions
|
|
374
502
|
} = options;
|
|
503
|
+
|
|
504
|
+
/** @type {import('vite').Plugin[]} */
|
|
375
505
|
const plugins = [];
|
|
506
|
+
|
|
376
507
|
if (!disableMeta) {
|
|
377
508
|
plugins.push(createMetaPlugin());
|
|
378
509
|
}
|
|
379
510
|
if (!disableConfig) {
|
|
380
|
-
plugins.push(createConfigPlugin());
|
|
511
|
+
plugins.push(createConfigPlugin$1());
|
|
381
512
|
}
|
|
382
513
|
if (!disableHmr) {
|
|
383
514
|
plugins.push(createHmrPlugin(restOptions));
|
|
@@ -388,6 +519,9 @@ function createBlockletPlugin(options = {}) {
|
|
|
388
519
|
if (!disableLoading) {
|
|
389
520
|
plugins.push(createLoadingPlugin(restOptions));
|
|
390
521
|
}
|
|
522
|
+
if (!disableDebug) {
|
|
523
|
+
plugins.push(createConfigPlugin(restOptions));
|
|
524
|
+
}
|
|
391
525
|
|
|
392
526
|
return plugins;
|
|
393
527
|
}
|
|
@@ -396,7 +530,8 @@ Object.defineProperty(exports, 'nodePolyfills', {
|
|
|
396
530
|
enumerable: true,
|
|
397
531
|
get: function () { return vitePluginNodePolyfills.nodePolyfills; }
|
|
398
532
|
});
|
|
399
|
-
exports.createBlockletConfig = createConfigPlugin;
|
|
533
|
+
exports.createBlockletConfig = createConfigPlugin$1;
|
|
534
|
+
exports.createBlockletDebug = createConfigPlugin;
|
|
400
535
|
exports.createBlockletHmr = createHmrPlugin;
|
|
401
536
|
exports.createBlockletLoading = createLoadingPlugin;
|
|
402
537
|
exports.createBlockletMeta = createMetaPlugin;
|
package/index.js
CHANGED
|
@@ -3,26 +3,32 @@ import createHmrPlugin from './libs/hmr.js';
|
|
|
3
3
|
import createConfigPlugin from './libs/config.js';
|
|
4
4
|
import createMetaPlugin from './libs/meta.js';
|
|
5
5
|
import createLoadingPlugin from './libs/loading.js';
|
|
6
|
+
import createDebugPlugin from './libs/debug.js';
|
|
6
7
|
import setupClient from './libs/client.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
* disableNodePolyfills?: boolean;
|
|
11
|
-
* disableConfig?: boolean;
|
|
12
|
-
* disableMeta?: boolean;
|
|
13
|
-
* disableHmr?: boolean;
|
|
14
|
-
* disableLoading?: boolean;
|
|
10
|
+
* Plugin options.
|
|
15
11
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* }
|
|
12
|
+
* @typedef {Object} PluginOptions
|
|
13
|
+
* @property {boolean} [disableNodePolyfills=false] - Disable node polyfills.
|
|
14
|
+
* @property {boolean} [disableConfig=false] - Disable config plugin.
|
|
15
|
+
* @property {boolean} [disableMeta=false] - Disable meta plugin.
|
|
16
|
+
* @property {boolean} [disableHmr=false] - Disable hmr plugin.
|
|
17
|
+
* @property {boolean} [disableLoading=false] - Disable loading plugin.
|
|
18
|
+
* @property {boolean} [disableDebug=false] - Disable debug plugin.
|
|
19
|
+
*
|
|
20
|
+
* @property {string} [loadingElementId]
|
|
21
|
+
* @property {string} [loadingColor]
|
|
22
|
+
* @property {string} [loadingImage]
|
|
23
|
+
* @property {'all'|'mobile'|'desktop'} [debugPlatform='mobile']
|
|
24
|
+
* @property {string} [debugScript]
|
|
20
25
|
*/
|
|
21
26
|
|
|
22
27
|
/**
|
|
28
|
+
* Create blocklet plugins.
|
|
23
29
|
*
|
|
24
30
|
* @param {PluginOptions} options
|
|
25
|
-
* @returns
|
|
31
|
+
* @returns {import('vite').Plugin[]}
|
|
26
32
|
*/
|
|
27
33
|
export function createBlockletPlugin(options = {}) {
|
|
28
34
|
const {
|
|
@@ -33,9 +39,13 @@ export function createBlockletPlugin(options = {}) {
|
|
|
33
39
|
disableMeta = false,
|
|
34
40
|
disableHmr = false,
|
|
35
41
|
disableLoading = false,
|
|
42
|
+
disableDebug = false,
|
|
36
43
|
...restOptions
|
|
37
44
|
} = options;
|
|
45
|
+
|
|
46
|
+
/** @type {import('vite').Plugin[]} */
|
|
38
47
|
const plugins = [];
|
|
48
|
+
|
|
39
49
|
if (!disableMeta) {
|
|
40
50
|
plugins.push(createMetaPlugin(restOptions));
|
|
41
51
|
}
|
|
@@ -51,6 +61,9 @@ export function createBlockletPlugin(options = {}) {
|
|
|
51
61
|
if (!disableLoading) {
|
|
52
62
|
plugins.push(createLoadingPlugin(restOptions));
|
|
53
63
|
}
|
|
64
|
+
if (!disableDebug) {
|
|
65
|
+
plugins.push(createDebugPlugin(restOptions));
|
|
66
|
+
}
|
|
54
67
|
|
|
55
68
|
return plugins;
|
|
56
69
|
}
|
|
@@ -61,5 +74,6 @@ export {
|
|
|
61
74
|
createConfigPlugin as createBlockletConfig,
|
|
62
75
|
createMetaPlugin as createBlockletMeta,
|
|
63
76
|
createLoadingPlugin as createBlockletLoading,
|
|
77
|
+
createDebugPlugin as createBlockletDebug,
|
|
64
78
|
nodePolyfills,
|
|
65
79
|
};
|
package/libs/client.js
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
1
3
|
import getPort from 'get-port';
|
|
2
4
|
import { createServer } from 'vite';
|
|
3
5
|
import mri from 'mri';
|
|
6
|
+
import dotenv from 'dotenv';
|
|
4
7
|
|
|
5
8
|
const argv = process.argv.slice(2);
|
|
6
9
|
const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
|
|
7
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Sets up the client for the application.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} app - The application object.
|
|
15
|
+
* @param {Object} [options={}] - The options object.
|
|
16
|
+
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
17
|
+
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
18
|
+
* @param {number} [options.port] - The port for the server.
|
|
19
|
+
* @param {string} [options.configFile=''] - The path to the config file.
|
|
20
|
+
* @param {string} [options.appType='spa'] - The type of the application.
|
|
21
|
+
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
22
|
+
*/
|
|
8
23
|
export default async function setupClient(app, options = {}) {
|
|
9
24
|
if (!isProduction) {
|
|
10
25
|
const params = mri(argv, {
|
|
@@ -13,7 +28,33 @@ export default async function setupClient(app, options = {}) {
|
|
|
13
28
|
},
|
|
14
29
|
});
|
|
15
30
|
const { host = '127.0.0.1', protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
16
|
-
|
|
31
|
+
let skipWritePort = true;
|
|
32
|
+
let envAppendContent = '';
|
|
33
|
+
let envObject = '';
|
|
34
|
+
const envFilePath = path.join(process.cwd(), '.env.development.local');
|
|
35
|
+
let port;
|
|
36
|
+
|
|
37
|
+
if (!fs.existsSync(envFilePath)) {
|
|
38
|
+
skipWritePort = false;
|
|
39
|
+
port = await getPort({ port: inputPort });
|
|
40
|
+
envAppendContent = `BLOCKLET_VITE_PORT=${port}`;
|
|
41
|
+
} else {
|
|
42
|
+
port = await getPort({ port: inputPort });
|
|
43
|
+
const envContent = await fs.promises.readFile(envFilePath, 'utf-8');
|
|
44
|
+
envObject = dotenv.parse(envContent);
|
|
45
|
+
|
|
46
|
+
if (!envObject.BLOCKLET_VITE_PORT) {
|
|
47
|
+
skipWritePort = false;
|
|
48
|
+
envAppendContent = `${envContent}\nBLOCKLET_VITE_PORT=${port}`;
|
|
49
|
+
} else {
|
|
50
|
+
port = process.env.BLOCKLET_VITE_PORT;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!skipWritePort && envAppendContent) {
|
|
54
|
+
// TODO @zhanghan 常见的 env file 处理暂不支持保留 comment,所以不能通过解析后的对象来写入文件
|
|
55
|
+
// @see https://github.com/bevry/envfile/pull/213
|
|
56
|
+
await fs.promises.writeFile(envFilePath, envAppendContent);
|
|
57
|
+
}
|
|
17
58
|
// 以中间件模式创建 Vite 服务器
|
|
18
59
|
const vite = await createServer({
|
|
19
60
|
configFile: params.config || configFile || undefined,
|
package/libs/config.js
CHANGED
package/libs/debug.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import isMobile from 'ismobilejs';
|
|
2
|
+
import { isInBlocklet } from './utils.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a config plugin for Vite development server.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} options - The options for the plugin.
|
|
8
|
+
* @param {'all'|'mobile'|'desktop'} [options.debugPlatform='mobile'] - The platforms to enable debug mode for.
|
|
9
|
+
* @param {string} [options.debugScript] - The initialization code for the debug script.
|
|
10
|
+
* @return {object} The Vite config plugin.
|
|
11
|
+
*/
|
|
12
|
+
export default function createConfigPlugin(options) {
|
|
13
|
+
return {
|
|
14
|
+
name: 'blocklet:debug', // plugin name
|
|
15
|
+
/**
|
|
16
|
+
* Configure server
|
|
17
|
+
* @param {import('vite').ViteDevServer} server vite server
|
|
18
|
+
*/
|
|
19
|
+
configureServer(server) {
|
|
20
|
+
if (isInBlocklet) {
|
|
21
|
+
server.middlewares.use((req, res, next) => {
|
|
22
|
+
/**
|
|
23
|
+
* Enabled debug mode by platform
|
|
24
|
+
* @type {boolean}
|
|
25
|
+
*/
|
|
26
|
+
let enabled = false;
|
|
27
|
+
const debugPlatform = options.debugPlatform || 'mobile';
|
|
28
|
+
if (debugPlatform.includes('all')) {
|
|
29
|
+
enabled = true;
|
|
30
|
+
}
|
|
31
|
+
const isMobileFn = isMobile.default ? isMobile.default : isMobile;
|
|
32
|
+
if (isMobileFn(req.headers['user-agent']).any) {
|
|
33
|
+
if (debugPlatform.includes('mobile')) {
|
|
34
|
+
enabled = true;
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
if (debugPlatform.includes('desktop')) {
|
|
38
|
+
enabled = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (enabled) {
|
|
42
|
+
try {
|
|
43
|
+
const url = new URL(req.url, 'http://localhost');
|
|
44
|
+
if (url.pathname === '/') {
|
|
45
|
+
url.searchParams.set('debug', '');
|
|
46
|
+
req.originalUrl = url.pathname + url.search;
|
|
47
|
+
}
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
return next();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
/**
|
|
55
|
+
* Transform index html
|
|
56
|
+
* @param {string} html original html content
|
|
57
|
+
* @param {import('vite').IndexHtmlTransformContext} ctx vite context
|
|
58
|
+
* @returns {import('vite').IndexHtmlTransformResult}
|
|
59
|
+
*/
|
|
60
|
+
transformIndexHtml(html, ctx) {
|
|
61
|
+
const debugScript =
|
|
62
|
+
options.debugScript || "import('https://esm.run/vconsole').then(({ default: vConsole }) => new vConsole())";
|
|
63
|
+
try {
|
|
64
|
+
const url = new URL(ctx.originalUrl, 'http://localhost');
|
|
65
|
+
if (url.searchParams.has('debug')) {
|
|
66
|
+
return {
|
|
67
|
+
html,
|
|
68
|
+
tags: [
|
|
69
|
+
{
|
|
70
|
+
tag: 'script',
|
|
71
|
+
children: debugScript,
|
|
72
|
+
injectTo: 'body',
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
} catch {}
|
|
78
|
+
return html;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
package/libs/hmr.js
CHANGED
|
@@ -2,6 +2,13 @@ import { version as viteVersion } from 'vite';
|
|
|
2
2
|
import semver from 'semver';
|
|
3
3
|
import { isInBlocklet } from './utils.js';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Creates a HMR plugin with the given options.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} options - The options for the HMR plugin.
|
|
9
|
+
* @param {string} options.version - The version of the vite version.
|
|
10
|
+
* @return {Object} The HMR plugin object.
|
|
11
|
+
*/
|
|
5
12
|
export default function createHmrPlugin(options = {}) {
|
|
6
13
|
const { version = viteVersion } = options;
|
|
7
14
|
return {
|
package/libs/loading.js
CHANGED
|
@@ -1,20 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* color: string;
|
|
4
|
-
* image: string;
|
|
5
|
-
* }} GenerateOptions
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* @typedef {{
|
|
9
|
-
* loadingElementId?: string;
|
|
10
|
-
* loadingColor?: string;
|
|
11
|
-
* loadingImage?: string;
|
|
12
|
-
* }} LoadingPluginOptions
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
2
|
+
* Generates an HTML string containing a spinner with optional color and image.
|
|
16
3
|
*
|
|
17
|
-
* @param {
|
|
4
|
+
* @param {Object} options - The options for generating the HTML.
|
|
5
|
+
* @param {string} options.color - The color of the spinner. Defaults to "#333".
|
|
6
|
+
* @param {string} options.image - The URL of the image to display alongside the spinner.
|
|
7
|
+
* @return {string} The generated HTML string.
|
|
18
8
|
*/
|
|
19
9
|
function generateHtml({ color, image }) {
|
|
20
10
|
return `<style>
|
|
@@ -116,14 +106,18 @@ function generateHtml({ color, image }) {
|
|
|
116
106
|
}
|
|
117
107
|
|
|
118
108
|
/**
|
|
109
|
+
* Creates a loading plugin for Vite.
|
|
119
110
|
*
|
|
120
|
-
* @param {
|
|
121
|
-
* @
|
|
111
|
+
* @param {Object} options - The options for the loading plugin.
|
|
112
|
+
* @param {string} [options.loadingElementId='app'] - The ID of the loading element.
|
|
113
|
+
* @param {string} [options.loadingColor='#8abaf0'] - The color of the loading animation.
|
|
114
|
+
* @param {string} [options.loadingImage='/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80'] - The URL of the loading image.
|
|
115
|
+
* @return {Object} - The Vite plugin object.
|
|
122
116
|
*/
|
|
123
117
|
export default function createLoadingPlugin({
|
|
124
118
|
loadingElementId = 'app',
|
|
125
119
|
loadingColor = '#8abaf0',
|
|
126
|
-
loadingImage = '/.well-known/service/blocklet/logo?imageFilter=
|
|
120
|
+
loadingImage = '/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80',
|
|
127
121
|
} = {}) {
|
|
128
122
|
const injectHtml = generateHtml({
|
|
129
123
|
color: loadingColor,
|
package/libs/meta.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-blocklet",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.7.
|
|
4
|
+
"version": "0.7.6",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"files": [
|
|
@@ -30,7 +30,9 @@
|
|
|
30
30
|
"@arcblock/did": "^1.18.108",
|
|
31
31
|
"@ocap/mcrypto": "^1.18.108",
|
|
32
32
|
"@ocap/util": "^1.18.108",
|
|
33
|
+
"dotenv": "^16.4.5",
|
|
33
34
|
"get-port": "^5.1.1",
|
|
35
|
+
"ismobilejs": "^1.1.1",
|
|
34
36
|
"mri": "^1.2.0",
|
|
35
37
|
"semver": "^7.5.4",
|
|
36
38
|
"vite-plugin-node-polyfills": "^0.17.0",
|