Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ jobs:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: actions/setup-node@v4
with:
node-version: '22'
- uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.6
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Optimize assets
run: CI=true bun run optimize
# Use Node.js for sharp to avoid Bun's pas allocator crash (SIGTRAP)
run: CI=true node ./scripts/optimize-assets.js
- name: Check for uncommitted changes
run: |
if ! git diff --exit-code; then
Expand Down
28 changes: 12 additions & 16 deletions scripts/optimize-assets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readdirSync, statSync } from 'fs';
import { readdirSync, statSync, readFileSync, writeFileSync, unlinkSync } from 'fs';
import { join, relative } from 'path';
import sharp from 'sharp';
import { fileURLToPath } from 'url';
Expand Down Expand Up @@ -75,15 +75,15 @@ async function main() {
meta = await image.metadata();
} catch (err) {
const msg = `${relative_path} failed: ${err.message}`;
if (Bun.env.CI) {
if (process.env.CI) {
throw new Error(msg);
}

console.log(msg);
continue;
}

if (Bun.env.CI && (meta.width > 1980 || meta.height > 1980)) {
if (process.env.CI && (meta.width > 1980 || meta.height > 1980)) {
const msg = `${relative_path} is too large: ${meta.width}x${meta.height}`;
throw new Error(msg);
}
Expand All @@ -94,33 +94,29 @@ async function main() {

await sharp(buffer).toFile(file + '.optimized');

const file_before = Bun.file(file);
await file_before.arrayBuffer();
const size_before = file_before.size;

const file_after = Bun.file(file + '.optimized');
const file_after_contents = await file_after.arrayBuffer();
const size_after = file_after.size;
const size_before = statSync(file).size;
const size_after = statSync(file + '.optimized').size;

const size_diff = size_before - size_after;
if (size_diff <= 0) {
await Bun.file(file + '.optimized').delete();
unlinkSync(file + '.optimized');
continue;
}

const size_diff_percent = size_diff / size_before;
if (size_diff_percent < 0.2) {
await Bun.file(file + '.optimized').delete();
unlinkSync(file + '.optimized');
continue;
}

// Atomic rewrite
try {
await Bun.write(file, file_after_contents);
await Bun.file(file + '.optimized').delete();
const file_after_contents = readFileSync(file + '.optimized');
writeFileSync(file, file_after_contents);
unlinkSync(file + '.optimized');
} catch (error) {
try {
await Bun.file(file + '.optimized').delete();
unlinkSync(file + '.optimized');
} catch {
// Silenced
}
Expand All @@ -131,7 +127,7 @@ async function main() {
const diff_verbose = Math.round(size_diff_percent * 100);
console.log(`✅ ${relative_path} has been optimized (-${diff_verbose}%)`);

if (Bun.env.CI) {
if (process.env.CI) {
console.log(
`Stopping optimization in CI/CD env, as one diff is enough to make test fail`
);
Expand Down