Skip to content
Open
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
4 changes: 4 additions & 0 deletions Classes/Service/MinifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class MinifyService
{
public function minify(string $html): string
{
if ($this->isFeatureActive('disabled')) {
return $html;
}

$htmlMin = new HtmlMin();
$htmlMin->doOptimizeViaHtmlDomParser($this->isFeatureActive('optimize_via_html_dom_parser'));
$htmlMin->doSumUpWhitespace($this->isFeatureActive('sum_up_whitespace'));
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ These can be configured by extension configuration.

`composer req pluswerk/minify`

# Disabling minification

Set the `disabled` extension configuration option to `1` to switch minification
off completely. The default is `0` (minification active).

This is handy for environment-specific behaviour. To keep the HTML readable in
development while leaving it active everywhere else, set it per context in
`config/system/additional.php`:

```php
use TYPO3\CMS\Core\Core\Environment;

if (Environment::getContext()->isDevelopment()) {
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['disabled'] = '1';
}
```

# Benefits
- Less Data to transfer
- Nice looking code
Expand Down
20 changes: 20 additions & 0 deletions Tests/Unit/Service/MinifyServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ public function minifyRemovesRegularCommentButKeepsTypo3CommentWhenBothFlagsAreE
self::assertStringNotContainsString('<!-- drop me -->', $result);
}

// -------------------------------------------------------------------------
// Master switch — disabled
// -------------------------------------------------------------------------

#[Test]
public function minifyReturnsHtmlUntouchedWhenDisabled(): void
{
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['disabled'] = '1';
// Features that would otherwise transform the output are on, to prove
// the disabled switch short-circuits before any minification happens.
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['optimize_via_html_dom_parser'] = '1';
$GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['minify']['remove_comments'] = '1';

$html = '<html><head><meta charset="utf-8"><!-- drop me --></head><body> <p>Hello</p> </body></html>';

$result = $this->subject->minify($html);

self::assertSame($html, $result);
}

// -------------------------------------------------------------------------
// Output integrity
// -------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# cat=plus_minify; type=boolean; label=disable minification entirely (e.g. set to 1 in Development via AdditionalConfiguration.php)
disabled = 0

# cat=plus_minify; type=boolean; label=optimize html via "HtmlDomParser()"
optimize_via_html_dom_parser = 1

Expand Down