vite-plugin-blocklet 0.8.3 → 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 +37 -23
- package/libs/client.js +28 -14
- package/libs/express.js +4 -6
- package/libs/hmr.js +6 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -14,7 +14,6 @@ var getPort = require('get-port');
|
|
|
14
14
|
var mri = require('mri');
|
|
15
15
|
var httpProxyMiddleware = require('http-proxy-middleware');
|
|
16
16
|
|
|
17
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
18
17
|
const { types } = Mcrypto;
|
|
19
18
|
|
|
20
19
|
function toBlockletDid(name) {
|
|
@@ -35,11 +34,11 @@ const blockletPrefix = process.env.BLOCKLET_DEV_MOUNT_POINT || '/';
|
|
|
35
34
|
*
|
|
36
35
|
* @param {Object} options - The options for the HMR plugin.
|
|
37
36
|
* @param {string} options.version - The version of the vite version.
|
|
38
|
-
* @param {'middleware'|'client'} options.hmrMode - The version of the vite version.
|
|
37
|
+
* @param {'middleware'|'client'|'server'} options.hmrMode - The version of the vite version.
|
|
39
38
|
* @return {Object} The HMR plugin object.
|
|
40
39
|
*/
|
|
41
40
|
function createHmrPlugin(options = {}) {
|
|
42
|
-
const { version = vite.version } = options;
|
|
41
|
+
const { version = vite.version, hmrMode = process.env.VITE_HMR_MODE || 'client' } = options || {};
|
|
43
42
|
return {
|
|
44
43
|
name: 'blocklet:hmr',
|
|
45
44
|
apply: 'serve',
|
|
@@ -56,9 +55,11 @@ function createHmrPlugin(options = {}) {
|
|
|
56
55
|
return replacedCode;
|
|
57
56
|
}
|
|
58
57
|
|
|
59
|
-
|
|
58
|
+
if (['client', 'middleware'].includes(hmrMode)) {
|
|
59
|
+
replacedCode = replacedCode.replace(/__HMR_BASE__/g, `"${blockletPrefix}"+__HMR_BASE__`);
|
|
60
|
+
}
|
|
60
61
|
|
|
61
|
-
if (
|
|
62
|
+
if (hmrMode === 'middleware') {
|
|
62
63
|
// 根据页面的协议自动判断端口
|
|
63
64
|
replacedCode = replacedCode.replace(
|
|
64
65
|
/__HMR_PORT__/g,
|
|
@@ -406,13 +407,12 @@ function createConfigPlugin(options) {
|
|
|
406
407
|
*
|
|
407
408
|
* @param {object} options - The options for the plugin.
|
|
408
409
|
* @param {string} options.entryPath - The entry path of the Express app.
|
|
410
|
+
* @param {array} options.ignorePath - The entry path of the Express app.
|
|
409
411
|
* @return {object} The Vite config plugin.
|
|
410
412
|
*/
|
|
411
|
-
function createExpressPlugin({ entryPath }) {
|
|
413
|
+
function createExpressPlugin({ entryPath, ignorePath = [] }) {
|
|
414
|
+
ignorePath = Array.isArray(ignorePath) ? ignorePath : [ignorePath];
|
|
412
415
|
// 此处需要解构 import.meta 对象,才能拿到 vite.config 的地址,否则拿到的地址会是 express.js 文件的地址
|
|
413
|
-
const { dirname } = { ...({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)) }) };
|
|
414
|
-
const fullPath = path.join(dirname, entryPath);
|
|
415
|
-
const folderPath = path.dirname(fullPath);
|
|
416
416
|
return {
|
|
417
417
|
name: 'blocklet:express',
|
|
418
418
|
apply: 'serve',
|
|
@@ -432,7 +432,7 @@ function createExpressPlugin({ entryPath }) {
|
|
|
432
432
|
const invalidatedModules = [];
|
|
433
433
|
|
|
434
434
|
for (const mod of modules) {
|
|
435
|
-
if (mod.file.
|
|
435
|
+
if (ignorePath.some((x) => mod.file.startsWith(x))) {
|
|
436
436
|
invalidatedModules.push(mod);
|
|
437
437
|
} else {
|
|
438
438
|
validatedModules.push(mod);
|
|
@@ -458,7 +458,8 @@ const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NO
|
|
|
458
458
|
* @param {Object} [options={}] - The options object.
|
|
459
459
|
* @param {string} [options.host='127.0.0.1'] - The host for the server.
|
|
460
460
|
* @param {string} [options.protocol='ws'] - The protocol for the server.
|
|
461
|
-
* @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.
|
|
462
463
|
* @param {string} [options.configFile=''] - The path to the config file.
|
|
463
464
|
* @param {string} [options.appType='spa'] - The type of the application.
|
|
464
465
|
* @return {Promise<Object>} A promise that resolves to the Vite server object.
|
|
@@ -470,25 +471,38 @@ async function setupClient(app, options = {}) {
|
|
|
470
471
|
config: 'c',
|
|
471
472
|
},
|
|
472
473
|
});
|
|
473
|
-
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
474
|
+
const { host, protocol = 'ws', port: inputPort, configFile = '', appType = 'spa' } = options || {};
|
|
474
475
|
const port = await getPort({ port: inputPort });
|
|
475
|
-
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
+
}
|
|
482
489
|
|
|
483
490
|
// 以中间件模式创建 Vite 服务器
|
|
484
491
|
const vite$1 = await vite.createServer({
|
|
485
492
|
configFile: params.config || configFile || undefined,
|
|
486
493
|
server: {
|
|
487
494
|
middlewareMode: true,
|
|
488
|
-
hmr:
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
495
|
+
hmr: enableWsMiddleware
|
|
496
|
+
? {
|
|
497
|
+
port,
|
|
498
|
+
path: '/__vite_hmr__',
|
|
499
|
+
}
|
|
500
|
+
: {
|
|
501
|
+
host,
|
|
502
|
+
port,
|
|
503
|
+
clientPort,
|
|
504
|
+
protocol,
|
|
505
|
+
},
|
|
492
506
|
},
|
|
493
507
|
appType,
|
|
494
508
|
});
|
package/libs/client.js
CHANGED
|
@@ -3,7 +3,7 @@ import getPort from 'get-port';
|
|
|
3
3
|
import { createServer } from 'vite';
|
|
4
4
|
import mri from 'mri';
|
|
5
5
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
6
|
-
import { blockletPrefix } from './utils';
|
|
6
|
+
import { blockletPrefix } from './utils.js';
|
|
7
7
|
|
|
8
8
|
const argv = process.argv.slice(2);
|
|
9
9
|
const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
|
|
@@ -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/express.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
1
|
/**
|
|
3
2
|
* Creates a config plugin for Vite development server.
|
|
4
3
|
*
|
|
5
4
|
* @param {object} options - The options for the plugin.
|
|
6
5
|
* @param {string} options.entryPath - The entry path of the Express app.
|
|
6
|
+
* @param {array} options.ignorePath - The entry path of the Express app.
|
|
7
7
|
* @return {object} The Vite config plugin.
|
|
8
8
|
*/
|
|
9
|
-
export default function createExpressPlugin({ entryPath }) {
|
|
9
|
+
export default function createExpressPlugin({ entryPath, ignorePath = [] }) {
|
|
10
|
+
ignorePath = Array.isArray(ignorePath) ? ignorePath : [ignorePath];
|
|
10
11
|
// 此处需要解构 import.meta 对象,才能拿到 vite.config 的地址,否则拿到的地址会是 express.js 文件的地址
|
|
11
|
-
const { dirname } = { ...import.meta };
|
|
12
|
-
const fullPath = path.join(dirname, entryPath);
|
|
13
|
-
const folderPath = path.dirname(fullPath);
|
|
14
12
|
return {
|
|
15
13
|
name: 'blocklet:express',
|
|
16
14
|
apply: 'serve',
|
|
@@ -30,7 +28,7 @@ export default function createExpressPlugin({ entryPath }) {
|
|
|
30
28
|
const invalidatedModules = [];
|
|
31
29
|
|
|
32
30
|
for (const mod of modules) {
|
|
33
|
-
if (mod.file.
|
|
31
|
+
if (ignorePath.some((x) => mod.file.startsWith(x))) {
|
|
34
32
|
invalidatedModules.push(mod);
|
|
35
33
|
} else {
|
|
36
34
|
validatedModules.push(mod);
|
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,
|