8000 [9.x] Vite by jessarcher · Pull Request #42785 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[9.x] Vite #42785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 22, 2022
Prev Previous commit
Next Next commit
Remove vite helper
  • Loading branch information
jessarcher committed May 12, 2022
commit af9058aaf162f3c602ec62966e06bc3e5dafb693
16 changes: 0 additions & 16 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,22 +523,6 @@ function method_field($method)
}
}

if (! function_exists('vite')) {
/**
* Generate Vite tags for entrypoints.
*
* @param string|string[] $entrypoints
* @param string $buildDirectory
* @return \Illuminate\Support\HtmlString
*
* @throws \Exception
*/
function vite($entrypoints, $buildDirectory = 'build')
{
return app(Vite::class)(...func_get_args());
}
}

if (! function_exists('mix')) {
/**
* Get the path to a versioned Mix file.
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/View/Compilers/Concerns/CompilesHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\View\Compilers\Concerns;

use Illuminate\Foundation\Vite;

trait CompilesHelpers
{
/**
Expand Down Expand Up @@ -55,6 +57,8 @@ protected function compileMethod($method)
*/
protected function compileVite($arguments)
{
return "<?php echo vite{$arguments}; ?>";
$class = Vite::class;

return "<?php echo app('$class'){$arguments}; ?>";
}
}
108 changes: 0 additions & 108 deletions tests/Foundation/FoundationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,62 +43,6 @@ public function testCache()
$this->assertSame('default', cache('baz', 'default'));
}

public function testViteWithoutCss()
{
$this->makeViteManifest();

$result = vite(['resources/js/app-without-css.js']);

$this->cleanViteManifest();

$this->assertSame('<script type="module" src="/build/assets/app-without-css.versioned.js"></script>', $result->toHtml());
}

public function testViteWithCss()
{
$this->makeViteManifest();

$result = vite(['resources/js/app-with-css.js']);

$this->cleanViteManifest();

$this->assertSame(
'<link rel="stylesheet" href="/build/assets/app.versioned.css" />'
. '<script type="module" src="/build/assets/app-with-css.versioned.js"></script>',
$result->toHtml()
);
}

public function testViteWithSharedCss()
{
$this->makeViteManifest();

$result = vite(['resources/js/app-with-shared-css.js']);

$this->cleanViteManifest();

$this->assertSame(
'<link rel="stylesheet" href="/build/assets/app.versioned.css" />'
. '<script type="module" src="/build/assets/app-with-shared-css.versioned.js"></script>',
$result->toHtml()
);
}

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

$result = vite(['resources/js/app-with-css.js']);

$this->cleanViteHotFile();

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

public function testMixDoesNotIncludeHost()
{
$app = new Application;
Expand Down Expand Up @@ -300,56 +244,4 @@ public function testMixIsSwappableForTests()

$this->assertSame('expected', mix('asset.png'));
}

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

if (! file_exists(public_path('build'))) {
mkdir(public_path('build'));
}

$manifest = json_encode([
'resources/js/app-without-css.js' => [
'file' => 'assets/app-without-css.versioned.js',
],
'resources/js/app-with-css.js' => [
'file' => 'assets/app-with-css.versioned.js',
'css' => [
'assets/app.versioned.css',
],
],
'resources/js/app-with-shared-css.js' => [
'file' => 'assets/app-with-shared-css.versioned.js',
'imports' => [
'_someFile.js',
],
],
'_someFile.js' => [
'css' => [
'assets/app.versioned.css',
],
],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

file_put_contents(public_path('build/manifest.json'), $manifest);
}

private function cleanViteManifest()
{
unlink(public_path('build/manifest.json'));
rmdir(public_path('build'));
}

private function makeViteHotFile()
{
app()->singleton('path.public', fn () => __DIR__);

file_put_contents(public_path('hot'), 'http://localhost:3000');
}

private function cleanViteHotFile()
{
unlink(public_path('hot'));
}
}
116 changes: 116 additions & 0 deletions tests/Foundation/FoundationViteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Illuminate\Foundation;

use PHPUnit\Framework\TestCase;

class FoundationViteTest extends TestCase
{
public function testViteWithoutCss()
{
$this->makeViteManifest();

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

$this->cleanViteManifest();

$this->assertSame('<script type="module" src="/build/assets/app-without-css.versioned.js"></script>', $result->toHtml());
}

public function testViteWithCss()
{
$this->makeViteManifest();

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

$this->cleanViteManifest();

$this->assertSame(
'<link rel="stylesheet" href="/build/assets/app.versioned.css" />'
. '<script type="module" src="/build/assets/app-with-css.versioned.js"></script>',
$result->toHtml()
);
}

public function testViteWithSharedCss()
{
$this-> B422 makeViteManifest();

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

$this->cleanViteManifest();

$this->assertSame(
'<link rel="stylesheet" href="/build/assets/app.versioned.css" />'
. '<script type="module" src="/build/assets/app-with-shared-css.versioned.js"></script>',
$result->toHtml()
);
}

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

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

$this->cleanViteHotFile();

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

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

if (! file_exists(public_path('build'))) {
mkdir(public_path('build'));
}

$manifest = json_encode([
'resources/js/app-without-css.js' => [
'file' => 'assets/app-without-css.versioned.js',
],
'resources/js/app-with-css.js' => [
'file' => 'assets/app-with-css.versioned.js',
'css' => [
'assets/app.versioned.css',
],
],
'resources/js/app-with-shared-css.js' => [
'file' => 'assets/app-with-shared-css.versioned.js',
'imports' => [
'_someFile.js',
],
],
'_someFile.js' => [
'css' => [
'assets/app.versioned.css',
],
],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

file_put_contents(public_path('build/manifest.json'), $manifest);
}

protected function cleanViteManifest()
{
unlink(public_path('build/manifest.json'));
rmdir(public_path('build'));
}

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

file_put_contents(public_path('hot'), 'http://localhost:3000');
}

protected function cleanViteHotFile()
{
unlink(public_path('hot'));
}
}
D938
2 changes: 1 addition & 1 deletion tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testWithoutViteBindsEmptyHandlerAndReturnsInstance()
{
$instance = $this->withoutVite();

$this->assertSame('', vite(['resources/js/app.js']));
$this->assertSame('', app(Vite::class)(['resources/js/app.js']));
$this->assertSame($this, $instance);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/View/Blade/BladeHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public function testEchosAreCompiled()
$this->assertSame('<?php dd($var1); ?>', $this->compiler->compileString('@dd($var1)'));
$this->assertSame('<?php dd($var1, $var2); ?>', $this->compiler->compileString('@dd($var1, $var2)'));
$this->assertSame('<?php dump($var1, $var2); ?>', $this->compiler->compileString('@dump($var1, $var2)'));
$this->assertSame('<?php echo vite([\'resources/js/app.js\']); ?>', $this->compiler->compileString('@vite([\'resources/js/app.js\'])'));
$this->assertSame('<?php echo app(\'Illuminate\Foundation\Vite\')([\'resources/js/app.js\']); ?>', $this->compiler->compileString('@vite([\'resources/js/app.js\'])'));
}
}
0