vite-plugin-blocklet 0.13.2 → 0.14.0
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 +82 -8
- package/index.js +1 -0
- package/libs/config.js +78 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -8,9 +8,11 @@ var YAML = require('yaml');
|
|
|
8
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');
|
|
@@ -148,7 +150,17 @@ runInit();`,
|
|
|
148
150
|
};
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
|
|
153
|
+
const SIZE = 1000;
|
|
154
|
+
const MAX_CHUNK_SIZE = 2000;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {object} options
|
|
158
|
+
* @param {number} [options.chunkSizeLimit=2048] - The chunk size limit in KB.
|
|
159
|
+
* @return {import('vite').Plugin} The Vite config plugin.
|
|
160
|
+
*/
|
|
161
|
+
function createConfigPlugin$1({ chunkSizeLimit = MAX_CHUNK_SIZE }) {
|
|
162
|
+
let resolvedConfig;
|
|
163
|
+
|
|
152
164
|
return {
|
|
153
165
|
name: 'blocklet:config',
|
|
154
166
|
configureServer(server) {
|
|
@@ -182,6 +194,11 @@ function createConfigPlugin$1() {
|
|
|
182
194
|
}
|
|
183
195
|
|
|
184
196
|
if (command === 'build') {
|
|
197
|
+
let targetConfig = {
|
|
198
|
+
build: {
|
|
199
|
+
chunkSizeWarningLimit: chunkSizeLimit,
|
|
200
|
+
},
|
|
201
|
+
};
|
|
185
202
|
if (!config.base) {
|
|
186
203
|
try {
|
|
187
204
|
let { name, did } = await getBlockletYAML();
|
|
@@ -190,19 +207,75 @@ function createConfigPlugin$1() {
|
|
|
190
207
|
}
|
|
191
208
|
if (did) {
|
|
192
209
|
const base = `/.blocklet/proxy/${did}/`;
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
};
|
|
210
|
+
targetConfig.base = base;
|
|
211
|
+
return targetConfig;
|
|
196
212
|
}
|
|
197
213
|
} catch (err) {
|
|
198
214
|
console.error(err);
|
|
199
|
-
return
|
|
215
|
+
return targetConfig;
|
|
200
216
|
}
|
|
201
217
|
}
|
|
202
218
|
}
|
|
203
219
|
|
|
204
220
|
return {};
|
|
205
221
|
},
|
|
222
|
+
configResolved(config) {
|
|
223
|
+
resolvedConfig = config;
|
|
224
|
+
},
|
|
225
|
+
closeBundle() {
|
|
226
|
+
const limitInKB = chunkSizeLimit;
|
|
227
|
+
const outDir = resolvedConfig.build.outDir || 'dist';
|
|
228
|
+
const distPath = path.resolve(resolvedConfig.root, outDir);
|
|
229
|
+
|
|
230
|
+
if (!fs.existsSync(distPath)) return;
|
|
231
|
+
|
|
232
|
+
// 递归获取所有文件(包含子目录,如 assets/)
|
|
233
|
+
const getAllFiles = (dirPath, arrayOfFiles = []) => {
|
|
234
|
+
const files = fs.readdirSync(dirPath);
|
|
235
|
+
files.forEach((file) => {
|
|
236
|
+
const fullPath = path.join(dirPath, file);
|
|
237
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
238
|
+
getAllFiles(fullPath, arrayOfFiles);
|
|
239
|
+
} else {
|
|
240
|
+
arrayOfFiles.push(fullPath);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
return arrayOfFiles;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const allFiles = getAllFiles(distPath);
|
|
247
|
+
const overSizedFiles = [];
|
|
248
|
+
|
|
249
|
+
allFiles.forEach((filePath) => {
|
|
250
|
+
// 只检查 JS 和 CSS
|
|
251
|
+
if (filePath.endsWith('.js') || filePath.endsWith('.css')) {
|
|
252
|
+
const stats = fs.statSync(filePath);
|
|
253
|
+
const sizeKB = Number((stats.size / SIZE).toFixed(2));
|
|
254
|
+
|
|
255
|
+
if (sizeKB > limitInKB) {
|
|
256
|
+
overSizedFiles.push({
|
|
257
|
+
// 显示相对路径,方便查看是哪个 chunk
|
|
258
|
+
File: path.relative(distPath, filePath),
|
|
259
|
+
'Size(KB)': sizeKB,
|
|
260
|
+
'Limit(KB)': limitInKB,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
if (overSizedFiles.length > 0) {
|
|
267
|
+
overSizedFiles.sort((a, b) => b['Size(KB)'] - a['Size(KB)']);
|
|
268
|
+
console.log('\n\x1b[41m\x1b[37m ERROR \x1b[0m \x1b[31mChunk Size Limit Exceeded:\x1b[0m');
|
|
269
|
+
console.table(overSizedFiles); // 以表格形式漂亮地打印
|
|
270
|
+
|
|
271
|
+
console.error(`\x1b[31mTotal ${overSizedFiles.length} files exceeded the ${limitInKB}KB limit.\x1b[0m\n`);
|
|
272
|
+
|
|
273
|
+
// 强制退出,确保 CI/CD 流程中断
|
|
274
|
+
process.exit(1);
|
|
275
|
+
} else {
|
|
276
|
+
console.log('\n\x1b[42m\x1b[30m DONE \x1b[0m All chunks are within the size limit.');
|
|
277
|
+
}
|
|
278
|
+
},
|
|
206
279
|
};
|
|
207
280
|
}
|
|
208
281
|
|
|
@@ -735,7 +808,7 @@ function createEmbedPlugin(options) {
|
|
|
735
808
|
lib: {
|
|
736
809
|
entry: entryItem,
|
|
737
810
|
formats: ['es'],
|
|
738
|
-
fileName: path.parse(ENTRY_FILE).name,
|
|
811
|
+
fileName: path$1.parse(ENTRY_FILE).name,
|
|
739
812
|
},
|
|
740
813
|
rollupOptions: {
|
|
741
814
|
external,
|
|
@@ -928,6 +1001,7 @@ async function setupClient(app, options = {}) {
|
|
|
928
1001
|
* @property {number} [embedBuildConcurrency=0] - The plugins to be used in the embeds.
|
|
929
1002
|
*
|
|
930
1003
|
* @property {'middleware'|'client'|'server'|'wsUpgrade'} [hmrMode='middleware'] - 当未传入任何 option 参数时,会自动变为 middleware 模式
|
|
1004
|
+
* @property {number} [chunkSizeLimit=2000] - The chunk size limit in KB.
|
|
931
1005
|
*/
|
|
932
1006
|
|
|
933
1007
|
/**
|
|
@@ -959,7 +1033,7 @@ function createBlockletPlugin(options = {}) {
|
|
|
959
1033
|
plugins.push(createMetaPlugin());
|
|
960
1034
|
}
|
|
961
1035
|
if (!disableConfig) {
|
|
962
|
-
plugins.push(createConfigPlugin$1());
|
|
1036
|
+
plugins.push(createConfigPlugin$1(restOptions));
|
|
963
1037
|
}
|
|
964
1038
|
if (!disableHmr) {
|
|
965
1039
|
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
|
-
|
|
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
|
-
|
|
47
|
-
|
|
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
|
}
|