a-r-m-i-n / min

TYPO3 CMS extension "min" - Compressed CSS, JS and HTML output for TYPO3
7 stars 8 forks source link

Relative CSS paths are wrong #11

Closed wazum closed 2 years ago

wazum commented 2 years ago

This issue did already exist in the forge and bitbucket tracker, but was never fixed. Now I changed to the recent version and had to adapt my patch.

Example: in the CSS file generated by TYPO3 there's path like ../../../typo3conf/ext/my_ext/Resources/Public/fonts/myfont.xxx and the problem is that your extension passes the content of the file to the $minifier->add method, which results in appending another ../../.. in front of the already correct relative paths (because the source is missing). The solution is to write the output of compressCss to a file and pass the file name to $minifier->add, then the source path is correctly determined by minifier and not changed.

Here's my local patch:

+++ Classes/Minifier.php
@@ -121,7 +121,8 @@
             $minifier = new $minifierClassName();
             if ($type === self::TYPE_STYLESHEET) {
                 $minifier->setImportExtensions([]);
-                $minifier->add($this->compressCss(file_get_contents($config['file'])));
+                file_put_contents($pathSite . 'tmp' . $targetFilename, $this->compressCss(file_get_contents($config['file'])));
+                $minifier->add($targetFilename);
             } else {
                 $minifier->add($config['file']);
             }
wazum commented 2 years ago

See vendor/matthiasmullie/minify/src/Minify.php:89 for details on how the key is generated and then used in the execute method vendor/matthiasmullie/minify/src/CSS.php:301 as $source variable to determine the correct path.

a-r-m-i-n commented 2 years ago

Hi, thanks for you issue. I've recently used EXT:min in a project including fonts and there I had no issues.

When your CSS is located in Resources/Public/Css/ and your font in Resources/Public/fonts you can simply address the font in CSS like this:

src: url("../fonts/my-font-regular.eot");

Also I'm suspisious that your approach really works, because when you create the $pathSite . $targetFilename file on your own, minifier will not minify it anymore (see condition in line 129) https://github.com/a-r-m-i-n/min/blob/master/Classes/Minifier.php#L129). Only the additional CSS compression will be applied to your CSS, then.

wazum commented 2 years ago

@a-r-m-i-n thanks, the same file name is a mistake, point taken, the rest is still valid, but maybe you have an idea how to solve it:

a-r-m-i-n commented 2 years ago

Hey. I'm currently working on #2 and I think I know where the problem is. When you concatenate css files, TYPO3 is fixing the relative links, because the location of the actual called file moved to typo3temp/assets/compressed

When min is doing the same, the file paths are not getting adjusted. Need to investigate further, but it looks promising. Now, I'm on your page ;)

a-r-m-i-n commented 2 years ago

@wazum I think I got it :) Could you please test with the latest master? Thank you

Patta commented 2 years ago

@a-r-m-i-n thanks for version 2.1.0. The relative css paths of fonts in a compressed file are working now, but not if you already use the composer-installer v4 package with TYPO3 11, which has a different folder structure of public assets. See also: https://b13.com/core-insights/typo3-and-composer-weve-come-a-long-way

a-r-m-i-n commented 2 years ago

Thanks for your response. I think this worth a dedicated issue.