diff --git a/src/Actions/ReplaceOrAppendTags.php b/src/Actions/ReplaceOrAppendTags.php new file mode 100644 index 0000000..b266dca --- /dev/null +++ b/src/Actions/ReplaceOrAppendTags.php @@ -0,0 +1,26 @@ +)/'; + + public function __invoke(string $contents) + { + if (preg_match(self::VITE_DIRECTIVE_PATTERN, $contents)) { + return preg_replace( + static::VITE_DIRECTIVE_PATTERN, + "\\1", + $contents, + ); + } + + return preg_replace( + static::CLOSING_HEAD_TAG_PATTERN, + "\n\\1 \\1\\2", + $contents, + ); + } +} diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 9e94d60..f47dffb 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -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')] @@ -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( - "", - '', - 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.*/', - '', - 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 \n\\1\\2", - File::get($file), - ), + (new ReplaceOrAppendTags)(File::get($file)), )); return self::SUCCESS; diff --git a/tests/ReplaceOrAppendTagsTest.php b/tests/ReplaceOrAppendTagsTest.php new file mode 100644 index 0000000..2d27d2e --- /dev/null +++ b/tests/ReplaceOrAppendTagsTest.php @@ -0,0 +1,108 @@ + + + + + + + + {{ config('app.name', 'Laravel') }} + + + + + + + @vite(['resources/js/app.js', 'resources/css/app.css']) + + + + + + BLADE; + + $expected = <<<'BLADE' + + + + + + + + {{ config('app.name', 'Laravel') }} + + + + + + + + + + + + + BLADE; + + $this->assertEquals($expected, (new ReplaceOrAppendTags)($contents)); + } + + /** @test */ + public function appends_to_before_closing_head_tag_when_vite_directive_is_missing() + { + $contents = <<<'BLADE' + + + + + + + + {{ config('app.name', 'Laravel') }} + + + + + + + + + + BLADE; + + $expected = <<<'BLADE' + + + + + + + + {{ config('app.name', 'Laravel') }} + + + + + + + + + + + + BLADE; + + $this->assertEquals($expected, (new ReplaceOrAppendTags)($contents)); + } +}