1 | import path from 'node:path';
|
2 | import postcss from 'postcss';
|
3 | import fs from 'node:fs/promises';
|
4 | import pkg from '../postcss.config.cjs';
|
5 | import { createRequire } from 'node:module';
|
6 | import { sync as brotli } from 'brotli-size';
|
7 | import { gzipSizeSync as gzip } from 'gzip-size';
|
8 |
|
9 |
|
10 | const { plugins } = pkg;
|
11 |
|
12 |
|
13 | const require = createRequire(import.meta.url);
|
14 | const { scripts } = require('../package.json');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const filtered = Object.keys(scripts)
|
21 | .filter((key) => key.startsWith('lib:'))
|
22 | .reduce((obj, key) => {
|
23 | obj[key] = scripts[key];
|
24 | return obj;
|
25 | }, {});
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | const regex = /postcss\s(?<filepath>\S+)\s\-[o]\s(?<filename>.*\.css)(?:.*$)/;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | let sizes = {}
|
50 |
|
51 | for (const [_, script] of Object.entries(filtered)) {
|
52 | const found = script.match(regex);
|
53 |
|
54 | if (!found) continue;
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | const { filepath, filename } = found.groups;
|
63 |
|
64 | |
65 |
|
66 |
|
67 | const options = { from: path.resolve(`../${filepath}`), to: undefined };
|
68 | const css = await fs.readFile(path.resolve(`../${filepath}`), 'utf-8');
|
69 | |
70 |
|
71 |
|
72 |
|
73 | const code = await postcss(plugins.slice(0, -1)).process(css, options);
|
74 | |
75 |
|
76 |
|
77 | const minified = await postcss(plugins).process(css, options);
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 | sizes[filename.replace('.min', '')] = {
|
84 | raw: code.css.length,
|
85 | size: (code.css.length / 1024).toFixed(2),
|
86 | minified: (minified.css.length / 1024).toFixed(2),
|
87 | brotli: (brotli(minified.css) / 1024).toFixed(2),
|
88 | gzip: (gzip(minified.css) / 1024).toFixed(2),
|
89 | }
|
90 | }
|
91 |
|
92 | await fs.writeFile('bundle-sizes.json', JSON.stringify(sizes, null, 2), { encoding: 'utf8' });
|