vite-plugin-blocklet 0.13.2 → 0.14.1

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
@@ -5,12 +5,14 @@ var vite = require('vite');
5
5
  var semver = require('semver');
6
6
  var node_fs = require('node:fs');
7
7
  var YAML = require('yaml');
8
- var Mcrypto = require('@ocap/mcrypto');
8
+ var mcrypto = require('@ocap/mcrypto');
9
9
  var util = require('@ocap/util');
10
10
  var did = require('@arcblock/did');
11
+ var fs = require('fs');
12
+ var path = require('path');
11
13
  var ufo = require('ufo');
12
14
  var isMobile = require('ismobilejs');
13
- var path = require('node:path');
15
+ var path$1 = require('node:path');
14
16
  var mlly = require('mlly');
15
17
  var externalGlobals = require('rollup-plugin-external-globals');
16
18
  var pMap = require('p-map');
@@ -18,15 +20,13 @@ var getPort = require('get-port');
18
20
  var mri = require('mri');
19
21
  var httpProxyMiddleware = require('http-proxy-middleware');
20
22
 
21
- const { types } = Mcrypto;
22
-
23
23
  function toBlockletDid(name) {
24
24
  if (did.isValid(name)) {
25
25
  return name;
26
26
  }
27
27
 
28
28
  const pk = util.toHex(Buffer.from(typeof name === 'string' ? name.trim() : name));
29
- return did.fromPublicKey(pk, { role: types.RoleType.ROLE_ANY });
29
+ return did.fromPublicKey(pk, { role: mcrypto.types.RoleType.ROLE_ANY });
30
30
  }
31
31
 
