generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure installation replaces vite directive or appends to the layout
- Loading branch information
Showing
3 changed files
with
137 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tonysm\ImportmapLaravel\Actions; | ||
|
||
class ReplaceOrAppendTags | ||
{ | ||
const VITE_DIRECTIVE_PATTERN = '/(\s*)\@vite\(.+\)/'; | ||
const CLOSING_HEAD_TAG_PATTERN = '/(\s*)(<\/head>)/'; | ||
|
||
public function __invoke(string $contents) | ||
{ | ||
if (preg_match(self::VITE_DIRECTIVE_PATTERN, $contents)) { | ||
return preg_replace( | ||
static::VITE_DIRECTIVE_PATTERN, | ||
"\\1<x-importmap::tags />", | ||
$contents, | ||
); | ||
} | ||
|
||
return preg_replace( | ||
static::CLOSING_HEAD_TAG_PATTERN, | ||
"\n\\1 <x-importmap::tags />\\1\\2", | ||
$contents, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace Tonysm\ImportmapLaravel\Tests; | ||
|
||
use Tonysm\ImportmapLaravel\Actions\ReplaceOrAppendTags; | ||
|
||
class ReplaceOrAppendTagsTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function replace_vite_tags() | ||
{ | ||
$contents = <<<'BLADE' | ||
<!DOCTYPE html> | ||
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="csrf-token" content="{{ csrf_token() }}"> | ||
<title>{{ config('app.name', 'Laravel') }}</title> | ||
<!-- Fonts --> | ||
<link rel="preconnect" href="https://fonts.bunny.net"> | ||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" /> | ||
<!-- Scripts --> | ||
@vite(['resources/js/app.js', 'resources/css/app.css']) | ||
</head> | ||
<body class="font-sans antialiased"> | ||
<!-- ... --> | ||
</body> | ||
</html> | ||
BLADE; | ||
|
||
$expected = <<<'BLADE' | ||
<!DOCTYPE html> | ||
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="csrf-token" content="{{ csrf_token() }}"> | ||
<title>{{ config('app.name', 'Laravel') }}</title> | ||
<!-- Fonts --> | ||
<link rel="preconnect" href="https://fonts.bunny.net"> | ||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" /> | ||
<!-- Scripts --> | ||
<x-importmap::tags /> | ||
</head> | ||
<body class="font-sans antialiased"> | ||
<!-- ... --> | ||
</body> | ||
</html> | ||
BLADE; | ||
|
||
$this->assertEquals($expected, (new ReplaceOrAppendTags)($contents)); | ||
} | ||
|
||
/** @test */ | ||
public function appends_to_before_closing_head_tag_when_vite_directive_is_missing() | ||
{ | ||
$contents = <<<'BLADE' | ||
<!DOCTYPE html> | ||
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="csrf-token" content="{{ csrf_token() }}"> | ||
<title>{{ config('app.name', 'Laravel') }}</title> | ||
<!-- Fonts --> | ||
<link rel="preconnect" href="https://fonts.bunny.net"> | ||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" /> | ||
</head> | ||
<body class="font-sans antialiased"> | ||
<!-- ... --> | ||
</body> | ||
</html> | ||
BLADE; | ||
|
||
$expected = <<<'BLADE' | ||
<!DOCTYPE html> | ||
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta name="csrf-token" content="{{ csrf_token() }}"> | ||
<title>{{ config('app.name', 'Laravel') }}</title> | ||
<!-- Fonts --> | ||
<link rel="preconnect" href="https://fonts.bunny.net"> | ||
<link href="https://fonts.bunny.net/css?family=figtree:400,500,600&display=swap" rel="stylesheet" /> | ||
<x-importmap::tags /> | ||
</head> | ||
<body class="font-sans antialiased"> | ||
<!-- ... --> | ||
</body> | ||
</html> | ||
BLADE; | ||
|
||
$this->assertEquals($expected, (new ReplaceOrAppendTags)($contents)); | ||
Check failure on line 106 in tests/ReplaceOrAppendTagsTest.php GitHub Actions / P8.1 - L9.45.* - prefer-lowest - windows-latest
Check failure on line 106 in tests/ReplaceOrAppendTagsTest.php GitHub Actions / P8.1 - L10.* - prefer-lowest - windows-latest
|
||
} | ||
} |