diff --git a/src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php b/src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php index b99f5ffbe5c..9799cdd3ec1 100644 --- a/src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php +++ b/src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php @@ -70,6 +70,11 @@ private function loadCustomControllers(): array $controllersMap = []; foreach ($finder as $file) { + // Skip .ts controller if .js version is available + if ('ts' === $file->getExtension() && file_exists(substr($file->getRealPath(), 0, -2).'js')) { + continue; + } + $name = $file->getRelativePathname(); // use regex to extract 'controller'-postfix including extension preg_match(self::FILENAME_REGEX, $name, $matches); @@ -77,6 +82,10 @@ private function loadCustomControllers(): array $name = str_replace(['_', '/', '\\'], ['-', '--', '--'], $name); $asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath()); + if (!$asset) { + throw new \RuntimeException(\sprintf('Could not find an asset mapper path that points to the "%s" controller.', $name)); + } + $content = file_get_contents($asset->sourcePath); $isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $content); diff --git a/src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php b/src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php index 21277b28003..15004969447 100644 --- a/src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php +++ b/src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php @@ -34,6 +34,8 @@ public function testGetControllersMap() $logicalPath = 'fake-vendor/ux-package1/package-controller-second.js'; } elseif (str_ends_with($path, 'package-hello-controller.js')) { $logicalPath = 'fake-vendor/ux-package2/package-hello-controller.js'; + } elseif (str_ends_with($path, 'other-controller.ts') || str_ends_with($path, 'excluded-controller.js')) { + return null; } else { // replace windows slashes $path = str_replace('\\', '/', $path); @@ -75,15 +77,18 @@ public function testGetControllersMap() $autoImportLocator, ); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Could not find an asset mapper path that points to the "excluded" controller.'); $map = $generator->getControllersMap(); // + 3 controller.json UX controllers // - 1 controllers.json UX controller is disabled - // + 10 custom controllers (1 file is not a controller & 1 is overridden) - $this->assertCount(12, $map); + // + 11 custom controllers (1 file is not a controller, 1 is overridden) + $this->assertCount(13, $map); $packageNames = array_keys($map); sort($packageNames); $this->assertSame([ 'bye', + 'excluded', 'fake-vendor--ux-package1--controller-second', 'fake-vendor--ux-package2--hello-controller', 'hello', diff --git a/src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php b/src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php index 58bb6c8c97c..346fca2fa32 100644 --- a/src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php +++ b/src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php @@ -63,7 +63,8 @@ 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', - // 3x from more-controllers + // 4x from more-controllers + '/assets/more-controllers/excluded-controller.js', '/assets/more-controllers/hello-controller.js', '/assets/more-controllers/minified-controller.js', '/assets/more-controllers/other-controller.js', diff --git a/src/StimulusBundle/tests/fixtures/assets/more-controllers/excluded-controller.js b/src/StimulusBundle/tests/fixtures/assets/more-controllers/excluded-controller.js new file mode 100644 index 00000000000..967c4832525 --- /dev/null +++ b/src/StimulusBundle/tests/fixtures/assets/more-controllers/excluded-controller.js @@ -0,0 +1,6 @@ +// excluded-controller.js +import { Controller } from '@hotwired/stimulus'; + +/* stimulusFetch: 'lazy' */ +export default class extends Controller { +} diff --git a/src/StimulusBundle/tests/fixtures/assets/more-controllers/other-controller.ts b/src/StimulusBundle/tests/fixtures/assets/more-controllers/other-controller.ts new file mode 100644 index 00000000000..1e6f9ccdbc6 --- /dev/null +++ b/src/StimulusBundle/tests/fixtures/assets/more-controllers/other-controller.ts @@ -0,0 +1,7 @@ +// other-controller.js +// @ts-ignore +import { Controller } from '@hotwired/stimulus'; + +/* stimulusFetch: 'lazy' */ +export default class extends Controller { +}