Skip to content

Commit a66c2b8

Browse files
committed
chore: update package configuration and add minification support
- Change main entry point to use minified bundle - Add minification script using Terser - Introduce bundle analysis script to evaluate file sizes - Update Rolldown configuration for minification and tree-shaking options - Include coverage directory in .gitignore
1 parent f932229 commit a66c2b8

File tree

6 files changed

+186
-31
lines changed

6 files changed

+186
-31
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ yarn-error.log
1919
package-lock.json
2020
metadata.json
2121
.eslintcache
22+
coverage/

Diff for: packages/core/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
"version": "2.0.2",
44
"description": "React hooks for Web Workers with TypeScript support",
55
"type": "module",
6-
"main": "./dist/bundle.js",
6+
"main": "./dist/bundle.min.js",
77
"types": "./dist/index.d.ts",
88
"exports": {
99
".": {
1010
"types": "./dist/index.d.ts",
11-
"import": "./dist/bundle.js",
12-
"default": "./dist/bundle.js"
11+
"import": "./dist/bundle.min.js",
12+
"default": "./dist/bundle.min.js"
1313
}
1414
},
1515
"files": [
1616
"dist",
1717
"dist/index.d.ts",
18+
"dist/bundle.min.js",
1819
"README.md",
1920
"README_CN.md"
2021
],
2122
"scripts": {
2223
"dev": "node scripts/dev.js --watch",
23-
"build": "node scripts/copy-readme.js && node scripts/dev.js && rolldown -c",
24+
"build": "node scripts/copy-readme.js && node scripts/dev.js && rolldown -c && node scripts/minify.js",
2425
"test:watch": "vitest",
2526
"test": "vitest run --coverage",
2627
"lint": "eslint src --ext .ts,.tsx",
@@ -75,6 +76,7 @@
7576
"jsdom": "^24.0.0",
7677
"react": "^18.2.0",
7778
"rolldown": "1.0.0-beta.6",
79+
"terser": "^5.39.0",
7880
"typescript": "^4.5.0",
7981
"vitest": "^1.0.0"
8082
},

Diff for: packages/core/rolldown.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,12 @@ export default {
77
},
88
],
99
external: ['react'],
10+
minify: true,
11+
treeshake: {
12+
moduleSideEffects: false,
13+
propertyReadSideEffects: false,
14+
tryCatchDeoptimization: false,
15+
},
16+
mangleProps: /^_/,
17+
target: 'es2020',
1018
};

Diff for: packages/core/scripts/analyze-bundle.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
import zlib from 'zlib';
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
const distPath = path.resolve(__dirname, '../dist');
8+
9+
function formatSize(size) {
10+
return `${(size / 1024).toFixed(2)} KB`;
11+
}
12+
13+
function analyzeFile(filepath) {
14+
const content = fs.readFileSync(filepath, 'utf8');
15+
const gzipped = zlib.gzipSync(content);
16+
const brotli = zlib.brotliCompressSync(content);
17+
18+
console.log(`\nFile: ${path.basename(filepath)}`);
19+
console.log(`Raw size: ${formatSize(content.length)}`);
20+
console.log(`Gzipped: ${formatSize(gzipped.length)}`);
21+
console.log(`Brotli: ${formatSize(brotli.length)}`);
22+
23+
return {
24+
raw: content.length,
25+
gzip: gzipped.length,
26+
brotli: brotli.length,
27+
};
28+
}
29+
30+
function analyzeBundles() {
31+
console.log('Analyzing bundles in dist directory...');
32+
33+
const bundlePath = path.join(distPath, 'bundle.js');
34+
const minPath = path.join(distPath, 'bundle.min.js');
35+
36+
if (fs.existsSync(bundlePath)) {
37+
analyzeFile(bundlePath);
38+
} else {
39+
console.log('Original bundle.js not found');
40+
}
41+
42+
if (fs.existsSync(minPath)) {
43+
analyzeFile(minPath);
44+
} else {
45+
console.log('Minified bundle.min.js not found');
46+
}
47+
}
48+
49+
analyzeBundles();

Diff for: packages/core/scripts/minify.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { minify } from 'terser';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import { fileURLToPath } from 'url';
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
const bundlePath = path.resolve(__dirname, '../dist/bundle.js');
8+
const minifiedPath = path.resolve(__dirname, '../dist/bundle.min.js');
9+
10+
async function minifyBundle() {
11+
console.log('Reading bundle...');
12+
const code = fs.readFileSync(bundlePath, 'utf8');
13+
14+
console.log('Minifying bundle...');
15+
const result = await minify(code, {
16+
compress: {
17+
drop_console: true,
18+
drop_debugger: true,
19+
pure_getters: true,
20+
unsafe: true,
21+
unsafe_arrows: true,
22+
unsafe_comps: true,
23+
unsafe_Function: true,
24+
unsafe_math: true,
25+
unsafe_methods: true,
26+
unsafe_proto: true,
27+
unsafe_regexp: true,
28+
unsafe_undefined: true,
29+
passes: 3,
30+
},
31+
mangle: {
32+
properties: {
33+
regex: /^_/,
34+
},
35+
},
36+
format: {
37+
comments: false,
38+
ecma: 2020,
39+
},
40+
ecma: 2020,
41+
module: true,
42+
});
43+
44+
if (result.code) {
45+
fs.writeFileSync(minifiedPath, result.code);
46+
console.log(`Original size: ${(code.length / 1024).toFixed(2)} KB`);
47+
console.log(`Minified size: ${(result.code.length / 1024).toFixed(2)} KB`);
48+
console.log(`Reduction: ${(100 - (result.code.length / code.length) * 100).toFixed(2)}%`);
49+
50+
// 更新package.json以使用最小化版本
51+
const packageJsonPath = path.resolve(__dirname, '../package.json');
52+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
53+
packageJson.exports['.'].import = './dist/bundle.min.js';
54+
packageJson.exports['.'].default = './dist/bundle.min.js';
55+
packageJson.main = './dist/bundle.min.js';
56+
57+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
58+
console.log('Updated package.json to use minified bundle');
59+
} else {
60+
console.error('Minification failed');
61+
}
62+
}
63+
64+
minifyBundle().catch(console.error);

0 commit comments

Comments
 (0)