@ai.all/ds 0.0.0-stage → 0.1.0-beta.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/ALL.md +32 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +184 -2
- package/bin/ds.js +9 -0
- package/brand/ai-all.svg +5 -0
- package/brand/aiall.svg +11 -0
- package/brand/ds.svg +9 -0
- package/brand/ds7.svg +10 -0
- package/brand/ui.svg +10 -0
- package/docs/ARCHITECTURE.md +38 -0
- package/docs/CONFIG.md +50 -0
- package/docs/CRITICAL-CSS.md +36 -0
- package/docs/DELIVERY.md +43 -0
- package/docs/ECOSYSTEM.md +48 -0
- package/docs/INTEGRATION.md +79 -0
- package/docs/RELEASE.md +22 -0
- package/docs/STATIC-SITES.md +34 -0
- package/ds.config.schema.json +91 -0
- package/nuxt/module.mjs +17 -0
- package/package.json +71 -4
- package/src/build.mjs +18 -0
- package/src/cli.mjs +172 -0
- package/src/config.mjs +231 -0
- package/src/files.mjs +36 -0
- package/src/index.mjs +10 -0
- package/src/nuxt-options.mjs +27 -0
- package/src/optimize.mjs +163 -0
- package/src/report.mjs +102 -0
package/src/config.mjs
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_CONFIG = {
|
|
5
|
+
$schema:
|
|
6
|
+
'https://raw.githubusercontent.com/ai-all-ai/ds/main/ds.config.schema.json',
|
|
7
|
+
schemaVersion: 1,
|
|
8
|
+
generator: {
|
|
9
|
+
command: ['npm', 'run', 'generate'],
|
|
10
|
+
outputDir: '.output/public',
|
|
11
|
+
},
|
|
12
|
+
optimization: {
|
|
13
|
+
html: { minify: false },
|
|
14
|
+
criticalCss: {
|
|
15
|
+
enabled: false,
|
|
16
|
+
routes: [],
|
|
17
|
+
allowRules: [],
|
|
18
|
+
preload: 'body',
|
|
19
|
+
},
|
|
20
|
+
compression: { gzip: true, brotli: true },
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function isRecord(value) {
|
|
25
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function assertRecord(value, name) {
|
|
29
|
+
if (!isRecord(value)) throw new Error(`${name} must be an object.`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function assertKeys(value, name, allowed) {
|
|
33
|
+
const unexpected = Object.keys(value).find((key) => !allowed.includes(key));
|
|
34
|
+
if (unexpected) throw new Error(`Unknown ${name} option: ${unexpected}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function assertStringArray(value, name, { allowEmpty = true } = {}) {
|
|
38
|
+
if (!Array.isArray(value) || (!allowEmpty && value.length === 0)) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`${name} must be ${allowEmpty ? 'an' : 'a non-empty'} array of strings.`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
if (value.some((item) => typeof item !== 'string' || item.length === 0)) {
|
|
44
|
+
throw new Error(`${name} must contain only non-empty strings.`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function validateConfig(input) {
|
|
49
|
+
assertRecord(input, 'Configuration');
|
|
50
|
+
assertKeys(input, 'configuration', [
|
|
51
|
+
'$schema',
|
|
52
|
+
'schemaVersion',
|
|
53
|
+
'generator',
|
|
54
|
+
'optimization',
|
|
55
|
+
]);
|
|
56
|
+
if (input.$schema !== undefined && typeof input.$schema !== 'string') {
|
|
57
|
+
throw new Error('$schema must be a string.');
|
|
58
|
+
}
|
|
59
|
+
if (input.schemaVersion !== 1) {
|
|
60
|
+
throw new Error('Unsupported or missing schemaVersion; expected 1.');
|
|
61
|
+
}
|
|
62
|
+
assertRecord(input.generator, 'generator');
|
|
63
|
+
assertKeys(input.generator, 'generator', ['command', 'outputDir']);
|
|
64
|
+
assertStringArray(input.generator.command, 'generator.command', {
|
|
65
|
+
allowEmpty: false,
|
|
66
|
+
});
|
|
67
|
+
if (
|
|
68
|
+
typeof input.generator.outputDir !== 'string' ||
|
|
69
|
+
!input.generator.outputDir.trim()
|
|
70
|
+
) {
|
|
71
|
+
throw new Error('generator.outputDir must be a non-empty path.');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
assertRecord(input.optimization, 'optimization');
|
|
75
|
+
assertKeys(input.optimization, 'optimization', [
|
|
76
|
+
'html',
|
|
77
|
+
'criticalCss',
|
|
78
|
+
'compression',
|
|
79
|
+
]);
|
|
80
|
+
assertRecord(input.optimization.html, 'optimization.html');
|
|
81
|
+
assertKeys(input.optimization.html, 'optimization.html', ['minify']);
|
|
82
|
+
if (typeof input.optimization.html.minify !== 'boolean') {
|
|
83
|
+
throw new Error('optimization.html.minify must be a boolean.');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
assertRecord(input.optimization.criticalCss, 'optimization.criticalCss');
|
|
87
|
+
const critical = input.optimization.criticalCss;
|
|
88
|
+
assertKeys(critical, 'optimization.criticalCss', [
|
|
89
|
+
'enabled',
|
|
90
|
+
'routes',
|
|
91
|
+
'allowRules',
|
|
92
|
+
'preload',
|
|
93
|
+
]);
|
|
94
|
+
if (typeof critical.enabled !== 'boolean') {
|
|
95
|
+
throw new Error('optimization.criticalCss.enabled must be a boolean.');
|
|
96
|
+
}
|
|
97
|
+
assertStringArray(critical.routes, 'optimization.criticalCss.routes');
|
|
98
|
+
assertStringArray(critical.allowRules, 'optimization.criticalCss.allowRules');
|
|
99
|
+
if (critical.preload !== 'body' && critical.preload !== false) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
'optimization.criticalCss.preload must be "body" or false.',
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
if (critical.enabled && critical.routes.length === 0) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
'List at least one path in optimization.criticalCss.routes when critical CSS is enabled.',
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
for (const route of critical.routes) {
|
|
110
|
+
if (
|
|
111
|
+
route.includes('\\') ||
|
|
112
|
+
path.posix.isAbsolute(route) ||
|
|
113
|
+
route.split('/').some((part) => part === '..') ||
|
|
114
|
+
!route.toLowerCase().endsWith('.html')
|
|
115
|
+
) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`Critical CSS route must be a relative .html path without "..": ${route}`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
assertRecord(input.optimization.compression, 'optimization.compression');
|
|
123
|
+
assertKeys(input.optimization.compression, 'optimization.compression', [
|
|
124
|
+
'gzip',
|
|
125
|
+
'brotli',
|
|
126
|
+
]);
|
|
127
|
+
for (const algorithm of ['gzip', 'brotli']) {
|
|
128
|
+
if (typeof input.optimization.compression[algorithm] !== 'boolean') {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`optimization.compression.${algorithm} must be a boolean.`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
...(input.$schema ? { $schema: input.$schema } : {}),
|
|
137
|
+
schemaVersion: 1,
|
|
138
|
+
generator: {
|
|
139
|
+
command: [...input.generator.command],
|
|
140
|
+
outputDir: input.generator.outputDir,
|
|
141
|
+
},
|
|
142
|
+
optimization: {
|
|
143
|
+
html: { minify: input.optimization.html.minify },
|
|
144
|
+
criticalCss: {
|
|
145
|
+
enabled: critical.enabled,
|
|
146
|
+
routes: [...critical.routes],
|
|
147
|
+
allowRules: [...critical.allowRules],
|
|
148
|
+
preload: critical.preload,
|
|
149
|
+
},
|
|
150
|
+
compression: { ...input.optimization.compression },
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export async function loadConfig(projectRoot, configFile = 'ds.config.json') {
|
|
156
|
+
const configPath = path.resolve(projectRoot, configFile);
|
|
157
|
+
let contents;
|
|
158
|
+
try {
|
|
159
|
+
contents = await fs.readFile(configPath, 'utf8');
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error.code === 'ENOENT') {
|
|
162
|
+
throw new Error(
|
|
163
|
+
`Configuration not found: ${configPath}. Run "ds init" first.`,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
let parsed;
|
|
170
|
+
try {
|
|
171
|
+
parsed = JSON.parse(contents);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw new Error(`Invalid JSON in ${configPath}: ${error.message}`);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const merged = {
|
|
177
|
+
...DEFAULT_CONFIG,
|
|
178
|
+
...parsed,
|
|
179
|
+
generator: { ...DEFAULT_CONFIG.generator, ...parsed?.generator },
|
|
180
|
+
optimization: {
|
|
181
|
+
...DEFAULT_CONFIG.optimization,
|
|
182
|
+
...parsed?.optimization,
|
|
183
|
+
html: {
|
|
184
|
+
...DEFAULT_CONFIG.optimization.html,
|
|
185
|
+
...parsed?.optimization?.html,
|
|
186
|
+
},
|
|
187
|
+
criticalCss: {
|
|
188
|
+
...DEFAULT_CONFIG.optimization.criticalCss,
|
|
189
|
+
...parsed?.optimization?.criticalCss,
|
|
190
|
+
},
|
|
191
|
+
compression: {
|
|
192
|
+
...DEFAULT_CONFIG.optimization.compression,
|
|
193
|
+
...parsed?.optimization?.compression,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
return validateConfig(merged);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function resolveOutputDirectory(projectRoot, outputDir) {
|
|
201
|
+
const root = await fs.realpath(projectRoot);
|
|
202
|
+
const output = path.resolve(root, outputDir);
|
|
203
|
+
const relative = path.relative(root, output);
|
|
204
|
+
if (
|
|
205
|
+
!relative ||
|
|
206
|
+
relative === '..' ||
|
|
207
|
+
relative.startsWith(`..${path.sep}`) ||
|
|
208
|
+
path.isAbsolute(relative)
|
|
209
|
+
) {
|
|
210
|
+
throw new Error(
|
|
211
|
+
'The output directory must be a child of the project directory.',
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const realOutput = await fs.realpath(output);
|
|
216
|
+
const realRelative = path.relative(root, realOutput);
|
|
217
|
+
if (
|
|
218
|
+
!realRelative ||
|
|
219
|
+
realRelative === '..' ||
|
|
220
|
+
realRelative.startsWith(`..${path.sep}`) ||
|
|
221
|
+
path.isAbsolute(realRelative)
|
|
222
|
+
) {
|
|
223
|
+
throw new Error(
|
|
224
|
+
'The resolved output directory must remain inside the project directory.',
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
const stat = await fs.stat(realOutput);
|
|
228
|
+
if (!stat.isDirectory())
|
|
229
|
+
throw new Error(`Output path is not a directory: ${output}`);
|
|
230
|
+
return realOutput;
|
|
231
|
+
}
|
package/src/files.mjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const COMPRESSIBLE_EXTENSIONS = new Set([
|
|
5
|
+
'.css',
|
|
6
|
+
'.cjs',
|
|
7
|
+
'.html',
|
|
8
|
+
'.htm',
|
|
9
|
+
'.js',
|
|
10
|
+
'.json',
|
|
11
|
+
'.map',
|
|
12
|
+
'.mjs',
|
|
13
|
+
'.svg',
|
|
14
|
+
'.txt',
|
|
15
|
+
'.wasm',
|
|
16
|
+
'.webmanifest',
|
|
17
|
+
'.xml',
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
export async function listFiles(root, current = root, files = []) {
|
|
21
|
+
const entries = await fs.readdir(current, { withFileTypes: true });
|
|
22
|
+
for (const entry of entries) {
|
|
23
|
+
const absolute = path.join(current, entry.name);
|
|
24
|
+
if (entry.isDirectory()) await listFiles(root, absolute, files);
|
|
25
|
+
else if (entry.isFile()) files.push(absolute);
|
|
26
|
+
}
|
|
27
|
+
return files;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isCompressible(file) {
|
|
31
|
+
return COMPRESSIBLE_EXTENSIONS.has(path.extname(file).toLowerCase());
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function relativePosix(root, file) {
|
|
35
|
+
return path.relative(root, file).split(path.sep).join('/');
|
|
36
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export {
|
|
2
|
+
DEFAULT_CONFIG,
|
|
3
|
+
loadConfig,
|
|
4
|
+
resolveOutputDirectory,
|
|
5
|
+
validateConfig,
|
|
6
|
+
} from './config.mjs';
|
|
7
|
+
export { runGenerator } from './build.mjs';
|
|
8
|
+
export { optimizeOutput } from './optimize.mjs';
|
|
9
|
+
export { analyzeOutput, formatReport } from './report.mjs';
|
|
10
|
+
export { applyProductionDefaults } from './nuxt-options.mjs';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function applyProductionDefaults(nuxt, options) {
|
|
2
|
+
if (nuxt.options.dev) return;
|
|
3
|
+
|
|
4
|
+
if (options.minify) {
|
|
5
|
+
nuxt.options.vite ??= {};
|
|
6
|
+
nuxt.options.vite.build ??= {};
|
|
7
|
+
if (nuxt.options.vite.build.minify === undefined) {
|
|
8
|
+
nuxt.options.vite.build.minify = true;
|
|
9
|
+
}
|
|
10
|
+
if (nuxt.options.vite.build.cssMinify === undefined) {
|
|
11
|
+
nuxt.options.vite.build.cssMinify = true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (!options.compression) return;
|
|
16
|
+
nuxt.options.nitro ??= {};
|
|
17
|
+
const compression = nuxt.options.nitro.compressPublicAssets;
|
|
18
|
+
if (compression === undefined) {
|
|
19
|
+
nuxt.options.nitro.compressPublicAssets = {
|
|
20
|
+
gzip: options.compression.gzip,
|
|
21
|
+
brotli: options.compression.brotli,
|
|
22
|
+
};
|
|
23
|
+
} else if (compression && typeof compression === 'object') {
|
|
24
|
+
compression.gzip ??= options.compression.gzip;
|
|
25
|
+
compression.brotli ??= options.compression.brotli;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/optimize.mjs
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import Beasties from 'beasties';
|
|
5
|
+
import { minify as minifyHtml } from 'html-minifier-terser';
|
|
6
|
+
import { listFiles, relativePosix, isCompressible } from './files.mjs';
|
|
7
|
+
import { analyzeOutput } from './report.mjs';
|
|
8
|
+
import { promisify } from 'node:util';
|
|
9
|
+
import {
|
|
10
|
+
brotliCompress as brotliCompressCallback,
|
|
11
|
+
constants as zlibConstants,
|
|
12
|
+
gzip as gzipCallback,
|
|
13
|
+
} from 'node:zlib';
|
|
14
|
+
|
|
15
|
+
const gzip = promisify(gzipCallback);
|
|
16
|
+
const brotliCompress = promisify(brotliCompressCallback);
|
|
17
|
+
|
|
18
|
+
async function writeAtomic(file, contents) {
|
|
19
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
20
|
+
try {
|
|
21
|
+
await fs.writeFile(temporary, contents);
|
|
22
|
+
await fs.rename(temporary, file);
|
|
23
|
+
} finally {
|
|
24
|
+
await fs.rm(temporary, { force: true });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function exists(file) {
|
|
29
|
+
try {
|
|
30
|
+
await fs.access(file);
|
|
31
|
+
return true;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (error.code === 'ENOENT') return false;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function validateCriticalRoutes(root, routes, htmlFiles) {
|
|
39
|
+
const available = new Set(htmlFiles.map((file) => relativePosix(root, file)));
|
|
40
|
+
for (const route of routes) {
|
|
41
|
+
if (!available.has(route)) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Critical CSS route does not exist in the output: ${route}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function optimizeHtml(root, config) {
|
|
50
|
+
const files = await listFiles(root);
|
|
51
|
+
const htmlFiles = files.filter((file) =>
|
|
52
|
+
['.html', '.htm'].includes(path.extname(file).toLowerCase()),
|
|
53
|
+
);
|
|
54
|
+
const critical = config.criticalCss;
|
|
55
|
+
if (critical.enabled)
|
|
56
|
+
validateCriticalRoutes(root, critical.routes, htmlFiles);
|
|
57
|
+
|
|
58
|
+
const criticalRoutes = new Set(critical.routes);
|
|
59
|
+
const beasties = critical.enabled
|
|
60
|
+
? new Beasties({
|
|
61
|
+
path: root,
|
|
62
|
+
publicPath: '/',
|
|
63
|
+
allowRules: critical.allowRules,
|
|
64
|
+
compress: true,
|
|
65
|
+
preload: critical.preload,
|
|
66
|
+
pruneSource: false,
|
|
67
|
+
})
|
|
68
|
+
: null;
|
|
69
|
+
const pendingWrites = [];
|
|
70
|
+
const changedFiles = new Set();
|
|
71
|
+
|
|
72
|
+
for (const file of htmlFiles) {
|
|
73
|
+
const original = await fs.readFile(file, 'utf8');
|
|
74
|
+
let output = original;
|
|
75
|
+
if (beasties && criticalRoutes.has(relativePosix(root, file))) {
|
|
76
|
+
output = await beasties.process(output);
|
|
77
|
+
}
|
|
78
|
+
if (config.html.minify) {
|
|
79
|
+
output = await minifyHtml(output, {
|
|
80
|
+
collapseWhitespace: true,
|
|
81
|
+
conservativeCollapse: true,
|
|
82
|
+
removeComments: false,
|
|
83
|
+
minifyCSS: false,
|
|
84
|
+
minifyJS: false,
|
|
85
|
+
removeOptionalTags: false,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (output !== original) {
|
|
89
|
+
pendingWrites.push([file, output]);
|
|
90
|
+
changedFiles.add(file);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const [file, contents] of pendingWrites)
|
|
95
|
+
await writeAtomic(file, contents);
|
|
96
|
+
return { htmlFiles: htmlFiles.length, changedFiles };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function writeCompressionSidecars(
|
|
100
|
+
root,
|
|
101
|
+
files,
|
|
102
|
+
changedFiles,
|
|
103
|
+
compression,
|
|
104
|
+
) {
|
|
105
|
+
let gzipCount = 0;
|
|
106
|
+
let brotliCount = 0;
|
|
107
|
+
|
|
108
|
+
for (const file of files) {
|
|
109
|
+
if (!isCompressible(file)) continue;
|
|
110
|
+
const shouldRefresh = changedFiles.has(file);
|
|
111
|
+
const gzipPath = `${file}.gz`;
|
|
112
|
+
const brotliPath = `${file}.br`;
|
|
113
|
+
const writeGzip =
|
|
114
|
+
compression.gzip || (shouldRefresh && (await exists(gzipPath)));
|
|
115
|
+
const writeBrotli =
|
|
116
|
+
compression.brotli || (shouldRefresh && (await exists(brotliPath)));
|
|
117
|
+
if (!writeGzip && !writeBrotli) continue;
|
|
118
|
+
|
|
119
|
+
const contents = await fs.readFile(file);
|
|
120
|
+
if (writeGzip) {
|
|
121
|
+
const compressed = await gzip(contents, {
|
|
122
|
+
level: zlibConstants.Z_BEST_COMPRESSION,
|
|
123
|
+
});
|
|
124
|
+
await writeAtomic(gzipPath, compressed);
|
|
125
|
+
gzipCount += 1;
|
|
126
|
+
}
|
|
127
|
+
if (writeBrotli) {
|
|
128
|
+
const compressed = await brotliCompress(contents, {
|
|
129
|
+
params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 11 },
|
|
130
|
+
});
|
|
131
|
+
await writeAtomic(brotliPath, compressed);
|
|
132
|
+
brotliCount += 1;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { gzipCount, brotliCount };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function optimizeOutput(outputDir, optimization) {
|
|
139
|
+
const initialFiles = await listFiles(outputDir);
|
|
140
|
+
const { htmlFiles, changedFiles } = await optimizeHtml(
|
|
141
|
+
outputDir,
|
|
142
|
+
optimization,
|
|
143
|
+
);
|
|
144
|
+
const files = await listFiles(outputDir);
|
|
145
|
+
const compressed = await writeCompressionSidecars(
|
|
146
|
+
outputDir,
|
|
147
|
+
files.filter((file) => !file.endsWith('.gz') && !file.endsWith('.br')),
|
|
148
|
+
changedFiles,
|
|
149
|
+
optimization.compression,
|
|
150
|
+
);
|
|
151
|
+
return {
|
|
152
|
+
htmlFiles,
|
|
153
|
+
changedHtmlFiles: changedFiles.size,
|
|
154
|
+
criticalCssFiles: optimization.criticalCss.enabled
|
|
155
|
+
? optimization.criticalCss.routes.length
|
|
156
|
+
: 0,
|
|
157
|
+
gzipFiles: compressed.gzipCount,
|
|
158
|
+
brotliFiles: compressed.brotliCount,
|
|
159
|
+
addedOrUpdatedSidecars: compressed.gzipCount + compressed.brotliCount,
|
|
160
|
+
report: await analyzeOutput(outputDir),
|
|
161
|
+
initialFileCount: initialFiles.length,
|
|
162
|
+
};
|
|
163
|
+
}
|
package/src/report.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import {
|
|
5
|
+
brotliCompress as brotliCompressCallback,
|
|
6
|
+
constants as zlibConstants,
|
|
7
|
+
gzip as gzipCallback,
|
|
8
|
+
} from 'node:zlib';
|
|
9
|
+
import { isCompressible, listFiles } from './files.mjs';
|
|
10
|
+
|
|
11
|
+
const gzip = promisify(gzipCallback);
|
|
12
|
+
const brotliCompress = promisify(brotliCompressCallback);
|
|
13
|
+
|
|
14
|
+
function groupFor(file) {
|
|
15
|
+
switch (path.extname(file).toLowerCase()) {
|
|
16
|
+
case '.html':
|
|
17
|
+
case '.htm':
|
|
18
|
+
return 'html';
|
|
19
|
+
case '.css':
|
|
20
|
+
return 'css';
|
|
21
|
+
case '.js':
|
|
22
|
+
case '.mjs':
|
|
23
|
+
case '.cjs':
|
|
24
|
+
return 'javascript';
|
|
25
|
+
default:
|
|
26
|
+
return 'other';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function emptyGroup() {
|
|
31
|
+
return { files: 0, rawBytes: 0, gzipBytes: 0, brotliBytes: 0 };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function analyzeOutput(outputDir) {
|
|
35
|
+
const allFiles = await listFiles(outputDir);
|
|
36
|
+
const files = allFiles.filter(
|
|
37
|
+
(file) => !file.endsWith('.gz') && !file.endsWith('.br'),
|
|
38
|
+
);
|
|
39
|
+
const report = {
|
|
40
|
+
fileCount: files.length,
|
|
41
|
+
totalRawBytes: 0,
|
|
42
|
+
compressible: {
|
|
43
|
+
files: 0,
|
|
44
|
+
rawBytes: 0,
|
|
45
|
+
gzipBytes: 0,
|
|
46
|
+
brotliBytes: 0,
|
|
47
|
+
},
|
|
48
|
+
groups: {
|
|
49
|
+
html: emptyGroup(),
|
|
50
|
+
css: emptyGroup(),
|
|
51
|
+
javascript: emptyGroup(),
|
|
52
|
+
other: emptyGroup(),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
for (const file of files) {
|
|
57
|
+
const contents = await fs.readFile(file);
|
|
58
|
+
report.totalRawBytes += contents.byteLength;
|
|
59
|
+
if (!isCompressible(file)) continue;
|
|
60
|
+
|
|
61
|
+
const gzipContents = await gzip(contents, {
|
|
62
|
+
level: zlibConstants.Z_BEST_COMPRESSION,
|
|
63
|
+
});
|
|
64
|
+
const brotliContents = await brotliCompress(contents, {
|
|
65
|
+
params: { [zlibConstants.BROTLI_PARAM_QUALITY]: 11 },
|
|
66
|
+
});
|
|
67
|
+
const group = report.groups[groupFor(file)];
|
|
68
|
+
group.files += 1;
|
|
69
|
+
group.rawBytes += contents.byteLength;
|
|
70
|
+
group.gzipBytes += gzipContents.byteLength;
|
|
71
|
+
group.brotliBytes += brotliContents.byteLength;
|
|
72
|
+
report.compressible.files += 1;
|
|
73
|
+
report.compressible.rawBytes += contents.byteLength;
|
|
74
|
+
report.compressible.gzipBytes += gzipContents.byteLength;
|
|
75
|
+
report.compressible.brotliBytes += brotliContents.byteLength;
|
|
76
|
+
}
|
|
77
|
+
return report;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function formatBytes(bytes) {
|
|
81
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
82
|
+
return `${(bytes / 1024).toFixed(1)} KiB`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function formatReport(report, outputDir) {
|
|
86
|
+
const lines = [
|
|
87
|
+
`Output: ${outputDir}`,
|
|
88
|
+
`Files: ${report.fileCount} (${formatBytes(report.totalRawBytes)} raw total)`,
|
|
89
|
+
'',
|
|
90
|
+
'Compressible assets Raw Gzip Brotli',
|
|
91
|
+
];
|
|
92
|
+
for (const [name, group] of Object.entries(report.groups)) {
|
|
93
|
+
if (group.files === 0) continue;
|
|
94
|
+
lines.push(
|
|
95
|
+
`${name.padEnd(27)} ${formatBytes(group.rawBytes).padStart(9)} ${formatBytes(group.gzipBytes).padStart(9)} ${formatBytes(group.brotliBytes).padStart(9)}`,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
lines.push(
|
|
99
|
+
`${'Total'.padEnd(27)} ${formatBytes(report.compressible.rawBytes).padStart(9)} ${formatBytes(report.compressible.gzipBytes).padStart(9)} ${formatBytes(report.compressible.brotliBytes).padStart(9)}`,
|
|
100
|
+
);
|
|
101
|
+
return lines.join('\n');
|
|
102
|
+
}
|