@ai.all/ds 0.0.0-stage → 0.1.0-beta.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/ALL.md +29 -0
- package/CHANGELOG.md +18 -0
- package/LICENSE +21 -0
- package/README.md +155 -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 +36 -0
- package/docs/CRITICAL-CSS.md +36 -0
- package/docs/ECOSYSTEM.md +37 -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/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
|
+
}
|