vite-plugin-blocklet 0.6.3 → 0.6.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 CHANGED
@@ -37,7 +37,8 @@ function toBlockletDid(name) {
37
37
 
38
38
  const isInBlocklet = !!process.env.BLOCKLET_PORT;
39
39
 
40
- function createHmrPlugin({ version = vite.version } = {}) {
40
+ function createHmrPlugin(options = {}) {
41
+ const { version = vite.version } = options;
41
42
  return {
42
43
  name: 'blocklet:hmr',
43
44
  apply: 'serve',
@@ -158,6 +159,124 @@ function createMetaPlugin() {
158
159
  };
159
160
  }
160
161
 
162
+ /**
163
+ * @typedef {{
164
+ * color: string;
165
+ * image: string;
166
+ * }} GenerateOptions
167
+ */
168
+ /**
169
+ * @typedef {{
170
+ * loadingElementId?: string;
171
+ * loadingColor?: string;
172
+ * loadingImage?: string;
173
+ * }} LoadingPluginOptions
174
+ */
175
+
176
+ /**
177
+ *
178
+ * @param {GenerateOptions} options
179
+ */
180
+ function generateHtml({ color, image }) {
181
+ return `<style>
182
+ body,
183
+ html,
184
+ #app {
185
+ background: white;
186
+ width: 100%;
187
+ height: 100%;
188
+ display: flex;
189
+ flex-direction: column;
190
+ justify-content: center;
191
+ align-items: center;
192
+ margin: 0;
193
+ padding: 0;
194
+ }
195
+
196
+ .spinner {
197
+ width: 70px;
198
+ text-align: center;
199
+ }
200
+
201
+ .spinner > div {
202
+ width: 18px;
203
+ height: 18px;
204
+ background-color: ${color};
205
+
206
+ border-radius: 100%;
207
+ display: inline-block;
208
+ -webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
209
+ animation: sk-bouncedelay 1.4s infinite ease-in-out both;
210
+ }
211
+
212
+ .spinner .bounce1 {
213
+ -webkit-animation-delay: -0.32s;
214
+ animation-delay: -0.32s;
215
+ }
216
+
217
+ .spinner .bounce2 {
218
+ -webkit-animation-delay: -0.16s;
219
+ animation-delay: -0.16s;
220
+ }
221
+
222
+ @-webkit-keyframes sk-bouncedelay {
223
+ 0%,
224
+ 80%,
225
+ 100% {
226
+ -webkit-transform: scale(0);
227
+ }
228
+ 40% {
229
+ -webkit-transform: scale(1);
230
+ }
231
+ }
232
+
233
+ @keyframes sk-bouncedelay {
234
+ 0%,
235
+ 80%,
236
+ 100% {
237
+ -webkit-transform: scale(0);
238
+ transform: scale(0);
239
+ }
240
+ 40% {
241
+ -webkit-transform: scale(1);
242
+ transform: scale(1);
243
+ }
244
+ }
245
+ </style>
246
+
247
+ <img src="${image}" width="90" height="90" style="margin-bottom: 30px" />
248
+
249
+ <div class="spinner">
250
+ <div class="bounce1"></div>
251
+ <div class="bounce2"></div>
252
+ <div class="bounce3"></div>
253
+ </div>`;
254
+ }
255
+
256
+ /**
257
+ *
258
+ * @param {LoadingPluginOptions} options
259
+ * @returns
260
+ */
261
+ function createLoadingPlugin({
262
+ loadingElementId = 'app',
263
+ loadingColor = '#8abaf0',
264
+ loadingImage = '/.well-known/service/blocklet/logo/',
265
+ } = {}) {
266
+ const injectHtml = generateHtml({
267
+ color: loadingColor,
268
+ image: loadingImage,
269
+ });
270
+
271
+ return {
272
+ name: 'blocklet:loading',
273
+ enforce: 'post',
274
+ transformIndexHtml(html) {
275
+ return html.replace(`<div id="${loadingElementId}"></div>`, `<div id="${loadingElementId}">${injectHtml}</div>`);
276
+ },
277
+ };
278
+ }
279
+
161
280
  const argv = process.argv.slice(2);
162
281
  const isProduction = process.env.NODE_ENV === 'production' || process.env.ABT_NODE_SERVICE_ENV === 'production';
163
282
 
@@ -189,6 +308,25 @@ async function setupClient(app, options = {}) {
189
308
  }
190
309
  }
191
310
 
311
+ /**
312
+ * @typedef {{
313
+ * disableNodePolyfills?: boolean;
314
+ * disableConfig?: boolean;
315
+ * disableMeta?: boolean;
316
+ * disableHmr?: boolean;
317
+ * disableLoading?: boolean;
318
+ *
319
+ * loadingElementId?: string;
320
+ * loadingColor?: string;
321
+ * loadingImage?: string;
322
+ * }} PluginOptions
323
+ */
324
+
325
+ /**
326
+ *
327
+ * @param {PluginOptions} options
328
+ * @returns
329
+ */
192
330
  function createBlockletPlugin(options = {}) {
193
331
  const {
194
332
  // NOTICE: 由于 polyfill 不是每个项目都必须的,并且有些现有的项目已经配置了 polyfill,所以这个配置默认 disable 会比较好
@@ -197,6 +335,8 @@ function createBlockletPlugin(options = {}) {
197
335
  disableConfig = false,
198
336
  disableMeta = false,
199
337
  disableHmr = false,
338
+ disableLoading = false,
339
+ ...restOptions
200
340
  } = options;
201
341
  const plugins = [];
202
342
  if (!disableMeta) {
@@ -206,11 +346,14 @@ function createBlockletPlugin(options = {}) {
206
346
  plugins.push(createConfigPlugin());
207
347
  }
208
348
  if (!disableHmr) {
209
- plugins.push(createHmrPlugin(options));
349
+ plugins.push(createHmrPlugin(restOptions));
210
350
  }
211
351
  if (!disableNodePolyfills) {
212
352
  plugins.push(vitePluginNodePolyfills.nodePolyfills({ protocolImports: true }));
213
353
  }
354
+ if (!disableLoading) {
355
+ plugins.push(createLoadingPlugin(restOptions));
356
+ }
214
357
 
215
358
  return plugins;
216
359
  }
@@ -221,6 +364,7 @@ Object.defineProperty(exports, 'nodePolyfills', {
221
364
  });
222
365
  exports.createBlockletConfig = createConfigPlugin;
223
366
  exports.createBlockletHmr = createHmrPlugin;
367
+ exports.createBlockletLoading = createLoadingPlugin;
224
368
  exports.createBlockletMeta = createMetaPlugin;
225
369
  exports.createBlockletPlugin = createBlockletPlugin;
226
370
  exports.setupClient = setupClient;
package/index.js CHANGED
@@ -2,8 +2,28 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills';
2
2
  import createHmrPlugin from './libs/hmr.js';
3
3
  import createConfigPlugin from './libs/config.js';
4
4
  import createMetaPlugin from './libs/meta.js';
5
+ import createLoadingPlugin from './libs/loading.js';
5
6
  import setupClient from './libs/client.js';
6
7
 
8
+ /**
9
+ * @typedef {{
10
+ * disableNodePolyfills?: boolean;
11
+ * disableConfig?: boolean;
12
+ * disableMeta?: boolean;
13
+ * disableHmr?: boolean;
14
+ * disableLoading?: boolean;
15
+ *
16
+ * loadingElementId?: string;
17
+ * loadingColor?: string;
18
+ * loadingImage?: string;
19
+ * }} PluginOptions
20
+ */
21
+
22
+ /**
23
+ *
24
+ * @param {PluginOptions} options
25
+ * @returns
26
+ */
7
27
  export function createBlockletPlugin(options = {}) {
8
28
  const {
9
29
  // NOTICE: 由于 polyfill 不是每个项目都必须的,并且有些现有的项目已经配置了 polyfill,所以这个配置默认 disable 会比较好
@@ -12,20 +32,25 @@ export function createBlockletPlugin(options = {}) {
12
32
  disableConfig = false,
13
33
  disableMeta = false,
14
34
  disableHmr = false,
35
+ disableLoading = false,
36
+ ...restOptions
15
37
  } = options;
16
38
  const plugins = [];
17
39
  if (!disableMeta) {
18
- plugins.push(createMetaPlugin(options));
40
+ plugins.push(createMetaPlugin(restOptions));
19
41
  }
20
42
  if (!disableConfig) {
21
- plugins.push(createConfigPlugin(options));
43
+ plugins.push(createConfigPlugin(restOptions));
22
44
  }
23
45
  if (!disableHmr) {
24
- plugins.push(createHmrPlugin(options));
46
+ plugins.push(createHmrPlugin(restOptions));
25
47
  }
26
48
  if (!disableNodePolyfills) {
27
49
  plugins.push(nodePolyfills({ protocolImports: true }));
28
50
  }
51
+ if (!disableLoading) {
52
+ plugins.push(createLoadingPlugin(restOptions));
53
+ }
29
54
 
30
55
  return plugins;
31
56
  }
@@ -35,5 +60,6 @@ export {
35
60
  createHmrPlugin as createBlockletHmr,
36
61
  createConfigPlugin as createBlockletConfig,
37
62
  createMetaPlugin as createBlockletMeta,
63
+ createLoadingPlugin as createBlockletLoading,
38
64
  nodePolyfills,
39
65
  };
package/libs/hmr.js CHANGED
@@ -2,7 +2,8 @@ import { version as viteVersion } from 'vite';
2
2
  import semver from 'semver';
3
3
  import { isInBlocklet } from './utils.js';
4
4
 
5
- export default function createHmrPlugin({ version = viteVersion } = {}) {
5
+ export default function createHmrPlugin(options = {}) {
6
+ const { version = viteVersion } = options;
6
7
  return {
7
8
  name: 'blocklet:hmr',
8
9
  apply: 'serve',
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @typedef {{
3
+ * color: string;
4
+ * image: string;
5
+ * }} GenerateOptions
6
+ */
7
+ /**
8
+ * @typedef {{
9
+ * loadingElementId?: string;
10
+ * loadingColor?: string;
11
+ * loadingImage?: string;
12
+ * }} LoadingPluginOptions
13
+ */
14
+
15
+ /**
16
+ *
17
+ * @param {GenerateOptions} options
18
+ */
19
+ function generateHtml({ color, image }) {
20
+ return `<style>
21
+ body,
22
+ html,
23
+ #app {
24
+ background: white;
25
+ width: 100%;
26
+ height: 100%;
27
+ display: flex;
28
+ flex-direction: column;
29
+ justify-content: center;
30
+ align-items: center;
31
+ margin: 0;
32
+ padding: 0;
33
+ }
34
+
35
+ .spinner {
36
+ width: 70px;
37
+ text-align: center;
38
+ }
39
+
40
+ .spinner > div {
41
+ width: 18px;
42
+ height: 18px;
43
+ background-color: ${color};
44
+
45
+ border-radius: 100%;
46
+ display: inline-block;
47
+ -webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;
48
+ animation: sk-bouncedelay 1.4s infinite ease-in-out both;
49
+ }
50
+
51
+ .spinner .bounce1 {
52
+ -webkit-animation-delay: -0.32s;
53
+ animation-delay: -0.32s;
54
+ }
55
+
56
+ .spinner .bounce2 {
57
+ -webkit-animation-delay: -0.16s;
58
+ animation-delay: -0.16s;
59
+ }
60
+
61
+ @-webkit-keyframes sk-bouncedelay {
62
+ 0%,
63
+ 80%,
64
+ 100% {
65
+ -webkit-transform: scale(0);
66
+ }
67
+ 40% {
68
+ -webkit-transform: scale(1);
69
+ }
70
+ }
71
+
72
+ @keyframes sk-bouncedelay {
73
+ 0%,
74
+ 80%,
75
+ 100% {
76
+ -webkit-transform: scale(0);
77
+ transform: scale(0);
78
+ }
79
+ 40% {
80
+ -webkit-transform: scale(1);
81
+ transform: scale(1);
82
+ }
83
+ }
84
+ </style>
85
+
86
+ <img src="${image}" width="90" height="90" style="margin-bottom: 30px" />
87
+
88
+ <div class="spinner">
89
+ <div class="bounce1"></div>
90
+ <div class="bounce2"></div>
91
+ <div class="bounce3"></div>
92
+ </div>`;
93
+ }
94
+
95
+ /**
96
+ *
97
+ * @param {LoadingPluginOptions} options
98
+ * @returns
99
+ */
100
+ export default function createLoadingPlugin({
101
+ loadingElementId = 'app',
102
+ loadingColor = '#8abaf0',
103
+ loadingImage = '/.well-known/service/blocklet/logo/',
104
+ } = {}) {
105
+ const injectHtml = generateHtml({
106
+ color: loadingColor,
107
+ image: loadingImage,
108
+ });
109
+
110
+ return {
111
+ name: 'blocklet:loading',
112
+ enforce: 'post',
113
+ transformIndexHtml(html) {
114
+ return html.replace(`<div id="${loadingElementId}"></div>`, `<div id="${loadingElementId}">${injectHtml}</div>`);
115
+ },
116
+ };
117
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-blocklet",
3
3
  "type": "module",
4
- "version": "0.6.3",
4
+ "version": "0.6.5",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "files": [