vite-plugin-blocklet 0.8.4 → 0.8.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 +53 -17
- package/libs/client.js +47 -13
- package/libs/hmr.js +6 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -34,11 +34,11 @@ const blockletPrefix = process.env.BLOCKLET_DEV_MOUNT_POINT || '/';
|
|
|
34
34
|
*
|
|
35
35
|
* @param {Object} options - The options for the HMR plugin.
|
|
36
36
|
* @param {string} options.version - The version of the vite version.
|
|
37
|
-
* @param {'middleware'|'client'} options.hmrMode - The version of the vite version.
|
|
37
|
+
* @param {'middleware'|'client'|'server'} options.hmrMode - The version of the vite version.
|
|
38
38
|
* @return {Object} The HMR plugin object.
|
|
39
39
|
*/
|
|
40
40
|
function createHmrPlugin(options = {}) {
|
|
41
|
-
const { version = vite.version } = options;
|
|
41
|
+
const { version = vite.version, hmrMode = process.env.VITE_HMR_MODE || 'client' } = options || {};
|
|
42
42
|
return {
|
|
43
43
|
name: 'blocklet:hmr',
|
|
44
44
|
apply: 'serve',
|
|
@@ -55,9 +55,11 @@ function createHmrPlugin(options = {}) {
|
|
|
55
55
|
return replacedCode;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
if (['client', 'middleware'].includes(hmrMode)) {
|
|
59
|
+
replacedCode = replacedCode.replace(/__HMR_BASE__/g, `"${blockletPrefix}"+__HMR_BASE__`);
|
|
60
|
+
}
|
|
59
61
|
|
|
60
|
-
if (
|
|
62
|
+
if (hmrMode === 'middleware') {
|
|
61
63
|
// 根据页面的协议自动判断端口
|
|
62
64
|
replacedCode = replacedCode.replace(
|
|
63
65
|
/__HMR_PORT__/g,
|
|
@@ -456,9 +458,12 @@ const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NO
|
|
|
456
458
|
* @param {Object} [options={}] - The options object.
|
|
457
459
|
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
458
460
|
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
459
|
-
* @param {number} [options.port] - The port for the server.
|
|
461
|
+
* @param {number} [options.port] - The port for the ws server.
|
|
462
|
+
* @param {number} [options.clientPort] - The clientPort for the ws server.
|
|
460
463
|
* @param {string} [options.configFile=''] - The path to the config file.
|
|
461
464
|
* @param {string} [options.appType='spa'] - The type of the application.
|
|
465
|
+
* @param {import('node:http').Server} [options.server] - The http server instance
|
|
466
|
+
* @param {object} [options.importMetaHot] - vite import.meta.hot
|
|
462
467
|
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
463
468
|
*/
|
|
464
469
|
async function setupClient(app, options = {}) {
|
|
@@ -468,30 +473,61 @@ async function setupClient(app, options = {}) {
|
|
|
468
473
|
config: 'c',
|
|
469
474
|
},
|
|
470
475
|
});
|
|
471
|
-
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
476
|
+
const { host, protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options || {};
|
|
472
477
|
const port = await getPort({ port: inputPort });
|
|
473
|
-
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
478
|
+
const clientPort = options?.clientPort || port;
|
|
479
|
+
const enableWsMiddleware = !host;
|
|
480
|
+
if (enableWsMiddleware) {
|
|
481
|
+
process.env.VITE_HMR_MODE = 'middleware';
|
|
482
|
+
// 创建 hmr proxy
|
|
483
|
+
const wsProxy = httpProxyMiddleware.createProxyMiddleware({
|
|
484
|
+
target: `ws://127.0.0.1:${port}`,
|
|
485
|
+
ws: true,
|
|
486
|
+
});
|
|
487
|
+
app.use(path.join(blockletPrefix, '/__vite_hmr__'), wsProxy);
|
|
488
|
+
} else {
|
|
489
|
+
process.env.VITE_HMR_MODE = 'server';
|
|
490
|
+
}
|
|
480
491
|
|
|
481
492
|
// 以中间件模式创建 Vite 服务器
|
|
482
493
|
const vite$1 = await vite.createServer({
|
|
483
494
|
configFile: params.config || configFile || undefined,
|
|
484
495
|
server: {
|
|
485
496
|
middlewareMode: true,
|
|
486
|
-
hmr:
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
497
|
+
hmr: enableWsMiddleware
|
|
498
|
+
? {
|
|
499
|
+
port,
|
|
500
|
+
path: '/__vite_hmr__',
|
|
501
|
+
}
|
|
502
|
+
: {
|
|
503
|
+
host,
|
|
504
|
+
port,
|
|
505
|
+
clientPort,
|
|
506
|
+
protocol,
|
|
507
|
+
},
|
|
490
508
|
},
|
|
491
509
|
appType,
|
|
492
510
|
});
|
|
493
511
|
// 将 vite 的 connect 实例作中间件使用
|
|
494
512
|
app.use(vite$1.middlewares);
|
|
513
|
+
// 用于 vite-node 进行服务重载时,先关闭原有服务的端口监听
|
|
514
|
+
if (options?.server && options?.importMetaHot && options.importMetaHot?.on && options.importMetaHot?.dispose) {
|
|
515
|
+
async function killServer() {
|
|
516
|
+
await options.server.close((err) => {
|
|
517
|
+
console.log('vite-plugin-blocklet: Server closed succeed');
|
|
518
|
+
console.error('vite-plugin-blocklet: Failed to close server', err);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
options.importMetaHot.on('vite:beforeFullReload', async () => {
|
|
522
|
+
console.log('vite-plugin-blocklet: Full reload');
|
|
523
|
+
await killServer();
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
options.importMetaHot.dispose(async () => {
|
|
527
|
+
console.log('vite-plugin-blocklet: Dispose');
|
|
528
|
+
await killServer();
|
|
529
|
+
});
|
|
530
|
+
}
|
|
495
531
|
return vite$1;
|
|
496
532
|
}
|
|
497
533
|
}
|
package/libs/client.js
CHANGED
|
@@ -15,9 +15,12 @@ const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NO
|
|
|
15
15
|
* @param {Object} [options={}] - The options object.
|
|
16
16
|
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
17
17
|
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
18
|
-
* @param {number} [options.port] - The port for the server.
|
|
18
|
+
* @param {number} [options.port] - The port for the ws server.
|
|
19
|
+
* @param {number} [options.clientPort] - The clientPort for the ws server.
|
|
19
20
|
* @param {string} [options.configFile=''] - The path to the config file.
|
|
20
21
|
* @param {string} [options.appType='spa'] - The type of the application.
|
|
22
|
+
* @param {import('node:http').Server} [options.server] - The http server instance
|
|
23
|
+
* @param {object} [options.importMetaHot] - vite import.meta.hot
|
|
21
24
|
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
22
25
|
*/
|
|
23
26
|
export default async function setupClient(app, options = {}) {
|
|
@@ -27,30 +30,61 @@ export default async function setupClient(app, options = {}) {
|
|
|
27
30
|
config: 'c',
|
|
28
31
|
},
|
|
29
32
|
});
|
|
30
|
-
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
33
|
+
const { host, protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options || {};
|
|
31
34
|
const port = await getPort({ port: inputPort });
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
const clientPort = options?.clientPort || port;
|
|
36
|
+
const enableWsMiddleware = !host;
|
|
37
|
+
if (enableWsMiddleware) {
|
|
38
|
+
process.env.VITE_HMR_MODE = 'middleware';
|
|
39
|
+
// 创建 hmr proxy
|
|
40
|
+
const wsProxy = createProxyMiddleware({
|
|
41
|
+
target: `ws://127.0.0.1:${port}`,
|
|
42
|
+
ws: true,
|
|
43
|
+
});
|
|
44
|
+
app.use(path.join(blockletPrefix, '/__vite_hmr__'), wsProxy);
|
|
45
|
+
} else {
|
|
46
|
+
process.env.VITE_HMR_MODE = 'server';
|
|
47
|
+
}
|
|
39
48
|
|
|
40
49
|
// 以中间件模式创建 Vite 服务器
|
|
41
50
|
const vite = await createServer({
|
|
42
51
|
configFile: params.config || configFile || undefined,
|
|
43
52
|
server: {
|
|
44
53
|
middlewareMode: true,
|
|
45
|
-
hmr:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
hmr: enableWsMiddleware
|
|
55
|
+
? {
|
|
56
|
+
port,
|
|
57
|
+
path: '/__vite_hmr__',
|
|
58
|
+
}
|
|
59
|
+
: {
|
|
60
|
+
host,
|
|
61
|
+
port,
|
|
62
|
+
clientPort,
|
|
63
|
+
protocol,
|
|
64
|
+
},
|
|
49
65
|
},
|
|
50
66
|
appType,
|
|
51
67
|
});
|
|
52
68
|
// 将 vite 的 connect 实例作中间件使用
|
|
53
69
|
app.use(vite.middlewares);
|
|
70
|
+
// 用于 vite-node 进行服务重载时,先关闭原有服务的端口监听
|
|
71
|
+
if (options?.server && options?.importMetaHot && options.importMetaHot?.on && options.importMetaHot?.dispose) {
|
|
72
|
+
async function killServer() {
|
|
73
|
+
await options.server.close((err) => {
|
|
74
|
+
console.log('vite-plugin-blocklet: Server closed succeed');
|
|
75
|
+
console.error('vite-plugin-blocklet: Failed to close server', err);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
options.importMetaHot.on('vite:beforeFullReload', async () => {
|
|
79
|
+
console.log('vite-plugin-blocklet: Full reload');
|
|
80
|
+
await killServer();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
options.importMetaHot.dispose(async () => {
|
|
84
|
+
console.log('vite-plugin-blocklet: Dispose');
|
|
85
|
+
await killServer();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
54
88
|
return vite;
|
|
55
89
|
}
|
|
56
90
|
}
|
package/libs/hmr.js
CHANGED
|
@@ -7,11 +7,11 @@ import { blockletPrefix, isInBlocklet } from './utils.js';
|
|
|
7
7
|
*
|
|
8
8
|
* @param {Object} options - The options for the HMR plugin.
|
|
9
9
|
* @param {string} options.version - The version of the vite version.
|
|
10
|
-
* @param {'middleware'|'client'} options.hmrMode - The version of the vite version.
|
|
10
|
+
* @param {'middleware'|'client'|'server'} options.hmrMode - The version of the vite version.
|
|
11
11
|
* @return {Object} The HMR plugin object.
|
|
12
12
|
*/
|
|
13
13
|
export default function createHmrPlugin(options = {}) {
|
|
14
|
-
const { version = viteVersion } = options;
|
|
14
|
+
const { version = viteVersion, hmrMode = process.env.VITE_HMR_MODE || 'client' } = options || {};
|
|
15
15
|
return {
|
|
16
16
|
name: 'blocklet:hmr',
|
|
17
17
|
apply: 'serve',
|
|
@@ -28,9 +28,11 @@ export default function createHmrPlugin(options = {}) {
|
|
|
28
28
|
return replacedCode;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
if (['client', 'middleware'].includes(hmrMode)) {
|
|
32
|
+
replacedCode = replacedCode.replace(/__HMR_BASE__/g, `"${blockletPrefix}"+__HMR_BASE__`);
|
|
33
|
+
}
|
|
32
34
|
|
|
33
|
-
if (
|
|
35
|
+
if (hmrMode === 'middleware') {
|
|
34
36
|
// 根据页面的协议自动判断端口
|
|
35
37
|
replacedCode = replacedCode.replace(
|
|
36
38
|
/__HMR_PORT__/g,
|