diff --git a/src/Precompilers/ExtractTemplate.php b/src/Precompilers/ExtractTemplate.php index 56619c4..ed5c7fc 100644 --- a/src/Precompilers/ExtractTemplate.php +++ b/src/Precompilers/ExtractTemplate.php @@ -44,15 +44,21 @@ protected function shouldExtractTemplate(string $template): bool */ protected function imports(string $template): string { - $phpScript = trim(preg_replace('/.*<\?php\s*(.*?)\s*\?>.*/s', '$1', $template)); - $phpScript = trim(preg_replace('/^(?!use\s+.*?;).*$/m', '', $phpScript)); - $phpScript = trim(preg_replace('/^use\s+function\s+Livewire\\\\Volt.*$/m', '', $phpScript)); + preg_match_all('/<\?php\s*(.*?)\s*\?>/s', $template, $matches); - if (! empty($phpScript)) { - $phpScript = ''."\n\n"; + $script = collect($matches[1]) + ->map(fn (string $script) => trim($script)) + ->reject(fn (string $script) => empty($script)) + ->implode("\n"); + + $script = trim(preg_replace('/^(?!use\s+.*?;).*$/m', '', $script)); + $script = trim(preg_replace('/^use\s+function\s+Livewire\\\\Volt.*$/m', '', $script)); + + if (! empty($script)) { + $script = ''."\n\n"; } - return $phpScript; + return $script; } /** @@ -60,7 +66,9 @@ protected function imports(string $template): string */ protected function html(string $template): string { - return trim(preg_replace('/<\?php\s*(.*?)\s*\?>/s', '', $template)); + $template = trim(preg_replace('/<\?php\s*(.*?)\s*\?>/s', '', $template)); + + return str($template)->beforeLast('trim()->value(); } /** diff --git a/tests/.pest/snapshots/Feature/FunctionalComponentTest/generated_code_with_data_set__dataset__component_with_user_imports_blade_php______tests_Feature_resources_view___de_php__.snap b/tests/.pest/snapshots/Feature/FunctionalComponentTest/generated_code_with_data_set__dataset__component_with_user_imports_blade_php______tests_Feature_resources_view___de_php__.snap new file mode 100644 index 0000000..d31a6cc --- /dev/null +++ b/tests/.pest/snapshots/Feature/FunctionalComponentTest/generated_code_with_data_set__dataset__component_with_user_imports_blade_php______tests_Feature_resources_view___de_php__.snap @@ -0,0 +1,32 @@ +execute(static::$__context, $this, get_defined_vars()); + + (new Actions\CallHook('mount'))->execute(static::$__context, $this, get_defined_vars()); + } + + public function increment() + { + $arguments = [static::$__context, $this, func_get_args()]; + + return (new Actions\CallMethod('increment'))->execute(...$arguments); + } + +}; \ No newline at end of file diff --git a/tests/Feature/FunctionalComponentTest.php b/tests/Feature/FunctionalComponentTest.php index a20d417..26b87ff 100644 --- a/tests/Feature/FunctionalComponentTest.php +++ b/tests/Feature/FunctionalComponentTest.php @@ -17,6 +17,7 @@ use Livewire\Volt\Volt; use Pest\TestSuite; use Tests\Fixtures\GlobalTrait; +use Tests\Fixtures\User; beforeEach(function () { View::setFinder(new FileViewFinder(app()['files'], [__DIR__.'/resources/views'])); @@ -789,3 +790,18 @@ public function render() ->assertSeeVolt('basic-component') ->assertOk(); }); + +test('user imports on bottom do not conflict', function () { + User::create([ + 'name' => 'Taylor', + 'email' => 'taylor@laravel.com', + 'password' => 'password', + ]); + + Livewire::test('component-with-user-imports', ['name' => 'Taylor']) + ->assertSet('counter', 0) + ->call('increment') + ->call('increment') + ->call('increment') + ->assertSet('counter', 3); +}); diff --git a/tests/Feature/resources/views/functional-api/component-with-user-imports.blade.php b/tests/Feature/resources/views/functional-api/component-with-user-imports.blade.php new file mode 100644 index 0000000..0844a58 --- /dev/null +++ b/tests/Feature/resources/views/functional-api/component-with-user-imports.blade.php @@ -0,0 +1,21 @@ +