32
32
  async function getBlockletYAML() {
@@ -148,7 +148,17 @@ runInit();`,
148
148
  };
149
149
  }
150
150
 
151
- function createConfigPlugin$1() {
151
+ const SIZE = 1000;
152
+ const MAX_CHUNK_SIZE = 2000;
153
+
154
+ /**
155
+ * @param {object} options
156
+ * @param {number} [options.chunkSizeLimit=2048] - The chunk size limit in KB.
157
+ * @return {import('vite').Plugin} The Vite config plugin.
158
+ */
159
+ function createConfigPlugin$1({ chunkSizeLimit = MAX_CHUNK_SIZE }) {
160
+ let resolvedConfig;
161
+
152
162
  return {
153
163
  name: 'blocklet:config',
154
164
  configureServer(server) {
@@ -182,6 +192,11 @@ function createConfigPlugin$1() {
182
192
  }
183
193
 
184
194
  if (command === 'build') {
195
+ let targetConfig = {
196
+ build: {
197
+ chunkSizeWarningLimit: chunkSizeLimit,
198
+ },
199
+ };
185
200
  if (!config.base) {
186
201
  try {
187
202
  let { name, did } = await getBlockletYAML();
@@ -190,19 +205,75 @@ function createConfigPlugin$1() {
190
205
  }
191
206
  if (did) {
192
207
  const base = `/.blocklet/proxy/${did}/`;
193
- return {
194
- base,
195
- };
208
+ targetConfig.base = base;
209
+ return targetConfig;
196
210
  }
197
211
  } catch (err) {
198
212
  console.error(err);
199
- return {};
213
+ return targetConfig;
200
214
  }
201
215
  }
202
216
  }
203
217
 
204
218
  return {};
205
219
  },
220
+ configResolved(config) {
221
+ resolvedConfig = config;
222
+ },
223
+ closeBundle() {
224
+ const limitInKB = chunkSizeLimit;
225
+ const outDir = resolvedConfig.build.outDir || 'dist';
226
+ const distPath = path.resolve(resolvedConfig.root, outDir);
227
+
228
+ if (!fs.existsSync(distPath)) return;
229
+
230
+ // 递归获取所有文件(包含子目录,如 assets/)
231
+ const getAllFiles = (dirPath, arrayOfFiles = []) => {
232
+ const files = fs.readdirSync(dirPath);
233
+ files.forEach((file) => {
234
+ const fullPath = path.join(dirPath, file);
235
+ if (fs.statSync(fullPath).isDirectory()) {
236
+ getAllFiles(fullPath, arrayOfFiles);
237
+ } else {
238
+ arrayOfFiles.push(fullPath);
239
+ }
240
+ });
241
+ return arrayOfFiles;
242
+ };
243
+
244
+ const allFiles = getAllFiles(distPath);
245
+ const overSizedFiles = [];
246
+
247
+ allFiles.forEach((filePath) => {
248
+ // 只检查 JS 和 CSS
249
+ if (filePath.endsWith('.js') || filePath.endsWith('.css')) {
250
+ const stats = fs.statSync(filePath);
251
+ const sizeKB = Number((stats.size / SIZE).toFixed(2));
252
+
253
+ if (sizeKB > limitInKB) {
254
+ overSizedFiles.push({
255
+ // 显示相对路径,方便查看是哪个 chunk
256
+ File: path.relative(distPath, filePath),
257
+ 'Size(KB)': sizeKB,
258
+ 'Limit(KB)': limitInKB,
259
+ });
260
+ }
261
+ }
262
+ });
263
+
264
+ if (overSizedFiles.length > 0) {
265
+ overSizedFiles.sort((a, b) => b['Size(KB)'] - a['Size(KB)']);
266
+ console.log('\n\x1b[41m\x1b[37m ERROR \x1b[0m \x1b[31mChunk Size Limit Exceeded:\x1b[0m');
267
+ console.table(overSizedFiles); // 以表格形式漂亮地打印
268
+
269
+ console.error(`\x1b[31mTotal ${overSizedFiles.length} files exceeded the ${limitInKB}KB limit.\x1b[0m\n`);
270
+
271
+ // 强制退出,确保 CI/CD 流程中断
272
+ process.exit(1);
273
+ } else {
274
+ console.log('\n\x1b[42m\x1b[30m DONE \x1b[0m All chunks are within the size limit.');
275
+ }
276
+ },
206
277
  };
207
278
  }
208
279
 
@@ -735,7 +806,7 @@ function createEmbedPlugin(options) {
735
806
  lib: {
736
807
  entry: entryItem,
737
808
  formats: ['es'],
738
- fileName: path.parse(ENTRY_FILE).name,
809
+ fileName: path$1.parse(ENTRY_FILE).name,
739
810
  },
740
811
  rollupOptions: {
741
812
  external,
@@ -928,6 +999,7 @@ async function setupClient(app, options = {}) {
928
999
  * @property {number} [embedBuildConcurrency=0] - The plugins to be used in the embeds.
929
1000
  *
930
1001
  * @property {'middleware'|'client'|'server'|'wsUpgrade'} [hmrMode='middleware'] - 当未传入任何 option 参数时,会自动变为 middleware 模式
1002
+ * @property {number} [chunkSizeLimit=2000] - The chunk size limit in KB.
931
1003
  */
932
1004
 
933
1005
  /**
@@ -959,7 +1031,7 @@ function createBlockletPlugin(options = {}) {
959
1031
  plugins.push(createMetaPlugin());
960
1032
  }
961
1033
  if (!disableConfig) {
962
- plugins.push(createConfigPlugin$1());
1034
+ plugins.push(createConfigPlugin$1(restOptions));
963
1035
  }
964
1036
  if (!disableHmr) {
965
1037
  plugins.push(createHmrPlugin(restOptions));
package/index.js CHANGED
@@ -41,6 +41,7 @@ import setupClient from './libs/client.js';
41
41
  * @property {number} [embedBuildConcurrency=0] - The plugins to be used in the embeds.
42
42
  *
43
43
  * @property {'middleware'|'client'|'server'|'wsUpgrade'} [hmrMode='middleware'] - 当未传入任何 option 参数时,会自动变为 middleware 模式
44
+ * @property {number} [chunkSizeLimit=2000] - The chunk size limit in KB.
44
45
  */
45
46
 
46
47
  /**
package/libs/config.js CHANGED
@@ -1,7 +1,19 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
1
3
  import { isEqual, joinURL, withTrailingSlash } from 'ufo';
2
4
  import { toBlockletDid, isInBlocklet, blockletPort, blockletPrefix, getBlockletYAML } from './utils.js';
3
5
 
4
- export default function createConfigPlugin() {
6
+ const SIZE = 1000;
7
+ const MAX_CHUNK_SIZE = 2000;
8
+
9
+ /**
10
+ * @param {object} options
11
+ * @param {number} [options.chunkSizeLimit=2048] - The chunk size limit in KB.
12
+ * @return {import('vite').Plugin} The Vite config plugin.
13
+ */
14
+ export default function createConfigPlugin({ chunkSizeLimit = MAX_CHUNK_SIZE }) {
15
+ let resolvedConfig;
16
+
5
17
  return {
6
18
  name: 'blocklet:config',
7
19
  configureServer(server) {
@@ -35,6 +47,11 @@ export default function createConfigPlugin() {
35
47
  }
36
48
 
37
49
  if (command === 'build') {
50
+ let targetConfig = {
51
+ build: {
52
+ chunkSizeWarningLimit: chunkSizeLimit,
53
+ },
54
+ };
38
55
  if (!config.base) {
39
56
  try {
40
57
  let { name, did } = await getBlockletYAML();
@@ -43,18 +60,74 @@ export default function createConfigPlugin() {
43
60
  }
44
61
  if (did) {
45
62
  const base = `/.blocklet/proxy/${did}/`;
46
- return {
47
- base,
48
- };
63
+ targetConfig.base = base;
64
+ return targetConfig;
49
65
  }
50
66
  } catch (err) {
51
67
  console.error(err);
52
- return {};
68
+ return targetConfig;
53
69
  }
54
70
  }
55
71
  }
56
72
 
57
73
  return {};
58
74
  },
75
+ configResolved(config) {
76
+ resolvedConfig = config;
77
+ },
78
+ closeBundle() {
79
+ const limitInKB = chunkSizeLimit;
80
+ const outDir = resolvedConfig.build.outDir || 'dist';
81
+ const distPath = path.resolve(resolvedConfig.root, outDir);
82
+
83
+ if (!fs.existsSync(distPath)) return;
84
+
85
+ // 递归获取所有文件(包含子目录,如 assets/)
86
+ const getAllFiles = (dirPath, arrayOfFiles = []) => {
87
+ const files = fs.readdirSync(dirPath);
88
+ files.forEach((file) => {
89
+ const fullPath = path.join(dirPath, file);
90
+ if (fs.statSync(fullPath).isDirectory()) {
91
+ getAllFiles(fullPath, arrayOfFiles);
92
+ } else {
93
+ arrayOfFiles.push(fullPath);
94
+ }
95
+ });
96
+ return arrayOfFiles;
97
+ };
98
+
99
+ const allFiles = getAllFiles(distPath);
100
+ const overSizedFiles = [];
101
+
102
+ allFiles.forEach((filePath) => {
103
+ // 只检查 JS 和 CSS
104
+ if (filePath.endsWith('.js') || filePath.endsWith('.css')) {
105
+ const stats = fs.statSync(filePath);
106
+ const sizeKB = Number((stats.size / SIZE).toFixed(2));
107
+
108
+ if (sizeKB > limitInKB) {
109
+ overSizedFiles.push({
110
+ // 显示相对路径,方便查看是哪个 chunk
111
+ File: path.relative(distPath, filePath),
112
+ 'Size(KB)': sizeKB,
113
+ 'Limit(KB)': limitInKB,
114
+ });
115
+ }
116
+ }
117
+ });
118
+
119
+ if (overSizedFiles.length > 0) {
120
+ overSizedFiles.sort((a, b) => b['Size(KB)'] - a['Size(KB)']);
121
+ console.log('\n\x1b[41m\x1b[37m ERROR \x1b[0m \x1b[31mChunk Size Limit Exceeded:\x1b[0m');
122
+ console.table(overSizedFiles); // 以表格形式漂亮地打印
123
+
124
+ console.error(`\x1b[31mTotal ${overSizedFiles.length} files exceeded the ${limitInKB}KB limit.\x1b[0m\n`);
125
+
126
+ // 强制退出,确保 CI/CD 流程中断
127
+ process.exit(1);
128
+ } else {
129
+ console.log('\n\x1b[42m\x1b[30m DONE \x1b[0m All chunks are within the size limit.');
130
+ }
131
+ },
59
132
  };
60
133
  }
package/libs/utils.js CHANGED
@@ -1,11 +1,9 @@
1
1
  import { promises as fs } from 'node:fs';
2
2
  import YAML from 'yaml';
3
- import Mcrypto from '@ocap/mcrypto';
3
+ import { types } from '@ocap/mcrypto';
4
4
  import { toHex } from '@ocap/util';
5
5
  import { fromPublicKey, isValid } from '@arcblock/did';
6
6
 
7
- const { types } = Mcrypto;
8
-
9
7
  export function toBlockletDid(name) {
10
8
  if (isValid(name)) {
11
9
  return name;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-blocklet",
3
3
  "type": "module",
4
- "version": "0.13.2",
4
+ "version": "0.14.1",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -30,9 +30,9 @@
30
30
  "rollup": "^4.34.8"
31
31
  },
32
32
  "dependencies": {
33
- "@arcblock/did": "^1.27.15",
34
- "@ocap/mcrypto": "^1.27.15",
35
- "@ocap/util": "^1.27.15",
33
+ "@arcblock/did": "^1.28.0",
34
+ "@ocap/mcrypto": "^1.28.0",
35
+ "@ocap/util": "^1.28.0",
36
36
  "get-port": "^5.1.1",
37
37
  "http-proxy-middleware": "^3.0.3",
38
38
  "ismobilejs": "^1.1.1",