Accessibility fixes
This commit is contained in:
@@ -8,71 +8,71 @@ import {
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
cpSync,
|
||||
readdirSync
|
||||
} from 'fs'
|
||||
import { join, dirname, extname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import Beasties from 'beasties'
|
||||
import { minify as minifyJs } from 'terser'
|
||||
import CleanCSS from 'clean-css'
|
||||
readdirSync,
|
||||
} from 'fs';
|
||||
import { join, dirname, extname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import Beasties from 'beasties';
|
||||
import { minify as minifyJs } from 'terser';
|
||||
import CleanCSS from 'clean-css';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const root = join(__dirname, '..')
|
||||
const srcDir = join(root, 'src')
|
||||
const distDir = join(root, 'dist')
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const root = join(__dirname, '..');
|
||||
const srcDir = join(root, 'src');
|
||||
const distDir = join(root, 'dist');
|
||||
|
||||
function getFiles(dir, files = []) {
|
||||
const entries = readdirSync(dir, { withFileTypes: true })
|
||||
const entries = readdirSync(dir, { withFileTypes: true });
|
||||
for (const e of entries) {
|
||||
const full = join(dir, e.name)
|
||||
if (e.isDirectory()) getFiles(full, files)
|
||||
else files.push(full)
|
||||
const full = join(dir, e.name);
|
||||
if (e.isDirectory()) getFiles(full, files);
|
||||
else files.push(full);
|
||||
}
|
||||
return files
|
||||
return files;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// 1. Clean and copy src → dist
|
||||
rmSync(distDir, { recursive: true, force: true })
|
||||
mkdirSync(distDir, { recursive: true })
|
||||
cpSync(srcDir, distDir, { recursive: true })
|
||||
rmSync(distDir, { recursive: true, force: true });
|
||||
mkdirSync(distDir, { recursive: true });
|
||||
cpSync(srcDir, distDir, { recursive: true });
|
||||
|
||||
const distFiles = getFiles(distDir)
|
||||
const distFiles = getFiles(distDir);
|
||||
|
||||
// 2. Minify JS
|
||||
const jsFiles = distFiles.filter((f) => extname(f) === '.js')
|
||||
const jsFiles = distFiles.filter((f) => extname(f) === '.js');
|
||||
for (const f of jsFiles) {
|
||||
const code = readFileSync(f, 'utf8')
|
||||
const result = await minifyJs(code, { format: { comments: false } })
|
||||
if (result.code) writeFileSync(f, result.code)
|
||||
const code = readFileSync(f, 'utf8');
|
||||
const result = await minifyJs(code, { format: { comments: false } });
|
||||
if (result.code) writeFileSync(f, result.code);
|
||||
}
|
||||
|
||||
// 3. Minify CSS
|
||||
const cleanCss = new CleanCSS({ level: 2 })
|
||||
const cssFiles = distFiles.filter((f) => extname(f) === '.css')
|
||||
const cleanCss = new CleanCSS({ level: 2 });
|
||||
const cssFiles = distFiles.filter((f) => extname(f) === '.css');
|
||||
for (const f of cssFiles) {
|
||||
const code = readFileSync(f, 'utf8')
|
||||
const result = cleanCss.minify(code)
|
||||
if (!result.errors.length) writeFileSync(f, result.styles)
|
||||
const code = readFileSync(f, 'utf8');
|
||||
const result = cleanCss.minify(code);
|
||||
if (!result.errors.length) writeFileSync(f, result.styles);
|
||||
}
|
||||
|
||||
// 4. Inline critical CSS with Beasties for all HTML files (no browser; works in CI)
|
||||
const htmlFiles = distFiles.filter((f) => extname(f) === '.html')
|
||||
const htmlFiles = distFiles.filter((f) => extname(f) === '.html');
|
||||
const beasties = new Beasties({
|
||||
path: distDir,
|
||||
preload: 'default',
|
||||
logLevel: 'warn'
|
||||
})
|
||||
logLevel: 'warn',
|
||||
});
|
||||
for (const htmlFile of htmlFiles) {
|
||||
const html = readFileSync(htmlFile, 'utf8')
|
||||
const inlined = await beasties.process(html)
|
||||
writeFileSync(htmlFile, inlined)
|
||||
const html = readFileSync(htmlFile, 'utf8');
|
||||
const inlined = await beasties.process(html);
|
||||
writeFileSync(htmlFile, inlined);
|
||||
}
|
||||
|
||||
console.log('Build complete: dist/')
|
||||
console.log('Build complete: dist/');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user