vite-plugin-blocklet 0.8.7 → 0.8.9
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 +47 -24
- package/libs/client.js +2 -2
- package/libs/config.js +6 -10
- package/libs/hmr.js +4 -1
- package/libs/loading.js +26 -7
- package/libs/meta.js +5 -8
- package/libs/utils.js +9 -0
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
var vitePluginNodePolyfills = require('vite-plugin-node-polyfills');
|
|
4
4
|
var vite = require('vite');
|
|
5
5
|
var semver = require('semver');
|
|
6
|
+
var node_fs = require('node:fs');
|
|
7
|
+
var YAML = require('yaml');
|
|
6
8
|
var Mcrypto = require('@ocap/mcrypto');
|
|
7
9
|
var util = require('@ocap/util');
|
|
8
10
|
var did = require('@arcblock/did');
|
|
9
|
-
var
|
|
10
|
-
var path = require('node:path');
|
|
11
|
-
var YAML = require('yaml');
|
|
11
|
+
var ufo = require('ufo');
|
|
12
12
|
var isMobile = require('ismobilejs');
|
|
13
13
|
var getPort = require('get-port');
|
|
14
14
|
var mri = require('mri');
|
|
@@ -25,6 +25,13 @@ function toBlockletDid(name) {
|
|
|
25
25
|
return did.fromPublicKey(pk, { role: types.RoleType.ROLE_ANY });
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
async function getBlockletYAML() {
|
|
29
|
+
const blockletYamlPath = './blocklet.yml';
|
|
30
|
+
const content = await node_fs.promises.readFile(blockletYamlPath, 'utf8');
|
|
31
|
+
const blockletYaml = YAML.parse(content);
|
|
32
|
+
return blockletYaml || {};
|
|
33
|
+
}
|
|
34
|
+
|
|
28
35
|
const isInBlocklet = !!process.env.BLOCKLET_PORT;
|
|
29
36
|
const blockletPort = process.env.BLOCKLET_PORT;
|
|
30
37
|
const blockletPrefix = process.env.BLOCKLET_DEV_MOUNT_POINT || '/';
|
|
@@ -57,7 +64,10 @@ function createHmrPlugin(options = {}) {
|
|
|
57
64
|
|
|
58
65
|
// 兼容不带服务端的情况、vite 以中间件形式挂载到服务端代码的情况
|
|
59
66
|
if (['client', 'middleware', 'wsUpgrade'].includes(hmrMode)) {
|
|
60
|
-
replacedCode = replacedCode.replace(
|
|
67
|
+
replacedCode = replacedCode.replace(
|
|
68
|
+
/__HMR_BASE__/g,
|
|
69
|
+
`"${blockletPrefix === '/' ? '' : blockletPrefix}"+__HMR_BASE__`,
|
|
70
|
+
);
|
|
61
71
|
}
|
|
62
72
|
|
|
63
73
|
// 兼容 vite 以中间件形式挂载到服务端代码的情况,无论 ws 是中间件挂载还是 ws 直接监听 upgrade 事件都支持
|
|
@@ -104,16 +114,16 @@ function createConfigPlugin$1() {
|
|
|
104
114
|
server.middlewares.use((req, res, next) => {
|
|
105
115
|
// blocklet server 会把设置的 base 从请求 url 中移除,所以需要再加回 base
|
|
106
116
|
if (!req.url.startsWith(blockletPrefix)) {
|
|
107
|
-
req.url =
|
|
117
|
+
req.url = ufo.joinURL(blockletPrefix || '/', req.url);
|
|
108
118
|
}
|
|
109
119
|
return next();
|
|
110
120
|
});
|
|
111
121
|
}
|
|
112
122
|
},
|
|
113
|
-
config(config, { command }) {
|
|
123
|
+
async config(config, { command }) {
|
|
114
124
|
if (command === 'serve') {
|
|
115
125
|
const targetConfig = {};
|
|
116
|
-
targetConfig.base =
|
|
126
|
+
targetConfig.base = ufo.withTrailingSlash(ufo.joinURL('/', config.base || blockletPrefix));
|
|
117
127
|
if (!(config.server && config.server.port)) {
|
|
118
128
|
const port = blockletPort || 3000;
|
|
119
129
|
targetConfig.server = {
|
|
@@ -126,9 +136,7 @@ function createConfigPlugin$1() {
|
|
|
126
136
|
if (command === 'build') {
|
|
127
137
|
if (!config.base) {
|
|
128
138
|
try {
|
|
129
|
-
|
|
130
|
-
const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
|
|
131
|
-
let { name, did } = blockletYaml;
|
|
139
|
+
let { name, did } = await getBlockletYAML();
|
|
132
140
|
if (!did && name) {
|
|
133
141
|
did = toBlockletDid(name);
|
|
134
142
|
}
|
|
@@ -153,7 +161,7 @@ function createConfigPlugin$1() {
|
|
|
153
161
|
function createMetaPlugin() {
|
|
154
162
|
return {
|
|
155
163
|
name: 'blocklet:meta',
|
|
156
|
-
transformIndexHtml(html, ctx) {
|
|
164
|
+
async transformIndexHtml(html, ctx) {
|
|
157
165
|
const tags = [];
|
|
158
166
|
if (ctx?.chunk?.isEntry) {
|
|
159
167
|
let script;
|
|
@@ -166,12 +174,10 @@ function createMetaPlugin() {
|
|
|
166
174
|
html = html.replace('</body>', `${script}</body>`);
|
|
167
175
|
}
|
|
168
176
|
}
|
|
169
|
-
|
|
170
|
-
if (!/<title>(.*?)<\/title>/.test(html)) {
|
|
171
|
-
const blockletYamlPath = './blocklet.yml';
|
|
172
|
-
const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
|
|
173
|
-
const { title } = blockletYaml;
|
|
177
|
+
const { title } = await getBlockletYAML();
|
|
174
178
|
|
|
179
|
+
// 如果 index.html 中没有设置 title,则自动注入 blocklet.yml 中的 title
|
|
180
|
+
if (title && !/<title>(.*?)<\/title>/.test(html)) {
|
|
175
181
|
tags.push({
|
|
176
182
|
tag: 'title',
|
|
177
183
|
children: title,
|
|
@@ -257,6 +263,10 @@ function generateHtml({ color, image }) {
|
|
|
257
263
|
-webkit-animation-delay: -0.16s;
|
|
258
264
|
animation-delay: -0.16s;
|
|
259
265
|
}
|
|
266
|
+
#loadingImage {
|
|
267
|
+
margin-bottom: 16px;
|
|
268
|
+
object-fit: contain;
|
|
269
|
+
}
|
|
260
270
|
|
|
261
271
|
@-webkit-keyframes fadeIn {
|
|
262
272
|
0% { opacity: 0; }
|
|
@@ -292,13 +302,23 @@ function generateHtml({ color, image }) {
|
|
|
292
302
|
}
|
|
293
303
|
</style>
|
|
294
304
|
<div class="spinner-wrapper">
|
|
295
|
-
<img
|
|
305
|
+
<img id="loadingImage" width="70" height="70" />
|
|
296
306
|
<div class="spinner">
|
|
297
307
|
<div class="bounce1"></div>
|
|
298
308
|
<div class="bounce2"></div>
|
|
299
309
|
<div class="bounce3"></div>
|
|
300
310
|
</div>
|
|
301
|
-
</div
|
|
311
|
+
</div>
|
|
312
|
+
<script>
|
|
313
|
+
(() => {
|
|
314
|
+
const loadingImage = document.getElementById('loadingImage');
|
|
315
|
+
if (window?.blocklet?.appLogo) {
|
|
316
|
+
loadingImage.src = window.blocklet.appLogo;
|
|
317
|
+
} else {
|
|
318
|
+
loadingImage.src = "${image}";
|
|
319
|
+
}
|
|
320
|
+
})();
|
|
321
|
+
</script>`;
|
|
302
322
|
}
|
|
303
323
|
|
|
304
324
|
/**
|
|
@@ -310,11 +330,14 @@ function generateHtml({ color, image }) {
|
|
|
310
330
|
* @param {string} [options.loadingImage='/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80'] - The URL of the loading image.
|
|
311
331
|
* @return {Object} - The Vite plugin object.
|
|
312
332
|
*/
|
|
313
|
-
function createLoadingPlugin({
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
333
|
+
function createLoadingPlugin({ loadingElementId = 'app', loadingColor = '#8abaf0', loadingImage } = {}) {
|
|
334
|
+
if (!loadingImage) {
|
|
335
|
+
loadingImage = ufo.withQuery('/.well-known/service/blocklet/logo', {
|
|
336
|
+
imageFilter: 'convert',
|
|
337
|
+
f: 'png',
|
|
338
|
+
w: 80,
|
|
339
|
+
});
|
|
340
|
+
}
|
|
318
341
|
const injectHtml = generateHtml({
|
|
319
342
|
color: loadingColor,
|
|
320
343
|
image: loadingImage,
|
|
@@ -485,7 +508,7 @@ async function setupClient(app, options = {}) {
|
|
|
485
508
|
const enableWsMiddleware = !host;
|
|
486
509
|
if (enableWsMiddleware) {
|
|
487
510
|
// 创建 hmr proxy
|
|
488
|
-
const hmrWsPath =
|
|
511
|
+
const hmrWsPath = ufo.joinURL(blockletPrefix, '/__vite_hmr__');
|
|
489
512
|
const wsProxy = httpProxyMiddleware.createProxyMiddleware({
|
|
490
513
|
target: `ws://127.0.0.1:${port}`,
|
|
491
514
|
ws: true,
|
package/libs/client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { joinURL } from 'ufo';
|
|
2
2
|
import getPort from 'get-port';
|
|
3
3
|
import { createServer } from 'vite';
|
|
4
4
|
import mri from 'mri';
|
|
@@ -36,7 +36,7 @@ export default async function setupClient(app, options = {}) {
|
|
|
36
36
|
const enableWsMiddleware = !host;
|
|
37
37
|
if (enableWsMiddleware) {
|
|
38
38
|
// 创建 hmr proxy
|
|
39
|
-
const hmrWsPath =
|
|
39
|
+
const hmrWsPath = joinURL(blockletPrefix, '/__vite_hmr__');
|
|
40
40
|
const wsProxy = createProxyMiddleware({
|
|
41
41
|
target: `ws://127.0.0.1:${port}`,
|
|
42
42
|
ws: true,
|
package/libs/config.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import YAML from 'yaml';
|
|
4
|
-
import { toBlockletDid, isInBlocklet, blockletPort, blockletPrefix } from './utils.js';
|
|
1
|
+
import { joinURL, withTrailingSlash } from 'ufo';
|
|
2
|
+
import { toBlockletDid, isInBlocklet, blockletPort, blockletPrefix, getBlockletYAML } from './utils.js';
|
|
5
3
|
|
|
6
4
|
export default function createConfigPlugin() {
|
|
7
5
|
return {
|
|
@@ -11,16 +9,16 @@ export default function createConfigPlugin() {
|
|
|
11
9
|
server.middlewares.use((req, res, next) => {
|
|
12
10
|
// blocklet server 会把设置的 base 从请求 url 中移除,所以需要再加回 base
|
|
13
11
|
if (!req.url.startsWith(blockletPrefix)) {
|
|
14
|
-
req.url =
|
|
12
|
+
req.url = joinURL(blockletPrefix || '/', req.url);
|
|
15
13
|
}
|
|
16
14
|
return next();
|
|
17
15
|
});
|
|
18
16
|
}
|
|
19
17
|
},
|
|
20
|
-
config(config, { command }) {
|
|
18
|
+
async config(config, { command }) {
|
|
21
19
|
if (command === 'serve') {
|
|
22
20
|
const targetConfig = {};
|
|
23
|
-
targetConfig.base =
|
|
21
|
+
targetConfig.base = withTrailingSlash(joinURL('/', config.base || blockletPrefix));
|
|
24
22
|
if (!(config.server && config.server.port)) {
|
|
25
23
|
const port = blockletPort || 3000;
|
|
26
24
|
targetConfig.server = {
|
|
@@ -33,9 +31,7 @@ export default function createConfigPlugin() {
|
|
|
33
31
|
if (command === 'build') {
|
|
34
32
|
if (!config.base) {
|
|
35
33
|
try {
|
|
36
|
-
|
|
37
|
-
const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
|
|
38
|
-
let { name, did } = blockletYaml;
|
|
34
|
+
let { name, did } = await getBlockletYAML();
|
|
39
35
|
if (!did && name) {
|
|
40
36
|
did = toBlockletDid(name);
|
|
41
37
|
}
|
package/libs/hmr.js
CHANGED
|
@@ -30,7 +30,10 @@ export default function createHmrPlugin(options = {}) {
|
|
|
30
30
|
|
|
31
31
|
// 兼容不带服务端的情况、vite 以中间件形式挂载到服务端代码的情况
|
|
32
32
|
if (['client', 'middleware', 'wsUpgrade'].includes(hmrMode)) {
|
|
33
|
-
replacedCode = replacedCode.replace(
|
|
33
|
+
replacedCode = replacedCode.replace(
|
|
34
|
+
/__HMR_BASE__/g,
|
|
35
|
+
`"${blockletPrefix === '/' ? '' : blockletPrefix}"+__HMR_BASE__`,
|
|
36
|
+
);
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
// 兼容 vite 以中间件形式挂载到服务端代码的情况,无论 ws 是中间件挂载还是 ws 直接监听 upgrade 事件都支持
|
package/libs/loading.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { withQuery } from 'ufo';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Generates an HTML string containing a spinner with optional color and image.
|
|
3
5
|
*
|
|
@@ -61,6 +63,10 @@ function generateHtml({ color, image }) {
|
|
|
61
63
|
-webkit-animation-delay: -0.16s;
|
|
62
64
|
animation-delay: -0.16s;
|
|
63
65
|
}
|
|
66
|
+
#loadingImage {
|
|
67
|
+
margin-bottom: 16px;
|
|
68
|
+
object-fit: contain;
|
|
69
|
+
}
|
|
64
70
|
|
|
65
71
|
@-webkit-keyframes fadeIn {
|
|
66
72
|
0% { opacity: 0; }
|
|
@@ -96,13 +102,23 @@ function generateHtml({ color, image }) {
|
|
|
96
102
|
}
|
|
97
103
|
</style>
|
|
98
104
|
<div class="spinner-wrapper">
|
|
99
|
-
<img
|
|
105
|
+
<img id="loadingImage" width="70" height="70" />
|
|
100
106
|
<div class="spinner">
|
|
101
107
|
<div class="bounce1"></div>
|
|
102
108
|
<div class="bounce2"></div>
|
|
103
109
|
<div class="bounce3"></div>
|
|
104
110
|
</div>
|
|
105
|
-
</div
|
|
111
|
+
</div>
|
|
112
|
+
<script>
|
|
113
|
+
(() => {
|
|
114
|
+
const loadingImage = document.getElementById('loadingImage');
|
|
115
|
+
if (window?.blocklet?.appLogo) {
|
|
116
|
+
loadingImage.src = window.blocklet.appLogo;
|
|
117
|
+
} else {
|
|
118
|
+
loadingImage.src = "${image}";
|
|
119
|
+
}
|
|
120
|
+
})();
|
|
121
|
+
</script>`;
|
|
106
122
|
}
|
|
107
123
|
|
|
108
124
|
/**
|
|
@@ -114,11 +130,14 @@ function generateHtml({ color, image }) {
|
|
|
114
130
|
* @param {string} [options.loadingImage='/.well-known/service/blocklet/logo?imageFilter=convert&f=png&w=80'] - The URL of the loading image.
|
|
115
131
|
* @return {Object} - The Vite plugin object.
|
|
116
132
|
*/
|
|
117
|
-
export default function createLoadingPlugin({
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
133
|
+
export default function createLoadingPlugin({ loadingElementId = 'app', loadingColor = '#8abaf0', loadingImage } = {}) {
|
|
134
|
+
if (!loadingImage) {
|
|
135
|
+
loadingImage = withQuery('/.well-known/service/blocklet/logo', {
|
|
136
|
+
imageFilter: 'convert',
|
|
137
|
+
f: 'png',
|
|
138
|
+
w: 80,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
122
141
|
const injectHtml = generateHtml({
|
|
123
142
|
color: loadingColor,
|
|
124
143
|
image: loadingImage,
|
package/libs/meta.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import YAML from 'yaml';
|
|
1
|
+
import { getBlockletYAML } from './utils.js';
|
|
3
2
|
|
|
4
3
|
export default function createMetaPlugin() {
|
|
5
4
|
return {
|
|
6
5
|
name: 'blocklet:meta',
|
|
7
|
-
transformIndexHtml(html, ctx) {
|
|
6
|
+
async transformIndexHtml(html, ctx) {
|
|
8
7
|
const tags = [];
|
|
9
8
|
if (ctx?.chunk?.isEntry) {
|
|
10
9
|
let script;
|
|
@@ -17,12 +16,10 @@ export default function createMetaPlugin() {
|
|
|
17
16
|
html = html.replace('</body>', `${script}</body>`);
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
|
-
|
|
21
|
-
if (!/<title>(.*?)<\/title>/.test(html)) {
|
|
22
|
-
const blockletYamlPath = './blocklet.yml';
|
|
23
|
-
const blockletYaml = YAML.parse(fs.readFileSync(blockletYamlPath, 'utf8'));
|
|
24
|
-
const { title } = blockletYaml;
|
|
19
|
+
const { title } = await getBlockletYAML();
|
|
25
20
|
|
|
21
|
+
// 如果 index.html 中没有设置 title,则自动注入 blocklet.yml 中的 title
|
|
22
|
+
if (title && !/<title>(.*?)<\/title>/.test(html)) {
|
|
26
23
|
tags.push({
|
|
27
24
|
tag: 'title',
|
|
28
25
|
children: title,
|
package/libs/utils.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import YAML from 'yaml';
|
|
1
3
|
import Mcrypto from '@ocap/mcrypto';
|
|
2
4
|
import { toHex } from '@ocap/util';
|
|
3
5
|
import { fromPublicKey, isValid } from '@arcblock/did';
|
|
@@ -13,6 +15,13 @@ export function toBlockletDid(name) {
|
|
|
13
15
|
return fromPublicKey(pk, { role: types.RoleType.ROLE_ANY });
|
|
14
16
|
}
|
|
15
17
|
|
|
18
|
+
export async function getBlockletYAML() {
|
|
19
|
+
const blockletYamlPath = './blocklet.yml';
|
|
20
|
+
const content = await fs.readFile(blockletYamlPath, 'utf8');
|
|
21
|
+
const blockletYaml = YAML.parse(content);
|
|
22
|
+
return blockletYaml || {};
|
|
23
|
+
}
|
|
24
|
+
|
|
16
25
|
export const isInBlocklet = !!process.env.BLOCKLET_PORT;
|
|
17
26
|
export const blockletPort = process.env.BLOCKLET_PORT;
|
|
18
27
|
export const blockletPrefix = process.env.BLOCKLET_DEV_MOUNT_POINT || '/';
|
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.9",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"files": [
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"ismobilejs": "^1.1.1",
|
|
36
36
|
"mri": "^1.2.0",
|
|
37
37
|
"semver": "^7.6.2",
|
|
38
|
+
"ufo": "^1.5.3",
|
|
38
39
|
"vite-plugin-node-polyfills": "^0.22.0",
|
|
39
40
|
"yaml": "^2.4.5"
|
|
40
41
|
},
|