Skip to content

Commit

Permalink
bug symfony#2304 [StimulusBundle] Check controllers source files for …
Browse files Browse the repository at this point in the history
…laziness (MatTheCat)

This PR was merged into the 2.x branch.

Discussion
----------

[StimulusBundle] Check controllers source files for laziness

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Issues        | Fix sensiolabs/minify-bundle#10
| License       | MIT

The StimulusBundle allows to mark a controller as lazy using a `/* stimulusFetch: 'lazy' */` comment, but it will be searched in said controller’s *compiled* content. If the compilation removes that comment (it typically happens when using minifiers), then the controller is no longer considered lazy by the `ControllersMapGenerator`.

This PR makes the comment searched in source files, so that its presence doesn’t depend on the compilation’s result.

Commits
-------

cd2e155 [StimulusBundle] Check controllers source files for laziness
  • Loading branch information
javiereguiluz committed Oct 30, 2024
2 parents 8dcf73d + cd2e155 commit fffd7c8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
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 {
}

0 comments on commit fffd7c8

Please sign in to comment.