vite-plugin-blocklet 0.8.1 → 0.8.2
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 +35 -31
- package/libs/client.js +11 -32
- package/libs/hmr.js +23 -0
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -12,7 +12,7 @@ var YAML = require('yaml');
|
|
|
12
12
|
var isMobile = require('ismobilejs');
|
|
13
13
|
var getPort = require('get-port');
|
|
14
14
|
var mri = require('mri');
|
|
15
|
-
var
|
|
15
|
+
var httpProxyMiddleware = require('http-proxy-middleware');
|
|
16
16
|
|
|
17
17
|
const { types } = Mcrypto;
|
|
18
18
|
|
|
@@ -51,6 +51,29 @@ function createHmrPlugin(options = {}) {
|
|
|
51
51
|
);
|
|
52
52
|
return replacedCode;
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
// 根据页面的协议自动判断端口
|
|
56
|
+
replacedCode = replacedCode.replace(
|
|
57
|
+
/__HMR_PORT__/g,
|
|
58
|
+
'location.port || (location.protocol === "https:" ? 443 : 80);',
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// 在页面加载时,触发一次 upgrade
|
|
62
|
+
replacedCode = replacedCode.replace(
|
|
63
|
+
'function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {',
|
|
64
|
+
'async function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {\nawait waitForSuccessfulPing(protocol, hostAndPath);\n',
|
|
65
|
+
);
|
|
66
|
+
replacedCode = replacedCode.replace('fallback = () => {', 'fallback = async () => {');
|
|
67
|
+
replacedCode = replacedCode.replace(/socket = setupWebSocket\(/g, 'socket = await setupWebSocket(');
|
|
68
|
+
|
|
69
|
+
if ([4, 5].includes(pureVersion)) {
|
|
70
|
+
// 改变刷新页面的判断
|
|
71
|
+
replacedCode = replacedCode.replace(
|
|
72
|
+
'const ping =',
|
|
73
|
+
"const ping = async () => {\ntry {\nawait fetch(`${pingHostProtocol}://${hostAndPath}`, {\nmode: 'no-cors',\nheaders: {\nAccept: 'text/x-vite-ping'\n}\n}).then(res => {\nif ([404, 502].includes(res.status)) {\nthrow new Error('waiting for server to restart...');\n}\n});\nreturn true;\n} catch {}\nreturn false;\n}\nconst pingBak =",
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return replacedCode;
|
|
54
77
|
}
|
|
55
78
|
},
|
|
56
79
|
};
|
|
@@ -403,43 +426,24 @@ async function setupClient(app, options = {}) {
|
|
|
403
426
|
config: 'c',
|
|
404
427
|
},
|
|
405
428
|
});
|
|
406
|
-
const {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
port = await getPort({ port: inputPort });
|
|
416
|
-
envAppendContent = `BLOCKLET_VITE_PORT=${port}`;
|
|
417
|
-
} else {
|
|
418
|
-
port = await getPort({ port: inputPort });
|
|
419
|
-
const envContent = await fs.promises.readFile(envFilePath, 'utf-8');
|
|
420
|
-
envObject = dotenv.parse(envContent);
|
|
421
|
-
|
|
422
|
-
if (!envObject.BLOCKLET_VITE_PORT) {
|
|
423
|
-
skipWritePort = false;
|
|
424
|
-
envAppendContent = `${envContent}\nBLOCKLET_VITE_PORT=${port}`;
|
|
425
|
-
} else {
|
|
426
|
-
port = process.env.BLOCKLET_VITE_PORT;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
if (!skipWritePort && envAppendContent) {
|
|
430
|
-
// TODO @zhanghan 常见的 env file 处理暂不支持保留 comment,所以不能通过解析后的对象来写入文件
|
|
431
|
-
// @see https://github.com/bevry/envfile/pull/213
|
|
432
|
-
await fs.promises.writeFile(envFilePath, envAppendContent);
|
|
433
|
-
}
|
|
429
|
+
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
430
|
+
const port = await getPort({ port: inputPort });
|
|
431
|
+
// 创建 hmr proxy
|
|
432
|
+
const wsProxy = httpProxyMiddleware.createProxyMiddleware({
|
|
433
|
+
target: `ws://127.0.0.1:${port}`,
|
|
434
|
+
ws: true,
|
|
435
|
+
});
|
|
436
|
+
app.use('/__vite_hmr__', wsProxy);
|
|
437
|
+
|
|
434
438
|
// 以中间件模式创建 Vite 服务器
|
|
435
439
|
const vite$1 = await vite.createServer({
|
|
436
440
|
configFile: params.config || configFile || undefined,
|
|
437
441
|
server: {
|
|
438
442
|
middlewareMode: true,
|
|
439
443
|
hmr: {
|
|
440
|
-
host,
|
|
441
444
|
port,
|
|
442
|
-
|
|
445
|
+
clientPort: 80,
|
|
446
|
+
path: '/__vite_hmr__',
|
|
443
447
|
},
|
|
444
448
|
},
|
|
445
449
|
appType,
|
package/libs/client.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
1
|
import getPort from 'get-port';
|
|
4
2
|
import { createServer } from 'vite';
|
|
5
3
|
import mri from 'mri';
|
|
6
|
-
import
|
|
4
|
+
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
7
5
|
|
|
8
6
|
const argv = process.argv.slice(2);
|
|
9
7
|
const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
|
|
@@ -27,43 +25,24 @@ export default async function setupClient(app, options = {}) {
|
|
|
27
25
|
config: 'c',
|
|
28
26
|
},
|
|
29
27
|
});
|
|
30
|
-
const {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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);
|
|
28
|
+
const { port: inputPort, configFile = '', appType = 'spa' } = options;
|
|
29
|
+
const port = await getPort({ port: inputPort });
|
|
30
|
+
// 创建 hmr proxy
|
|
31
|
+
const wsProxy = createProxyMiddleware({
|
|
32
|
+
target: `ws://127.0.0.1:${port}`,
|
|
33
|
+
ws: true,
|
|
34
|
+
});
|
|
35
|
+
app.use('/__vite_hmr__', wsProxy);
|
|
45
36
|
|
|
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
|
-
}
|
|
58
37
|
// 以中间件模式创建 Vite 服务器
|
|
59
38
|
const vite = await createServer({
|
|
60
39
|
configFile: params.config || configFile || undefined,
|
|
61
40
|
server: {
|
|
62
41
|
middlewareMode: true,
|
|
63
42
|
hmr: {
|
|
64
|
-
host,
|
|
65
43
|
port,
|
|
66
|
-
|
|
44
|
+
clientPort: 80,
|
|
45
|
+
path: '/__vite_hmr__',
|
|
67
46
|
},
|
|
68
47
|
},
|
|
69
48
|
appType,
|
package/libs/hmr.js
CHANGED
|
@@ -26,6 +26,29 @@ export default function createHmrPlugin(options = {}) {
|
|
|
26
26
|
);
|
|
27
27
|
return replacedCode;
|
|
28
28
|
}
|
|
29
|
+
|
|
30
|
+
// 根据页面的协议自动判断端口
|
|
31
|
+
replacedCode = replacedCode.replace(
|
|
32
|
+
/__HMR_PORT__/g,
|
|
33
|
+
'location.port || (location.protocol === "https:" ? 443 : 80);',
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// 在页面加载时,触发一次 upgrade
|
|
37
|
+
replacedCode = replacedCode.replace(
|
|
38
|
+
'function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {',
|
|
39
|
+
'async function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {\nawait waitForSuccessfulPing(protocol, hostAndPath);\n',
|
|
40
|
+
);
|
|
41
|
+
replacedCode = replacedCode.replace('fallback = () => {', 'fallback = async () => {');
|
|
42
|
+
replacedCode = replacedCode.replace(/socket = setupWebSocket\(/g, 'socket = await setupWebSocket(');
|
|
43
|
+
|
|
44
|
+
if ([4, 5].includes(pureVersion)) {
|
|
45
|
+
// 改变刷新页面的判断
|
|
46
|
+
replacedCode = replacedCode.replace(
|
|
47
|
+
'const ping =',
|
|
48
|
+
"const ping = async () => {\ntry {\nawait fetch(`${pingHostProtocol}://${hostAndPath}`, {\nmode: 'no-cors',\nheaders: {\nAccept: 'text/x-vite-ping'\n}\n}).then(res => {\nif ([404, 502].includes(res.status)) {\nthrow new Error('waiting for server to restart...');\n}\n});\nreturn true;\n} catch {}\nreturn false;\n}\nconst pingBak =",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return replacedCode;
|
|
29
52
|
}
|
|
30
53
|
},
|
|
31
54
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-blocklet",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.2",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"files": [
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"rollup": "^4.18.0"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@arcblock/did": "^1.18.
|
|
31
|
-
"@ocap/mcrypto": "^1.18.
|
|
32
|
-
"@ocap/util": "^1.18.
|
|
33
|
-
"dotenv": "^16.4.5",
|
|
30
|
+
"@arcblock/did": "^1.18.126",
|
|
31
|
+
"@ocap/mcrypto": "^1.18.126",
|
|
32
|
+
"@ocap/util": "^1.18.126",
|
|
34
33
|
"get-port": "^5.1.1",
|
|
34
|
+
"http-proxy-middleware": "^3.0.0",
|
|
35
35
|
"ismobilejs": "^1.1.1",
|
|
36
36
|
"mri": "^1.2.0",
|
|
37
37
|
"semver": "^7.6.2",
|