Fix #1727 Recompile SCSS/LESS files when imported file is changed

- import된 파일 목록을 따로 보관해 두었다가, 그 중 하나라도 변경되었다면 다시 컴파일
- PHP의 stat cache 덕분에 성능 영향은 크지 않을 것으로 보이나, 모니터링 필요
This commit is contained in:
Kijin Sung 2021-06-20 22:02:26 +09:00
parent f416b7c8a4
commit 664a68fc2a
2 changed files with 57 additions and 7 deletions

View file

@ -298,11 +298,47 @@ class FrontEndFileHandler extends Handler
$compiledFileName = $file->fileName . ($minify ? '.min' : '') . '.css';
$compiledFileHash = sha1($file->fileRealPath . ':' . serialize($file->vars));
$compiledFilePath = \RX_BASEDIR . self::$assetdir . '/compiled/' . $compiledFileHash . '.' . $compiledFileName;
$importedFileName = $file->fileName . ($minify ? '.min' : '') . '.imports.php';
$importedFilePath = \RX_BASEDIR . self::$assetdir . '/compiled/' . $compiledFileHash . '.' . $importedFileName;
if (!file_exists($compiledFilePath) || filemtime($compiledFilePath) < filemtime($file->fileFullPath))
if (!file_exists($compiledFilePath))
{
$recompile = 1;
}
else
{
$compiledTime = filemtime($compiledFilePath);
if ($compiledTime < filemtime($file->fileFullPath))
{
$recompile = 2;
}
else
{
$checklist = Rhymix\Framework\Storage::readPHPData($importedFilePath);
if (is_array($checklist))
{
$recompile = 0;
foreach ($checklist as $filename)
{
if (!file_exists($filename) || filemtime($filename) > $compiledTime)
{
$recompile = 3;
break;
}
}
}
else
{
$recompile = 4;
}
}
}
if ($recompile)
{
$method_name = 'compile' . $file->fileExtension;
$success = Rhymix\Framework\Formatter::$method_name($file->fileFullPath, $compiledFilePath, $file->vars, $minify);
Rhymix\Framework\Formatter::$method_name($file->fileFullPath, $compiledFilePath, $file->vars, $minify);
}
$file->fileName = $compiledFileHash . '.' . $compiledFileName;

View file

@ -179,7 +179,8 @@ class Formatter
public static function compileLESS($source_filename, $target_filename, $variables = array(), $minify = false)
{
// Get the cleaned and concatenated content.
$content = self::concatCSS($source_filename, $target_filename);
$imported_list = [];
$content = self::concatCSS($source_filename, $target_filename, true, $imported_list);
// Compile!
try
@ -206,6 +207,11 @@ class Formatter
// Save the result to the target file.
Storage::write($target_filename, $content);
// Save the list of imported files.
Storage::writePHPData(preg_replace('/\.css$/', '.imports.php', $target_filename), $imported_list, null, false);
// Also return the compiled CSS content.
return $result;
}
@ -221,7 +227,8 @@ class Formatter
public static function compileSCSS($source_filename, $target_filename, $variables = array(), $minify = false)
{
// Get the cleaned and concatenated content.
$content = self::concatCSS($source_filename, $target_filename);
$imported_list = [];
$content = self::concatCSS($source_filename, $target_filename, true, $imported_list);
// Compile!
try
@ -248,6 +255,11 @@ class Formatter
// Save the result to the target file.
Storage::write($target_filename, $content);
// Save the list of imported files.
Storage::writePHPData(preg_replace('/\.css$/', '.imports.php', $target_filename), $imported_list, null, false);
// Also return the compiled CSS content.
return $result;
}
@ -311,9 +323,10 @@ class Formatter
* @param string|array $source_filename
* @param string $target_filename
* @param bool $add_comment
* @param array &$imported_list
* @return string
*/
public static function concatCSS($source_filename, $target_filename, $add_comment = true)
public static function concatCSS($source_filename, $target_filename, $add_comment = true, &$imported_list = [])
{
$result = '';
@ -339,7 +352,7 @@ class Formatter
// Convert all paths in LESS and SCSS imports, too.
$import_type = ends_with('.scss', $filename) ? 'scss' : 'normal';
$content = preg_replace_callback('/@import\s+(?:\\([^()]+\\))?([^;]+);/', function($matches) use($filename, $target_filename, $import_type) {
$content = preg_replace_callback('/@import\s+(?:\\([^()]+\\))?([^;]+);/', function($matches) use($filename, $target_filename, $import_type, &$imported_list) {
$import_content = '';
$import_files = array_map(function($str) use($filename, $import_type) {
$str = trim(trim(trim(preg_replace('/^url\\(([^()]+)\\)$/', '$1', trim($str))), '"\''));
@ -377,7 +390,8 @@ class Formatter
{
if (!preg_match('!^(https?:)?//!i', $import_filename) && file_exists($import_filename))
{
$import_content .= self::concatCSS($import_filename, $target_filename, false);
$imported_list[] = $import_filename;
$import_content .= self::concatCSS($import_filename, $target_filename, false, $imported_list);
}
else
{