Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/Illuminate/Foundation/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

class Vite
{
/**
* The callback to be used when making script tags.
*
* @var \Closure|null
*/
protected static $scriptTagCallback;

/**
* The callback to be used when making stylesheet tags.
*
* @var \Closure|null
*/
protected static $stylesheetTagCallback;

/**
* Generate Vite tags for an entrypoint.
*
Expand Down Expand Up @@ -130,9 +144,24 @@ protected function makeTag($url)
*/
protected function makeScriptTag($url)
{
if (isset(static::$scriptTagCallback)) {
return (static::$scriptTagCallback)($url);
}

return sprintf('<script type="module" src="%s"></script>', $url);
}

/**
* Specify a callback to be used when making script tags.
*
* @param \Closure $callback
* @return void
*/
public static function makeScriptTagUsing($callback)
{
static::$scriptTagCallback = $callback;
}

/**
* Generate a stylesheet tag for the given URL.
*
Expand All @@ -141,9 +170,24 @@ protected function makeScriptTag($url)
*/
protected function makeStylesheetTag($url)
{
if (isset(static::$stylesheetTagCallback)) {
return (static::$stylesheetTagCallback)($url);
}

return sprintf('<link rel="stylesheet" href="%s" />', $url);
}

/**
* Specify a callback to be used when making stylesheet tags.
*
* @param \Closure $callback
* @return void
*/
public static function makeStylesheetTagUsing($callback)
{
static::$stylesheetTagCallback = $callback;
}

/**
* Determine whether the given path is a CSS file.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ protected function tearDown(): void
$this->cleanViteManifest();
$this->cleanViteHotFile();
m::close();
Vite::makeScriptTagUsing(null);
Vite::makeStylesheetTagUsing(null);
}

public function testViteWithJsOnly()
Expand Down Expand Up @@ -101,6 +103,42 @@ public function testViteHotModuleReplacementWithJsAndCss()
);
}

public function testViteCustomScriptTagGeneration()
{
$this->makeViteHotFile();

Vite::makeScriptTagUsing(function (string $url) {
return sprintf('<script type="module" src="%s" defer></script>', $url);
});

$result = (new Vite)(['resources/css/app.css', 'resources/js/app.js']);

$this->assertSame(
'<script type="module" src="http://localhost:3000/@vite/client" defer></script>'
.'<link rel="stylesheet" href="http://localhost:3000/resources/css/app.css" />'
.'<script type="module" src="http://localhost:3000/resources/js/app.js" defer></script>',
$result->toHtml()
);
}

public function testViteCustomStylesheetTagGeneration()
{
$this->makeViteHotFile();

Vite::makeStylesheetTagUsing(function (string $url) {
return sprintf('<link rel="stylesheet" href="%s" crossorigin />', $url);
});

$result = (new Vite)(['resources/css/app.css', 'resources/js/app.js']);

$this->assertSame(
'<script type="module" src="http://localhost:3000/@vite/client"></script>'
.'<link rel="stylesheet" href="http://localhost:3000/resources/css/app.css" crossorigin />'
.'<script type="module" src="http://localhost:3000/resources/js/app.js"></script>',
$result->toHtml()
);
}

protected function makeViteManifest()
{
app()->singleton('path.public', fn () => __DIR__);
Expand Down