vite-plugin-blocklet 0.9.1 → 0.9.3

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 CHANGED
@@ -61,7 +61,12 @@ function createHmrPlugin(options = {}) {
61
61
  replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
62
62
  replacedCode = replacedCode.replace(
63
63
  'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
64
- "const base = __BASE__ || '/';\nlet tmpPort = __HMR_PORT__;\nif (window.blocklet) {\ntmpPort = new URL(window.location.href).port + base;\n}\nconst socketHost = `${__HMR_HOSTNAME__ || location.hostname}${tmpPort ? `:${tmpPort}` : ''}`;",
64
+ `const base = __BASE__ || '/';
65
+ let tmpPort = __HMR_PORT__;
66
+ if (window.blocklet) {
67
+ tmpPort = new URL(window.location.href).port + base;
68
+ }
69
+ const socketHost = \`\$\{__HMR_HOSTNAME__ || location.hostname\}\$\{tmpPort ? \`:\$\{tmpPort\}\` : ''}\`;`,
65
70
  );
66
71
  return replacedCode;
67
72
  }
@@ -85,9 +90,26 @@ function createHmrPlugin(options = {}) {
85
90
  // 当 ws 是以中间件的形式挂载到服务端代码时,需要手动在页面触发一次 upgrade 事件
86
91
  if (hmrMode === 'middleware') {
87
92
  // 在页面加载时,触发一次 upgrade
93
+ replacedCode = replacedCode.replace(
94
+ `try {
95
+ let fallback;`,
96
+ `async function runInit() {
97
+ try {
98
+ let fallback;`,
99
+ );
100
+ replacedCode = replacedCode.replace(
101
+ `console.error(\`[vite] failed to connect to websocket (\$\{error\}). \`);
102
+ }`,
103
+ `console.error(\`[vite] failed to connect to websocket (\$\{error\}). \`);
104
+ }
105
+ }
106
+ runInit();`,
107
+ );
88
108
  replacedCode = replacedCode.replace(
89
109
  'function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {',
90
- 'async function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {\nawait waitForSuccessfulPing(protocol, hostAndPath);\n',
110
+ `async function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {
111
+ await waitForSuccessfulPing(protocol, hostAndPath);
112
+ `,
91
113
  );
92
114
  replacedCode = replacedCode.replace('fallback = () => {', 'fallback = async () => {');
93
115
  replacedCode = replacedCode.replace(/socket = setupWebSocket\(/g, 'socket = await setupWebSocket(');
@@ -98,7 +120,23 @@ function createHmrPlugin(options = {}) {
98
120
  // 改变刷新页面的判断
99
121
  replacedCode = replacedCode.replace(
100
122
  'const ping =',
101
- "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 =",
123
+ `const ping = async () => {
124
+ try {
125
+ await fetch(\`\$\{pingHostProtocol\}://\$\{hostAndPath\}\`, {
126
+ mode: 'no-cors',
127
+ headers: {
128
+ Accept: 'text/x-vite-ping'
129
+ }
130
+ }).then((res) => {
131
+ if ([404, 502].includes(res.status)) {
132
+ throw new Error('waiting for server to restart...');
133
+ }
134
+ });
135
+ return true;
136
+ } catch {}
137
+ return false;
138
+ }
139
+ const pingBak =`,
102
140
  );
103
141
  }
104
142
  }
package/libs/hmr.js CHANGED
@@ -23,7 +23,12 @@ export default function createHmrPlugin(options = {}) {
23
23
  replacedCode = replacedCode.replace("const base = __BASE__ || '/';\n", '');
24
24
  replacedCode = replacedCode.replace(
25
25
  'const socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;',
26
- "const base = __BASE__ || '/';\nlet tmpPort = __HMR_PORT__;\nif (window.blocklet) {\ntmpPort = new URL(window.location.href).port + base;\n}\nconst socketHost = `${__HMR_HOSTNAME__ || location.hostname}${tmpPort ? `:${tmpPort}` : ''}`;",
26
+ `const base = __BASE__ || '/';
27
+ let tmpPort = __HMR_PORT__;
28
+ if (window.blocklet) {
29
+ tmpPort = new URL(window.location.href).port + base;
30
+ }
31
+ const socketHost = \`\$\{__HMR_HOSTNAME__ || location.hostname\}\$\{tmpPort ? \`:\$\{tmpPort\}\` : ''}\`;`,
27
32
  );
28
33
  return replacedCode;
29
34
  }
@@ -47,9 +52,26 @@ export default function createHmrPlugin(options = {}) {
47
52
  // 当 ws 是以中间件的形式挂载到服务端代码时,需要手动在页面触发一次 upgrade 事件
48
53
  if (hmrMode === 'middleware') {
49
54
  // 在页面加载时,触发一次 upgrade
55
+ replacedCode = replacedCode.replace(
56
+ `try {
57
+ let fallback;`,
58
+ `async function runInit() {
59
+ try {
60
+ let fallback;`,
61
+ );
62
+ replacedCode = replacedCode.replace(
63
+ `console.error(\`[vite] failed to connect to websocket (\$\{error\}). \`);
64
+ }`,
65
+ `console.error(\`[vite] failed to connect to websocket (\$\{error\}). \`);
66
+ }
67
+ }
68
+ runInit();`,
69
+ );
50
70
  replacedCode = replacedCode.replace(
51
71
  'function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {',
52
- 'async function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {\nawait waitForSuccessfulPing(protocol, hostAndPath);\n',
72
+ `async function setupWebSocket(protocol, hostAndPath, onCloseWithoutOpen) {
73
+ await waitForSuccessfulPing(protocol, hostAndPath);
74
+ `,
53
75
  );
54
76
  replacedCode = replacedCode.replace('fallback = () => {', 'fallback = async () => {');
55
77
  replacedCode = replacedCode.replace(/socket = setupWebSocket\(/g, 'socket = await setupWebSocket(');
@@ -60,7 +82,23 @@ export default function createHmrPlugin(options = {}) {
60
82
  // 改变刷新页面的判断
61
83
  replacedCode = replacedCode.replace(
62
84
  'const ping =',
63
- "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 =",
85
+ `const ping = async () => {
86
+ try {
87
+ await fetch(\`\$\{pingHostProtocol\}://\$\{hostAndPath\}\`, {
88
+ mode: 'no-cors',
89
+ headers: {
90
+ Accept: 'text/x-vite-ping'
91
+ }
92
+ }).then((res) => {
93
+ if ([404, 502].includes(res.status)) {
94
+ throw new Error('waiting for server to restart...');
95
+ }
96
+ });
97
+ return true;
98
+ } catch {}
99
+ return false;
100
+ }
101
+ const pingBak =`,
64
102
  );
65
103
  }
66
104
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-blocklet",
3
3
  "type": "module",
4
- "version": "0.9.1",
4
+ "version": "0.9.3",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -27,9 +27,9 @@
27
27
  "rollup": "^4.20.0"
28
28
  },
29
29
  "dependencies": {
30
- "@arcblock/did": "^1.18.128",
31
- "@ocap/mcrypto": "^1.18.128",
32
- "@ocap/util": "^1.18.128",
30
+ "@arcblock/did": "^1.18.135",
31
+ "@ocap/mcrypto": "^1.18.135",
32
+ "@ocap/util": "^1.18.135",
33
33
  "get-port": "^5.1.1",
34
34
  "http-proxy-middleware": "^3.0.0",
35
35
  "ismobilejs": "^1.1.1",