Skip to content

Commit

Permalink
fix: Fixed an issue where craft.vite.entry() would fail if you were…
Browse files Browse the repository at this point in the history
… using Vite 5 or later, due to the `ManifestHelper::fileNameWithoutHash()` function not working correctly ([#24](#24))
  • Loading branch information
khalwat committed Mar 2, 2024
1 parent 26dbe7e commit 8936c5b
Showing 1 changed file with 74 additions and 74 deletions.
148 changes: 74 additions & 74 deletions src/helpers/ManifestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,55 +178,6 @@ public static function extractManifestTags(string $path, bool $asyncCss = true,
return $tags;
}

/**
* Extract any import files from entries recursively
*
* @param array $manifest
* @param string $manifestKey
* @param array $importFiles
*
* @return array
*/
protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array
{
$entry = $manifest[$manifestKey] ?? null;
if (!$entry) {
return [];
}

$imports = $entry['imports'] ?? [];
foreach ($imports as $import) {
$importFiles[$import] = $manifest[$import]['file'];
self::extractImportFiles($manifest, $import, $importFiles);
}

return $importFiles;
}

/**
* Extract any CSS files from entries recursively
*
* @param array $manifest
* @param string $manifestKey
* @param array $cssFiles
*
* @return array
*/
protected static function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array
{
$entry = $manifest[$manifestKey] ?? null;
if (!$entry) {
return [];
}
$cssFiles = array_merge($cssFiles, $entry['css'] ?? []);
$imports = $entry['imports'] ?? [];
foreach ($imports as $import) {
self::extractCssFiles($manifest, $import, $cssFiles);
}

return $cssFiles;
}

/**
* Return an array of tags from the manifest, for both modern and legacy builds
*
Expand All @@ -250,9 +201,6 @@ public static function legacyManifestTags(string $path, bool $asyncCss = true, a
return self::extractManifestTags($legacyPath, $asyncCss, $scriptTagAttrs, $cssTagAttrs, true);
}

// Protected Static Methods
// =========================================================================

/**
* Extract an entry file URL from all of the entries in the manifest
*
Expand Down Expand Up @@ -285,28 +233,6 @@ public static function extractEntry(string $path): string
return '';
}

/**
* Return a file name from the passed in $path, with any version hash removed from it
*
* @param string $path
* @return string
*/
protected static function filenameWithoutHash(string $path): string
{
// Get just the file name
$filenameParts = explode('/', $path);
$filename = end($filenameParts);
// If there is a version hash, remove it
$filenameParts = explode('.', $filename);
$dotSegments = count($filenameParts);
if ($dotSegments > 2) {
unset($filenameParts[$dotSegments - 2]);
$filename = implode('.', $filenameParts);
}

return (string)$filename;
}

/**
* Extract any asset files from all of the entries in the manifest
*
Expand All @@ -330,4 +256,78 @@ public static function extractAssetFiles(): array

return $assetFiles;
}

// Protected Static Methods
// =========================================================================

/**
* Extract any import files from entries recursively
*
* @param array $manifest
* @param string $manifestKey
* @param array $importFiles
*
* @return array
*/
protected static function extractImportFiles(array $manifest, string $manifestKey, array &$importFiles): array
{
$entry = $manifest[$manifestKey] ?? null;
if (!$entry) {
return [];
}

$imports = $entry['imports'] ?? [];
foreach ($imports as $import) {
$importFiles[$import] = $manifest[$import]['file'];
self::extractImportFiles($manifest, $import, $importFiles);
}

return $importFiles;
}

/**
* Extract any CSS files from entries recursively
*
* @param array $manifest
* @param string $manifestKey
* @param array $cssFiles
*
* @return array
*/
protected static function extractCssFiles(array $manifest, string $manifestKey, array &$cssFiles): array
{
$entry = $manifest[$manifestKey] ?? null;
if (!$entry) {
return [];
}
$cssFiles = array_merge($cssFiles, $entry['css'] ?? []);
$imports = $entry['imports'] ?? [];
foreach ($imports as $import) {
self::extractCssFiles($manifest, $import, $cssFiles);
}

return $cssFiles;
}

/**
* Return a file name from the passed in $path, with any version hash removed from it
*
* @param string $path
* @return string
*/
protected static function filenameWithoutHash(string $path): string
{
$pathInfo = pathinfo($path);
$filename = $pathInfo['filename'];
$extension = $pathInfo['extension'];
$hashPos = strpos($filename, '.') ?: strlen($filename);
$hash = substr($filename, $hashPos);
// Vite 5 now uses a `-` to separate the version hash, so account for that as well
if (empty($hash) && str_contains($filename, '-')) {
$hash = substr($filename, strpos($filename, '-'));
}
$filename = str_replace($hash, '', $filename);

return implode('.', [$filename, $extension]);
}
}

0 comments on commit 8936c5b

Please sign in to comment.