Skip to content
Snippets Groups Projects
Commit 7b8e85a1 authored by Luke Leber's avatar Luke Leber
Browse files

Issue #3254365 by Luke.Leber: Minify assets after aggregation

parent db6c097a
No related branches found
No related tags found
1 merge request!5Issue #3254365: Minify assets after aggregation
enabled: false
enabled_themes: { }
minify: false
......@@ -10,3 +10,6 @@ inline_all_css.settings:
sequence:
type: string
label: 'Enabled theme'
minify:
type: boolean
label: 'Minify assets after aggregation'
......@@ -11,3 +11,10 @@ services:
- '@event_dispatcher'
- '@http_client'
- '@logger.factory'
inline_all_css_minify_subscriber:
class: Drupal\inline_all_css\EventSubscriber\MinifySubscriber
arguments:
- '@config.factory'
tags:
- { name: 'event_subscriber', priority: -1000 }
<?php
namespace Drupal\inline_all_css\EventSubscriber;
use Drupal\Core\Asset\CssOptimizer;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\inline_all_css\Event\CssPreRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* An event subscriber that minifies the aggregated source.
*/
class MinifySubscriber extends CssOptimizer implements EventSubscriberInterface {
/**
* The module configuration.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Creates an event subscriber instance.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->config = $config_factory->get('inline_all_css.settings');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
CssPreRenderEvent::EVENT_NAME => 'onCssPreRender',
];
}
/**
* Optimizes the CSS that is being inlined.
*
* @param \Drupal\inline_all_css\Event\CssPreRenderEvent $event
* The css pre-render event.
*/
public function onCssPreRender(CssPreRenderEvent $event) {
if ($this->config->get('minify') === TRUE) {
$css = $event->getCss();
$css = $this->processCss($css, TRUE);
$event->setCss($css);
}
}
}
......@@ -79,6 +79,18 @@ HTML,
],
];
$form['minify'] = [
'#type' => 'checkbox',
'#title' => $this->t('Minify'),
'#description' => $this->t('If selected, assets will be minified after aggregation'),
'#default_value' => $config->get('minify'),
'#states' => [
'visible' => [
':input[name="enabled"]' => ['checked' => TRUE],
],
],
];
return parent::buildForm($form, $form_state);
}
......@@ -98,6 +110,7 @@ HTML,
$enabled_themes = array_filter($enabled_themes);
$config->set('enabled_themes', $enabled_themes);
$config->set('minify', $form_state->getValue('minify'));
$config->save();
parent::submitForm($form, $form_state);
}
......
<?php
namespace Drupal\Tests\inline_all_css\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\inline_all_css\Event\CssPreRenderEvent;
use Drupal\inline_all_css\EventSubscriber\MinifySubscriber;
use Drupal\Tests\UnitTestCase;
/**
* Test cases for the minify subscriber.
*
* @group inline_all_csss
*/
class MinifySubscriberTest extends UnitTestCase {
/**
* The expected events.
*/
protected const EXPECTED_EVENTS = [
CssPreRenderEvent::EVENT_NAME => 'onCssPreRender',
];
/**
* The non-minified source.
*/
protected const SOURCE = <<<CSS
* {
background-color: pink;
}
CSS;
/**
* The minified source.
*/
protected const MINIFIED = <<<CSS
*{background-color:pink;}
CSS;
/**
* The subject under test.
*
* @var \Drupal\inline_all_css\EventSubscriber\MinifySubscriber
*/
protected $instance;
/**
* The enable flag for minification.
*
* @var bool
*/
protected $minificationEnabled;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$config = $this->getMockBuilder(ImmutableConfig::class)
->disableOriginalConstructor()
->getMock();
$config
->method('get')
->willReturnCallback(function () {
return $this->minificationEnabled;
});
$config_factory = $this->getMockBuilder(ConfigFactoryInterface::class)
->disableOriginalConstructor()
->getMock();
$config_factory
->method('get')
->willReturn($config);
$this->instance = new MinifySubscriber($config_factory);
}
/**
* Test case for the minify subscriber.
*/
public function testMinifySubscriber() {
// Ensure the proper events are subscribed to.
static::assertSame(static::EXPECTED_EVENTS, MinifySubscriber::getSubscribedEvents());
$event = new CssPreRenderEvent(static::SOURCE);
// Ensure minification does not run if disabled.
$this->minificationEnabled = FALSE;
$this->instance->onCssPreRender($event);
static::assertSame(static::SOURCE, $event->getCss());
// Ensure minification does run if enabled.
$this->minificationEnabled = TRUE;
$this->instance->onCssPreRender($event);
static::assertSame(static::MINIFIED, $event->getCss());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment