vite-plugin-blocklet 0.7.3 → 0.7.5
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 +163 -35
- package/index.js +25 -11
- package/libs/client.js +36 -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 +2 -1
package/dist/index.cjs
CHANGED
|
@@ -8,9 +8,10 @@ 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');
|
|
16
17
|
|
|
@@ -21,6 +22,7 @@ var Mcrypto__default = /*#__PURE__*/_interopDefaultLegacy(Mcrypto);
|
|
|
21
22
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
22
23
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
23
24
|
var YAML__default = /*#__PURE__*/_interopDefaultLegacy(YAML);
|
|
25
|
+
var isMobile__default = /*#__PURE__*/_interopDefaultLegacy(isMobile);
|
|
24
26
|
var getPort__default = /*#__PURE__*/_interopDefaultLegacy(getPort);
|
|
25
27
|
var mri__default = /*#__PURE__*/_interopDefaultLegacy(mri);
|
|
26
28
|
|
|
@@ -37,6 +39,13 @@ function toBlockletDid(name) {
|
|
|
37
39
|
|
|
38
40
|
const isInBlocklet = !!process.env.BLOCKLET_PORT;
|
|
39
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Creates a HMR plugin with the given options.
|
|
44
|
+
*
|
|
45
|
+
* @param {Object} options - The options for the HMR plugin.
|
|
46
|
+
* @param {string} options.version - The version of the vite version.
|
|
47
|
+
* @return {Object} The HMR plugin object.
|
|
48
|
+
*/
|
|
40
49
|
function createHmrPlugin(options = {}) {
|
|
41
50
|
const { version = vite.version } = options;
|
|
42
51
|
return {
|
|
@@ -59,7 +68,7 @@ function createHmrPlugin(options = {}) {
|
|
|
59
68
|
};
|
|
60
69
|
}
|
|
61
70
|
|
|
62
|
-
function createConfigPlugin() {
|
|
71
|
+
function createConfigPlugin$1() {
|
|
63
72
|
return {
|
|
64
73
|
name: 'blocklet:config',
|
|
65
74
|
configureServer(server) {
|
|
@@ -171,22 +180,12 @@ function createMetaPlugin() {
|
|
|
171
180
|
}
|
|
172
181
|
|
|
173
182
|
/**
|
|
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
|
-
/**
|
|
183
|
+
* Generates an HTML string containing a spinner with optional color and image.
|
|
188
184
|
*
|
|
189
|
-
* @param {
|
|
185
|
+
* @param {Object} options - The options for generating the HTML.
|
|
186
|
+
* @param {string} options.color - The color of the spinner. Defaults to "#333".
|
|
187
|
+
* @param {string} options.image - The URL of the image to display alongside the spinner.
|
|
188
|
+
* @return {string} The generated HTML string.
|
|
190
189
|
*/
|
|
191
190
|
function generateHtml({ color, image }) {
|
|
192
191
|
return `<style>
|
|
@@ -288,14 +287,18 @@ function generateHtml({ color, image }) {
|
|
|
288
287
|
}
|
|
289
288
|
|
|
290
289
|
/**
|
|
290
|
+
* Creates a loading plugin for Vite.
|
|
291
291
|
*
|
|
292
|
-
* @param {
|
|
293
|
-
* @
|
|
292
|
+
* @param {Object} options - The options for the loading plugin.
|
|
293
|
+
* @param {string} [options.loadingElementId='app'] - The ID of the loading element.
|
|
294
|
+
* @param {string} [options.loadingColor='#8abaf0'] - The color of the loading animation.
|
|
295
|
+
* @param {string} [options.loadingImage='/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80'] - The URL of the loading image.
|
|
296
|
+
* @return {Object} - The Vite plugin object.
|
|
294
297
|
*/
|
|
295
298
|
function createLoadingPlugin({
|
|
296
299
|
loadingElementId = 'app',
|
|
297
300
|
loadingColor = '#8abaf0',
|
|
298
|
-
loadingImage = '/.well-known/service/blocklet/logo?imageFilter=
|
|
301
|
+
loadingImage = '/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80',
|
|
299
302
|
} = {}) {
|
|
300
303
|
const injectHtml = generateHtml({
|
|
301
304
|
color: loadingColor,
|
|
@@ -311,9 +314,100 @@ function createLoadingPlugin({
|
|
|
311
314
|
};
|
|
312
315
|
}
|
|
313
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Creates a config plugin for Vite development server.
|
|
319
|
+
*
|
|
320
|
+
* @param {object} options - The options for the plugin.
|
|
321
|
+
* @param {'all'|'mobile'|'desktop'} [options.debugPlatform='mobile'] - The platforms to enable debug mode for.
|
|
322
|
+
* @param {string} [options.debugScript] - The initialization code for the debug script.
|
|
323
|
+
* @return {object} The Vite config plugin.
|
|
324
|
+
*/
|
|
325
|
+
function createConfigPlugin(options) {
|
|
326
|
+
return {
|
|
327
|
+
name: 'blocklet:debug', // plugin name
|
|
328
|
+
/**
|
|
329
|
+
* Configure server
|
|
330
|
+
* @param {import('vite').ViteDevServer} server vite server
|
|
331
|
+
*/
|
|
332
|
+
configureServer(server) {
|
|
333
|
+
if (isInBlocklet) {
|
|
334
|
+
server.middlewares.use((req, res, next) => {
|
|
335
|
+
/**
|
|
336
|
+
* Enabled debug mode by platform
|
|
337
|
+
* @type {boolean}
|
|
338
|
+
*/
|
|
339
|
+
let enabled = false;
|
|
340
|
+
const debugPlatform = options.debugPlatform || 'mobile';
|
|
341
|
+
if (debugPlatform.includes('all')) {
|
|
342
|
+
enabled = true;
|
|
343
|
+
}
|
|
344
|
+
const isMobileFn = isMobile__default["default"].default ? isMobile__default["default"].default : isMobile__default["default"];
|
|
345
|
+
if (isMobileFn(req.headers['user-agent']).any) {
|
|
346
|
+
if (debugPlatform.includes('mobile')) {
|
|
347
|
+
enabled = true;
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
350
|
+
if (debugPlatform.includes('desktop')) {
|
|
351
|
+
enabled = true;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (enabled) {
|
|
355
|
+
try {
|
|
356
|
+
const url = new URL(req.url, 'http://localhost');
|
|
357
|
+
if (url.pathname === '/') {
|
|
358
|
+
url.searchParams.set('debug', '');
|
|
359
|
+
req.originalUrl = url.pathname + url.search;
|
|
360
|
+
}
|
|
361
|
+
} catch {}
|
|
362
|
+
}
|
|
363
|
+
return next();
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
/**
|
|
368
|
+
* Transform index html
|
|
369
|
+
* @param {string} html original html content
|
|
370
|
+
* @param {import('vite').IndexHtmlTransformContext} ctx vite context
|
|
371
|
+
* @returns {import('vite').IndexHtmlTransformResult}
|
|
372
|
+
*/
|
|
373
|
+
transformIndexHtml(html, ctx) {
|
|
374
|
+
const debugScript =
|
|
375
|
+
options.debugScript || "import('https://esm.run/vconsole').then(({ default: vConsole }) => new vConsole())";
|
|
376
|
+
try {
|
|
377
|
+
const url = new URL(ctx.originalUrl, 'http://localhost');
|
|
378
|
+
if (url.searchParams.has('debug')) {
|
|
379
|
+
return {
|
|
380
|
+
html,
|
|
381
|
+
tags: [
|
|
382
|
+
{
|
|
383
|
+
tag: 'script',
|
|
384
|
+
children: debugScript,
|
|
385
|
+
injectTo: 'body',
|
|
386
|
+
},
|
|
387
|
+
],
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
} catch {}
|
|
391
|
+
return html;
|
|
392
|
+
},
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
314
396
|
const argv = process.argv.slice(2);
|
|
315
397
|
const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
|
|
316
398
|
|
|
399
|
+
/**
|
|
400
|
+
* Sets up the client for the application.
|
|
401
|
+
*
|
|
402
|
+
* @param {Object} app - The application object.
|
|
403
|
+
* @param {Object} [options={}] - The options object.
|
|
404
|
+
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
405
|
+
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
406
|
+
* @param {number} [options.port] - The port for the server.
|
|
407
|
+
* @param {string} [options.configFile=''] - The path to the config file.
|
|
408
|
+
* @param {string} [options.appType='spa'] - The type of the application.
|
|
409
|
+
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
410
|
+
*/
|
|
317
411
|
async function setupClient(app, options = {}) {
|
|
318
412
|
if (!isProduction) {
|
|
319
413
|
const params = mri__default["default"](argv, {
|
|
@@ -322,7 +416,28 @@ async function setupClient(app, options = {}) {
|
|
|
322
416
|
},
|
|
323
417
|
});
|
|
324
418
|
const { host = '127.0.0.1', protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
325
|
-
|
|
419
|
+
let skipWritePort = true;
|
|
420
|
+
let envAppendContent = '';
|
|
421
|
+
const envFile = path__default["default"].join(process.cwd(), '.env.development.local');
|
|
422
|
+
let port;
|
|
423
|
+
|
|
424
|
+
if (!fs__default["default"].existsSync(envFile)) {
|
|
425
|
+
skipWritePort = false;
|
|
426
|
+
port = await getPort__default["default"]({ port: inputPort });
|
|
427
|
+
envAppendContent = `VITE_BLOCKLET_PORT=${port}`;
|
|
428
|
+
} else {
|
|
429
|
+
port = await getPort__default["default"]({ port: inputPort });
|
|
430
|
+
const content = await fs__default["default"].promises.readFile(envFile, 'utf-8');
|
|
431
|
+
if (!content.includes('VITE_BLOCKLET_PORT')) {
|
|
432
|
+
skipWritePort = false;
|
|
433
|
+
envAppendContent = `${content}\nVITE_BLOCKLET_PORT=${port}`;
|
|
434
|
+
} else {
|
|
435
|
+
port = process.env.VITE_BLOCKLET_PORT;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (!skipWritePort && envAppendContent) {
|
|
439
|
+
await fs__default["default"].promises.writeFile(envFile, envAppendContent);
|
|
440
|
+
}
|
|
326
441
|
// 以中间件模式创建 Vite 服务器
|
|
327
442
|
const vite$1 = await vite.createServer({
|
|
328
443
|
configFile: params.config || configFile || undefined,
|
|
@@ -343,23 +458,28 @@ async function setupClient(app, options = {}) {
|
|
|
343
458
|
}
|
|
344
459
|
|
|
345
460
|
/**
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
461
|
+
* Plugin options.
|
|
462
|
+
*
|
|
463
|
+
* @typedef {Object} PluginOptions
|
|
464
|
+
* @property {boolean} [disableNodePolyfills=false] - Disable node polyfills.
|
|
465
|
+
* @property {boolean} [disableConfig=false] - Disable config plugin.
|
|
466
|
+
* @property {boolean} [disableMeta=false] - Disable meta plugin.
|
|
467
|
+
* @property {boolean} [disableHmr=false] - Disable hmr plugin.
|
|
468
|
+
* @property {boolean} [disableLoading=false] - Disable loading plugin.
|
|
469
|
+
* @property {boolean} [disableDebug=false] - Disable debug plugin.
|
|
352
470
|
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
* }
|
|
471
|
+
* @property {string} [loadingElementId]
|
|
472
|
+
* @property {string} [loadingColor]
|
|
473
|
+
* @property {string} [loadingImage]
|
|
474
|
+
* @property {'all'|'mobile'|'desktop'} [debugPlatform='mobile']
|
|
475
|
+
* @property {string} [debugScript]
|
|
357
476
|
*/
|
|
358
477
|
|
|
359
478
|
/**
|
|
479
|
+
* Create blocklet plugins.
|
|
360
480
|
*
|
|
361
481
|
* @param {PluginOptions} options
|
|
362
|
-
* @returns
|
|
482
|
+
* @returns {import('vite').Plugin[]}
|
|
363
483
|
*/
|
|
364
484
|
function createBlockletPlugin(options = {}) {
|
|
365
485
|
const {
|
|
@@ -370,14 +490,18 @@ function createBlockletPlugin(options = {}) {
|
|
|
370
490
|
disableMeta = false,
|
|
371
491
|
disableHmr = false,
|
|
372
492
|
disableLoading = false,
|
|
493
|
+
disableDebug = false,
|
|
373
494
|
...restOptions
|
|
374
495
|
} = options;
|
|
496
|
+
|
|
497
|
+
/** @type {import('vite').Plugin[]} */
|
|
375
498
|
const plugins = [];
|
|
499
|
+
|
|
376
500
|
if (!disableMeta) {
|
|
377
501
|
plugins.push(createMetaPlugin());
|
|
378
502
|
}
|
|
379
503
|
if (!disableConfig) {
|
|
380
|
-
plugins.push(createConfigPlugin());
|
|
504
|
+
plugins.push(createConfigPlugin$1());
|
|
381
505
|
}
|
|
382
506
|
if (!disableHmr) {
|
|
383
507
|
plugins.push(createHmrPlugin(restOptions));
|
|
@@ -388,6 +512,9 @@ function createBlockletPlugin(options = {}) {
|
|
|
388
512
|
if (!disableLoading) {
|
|
389
513
|
plugins.push(createLoadingPlugin(restOptions));
|
|
390
514
|
}
|
|
515
|
+
if (!disableDebug) {
|
|
516
|
+
plugins.push(createConfigPlugin(restOptions));
|
|
517
|
+
}
|
|
391
518
|
|
|
392
519
|
return plugins;
|
|
393
520
|
}
|
|
@@ -396,7 +523,8 @@ Object.defineProperty(exports, 'nodePolyfills', {
|
|
|
396
523
|
enumerable: true,
|
|
397
524
|
get: function () { return vitePluginNodePolyfills.nodePolyfills; }
|
|
398
525
|
});
|
|
399
|
-
exports.createBlockletConfig = createConfigPlugin;
|
|
526
|
+
exports.createBlockletConfig = createConfigPlugin$1;
|
|
527
|
+
exports.createBlockletDebug = createConfigPlugin;
|
|
400
528
|
exports.createBlockletHmr = createHmrPlugin;
|
|
401
529
|
exports.createBlockletLoading = createLoadingPlugin;
|
|
402
530
|
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,3 +1,5 @@
|
|
|
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';
|
|
@@ -5,6 +7,18 @@ import mri from 'mri';
|
|
|
5
7
|
const argv = process.argv.slice(2);
|
|
6
8
|
const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
|
|
7
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Sets up the client for the application.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} app - The application object.
|
|
14
|
+
* @param {Object} [options={}] - The options object.
|
|
15
|
+
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
16
|
+
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
17
|
+
* @param {number} [options.port] - The port for the server.
|
|
18
|
+
* @param {string} [options.configFile=''] - The path to the config file.
|
|
19
|
+
* @param {string} [options.appType='spa'] - The type of the application.
|
|
20
|
+
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
21
|
+
*/
|
|
8
22
|
export default async function setupClient(app, options = {}) {
|
|
9
23
|
if (!isProduction) {
|
|
10
24
|
const params = mri(argv, {
|
|
@@ -13,7 +27,28 @@ export default async function setupClient(app, options = {}) {
|
|
|
13
27
|
},
|
|
14
28
|
});
|
|
15
29
|
const { host = '127.0.0.1', protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
16
|
-
|
|
30
|
+
let skipWritePort = true;
|
|
31
|
+
let envAppendContent = '';
|
|
32
|
+
const envFile = path.join(process.cwd(), '.env.development.local');
|
|
33
|
+
let port;
|
|
34
|
+
|
|
35
|
+
if (!fs.existsSync(envFile)) {
|
|
36
|
+
skipWritePort = false;
|
|
37
|
+
port = await getPort({ port: inputPort });
|
|
38
|
+
envAppendContent = `VITE_BLOCKLET_PORT=${port}`;
|
|
39
|
+
} else {
|
|
40
|
+
port = await getPort({ port: inputPort });
|
|
41
|
+
const content = await fs.promises.readFile(envFile, 'utf-8');
|
|
42
|
+
if (!content.includes('VITE_BLOCKLET_PORT')) {
|
|
43
|
+
skipWritePort = false;
|
|
44
|
+
envAppendContent = `${content}\nVITE_BLOCKLET_PORT=${port}`;
|
|
45
|
+
} else {
|
|
46
|
+
port = process.env.VITE_BLOCKLET_PORT;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!skipWritePort && envAppendContent) {
|
|
50
|
+
await fs.promises.writeFile(envFile, envAppendContent);
|
|
51
|
+
}
|
|
17
52
|
// 以中间件模式创建 Vite 服务器
|
|
18
53
|
const vite = await createServer({
|
|
19
54
|
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.5",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"files": [
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"@ocap/mcrypto": "^1.18.108",
|
|
32
32
|
"@ocap/util": "^1.18.108",
|
|
33
33
|
"get-port": "^5.1.1",
|
|
34
|
+
"ismobilejs": "^1.1.1",
|
|
34
35
|
"mri": "^1.2.0",
|
|
35
36
|
"semver": "^7.5.4",
|
|
36
37
|
"vite-plugin-node-polyfills": "^0.17.0",
|