Skip to content

Commit 285f31f

Browse files
committed
Fix Jetstream installation missing Alpine bridge
1 parent ccd2a4c commit 285f31f

File tree

4 files changed

+93
-19
lines changed

4 files changed

+93
-19
lines changed

src/Commands/TurboInstallCommand.php

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,74 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Str;
78

89
class TurboInstallCommand extends Command
910
{
10-
public $signature = 'turbo:install {--jet}';
11+
public $signature = 'turbo:install {--jet} {--stimulus : To install Stimulus or not.}';
1112
public $description = 'Install the Turbo resources';
1213

1314
public function handle()
1415
{
1516
$this->updateNodePackages(function ($packages) {
1617
return [
1718
'@hotwired/turbo' => '^7.0.0-beta.3',
18-
'stimulus' => '^2.0.0',
19-
'@stimulus/webpack-helpers' => '^2.0.0',
2019
'laravel-echo' => '^1.10.0',
2120
'pusher-js' => '^7.0.2',
2221
] + $packages;
2322
});
2423

24+
if ($this->option('stimulus')) {
25+
$this->updateNodePackages(function ($packages) {
26+
return [
27+
'stimulus' => '^2.0.0',
28+
'@stimulus/webpack-helpers' => '^2.0.0',
29+
] + $packages;
30+
});
31+
}
32+
2533
if ($this->option('jet')) {
2634
$this->updateNodePackages(function ($packages) {
2735
return [
2836
'alpinejs' => '^2.8.0',
37+
'alpine-turbo-drive-adapter' => '^1.0.1',
2938
] + $packages;
3039
});
3140

3241
if ((new Filesystem())->exists(resource_path('views/layouts/app.blade.php'))) {
33-
(new Filesystem())->put(
34-
resource_path('views/layouts/app.blade.php'),
35-
str_replace(
36-
' @livewireScripts',
37-
" @livewireScripts\n" . ' <script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks@v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false" data-turbo-eval="false"></script>',
38-
(new Filesystem())->get(resource_path('views/layouts/app.blade.php'))
39-
)
40-
);
42+
$this->updateJetstreamLayouts();
4143
}
4244
}
4345

4446
// JS scaffold...
45-
(new Filesystem())->ensureDirectoryExists(resource_path('js/controllers'));
4647
(new Filesystem())->ensureDirectoryExists(resource_path('js/elements'));
48+
4749
copy(__DIR__ . '/../../stubs/resources/js/app.js', resource_path('js/app.js'));
50+
51+
$this->replaceJsStub(
52+
resource_path('js/app.js'),
53+
'//=inject-alpine',
54+
$this->option('jet')
55+
? (new Filesystem())->get(__DIR__ . '/../../stubs/resources/js/alpine.js')
56+
: ''
57+
);
58+
59+
$this->replaceJsStub(
60+
resource_path('js/app.js'),
61+
'//=inject-stimulus',
62+
$this->option('stimulus')
63+
? (new Filesystem())->get(__DIR__ . '/../../stubs/resources/js/stimulus.js')
64+
: ''
65+
);
66+
4867
copy(__DIR__ . '/../../stubs/resources/js/bootstrap.js', resource_path('js/bootstrap.js'));
4968
copy(__DIR__ . '/../../stubs/resources/js/echo.js', resource_path('js/echo.js'));
5069
copy(__DIR__ . '/../../stubs/resources/js/elements/turbo-echo-stream-tag.js', resource_path('js/elements/turbo-echo-stream-tag.js'));
51-
copy(__DIR__ . '/../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
70+
71+
if ($this->option('stimulus')) {
72+
(new Filesystem())->ensureDirectoryExists(resource_path('js/controllers'));
73+
copy(__DIR__ . '/../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
74+
}
5275

5376
$this->info('Turbo Laravel scaffolding installed successfully.');
5477
$this->comment('Please execute the "npm install && npm run dev" command to build your assets.');
@@ -83,4 +106,51 @@ protected static function updateNodePackages(callable $callback, $dev = true)
83106
json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
84107
);
85108
}
109+
110+
private function replaceJsStub(string $inFile, string $lookFor, string $replaceWith): void
111+
{
112+
(new Filesystem())->put(
113+
$inFile,
114+
(string)Str::of((new Filesystem())->get($inFile))
115+
->replace($lookFor, $replaceWith
116+
)
117+
);
118+
}
119+
120+
private function updateJetstreamLayouts(): void
121+
{
122+
(new Filesystem())->put(
123+
resource_path('views/layouts/app.blade.php'),
124+
str_replace(
125+
' @livewireScripts',
126+
" @livewireScripts\n" . ' <script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks@v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false" data-turbo-eval="false"></script>',
127+
(new Filesystem())->get(resource_path('views/layouts/app.blade.php'))
128+
)
129+
);
130+
131+
// Also add the Livewire scripts to the guest layout. This is done because
132+
// Livewire and Alpine don't seem to play well with Turbo Drive when it
133+
// was already started, as app.js is loaded in the guests layout too.
134+
135+
(new Filesystem())->put(
136+
resource_path('views/layouts/guest.blade.php'),
137+
str_replace(
138+
' <link rel="stylesheet" href="{{ mix(\'css/app.css\') }}">',
139+
' <link rel="stylesheet" href="{{ mix(\'css/app.css\') }}">' .
140+
"\n @livewireStyles",
141+
(new Filesystem())->get(resource_path('views/layouts/guest.blade.php'))
142+
)
143+
);
144+
145+
(new Filesystem())->put(
146+
resource_path('views/layouts/guest.blade.php'),
147+
str_replace(
148+
' </body>',
149+
" @livewireScripts\n" .
150+
' <script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks@v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false" data-turbo-eval="false"></script>' .
151+
"\n </body>",
152+
(new Filesystem())->get(resource_path('views/layouts/guest.blade.php'))
153+
)
154+
);
155+
}
86156
}

stubs/resources/js/alpine.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import 'alpine-turbo-drive-adapter';
2+
import 'alpinejs';

stubs/resources/js/app.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import {
33
} from '@hotwired/turbo';
44
import('./bootstrap');
55
import('./elements/turbo-echo-stream-tag');
6-
import { Application } from 'stimulus'
7-
import { definitionsFromContext } from '@stimulus/webpack-helpers'
8-
import 'alpinejs';
6+
//=inject-alpine
7+
//=inject-stimulus
98

109
startTurbo();
11-
const application = Application.start()
12-
const context = require.context("./controllers", true, /\.js$/)
13-
application.load(definitionsFromContext(context))

stubs/resources/js/stimulus.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Application } from 'stimulus'
2+
import { definitionsFromContext } from '@stimulus/webpack-helpers'
3+
4+
const application = Application.start()
5+
const context = require.context("./controllers", true, /\.js$/)
6+
application.load(definitionsFromContext(context))

0 commit comments

Comments
 (0)