chore(bootstrap.php): separately minify php when files are requested

This commit is contained in:
Cory Dransfeldt 2025-06-15 11:32:01 -07:00
parent 2b22d10b46
commit 320f5e53db
No known key found for this signature in database
6 changed files with 259 additions and 14 deletions

View file

@ -1,6 +1,37 @@
<?php
define('PROJECT_ROOT', realpath(__DIR__.'/..'));
define('PROJECT_ROOT', realpath(__DIR__ . '/..'));
require_once PROJECT_ROOT.'/vendor/autoload.php';
require_once PROJECT_ROOT.'/app/Utils/init.php';
require_once PROJECT_ROOT . '/vendor/autoload.php';
require_once PROJECT_ROOT . '/app/Utils/init.php';
use voku\helper\HtmlMin;
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? '';
$isApi = str_starts_with($requestUri, '/api/');
$isHtml = stripos($acceptHeader, 'text/html') !== false || empty($acceptHeader);
if (!$isApi && $isHtml) {
ob_start(function ($buffer) {
if (stripos($buffer, '<body') !== false) {
try {
$minifier = new HtmlMin();
$buffer = $minifier->minify($buffer);
if (stripos($buffer, '</body>') === false) {
$buffer .= '</body>';
}
if (stripos($buffer, '</html>') === false) {
$buffer .= '</html>';
}
} catch (Throwable $e) {
error_log("HTML minification failed: " . $e->getMessage());
}
}
return $buffer;
});
}

View file

