Stylesheet: Don't parse library CSS as LESS

This commit is contained in:
Johannes Meyer 2022-06-13 09:45:47 +02:00
parent 3203cd8fea
commit c65ba3c2fc
1 changed files with 28 additions and 4 deletions

View File

@ -90,6 +90,9 @@ class StyleSheet
*/ */
protected $app; protected $app;
/** @var string[] Pre-compiled CSS files */
protected $cssFiles = [];
/** /**
* Less compiler * Less compiler
* *
@ -123,7 +126,11 @@ class StyleSheet
{ {
foreach ($this->app->getLibraries() as $library) { foreach ($this->app->getLibraries() as $library) {
foreach ($library->getCssAssets() as $lessFile) { foreach ($library->getCssAssets() as $lessFile) {
$this->lessCompiler->addLessFile($lessFile); if (substr($lessFile, -4) === '.css') {
$this->cssFiles[] = $lessFile;
} else {
$this->lessCompiler->addLessFile($lessFile);
}
} }
} }
@ -186,6 +193,16 @@ class StyleSheet
$this->lessCompiler->setThemeMode($this->pubPath . '/css/modes/'. $mode . '.less'); $this->lessCompiler->setThemeMode($this->pubPath . '/css/modes/'. $mode . '.less');
} }
/**
* Get all collected files
*
* @return string[]
*/
protected function getFiles(): array
{
return array_merge($this->cssFiles, $this->lessCompiler->getLessFiles());
}
/** /**
* Get the stylesheet for PDF export * Get the stylesheet for PDF export
* *
@ -213,7 +230,13 @@ class StyleSheet
if ($minified) { if ($minified) {
$this->lessCompiler->compress(); $this->lessCompiler->compress();
} }
return $this->lessCompiler->render();
$css = '';
foreach ($this->cssFiles as $cssFile) {
$css .= file_get_contents($cssFile);
}
return $css . $this->lessCompiler->render();
} }
/** /**
@ -233,14 +256,15 @@ class StyleSheet
$noCache = $request->getHeader('Cache-Control') === 'no-cache' || $request->getHeader('Pragma') === 'no-cache'; $noCache = $request->getHeader('Cache-Control') === 'no-cache' || $request->getHeader('Pragma') === 'no-cache';
if (! $noCache && FileCache::etagMatchesFiles($styleSheet->lessCompiler->getLessFiles())) { $collectedFiles = $styleSheet->getFiles();
if (! $noCache && FileCache::etagMatchesFiles($collectedFiles)) {
$response $response
->setHttpResponseCode(304) ->setHttpResponseCode(304)
->sendHeaders(); ->sendHeaders();
return; return;
} }
$etag = FileCache::etagForFiles($styleSheet->lessCompiler->getLessFiles()); $etag = FileCache::etagForFiles($collectedFiles);
$response->setHeader('ETag', $etag, true) $response->setHeader('ETag', $etag, true)
->setHeader('Content-Type', 'text/css', true); ->setHeader('Content-Type', 'text/css', true);