Skip to content

Commit 91c03aa

Browse files
committed
[StimulusBundle] Skip mapping .ts controller if .js version is available
1 parent 61819ae commit 91c03aa

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php

+9
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,22 @@ private function loadCustomControllers(): array
7070

7171
$controllersMap = [];
7272
foreach ($finder as $file) {
73+
// Skip .ts controller if .js version is available
74+
if ('ts' === $file->getExtension() && file_exists(substr($file->getRealPath(), 0, -2).'js')) {
75+
continue;
76+
}
77+
7378
$name = $file->getRelativePathname();
7479
// use regex to extract 'controller'-postfix including extension
7580
preg_match(self::FILENAME_REGEX, $name, $matches);
7681
$name = str_replace(['_'.$matches[1], '-'.$matches[1]], '', $name);
7782
$name = str_replace(['_', '/', '\\'], ['-', '--', '--'], $name);
7883

7984
$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath());
85+
if (!$asset) {
86+
throw new \RuntimeException(\sprintf('Could not find an asset mapper path that points to the "%s" controller.', $name));
87+
}
88+
8089
$content = file_get_contents($asset->sourcePath);
8190
$isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $content);
8291

src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function testGetControllersMap()
3434
$logicalPath = 'fake-vendor/ux-package1/package-controller-second.js';
3535
} elseif (str_ends_with($path, 'package-hello-controller.js')) {
3636
$logicalPath = 'fake-vendor/ux-package2/package-hello-controller.js';
37+
} elseif (str_ends_with($path, 'other-controller.ts')) {
38+
return null;
3739
} else {
3840
// replace windows slashes
3941
$path = str_replace('\\', '/', $path);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// other-controller.js
2+
// @ts-ignore
3+
import { Controller } from '@hotwired/stimulus';
4+
5+
/* stimulusFetch: 'lazy' */
6+
export default class extends Controller {
7+
}

0 commit comments

Comments
 (0)