Skip to content

Commit

Permalink
Merge pull request #110 from hotwired-laravel/fix-install-command-js-…
Browse files Browse the repository at this point in the history
…imports

Ensure the install command properly configures the JS imports
  • Loading branch information
tonysm authored May 13, 2023
2 parents 8449da6 + d3e51bb commit 8dc043a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
38 changes: 21 additions & 17 deletions src/Commands/TurboInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,14 @@ private function publishJsFiles()
File::copy(__DIR__ . '/../../stubs/resources/js/libs/turbo.js', resource_path('js/libs/turbo.js'));
File::copy(__DIR__ . '/../../stubs/resources/js/elements/turbo-echo-stream-tag.js', resource_path('js/elements/turbo-echo-stream-tag.js'));

if ($this->option('alpine') || $this->option('jet')) {
if ($this->option('jet')) {
File::copy(__DIR__ . '/../../stubs/resources/js/libs/alpine-jet.js', resource_path('js/libs/alpine.js'));
} elseif ($this->option('alpine')) {
File::copy(__DIR__ . '/../../stubs/resources/js/libs/alpine.js', resource_path('js/libs/alpine.js'));
}

$imports = $this->appJsImportLines();

File::put(
$appJsFile = resource_path('js/app.js'),
preg_replace(
'/(.*[\'"](?:\.\/)?bootstrap[\'"]\)?;?)/',
<<<JS
\\1
{$imports}
JS,
File::get($appJsFile),
),
);

File::put(resource_path('js/app.js'), $this->appJsImportLines());
File::put(resource_path('js/libs/index.js'), $this->libsIndexJsImportLines());

return self::SUCCESS;
});
Expand All @@ -69,12 +59,26 @@ private function appJsImportLines()
$prefix = $this->usingImportmaps() ? '' : './';

$imports = [
"import '{$prefix}bootstrap';",
"import '{$prefix}elements/turbo-echo-stream-tag';",
"import '{$prefix}libs/turbo';",
"import '{$prefix}libs';",
];

return implode("\n", $imports);
}

private function libsIndexJsImportLines()
{
$imports = [];

$imports[] = $this->usingImportmaps()
? "import 'libs/turbo';"
: "import './turbo';";

if ($this->option('alpine') || $this->option('jet')) {
$imports[] = "import '{$prefix}libs/alpine';";
$imports[] = $this->usingImportmaps()
? "import 'libs/alpine';"
: "import './alpine';";
}

return implode("\n", $imports);
Expand Down
27 changes: 27 additions & 0 deletions stubs/resources/js/libs/alpine-jet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Alpine from 'alpinejs';
import focus from '@alpinejs/focus';

window.Alpine = Alpine;

Alpine.plugin(focus);

Alpine.start();

function initAlpineTurboPermanentFix() {
document.addEventListener('turbo:before-render', () => {
let permanents = document.querySelectorAll('[data-turbo-permanent]');
let undos = Array.from(permanents).map(el => {
el._x_ignore = true;
return () => {
delete el._x_ignore;
};
});

document.addEventListener('turbo:render', function handler() {
while(undos.length) undos.shift()();
document.removeEventListener('turbo:render', handler);
});
});
}

initAlpineTurboPermanentFix();
10 changes: 7 additions & 3 deletions stubs/resources/js/libs/alpine.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import Alpine from 'alpinejs';

window.Alpine = Alpine;

Alpine.start();

function initAlpineTurboPermanentFix() {
document.addEventListener('turbo:before-render', () => {
let permanents = document.querySelectorAll('[data-turbo-permanent]');
Expand All @@ -15,6 +21,4 @@ function initAlpineTurboPermanentFix() {
});
}

if (window.Alpine !== undefined) {
initAlpineTurboPermanentFix();
}
initAlpineTurboPermanentFix();

0 comments on commit 8dc043a

Please sign in to comment.