Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[StimulusBundle] Check controllers source files for laziness #2304

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function loadCustomControllers(): array
$name = str_replace(['_', '/', '\\'], ['-', '--', '--'], $name);

$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath());
$content = $asset->content ?: file_get_contents($asset->sourcePath);
$content = file_get_contents($asset->sourcePath);
$isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $content);

$controllersMap[$name] = new MappedControllerAsset($asset, $isLazy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Symfony\UX\StimulusBundle\AssetMapper\MappedControllerAutoImport;
use Symfony\UX\StimulusBundle\Ux\UxPackageReader;

class ControllerMapGeneratorTest extends TestCase
class ControllersMapGeneratorTest extends TestCase
{
public function testGetControllersMap()
{
Expand All @@ -41,7 +41,12 @@ public function testGetControllersMap()
$logicalPath = substr($path, $assetsPosition + 1);
}

return new MappedAsset($logicalPath, $path, content: file_get_contents($path));
$content = null;
if (str_ends_with($path, 'minified-controller.js')) {
$content = 'import{Controller}from"@hotwired/stimulus";export default class extends Controller{}';
}

return new MappedAsset($logicalPath, $path, content: $content);
});

$packageReader = new UxPackageReader(__DIR__.'/../fixtures');
Expand Down Expand Up @@ -73,8 +78,8 @@ public function testGetControllersMap()
$map = $generator->getControllersMap();
// + 3 controller.json UX controllers
// - 1 controllers.json UX controller is disabled
// + 9 custom controllers (1 file is not a controller & 1 is overridden)
$this->assertCount(11, $map);
// + 10 custom controllers (1 file is not a controller & 1 is overridden)
$this->assertCount(12, $map);
$packageNames = array_keys($map);
sort($packageNames);
$this->assertSame([
Expand All @@ -84,6 +89,7 @@ public function testGetControllersMap()
'hello',
'hello-with-dashes',
'hello-with-underscores',
'minified',
'other',
'subdir--deeper',
'subdir--deeper-with-dashes',
Expand Down Expand Up @@ -115,5 +121,8 @@ public function testGetControllersMap()

$otherController = $map['other'];
$this->assertTrue($otherController->isLazy);

$minifiedController = $map['minified'];
$this->assertTrue($minifiedController->isLazy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public function testFullApplicationLoad()
// 2x from UX packages, which are enabled in controllers.json
'/assets/fake-vendor/ux-package1/package-controller-second.js',
'/assets/fake-vendor/ux-package2/package-hello-controller.js',
// 2x from more-controllers
// 3x from more-controllers
'/assets/more-controllers/hello-controller.js',
'/assets/more-controllers/minified-controller.js',
'/assets/more-controllers/other-controller.js',
// 5x from importmap.php
'@hotwired/stimulus',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// minified-controller.js
import { Controller } from '@hotwired/stimulus';

/* stimulusFetch: 'lazy' */
export default class extends Controller {
}