Skip to content

Commit

Permalink
feat: switch to external paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Aug 25, 2024
1 parent a84329f commit af50afd
Showing 1 changed file with 18 additions and 51 deletions.
69 changes: 18 additions & 51 deletions src/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ class Vite
'assets' => '/assets',
];

/**
* The path to the "hot" file.
*
* @var string|null
*/
protected static $hotFile;

/**
* The path to the build directory.
*
* @var string
*/
protected static $buildDirectory = 'build';

/**
* The name of the manifest file.
*
Expand Down Expand Up @@ -173,30 +159,7 @@ public static function useManifestFilename($filename)
*/
public static function hotFile()
{
$directory = getcwd();
$isMVCApp = is_dir("$directory/app/views") && file_exists("$directory/config/paths.php") && is_dir("$directory/public");

return static::$hotFile ?? $isMVCApp ? PublicPath('hot', false) : (static::$paths['hot'] ?? 'hot');
}

/**
* Set the Vite "hot" file path.
*
* @param string $path
*/
public static function useHotFile($path)
{
static::$hotFile = $path;
}

/**
* Set the Vite build directory.
*
* @param string $path
*/
public static function useBuildDirectory($path)
{
static::$buildDirectory = $path;
return static::$paths['hotFile'] ?? 'hot';
}

/**
Expand All @@ -207,7 +170,9 @@ public static function useBuildDirectory($path)
public static function useScriptTagAttributes($attributes)
{
if (!is_callable($attributes)) {
$attributes = fn () => $attributes;
$attributes = function () use ($attributes) {
return $attributes;
};
}

static::$scriptTagAttributesResolvers[] = $attributes;
Expand All @@ -221,7 +186,7 @@ public static function useScriptTagAttributes($attributes)
public static function useStyleTagAttributes($attributes)
{
if (!is_callable($attributes)) {
$attributes = fn () => $attributes;
$attributes = fn() => $attributes;
}

static::$styleTagAttributesResolvers[] = $attributes;
Expand All @@ -235,7 +200,7 @@ public static function useStyleTagAttributes($attributes)
public static function usePreloadTagAttributes($attributes)
{
if (!is_callable($attributes)) {
$attributes = fn () => $attributes;
$attributes = fn() => $attributes;
}

static::$preloadTagAttributesResolvers[] = $attributes;
Expand All @@ -253,13 +218,15 @@ public static function usePreloadTagAttributes($attributes)
public static function build($entrypoints, $buildDirectory = null)
{
$entrypoints = new Collection($entrypoints);
$buildDirectory ??= static::$buildDirectory;
$buildDirectory ??= static::$paths['build'];

if (static::isRunningHot()) {
return new HtmlString(
$entrypoints
->prepend('@vite/client')
->map(fn ($entrypoint) => static::makeTagForChunk($entrypoint, static::hotAsset($entrypoint), null, null))
->map(function ($entrypoint) {
return static::makeTagForChunk($entrypoint, static::hotAsset($entrypoint), null, null);
})
->join('')
);
}
Expand Down Expand Up @@ -332,11 +299,11 @@ public static function build($entrypoints, $buildDirectory = null)
}
}

[$stylesheets, $scripts] = $tags->unique()->partition(fn ($tag) => str_starts_with($tag, '<link'));
[$stylesheets, $scripts] = $tags->unique()->partition(fn($tag) => str_starts_with($tag, '<link'));

$preloads = $preloads->unique()
->sortByDesc(fn ($args) => static::isCssPath($args[1]))
->map(fn ($args) => static::makePreloadTagForChunk(...$args));
->sortByDesc(fn($args) => static::isCssPath($args[1]))
->map(fn($args) => static::makePreloadTagForChunk(...$args));

return new HtmlString($preloads->join('') . $stylesheets->join('') . $scripts->join(''));
}
Expand Down Expand Up @@ -555,9 +522,9 @@ protected static function isCssPath($path)
protected static function parseAttributes($attributes)
{
return Collection::make($attributes)
->reject(fn ($value, $key) => in_array($value, [false, null], true))
->flatMap(fn ($value, $key) => $value === true ? [$key] : [$key => $value])
->map(fn ($value, $key) => is_int($key) ? $value : $key . '="' . $value . '"')
->reject(fn($value, $key) => in_array($value, [false, null], true))
->flatMap(fn($value, $key) => $value === true ? [$key] : [$key => $value])
->map(fn($value, $key) => is_int($key) ? $value : $key . '="' . $value . '"')
->values()
->all();
}
Expand Down Expand Up @@ -613,7 +580,7 @@ protected static function hotAsset($asset)
*/
public static function asset($asset, $buildDirectory = null)
{
$buildDirectory ??= static::$buildDirectory;
$buildDirectory ??= static::$paths['build'];

if (static::isRunningHot()) {
return static::hotAsset($asset);
Expand Down Expand Up @@ -678,7 +645,7 @@ protected static function manifestPath($buildDirectory)
*/
public static function manifestHash($buildDirectory = null)
{
$buildDirectory ??= static::$buildDirectory;
$buildDirectory ??= static::$paths['build'];

if (static::isRunningHot()) {
return null;
Expand Down

0 comments on commit af50afd

Please sign in to comment.