From 91c03aa06cab8377f475b080959e4b7dba5e610d Mon Sep 17 00:00:00 2001 From: PHAS Developer Date: Tue, 29 Apr 2025 05:33:10 +0000 Subject: [PATCH] [StimulusBundle] Skip mapping .ts controller if .js version is available --- .../src/AssetMapper/ControllersMapGenerator.php | 9 +++++++++ .../tests/AssetMapper/ControllersMapGeneratorTest.php | 2 ++ .../fixtures/assets/more-controllers/other-controller.ts | 7 +++++++ 3 files changed, 18 insertions(+) create mode 100644 src/StimulusBundle/tests/fixtures/assets/more-controllers/other-controller.ts 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..335c21af0e7 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')) { + return null; } else { // replace windows slashes $path = str_replace('\\', '/', $path); 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 { +}