vite-plugin-wss-hmr 0.4.12
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/index.cjs +119 -0
- package/index.js +117 -0
- package/package.json +24 -0
- package/rollup.config.js +7 -0
package/index.cjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vite = require('vite');
|
|
4
|
+
|
|
5
|
+
async function createVitePlugin() {
|
|
6
|
+
let pongPoll, hmrServer;
|
|
7
|
+
|
|
8
|
+
let hmrServiceConfig = {
|
|
9
|
+
mode: 'development-hmr',
|
|
10
|
+
// Any legal user configuration options, plus `mode` and `configFile`
|
|
11
|
+
configFile: false,
|
|
12
|
+
server: {
|
|
13
|
+
https: true,
|
|
14
|
+
strictPort: true,
|
|
15
|
+
hmr: {
|
|
16
|
+
host: 'localhost',
|
|
17
|
+
protocol: 'wss',
|
|
18
|
+
},
|
|
19
|
+
fs: {
|
|
20
|
+
strict: false,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
async config(config, { mode }) {
|
|
27
|
+
if (mode === 'development') {
|
|
28
|
+
console.log(`[vite-plugin-wss-hmr] prepare to handle config`);
|
|
29
|
+
|
|
30
|
+
const hmrPort = Number(config?.server?.port || 3000) + 100;
|
|
31
|
+
|
|
32
|
+
// auto load base
|
|
33
|
+
if (config.base) {
|
|
34
|
+
hmrServiceConfig.base = config.base;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// auto set strictPort
|
|
38
|
+
if (!config.server.strictPort) {
|
|
39
|
+
config.server.strictPort = true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// auto set fs.strict
|
|
43
|
+
if (!config.server?.fs?.strict) {
|
|
44
|
+
config.server.fs = {
|
|
45
|
+
strict: false,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// auto set hmrServiceConfig
|
|
50
|
+
hmrServiceConfig.server.hmr = {
|
|
51
|
+
...config?.server?.hmr,
|
|
52
|
+
...hmrServiceConfig.server.hmr,
|
|
53
|
+
port: hmrPort,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
config.server.hmr = hmrServiceConfig.server.hmr;
|
|
57
|
+
|
|
58
|
+
// open hmr server
|
|
59
|
+
console.log(`[vite-plugin-wss-hmr] opening hmrServer`);
|
|
60
|
+
|
|
61
|
+
hmrServer = await vite.createServer(hmrServiceConfig);
|
|
62
|
+
|
|
63
|
+
pongPoll = setInterval(() => {
|
|
64
|
+
hmrServer?.ws.send({
|
|
65
|
+
type: 'pong',
|
|
66
|
+
});
|
|
67
|
+
}, 30 * 1000);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
buildStart() {
|
|
71
|
+
if (hmrServer.ws) {
|
|
72
|
+
const { base = '' } = hmrServiceConfig;
|
|
73
|
+
const { host, port } = hmrServiceConfig.server?.hmr;
|
|
74
|
+
console.log(`[vite-plugin-wss-hmr] hmrServer working on wss://${host}:${port}${base}`);
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
console.warn(
|
|
77
|
+
`[vite-plugin-wss-hmr] if you use this plugin for the first time, please visit https://${host}:${port}${base} , and add your browser(which used of dev) as trust`
|
|
78
|
+
);
|
|
79
|
+
}, 3000);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
handleHotUpdate({ modules }) {
|
|
83
|
+
const updateFileUrlList = [];
|
|
84
|
+
|
|
85
|
+
const updates = modules.map((item) => {
|
|
86
|
+
updateFileUrlList.push(item.url || '');
|
|
87
|
+
return {
|
|
88
|
+
type: `${item.type}-update`,
|
|
89
|
+
timestamp: item.lastInvalidationTimestamp,
|
|
90
|
+
path: item.url,
|
|
91
|
+
acceptedPath: item.url,
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (updates.length > 0) {
|
|
96
|
+
console.log(`[vite-plugin-wss-hmr] hot reloading: ${updateFileUrlList.join(' ')} updates`);
|
|
97
|
+
|
|
98
|
+
hmrServer.ws.send({
|
|
99
|
+
type: 'update',
|
|
100
|
+
updates,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return [];
|
|
105
|
+
},
|
|
106
|
+
buildEnd() {
|
|
107
|
+
console.log(`[vite-plugin-wss-hmr] hmrServer closed`);
|
|
108
|
+
|
|
109
|
+
if (pongPoll) {
|
|
110
|
+
clearInterval(pongPoll);
|
|
111
|
+
}
|
|
112
|
+
if (hmrServer) {
|
|
113
|
+
hmrServer.close();
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = createVitePlugin;
|
package/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { createServer as createViteServer } from 'vite';
|
|
2
|
+
|
|
3
|
+
async function createVitePlugin() {
|
|
4
|
+
let pongPoll, hmrServer;
|
|
5
|
+
|
|
6
|
+
let hmrServiceConfig = {
|
|
7
|
+
mode: 'development-hmr',
|
|
8
|
+
// Any legal user configuration options, plus `mode` and `configFile`
|
|
9
|
+
configFile: false,
|
|
10
|
+
server: {
|
|
11
|
+
https: true,
|
|
12
|
+
strictPort: true,
|
|
13
|
+
hmr: {
|
|
14
|
+
host: 'localhost',
|
|
15
|
+
protocol: 'wss',
|
|
16
|
+
},
|
|
17
|
+
fs: {
|
|
18
|
+
strict: false,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
async config(config, { mode }) {
|
|
25
|
+
if (mode === 'development') {
|
|
26
|
+
console.log(`[vite-plugin-wss-hmr] prepare to handle config`);
|
|
27
|
+
|
|
28
|
+
const hmrPort = Number(config?.server?.port || 3000) + 100;
|
|
29
|
+
|
|
30
|
+
// auto load base
|
|
31
|
+
if (config.base) {
|
|
32
|
+
hmrServiceConfig.base = config.base;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// auto set strictPort
|
|
36
|
+
if (!config.server.strictPort) {
|
|
37
|
+
config.server.strictPort = true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// auto set fs.strict
|
|
41
|
+
if (!config.server?.fs?.strict) {
|
|
42
|
+
config.server.fs = {
|
|
43
|
+
strict: false,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// auto set hmrServiceConfig
|
|
48
|
+
hmrServiceConfig.server.hmr = {
|
|
49
|
+
...config?.server?.hmr,
|
|
50
|
+
...hmrServiceConfig.server.hmr,
|
|
51
|
+
port: hmrPort,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
config.server.hmr = hmrServiceConfig.server.hmr;
|
|
55
|
+
|
|
56
|
+
// open hmr server
|
|
57
|
+
console.log(`[vite-plugin-wss-hmr] opening hmrServer`);
|
|
58
|
+
|
|
59
|
+
hmrServer = await createViteServer(hmrServiceConfig);
|
|
60
|
+
|
|
61
|
+
pongPoll = setInterval(() => {
|
|
62
|
+
hmrServer?.ws.send({
|
|
63
|
+
type: 'pong',
|
|
64
|
+
});
|
|
65
|
+
}, 30 * 1000);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
buildStart() {
|
|
69
|
+
if (hmrServer.ws) {
|
|
70
|
+
const { base = '' } = hmrServiceConfig;
|
|
71
|
+
const { host, port } = hmrServiceConfig.server?.hmr;
|
|
72
|
+
console.log(`[vite-plugin-wss-hmr] hmrServer working on wss://${host}:${port}${base}`);
|
|
73
|
+
setTimeout(() => {
|
|
74
|
+
console.warn(
|
|
75
|
+
`[vite-plugin-wss-hmr] if you use this plugin for the first time, please visit https://${host}:${port}${base} , and add your browser(which used of dev) as trust`
|
|
76
|
+
);
|
|
77
|
+
}, 3000);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
handleHotUpdate({ modules }) {
|
|
81
|
+
const updateFileUrlList = [];
|
|
82
|
+
|
|
83
|
+
const updates = modules.map((item) => {
|
|
84
|
+
updateFileUrlList.push(item.url || '');
|
|
85
|
+
return {
|
|
86
|
+
type: `${item.type}-update`,
|
|
87
|
+
timestamp: item.lastInvalidationTimestamp,
|
|
88
|
+
path: item.url,
|
|
89
|
+
acceptedPath: item.url,
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (updates.length > 0) {
|
|
94
|
+
console.log(`[vite-plugin-wss-hmr] hot reloading: ${updateFileUrlList.join(' ')} updates`);
|
|
95
|
+
|
|
96
|
+
hmrServer.ws.send({
|
|
97
|
+
type: 'update',
|
|
98
|
+
updates,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return [];
|
|
103
|
+
},
|
|
104
|
+
buildEnd() {
|
|
105
|
+
console.log(`[vite-plugin-wss-hmr] hmrServer closed`);
|
|
106
|
+
|
|
107
|
+
if (pongPoll) {
|
|
108
|
+
clearInterval(pongPoll);
|
|
109
|
+
}
|
|
110
|
+
if (hmrServer) {
|
|
111
|
+
hmrServer.close();
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
module.exports = createVitePlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-wss-hmr",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.4.12",
|
|
5
|
+
"description": "",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./index.cjs",
|
|
10
|
+
"import": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "Liang Yongzhuo",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"rollup": "^2.76.0",
|
|
18
|
+
"vite": "^2.9.14"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
+
"build": "rollup -c rollup.config.js"
|
|
23
|
+
}
|
|
24
|
+
}
|