@ -7,7 +7,8 @@
"guzzlehttp/guzzle": "^7.9",
"kaoken/markdown-it-php": "^14.1",
"sokil/php-isocodes": "^4.2",
"sokil/php-isocodes-db-only": "^4.0"
"sokil/php-isocodes-db-only": "^4.0",
"voku/html-min": "^4.5"
},
"require-dev": {
"laravel/pint": "^1.22"

215
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e9a46ab9eab33e697fc595a420b63325",
"content-hash": "f2370fcb43d46fa0ea5fad61c8d13341",
"packages": [
{
"name": "guzzlehttp/guzzle",
@ -686,6 +686,71 @@
},
"time": "2024-02-02T08:24:43+00:00"
},
{
"name": "symfony/css-selector",
"version": "v7.3.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/css-selector.git",
"reference": "601a5ce9aaad7bf10797e3663faefce9e26c24e2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/css-selector/zipball/601a5ce9aaad7bf10797e3663faefce9e26c24e2",
"reference": "601a5ce9aaad7bf10797e3663faefce9e26c24e2",
"shasum": ""
},
"require": {
"php": ">=8.2"
},
"type": "library",
"autoload": {
"psr-4": {
"Symfony\\Component\\CssSelector\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Jean-François Simon",
"email": "jeanfrancois.simon@sensiolabs.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Converts CSS selectors to XPath expressions",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/css-selector/tree/v7.3.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-09-25T14:21:43+00:00"
},
{
"name": "symfony/deprecation-contracts",
"version": "v3.6.0",
@ -752,6 +817,154 @@
}
],
"time": "2024-09-25T14:21:43+00:00"
},
{
"name": "voku/html-min",
"version": "4.5.1",
"source": {
"type": "git",
"url": "https://github.com/voku/HtmlMin.git",
"reference": "872dac444652d30791ca5d0c23fbe1e6b9cab5bc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/voku/HtmlMin/zipball/872dac444652d30791ca5d0c23fbe1e6b9cab5bc",
"reference": "872dac444652d30791ca5d0c23fbe1e6b9cab5bc",
"shasum": ""
},
"require": {
"ext-dom": "*",
"php": ">=7.0.0",
"voku/simple_html_dom": "~4.8.5"
},
"require-dev": {
"phpunit/phpunit": "~6.0 || ~7.0 || ~9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"voku\\": "src/voku/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Lars Moelleken",
"homepage": "https://www.moelleken.org/"
}
],
"description": "HTML Compressor and Minifier",
"homepage": "https://github.com/voku/HtmlMin",
"keywords": [
"compress",
"compression",
"compressor",
"html",
"minifier"
],
"support": {
"issues": "https://github.com/voku/HtmlMin/issues",
"source": "https://github.com/voku/HtmlMin/tree/4.5.1"
},
"funding": [
{
"url": "https://www.paypal.me/moelleken",
"type": "custom"
},
{
"url": "https://github.com/voku",
"type": "github"
},
{
"url": "https://www.patreon.com/voku",
"type": "patreon"
}
],
"time": "2024-05-25T08:01:45+00:00"
},
{
"name": "voku/simple_html_dom",
"version": "4.8.10",
"source": {
"type": "git",
"url": "https://github.com/voku/simple_html_dom.git",
"reference": "716822ed52ed3a1881542be07a786270de390e99"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/voku/simple_html_dom/zipball/716822ed52ed3a1881542be07a786270de390e99",
"reference": "716822ed52ed3a1881542be07a786270de390e99",
"shasum": ""
},
"require": {
"ext-dom": "*",
"ext-libxml": "*",
"ext-simplexml": "*",
"php": ">=7.0.0",
"symfony/css-selector": "~3.0 || ~4.0 || ~5.0 || ~6.0 || ~7.0"
},
"require-dev": {
"phpunit/phpunit": "~6.0 || ~7.0 || ~9.0"
},
"suggest": {
"voku/portable-utf8": "If you need e.g. UTF-8 fixed output."
},
"type": "library",
"autoload": {
"psr-4": {
"voku\\helper\\": "src/voku/helper/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "dimabdc",
"email": "support@titor.ru",
"homepage": "https://github.com/dimabdc",
"role": "Developer"
},
{
"name": "Lars Moelleken",
"homepage": "https://www.moelleken.org/",
"role": "Fork-Maintainer"
}
],
"description": "Simple HTML DOM package.",
"homepage": "https://github.com/voku/simple_html_dom",
"keywords": [
"HTML Parser",
"dom",
"php dom"
],
"support": {
"issues": "https://github.com/voku/simple_html_dom/issues",
"source": "https://github.com/voku/simple_html_dom/tree/4.8.10"
},
"funding": [
{
"url": "https://www.paypal.me/moelleken",
"type": "custom"
},
{
"url": "https://github.com/voku",
"type": "github"
},
{
"url": "https://www.patreon.com/voku",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/voku/simple_html_dom",
"type": "tidelift"
}
],
"time": "2024-07-03T16:05:14+00:00"
}
],
"packages-dev": [

View file

@ -2,7 +2,7 @@ import htmlmin from 'html-minifier-terser';
export const htmlConfig = (eleventyConfig) => {
eleventyConfig.addTransform('html-minify', (content, path) => {
if (path && (path.endsWith('.html') || path.endsWith('.php'))) {
if (path && path.endsWith('.html')) {
return htmlmin.minify(content, {
collapseBooleanAttributes: true,
collapseWhitespace: true,

12
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd.dev",
"version": "10.4.6",
"version": "10.5.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd.dev",
"version": "10.4.6",
"version": "10.5.6",
"license": "MIT",
"dependencies": {
"minisearch": "^7.1.2",
@ -23,7 +23,7 @@
"html-minifier-terser": "7.2.0",
"husky": "9.1.7",
"ics": "^3.8.1",
"lint-staged": "16.1.1",
"lint-staged": "16.1.2",
"markdown-it": "^14.1.0",
"markdown-it-anchor": "^9.2.0",
"markdown-it-footnote": "^4.0.0",
@ -2549,9 +2549,9 @@
}
},
"node_modules/lint-staged": {
"version": "16.1.1",
"resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-16.1.1.tgz",
"integrity": "sha512-kVZvRAHw9WuufENnwuZLiB1X/8B8YpGszzjMET0bP+uJSjjB7KXeaX2ckYRtQyHfeQdCWMc6tRK34t4geHL9sg==",
"version": "16.1.2",
"resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-16.1.2.tgz",
"integrity": "sha512-sQKw2Si2g9KUZNY3XNvRuDq4UJqpHwF0/FQzZR2M7I5MvtpWvibikCjUVJzZdGE0ByurEl3KQNvsGetd1ty1/Q==",
"dev": true,
"license": "MIT",
"dependencies": {

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "10.4.6",
"version": "10.5.6",
"description": "The source for my personal site. Built using 11ty (and other tools).",
"type": "module",
"engines": {
@ -52,7 +52,7 @@
"html-minifier-terser": "7.2.0",
"husky": "9.1.7",
"ics": "^3.8.1",
"lint-staged": "16.1.1",
"lint-staged": "16.1.2",
"markdown-it": "^14.1.0",
"markdown-it-anchor": "^9.2.0",
"markdown-it-footnote": "^4.0.0",