Skip to content

Commit fffd7c8

Browse files
committed
bug #2304 [StimulusBundle] Check controllers source files for 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
2 parents 8dcf73d + cd2e155 commit fffd7c8

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function loadCustomControllers(): array
7777
$name = str_replace(['_', '/', '\\'], ['-', '--', '--'], $name);
7878

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

8383
$controllersMap[$name] = new MappedControllerAsset($asset, $isLazy);

src/StimulusBundle/tests/AssetMapper/ControllerMapGeneratorTest.php renamed to src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\UX\StimulusBundle\AssetMapper\MappedControllerAutoImport;
2121
use Symfony\UX\StimulusBundle\Ux\UxPackageReader;
2222

23-
class ControllerMapGeneratorTest extends TestCase
23+
class ControllersMapGeneratorTest extends TestCase
2424
{
2525
public function testGetControllersMap()
2626
{
@@ -41,7 +41,12 @@ public function testGetControllersMap()
4141
$logicalPath = substr($path, $assetsPosition + 1);
4242
}
4343

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

4752
$packageReader = new UxPackageReader(__DIR__.'/../fixtures');
@@ -73,8 +78,8 @@ public function testGetControllersMap()
7378
$map = $generator->getControllersMap();
7479
// + 3 controller.json UX controllers
7580
// - 1 controllers.json UX controller is disabled
76-
// + 9 custom controllers (1 file is not a controller & 1 is overridden)
77-
$this->assertCount(11, $map);
81+
// + 10 custom controllers (1 file is not a controller & 1 is overridden)
82+
$this->assertCount(12, $map);
7883
$packageNames = array_keys($map);
7984
sort($packageNames);
8085
$this->assertSame([
@@ -84,6 +89,7 @@ public function testGetControllersMap()
8489
'hello',
8590
'hello-with-dashes',
8691
'hello-with-underscores',
92+
'minified',
8793
'other',
8894
'subdir--deeper',
8995
'subdir--deeper-with-dashes',
@@ -115,5 +121,8 @@ public function testGetControllersMap()
115121

116122
$otherController = $map['other'];
117123
$this->assertTrue($otherController->isLazy);
124+
125+
$minifiedController = $map['minified'];
126+
$this->assertTrue($minifiedController->isLazy);
118127
}
119128
}

src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public function testFullApplicationLoad()
6363
// 2x from UX packages, which are enabled in controllers.json
6464
'/assets/fake-vendor/ux-package1/package-controller-second.js',
6565
'/assets/fake-vendor/ux-package2/package-hello-controller.js',
66-
// 2x from more-controllers
66+
// 3x from more-controllers
6767
'/assets/more-controllers/hello-controller.js',
68+
'/assets/more-controllers/minified-controller.js',
6869
'/assets/more-controllers/other-controller.js',
6970
// 5x from importmap.php
7071
'@hotwired/stimulus',
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// minified-controller.js
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
/* stimulusFetch: 'lazy' */
5+
export default class extends Controller {
6+
}

0 commit comments

Comments
 (0)