vite-plugin-blocklet 0.8.4 → 0.8.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 +33 -17
- package/libs/client.js +27 -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,7 +458,8 @@ 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.
|
|
462
465
|
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
@@ -468,25 +471,38 @@ async function setupClient(app, options = {}) {
|
|
|
468
471
|
config: 'c',
|
|
469
472
|
},
|
|
470
473
|
});
|
|
471
|
-
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
474
|
+
const { host, protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options || {};
|
|
472
475
|
const port = await getPort({ port: inputPort });
|
|
473
|
-
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
476
|
+
const clientPort = options?.clientPort || port;
|
|
477
|
+
const enableWsMiddleware = !host;
|
|
478
|
+
if (enableWsMiddleware) {
|
|
479
|
+
process.env.VITE_HMR_MODE = 'middleware';
|
|
480
|
+
// 创建 hmr proxy
|
|
481
|
+
const wsProxy = httpProxyMiddleware.createProxyMiddleware({
|
|
482
|
+
target: `ws://127.0.0.1:${port}`,
|
|
483
|
+
ws: true,
|
|
484
|
+
});
|
|
485
|
+
app.use(path.join(blockletPrefix, '/__vite_hmr__'), wsProxy);
|
|
486
|
+
} else {
|
|
487
|
+
process.env.VITE_HMR_MODE = 'server';
|
|
488
|
+
}
|
|
480
489
|
|
|
481
490
|
// 以中间件模式创建 Vite 服务器
|
|
482
491
|
const vite$1 = await vite.createServer({
|
|
483
492
|
configFile: params.config || configFile || undefined,
|
|
484
493
|
server: {
|
|
485
494
|
middlewareMode: true,
|
|
486
|
-
hmr:
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
495
|
+
hmr: enableWsMiddleware
|
|
496
|
+
? {
|
|
497
|
+
port,
|
|
498
|
+
path: '/__vite_hmr__',
|
|
499
|
+
}
|
|
500
|
+
: {
|
|
501
|
+
host,
|
|
502
|
+
port,
|
|
503
|
+
clientPort,
|
|
504
|
+
protocol,
|
|
505
|
+
},
|
|
490
506
|
},
|
|
491
507
|
appType,
|
|
492
508
|
});
|
package/libs/client.js
CHANGED
|
@@ -15,7 +15,8 @@ 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.
|
|
21
22
|
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
@@ -27,25 +28,38 @@ export default async function setupClient(app, options = {}) {
|
|
|
27
28
|
config: 'c',
|
|
28
29
|
},
|
|
29
30
|
});
|
|
30
|
-
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
31
|
+
const { host, protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options || {};
|
|
31
32
|
const port = await getPort({ port: inputPort });
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
const clientPort = options?.clientPort || port;
|
|
34
|
+
const enableWsMiddleware = !host;
|
|
35
|
+
if (enableWsMiddleware) {
|
|
36
|
+
process.env.VITE_HMR_MODE = 'middleware';
|
|
37
|
+
// 创建 hmr proxy
|
|
38
|
+
const wsProxy = createProxyMiddleware({
|
|
39
|
+
target: `ws://127.0.0.1:${port}`,
|
|
40
|
+
ws: true,
|
|
41
|
+
});
|
|
42
|
+
app.use(path.join(blockletPrefix, '/__vite_hmr__'), wsProxy);
|
|
43
|
+
} else {
|
|
44
|
+
process.env.VITE_HMR_MODE = 'server';
|
|
45
|
+
}
|
|
39
46
|
|
|
40
47
|
// 以中间件模式创建 Vite 服务器
|
|
41
48
|
const vite = await createServer({
|
|
42
49
|
configFile: params.config || configFile || undefined,
|
|
43
50
|
server: {
|
|
44
51
|
middlewareMode: true,
|
|
45
|
-
hmr:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
hmr: enableWsMiddleware
|
|
53
|
+
? {
|
|
54
|
+
port,
|
|
55
|
+
path: '/__vite_hmr__',
|
|
56
|
+
}
|
|
57
|
+
: {
|
|
58
|
+
host,
|
|
59
|
+
port,
|
|
60
|
+
clientPort,
|
|
61
|
+
protocol,
|
|
62
|
+
},
|
|
49
63
|
},
|
|
50
64
|
appType,
|
|
51
65
|
});
|
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,
|