|
1 | 1 | import fs from 'node:fs/promises';
|
2 | 2 | import { join, basename } from 'node:path';
|
3 | 3 | import * as esbuild from 'esbuild';
|
| 4 | +import { minify } from 'terser'; |
4 | 5 | import packageInfo from '../package.json' with { type: 'json' };
|
5 | 6 |
|
6 | 7 | try {
|
7 | 8 | const outputFile = join(import.meta.dirname, '../css-doodle.min.js');
|
8 | 9 | console.time('Build time');
|
9 | 10 |
|
10 |
| - const result = await esbuild.build({ |
| 11 | + const { metafile, outputFiles } = await esbuild.build({ |
11 | 12 | entryPoints: ['./src/index.js'],
|
12 |
| - outfile: outputFile, |
13 | 13 | bundle: true,
|
14 |
| - minify: true, |
| 14 | + write: false, |
15 | 15 | platform: 'browser',
|
16 | 16 | metafile: true,
|
17 | 17 | banner: {
|
18 | 18 | js: `/*! css-doodle v${packageInfo.version} MIT licensed */`,
|
19 | 19 | },
|
20 | 20 | });
|
21 | 21 |
|
22 |
| - console.log(await esbuild.analyzeMetafile(result.metafile)); |
| 22 | + const { code } = await minify(outputFiles[0].text, { |
| 23 | + compress: true, |
| 24 | + mangle: true, |
| 25 | + format: { |
| 26 | + ascii_only: true, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + await fs.writeFile(outputFile, minifyWhitespace(code), 'utf8'); |
23 | 31 |
|
24 |
| - await minifyWhitespace(outputFile); |
25 |
| - const size = await getReadableSize(outputFile); |
26 |
| - console.log(`${basename(outputFile)} - ${size}`); |
| 32 | + console.log(await esbuild.analyzeMetafile(metafile)); |
| 33 | + console.log(`${basename(outputFile)} - ${await getReadableSize(outputFile)}`); |
27 | 34 | console.timeLog(`Build time`);
|
28 |
| -} catch { |
| 35 | +} catch (e) { |
| 36 | + console.log(e); |
29 | 37 | process.exit(1);
|
30 | 38 | }
|
31 | 39 |
|
32 |
| -async function minifyWhitespace(file) { |
33 |
| - const text = await fs.readFile(file, 'utf8'); |
34 |
| - const minified = text |
35 |
| - .replace(/([>;{}])\n/g, '$1') |
36 |
| - .replace(/([:;><{`])\s+/g, '$1') |
37 |
| - .replace(/\s+([:;><}`])/g, '$1') |
| 40 | +function minifyWhitespace(input) { |
| 41 | + return input |
| 42 | + .replace(/([>;{}])\\n/g, '$1') |
| 43 | + .replace(/([:;><{\`])\s+/g, '$1') |
| 44 | + .replace(/\s+([:;><}{\`])/g, '$1') |
38 | 45 | .replace(/\s+([{}])\s+/g, '$1')
|
39 |
| - .replace(/(\})\s+/g, '$1 ') |
40 | 46 | .replace(/>\s+</g, '><')
|
41 |
| - await fs.writeFile(file, minified, 'utf8'); |
| 47 | + .replace(/\s{2,}/g, ' '); |
42 | 48 | }
|
43 | 49 |
|
44 | 50 | async function getReadableSize(file) {
|
|
0 commit comments