Skip to content

Commit

Permalink
Ensure installation replaces vite directive or appends to the layout
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Feb 23, 2024
1 parent 672c0f6 commit ce30470
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 59 deletions.
26 changes: 26 additions & 0 deletions src/Actions/ReplaceOrAppendTags.php
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,
);
}
}
62 changes: 3 additions & 59 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Terminal;
use Tonysm\ImportmapLaravel\Actions\FixJsImportPaths;
use Tonysm\ImportmapLaravel\Actions\ReplaceOrAppendTags;
use Tonysm\ImportmapLaravel\Events\FailedToFixImportStatement;

#[AsCommand('importmap:install')]
Expand Down Expand Up @@ -126,68 +127,11 @@ private function importDependenciesFromNpm(): void

private function updateAppLayouts(): void
{
if (File::exists(base_path('webpack.mix.js'))) {
$this->updateAppLayoutsUsingMix();
} elseif (File::exists(base_path('vite.config.js'))) {
$this->updateAppLayoutsUsingVite();
} else {
$this->appendImportmapTagsToLayoutsHead();
}
}

private function updateAppLayoutsUsingMix()
{
$this->displayTask('replacing Mix functions in layouts', function () {
$this->existingLayoutFiles()
->each(fn ($file) => File::put(
$file,
str_replace(
"<script src=\"{{ mix('js/app.js') }}\" defer></script>",
'<x-importmap::tags />',
File::get($file),
),
));

return self::SUCCESS;
});
}

private function updateAppLayoutsUsingVite()
{
$this->displayTask('replacing Vite functions in layouts', function () {
$this->existingLayoutFiles()
->each(fn ($file) => File::put(
$file,
preg_replace(
'/\@vite.*/',
'<x-importmap::tags />',
File::get($file),
),
))
->each(fn ($file) => File::put(
$file,
preg_replace(
'/.*\@vite\(\[\]\).*\n/',
'',
File::get($file),
),
));

return self::SUCCESS;
});
}

private function appendImportmapTagsToLayoutsHead(): void
{
$this->displayTask('adding importmap tags to layouts', function () {
$this->displayTask('Updating layout files', function () {
$this->existingLayoutFiles()
->each(fn ($file) => File::put(
$file,
preg_replace(
'/(\s*)(<\/head>)/',
"\\1 <x-importmap::tags />\n\\1\\2",
File::get($file),
),
(new ReplaceOrAppendTags)(File::get($file)),
));

return self::SUCCESS;
Expand Down
108 changes: 108 additions & 0 deletions tests/ReplaceOrAppendTagsTest.php
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

View workflow job for this annotation

GitHub Actions / P8.1 - L9.45.* - prefer-lowest - windows-latest

Failed asserting that two strings are equal.

Check failure on line 106 in tests/ReplaceOrAppendTagsTest.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-lowest - windows-latest

Failed asserting that two strings are equal.

Check failure on line 106 in tests/ReplaceOrAppendTagsTest.php

View workflow job for this annotation

GitHub Actions / P8.1 - L10.* - prefer-stable - windows-latest

Failed asserting that two strings are equal.
}
}

0 comments on commit ce30470

Please sign in to comment.